Skip to content

Commit

Permalink
Merge branch 'develop' into add-diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
sgc-code authored Jul 1, 2024
2 parents e422a9d + f4d21a8 commit bcdc0b8
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ export async function claimInternal(args: {
gift: Gift;
receiver: string;
giftPrivateKey: string;
overrides?: { EscrowAccountAddress?: string; callToAddress?: string };
overrides?: { escrowAccountAddress?: string; callToAddress?: string };
details?: UniversalDetails;
}): Promise<TransactionReceipt> {
const escrowAddress = args.overrides?.EscrowAccountAddress || calculateEscrowAddress(args.gift);
const escrowAddress = args.overrides?.escrowAccountAddress || calculateEscrowAddress(args.gift);
const escrowAccount = getEscrowAccount(args.gift, args.giftPrivateKey, escrowAddress);
const response = await escrowAccount.execute(
[
Expand Down
12 changes: 12 additions & 0 deletions lib/signers/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ export class LegacyStarknetKeyPair extends LegacyKeyPair {
return [r.toString(), s.toString()];
}
}

export class LongSigner extends LegacyStarknetKeyPair {
public async signRaw(messageHash: string): Promise<string[]> {
return ["", ...(await super.signRaw(messageHash))];
}
}

export class WrongSigner extends LegacyStarknetKeyPair {
public async signRaw(messageHash: string): Promise<string[]> {
return ["0x1", "0x1"];
}
}
45 changes: 45 additions & 0 deletions tests-integration/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CallData } from "starknet";
import {
LongSigner,
WrongSigner,
buildGiftCallData,
calculateEscrowAddress,
claimInternal,
Expand All @@ -8,6 +10,7 @@ import {
executeActionOnAccount,
expectRevertWithErrorMessage,
getEscrowAccount,
manager,
randomReceiver,
setupGiftProtocol,
} from "../lib";
Expand Down Expand Up @@ -100,4 +103,46 @@ describe("Escrow Account", function () {
claimInternal({ gift, receiver, giftPrivateKey: giftPrivateKey, details: { skipValidate: false } }),
);
});

it(`Long signature shouldn't be accepted`, async function () {
const { factory } = await setupGiftProtocol();
const { gift, giftPrivateKey } = await defaultDepositTestSetup({ factory });
const receiver = randomReceiver();

const escrowAccount = getEscrowAccount(gift, giftPrivateKey);
escrowAccount.signer = new LongSigner();
await expectRevertWithErrorMessage("escrow/invalid-signature-len", () =>
escrowAccount.execute([
{
contractAddress: escrowAccount.address,
calldata: [buildGiftCallData(gift), receiver],
entrypoint: "claim_internal",
},
]),
);
});

it(`Wrong signature shouldn't be accepted`, async function () {
const { factory } = await setupGiftProtocol();
const { gift, giftPrivateKey } = await defaultDepositTestSetup({ factory });
const receiver = randomReceiver();

const escrowAccount = getEscrowAccount(gift, giftPrivateKey);
escrowAccount.signer = new WrongSigner();
await expectRevertWithErrorMessage("escrow/invalid-signature", () =>
escrowAccount.execute([
{
contractAddress: escrowAccount.address,
calldata: [buildGiftCallData(gift), receiver],
entrypoint: "claim_internal",
},
]),
);
});

it(`Shouldn't be possible to instantiate the library account`, async function () {
const classHash = await manager.declareLocalContract("EscrowLibrary");

await expectRevertWithErrorMessage("escr-lib/instance-not-recommend", () => deployer.deployContract({ classHash }));
});
});
32 changes: 32 additions & 0 deletions tests-integration/claim_internal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { num } from "starknet";
import {
ETH_GIFT_MAX_FEE,
STRK_GIFT_MAX_FEE,
buildGiftCallData,
calculateEscrowAddress,
claimInternal,
defaultDepositTestSetup,
expectRevertWithErrorMessage,
getEscrowAccount,
manager,
randomReceiver,
setupGiftProtocol,
Expand All @@ -27,6 +29,36 @@ describe("Claim Internal", function () {
await manager.tokens.tokenBalance(receiver, gift.gift_token).should.eventually.equal(gift.gift_amount);
});

it(`Invalid gift data txV3: ${useTxV3}`, async function () {
const { factory } = await setupGiftProtocol();
const { gift, giftPrivateKey } = await defaultDepositTestSetup({ factory, useTxV3 });
const receiver = randomReceiver();
const escrowAddress = calculateEscrowAddress(gift);

const escrowAccountAddress = getEscrowAccount(gift, giftPrivateKey, escrowAddress).address;
gift.fee_amount = 42n;
await expectRevertWithErrorMessage("escrow/invalid-escrow-address", () =>
claimInternal({ gift, receiver, giftPrivateKey, overrides: { escrowAccountAddress } }),
);
});

it(`Invalid calldata using txV3: ${useTxV3}`, async function () {
const { factory } = await setupGiftProtocol();
const { gift, giftPrivateKey } = await defaultDepositTestSetup({ factory, useTxV3 });
const receiver = randomReceiver();

const escrowAccount = getEscrowAccount(gift, giftPrivateKey);
await expectRevertWithErrorMessage("escrow/invalid-calldata", () =>
escrowAccount.execute([
{
contractAddress: escrowAccount.address,
calldata: [buildGiftCallData(gift), receiver, 1],
entrypoint: "claim_internal",
},
]),
);
});

it(`Can't claim if no fee amount deposited (fee token == gift token) using txV3: ${useTxV3}`, async function () {
const { factory } = await setupGiftProtocol();
const receiver = randomReceiver();
Expand Down
14 changes: 14 additions & 0 deletions tests-integration/deposit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe("Deposit", function () {
return txReceipt;
});
});

it(`Fee not ETH nor STRK`, async function () {
const { factory } = await setupGiftProtocol();
const mockERC20 = await deployMockERC20();

await expectRevertWithErrorMessage("gift-fac/invalid-fee-token", async () => {
const { txReceipt } = await defaultDepositTestSetup({
factory,
useTxV3,
overrides: { feeTokenAddress: mockERC20.address },
});
return txReceipt;
});
});
}

it("Deposit fails class hash passed != class hash in factory storage", async function () {
Expand Down
8 changes: 8 additions & 0 deletions tests-integration/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe("Test Core Factory Functions", function () {
await manager.tokens.tokenBalance(escrowAddress, gift.gift_token).should.eventually.equal(0n);
await manager.tokens.tokenBalance(dustReceiver, gift.gift_token).should.eventually.equal(dustBalance);
});

it(`Shouldn't be possible to claim_dust for an unclaimed gift: ${useTxV3}`, async function () {
const { factory } = await setupGiftProtocol();
const { gift } = await defaultDepositTestSetup({ factory, useTxV3 });
const dustReceiver = randomReceiver();

await expectRevertWithErrorMessage("escr-lib/not-yet-claimed", () => claimDust({ gift, receiver: dustReceiver }));
});
}

it(`Pausable`, async function () {
Expand Down

0 comments on commit bcdc0b8

Please sign in to comment.