Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: assert root delegation caveat enforcement #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion test/enforcers/ERC20AllowanceEnforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
// @ts-ignore
import { generateUtil } from "eth-delegatable-utils";
import { getPrivateKeys } from "../../utils/getPrivateKeys";
import { generateDelegation } from "../utils";
import { generateDelegation, prepend0x } from "../utils";

const { getSigners } = ethers;

Expand Down Expand Up @@ -165,6 +165,89 @@ describe("ERC20AllowanceEnforcer", () => {
).to.be.revertedWith("ERC20AllowanceEnforcer:allowance-exceeded");
});

it("should FAIL to transfer more than initial delegation permits", async () => {
expect(await ERC20Delegatable.balanceOf(wallet0.address)).to.eq(
ethers.utils.parseEther("1")
);

// root delegation (signer will be set to the message sender and therefore tokens will transfer from signer's account)
// root delegation provides allowance for 0.1
const _delegation0 = generateDelegation(
CONTACT_NAME,
ERC20Delegatable,
PK0,
wallet1.address,
[
{
enforcer: ERC20AllowanceEnforcer.address,
terms: ethers.utils.hexZeroPad(
utils.parseEther("0.1").toHexString(),
32
),
},
]
);

const _delegationHash0 =
delegatableUtils.createSignedDelegationHash(_delegation0);
const _delegationHash0Hex = prepend0x(_delegationHash0.toString("hex"));

// this delegation says allowance is 0.2
const _delegation1 = generateDelegation(
CONTACT_NAME,
ERC20Delegatable,
PK1,
wallet2.address,
[
{
enforcer: ERC20AllowanceEnforcer.address,
terms: ethers.utils.hexZeroPad(
utils.parseEther("0.2").toHexString(),
32
),
},
],
_delegationHash0Hex
);

// invocation will try to transfer 0.2
const INVOCATION_MESSAGE = {
replayProtection: {
nonce: "0x01",
queue: "0x00",
},
batch: [
{
authority: [_delegation0, _delegation1],
transaction: {
to: ERC20Delegatable.address,
gasLimit: "210000000000000000",
data: (
await ERC20Delegatable.populateTransaction.transfer(
wallet1.address,
ethers.utils.parseEther("0.2")
)
).data,
},
},
],
};
const invocation = delegatableUtils.signInvocation(INVOCATION_MESSAGE, PK2);

await expect(
ERC20Delegatable.invoke([
{
signature: invocation.signature,
invocations: invocation.invocations,
},
])
).to.be.revertedWith("ERC20AllowanceEnforcer:allowance-exceeded");

expect(await ERC20Delegatable.balanceOf(wallet0.address)).to.eq(
ethers.utils.parseEther("1")
);
});

it("should FAIL to INVOKE invalid method", async () => {
const PK = wallet0._signingKey().privateKey.substring(2);
expect(await ERC20Delegatable.balanceOf(wallet0.address)).to.eq(
Expand Down
7 changes: 7 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { generateUtil } from "eth-delegatable-utils";
const BASE_AUTH =
"0x0000000000000000000000000000000000000000000000000000000000000000";

export function prepend0x(hex: string): string {
if (hex.toLowerCase().slice(0, 2) === "0x") {
return hex;
}
return "0x" + hex;
}

export function generateDelegation(
name: any,
contract: any,
Expand Down