Skip to content

Commit

Permalink
[LIVE-13976] Bugfix - Allow missing coinRef with EIP-712 filters V2 (#…
Browse files Browse the repository at this point in the history
…7779)

* Allow missing tokens in CAL for EIP-712 v2 filters

* changeset
  • Loading branch information
lambertkevin authored Sep 9, 2024
1 parent 1503fd7 commit 9abf63b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-pans-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/hw-app-eth": patch
---

Allow token to not be provided to the device (might not be existing in CAL) when providing filters V2 for the EIP-712 messages
17 changes: 8 additions & 9 deletions libs/ledgerjs/packages/hw-app-eth/src/modules/EIP712/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ export const getCoinRefTokensMap = (
const coinRefsTokensMap: Record<number, { token: string; coinRefMemorySlot?: number }> = {};
if (shouldUseV1Filters || !filters) return coinRefsTokensMap;

const tokenFilters = filters.fields.filter(({ format }) => format === "token");
const tokenFilters = filters.fields
.filter(({ format }) => format === "token")
.sort((a, b) => (a.coin_ref || 0) - (b.coin_ref || 0));
const tokens = tokenFilters.reduce<{ token: string; coinRef: number }[]>((acc, filter) => {
const token = getValueFromPath(filter.path, message);
if (Array.isArray(token)) {
Expand Down Expand Up @@ -367,22 +369,19 @@ export const getPayloadForFilterV2 = (

case "token": {
const { deviceTokenIndex } = coinRefsTokensMap[coinRef!];
if (typeof deviceTokenIndex === "undefined") {
throw new Error("Missing coinRef");
}

return Buffer.concat([Buffer.from(intAsHexBytes(deviceTokenIndex, 1), "hex"), sigBuffer]);
return Buffer.concat([
Buffer.from(intAsHexBytes(deviceTokenIndex || coinRef || 0, 1), "hex"),
sigBuffer,
]);
}

case "amount": {
const { deviceTokenIndex } = coinRefsTokensMap[coinRef!];
if (typeof deviceTokenIndex === "undefined") {
throw new Error("Missing coinRef");
}

return Buffer.concat([
displayNameBuffer,
Buffer.from(intAsHexBytes(deviceTokenIndex, 1), "hex"),
Buffer.from(intAsHexBytes(deviceTokenIndex || coinRef || 0, 1), "hex"),
sigBuffer,
]);
}
Expand Down
69 changes: 63 additions & 6 deletions libs/ledgerjs/packages/hw-app-eth/tests/EIP712/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,63 @@ describe("EIP712", () => {
"Array of tokens is not supported with a single coin ref",
);
});

it("should order correctly the map", () => {
const filters = {
fields: [
{
coin_ref: 1,
format: "token",
label: "Amount allowance",
path: "details.token2",
signature:
"304402203b28bb137a21a6f08903489c6b158fd54280367d6bb72f87bf3e2f287a92440f02207ecc609b12b363cd0e8cbef7079776dfb363cef2fc11da39750598ee4cda4877",
},
{
coin_ref: 1,
format: "amount",
label: "Amount allowance",
path: "details.amount2",
signature:
"30440220574f7322c9cd212d295c15d92a48aeb6b490978cb87d61fe8afb71b97053ceb7022016489970af3ff80903a45a966ea07dd9ca1435f6b6da9124e03f3087485d1c5b",
},
{
coin_ref: 0,
format: "token",
label: "Amount allowance",
path: "details.token",
signature:
"304402203b28bb137a21a6f08903489c6b158fd54280367d6bb72f87bf3e2f287a92440f02207ecc609b12b363cd0e8cbef7079776dfb363cef2fc11da39750598ee4cda4877",
},
{
coin_ref: 0,
format: "amount",
label: "Amount allowance",
path: "details.amount",
signature:
"30440220574f7322c9cd212d295c15d92a48aeb6b490978cb87d61fe8afb71b97053ceb7022016489970af3ff80903a45a966ea07dd9ca1435f6b6da9124e03f3087485d1c5b",
},
{
format: "raw",
label: "Approve to spender",
path: "spender",
signature:
"30450221008eecd0e1f432daf722fd00c54038a4cd4d96624cc117ddfb12c7ed10a59b260d02203d34c811a5918c2654e301a071b624088aa9a0813f19dbfa1c803f3dcec64557",
},
],
} as any;
const message = {
message: {
details: { token: "0x01", amount: "0x02", token2: "0x03", amount2: "0x04" },
spender: "0x05",
},
} as any;

expect(getCoinRefTokensMap(filters, false, message)).toEqual({
0: { token: "0x01" },
1: { token: "0x03" },
});
});
});

describe("getAppAndVersion", () => {
Expand Down Expand Up @@ -685,10 +742,10 @@ describe("EIP712", () => {
).toEqual(Buffer.concat([Buffer.from("7B", "hex"), sigBuffer]));
});

it("should should throw if a token hasn't been registered by the device", () => {
expect(() =>
it("should return the coinRef if a token hasn't been registered by the device (token not in CAL for example)", () => {
expect(
getPayloadForFilterV2("token", 0, { 0: { token: "0x01" } }, displayNameBuffer, sigBuffer),
).toThrow("Missing coinRef");
).toEqual(Buffer.concat([Buffer.from("00", "hex"), sigBuffer]));
});

it("should return a payload for an amount filter", () => {
Expand All @@ -703,16 +760,16 @@ describe("EIP712", () => {
).toEqual(Buffer.concat([displayNameBuffer, Buffer.from("7B", "hex"), sigBuffer]));
});

it("should should throw if a token hasn't been registered by the device", () => {
expect(() =>
it("should return the coinRef if a token hasn't been registered by the device (token not in CAL for example)", () => {
expect(
getPayloadForFilterV2(
"amount",
0,
{ 0: { token: "0x01" } },
displayNameBuffer,
sigBuffer,
),
).toThrow("Missing coinRef");
).toEqual(Buffer.concat([displayNameBuffer, Buffer.from("00", "hex"), sigBuffer]));
});

it("should should throw for an unknown filter format", () => {
Expand Down

0 comments on commit 9abf63b

Please sign in to comment.