Skip to content

Commit

Permalink
test: assert chained delegations can not be used to transfer more ERC…
Browse files Browse the repository at this point in the history
…20 tokens than the root delegation permits
  • Loading branch information
aa-eyup committed Jun 4, 2023
1 parent 2e2a6ca commit 3543930
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
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

0 comments on commit 3543930

Please sign in to comment.