Skip to content

Commit

Permalink
šŸ› (signer-eth) [DSDK-582]: Ethereum/Bitcoin internal transaction mappā€¦
Browse files Browse the repository at this point in the history
ā€¦ers - Improve typeguards to not rely on instanceof (#508)
  • Loading branch information
jdabbech-ledger authored Nov 19, 2024
2 parents 49693db + 0eab94e commit 4b2aef5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-tables-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-signer-kit-btc": patch
---

Set bitcoin-js as peer dep
5 changes: 5 additions & 0 deletions .changeset/rich-pumpkins-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-signer-kit-ethereum": patch
---

Improve transaction v6 typeguard
5 changes: 3 additions & 2 deletions packages/signer/signer-btc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"dependencies": {
"@types/crypto-js": "^4.2.2",
"bitcoinjs-lib": "^6.1.6",
"crypto-js": "^4.2.0",
"inversify": "^6.0.3",
"inversify-logger-middleware": "^3.1.0",
Expand All @@ -53,9 +52,11 @@
"@ledgerhq/prettier-config-dsdk": "workspace:*",
"@ledgerhq/signer-utils": "workspace:*",
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"bitcoinjs-lib": "^6.1.6",
"ts-node": "^10.9.2"
},
"peerDependencies": {
"@ledgerhq/device-management-kit": "workspace:*"
"@ledgerhq/device-management-kit": "workspace:*",
"bitcoinjs-lib": "^6.1.6"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BigNumber as EthersV5BigNumber,
type Transaction as EthersV5Transaction,
} from "ethers-v5";
import { Transaction as EthersV6Transaction } from "ethers-v6";
import { Just } from "purify-ts";

import { type Transaction } from "@api/index";
Expand Down Expand Up @@ -126,5 +127,20 @@ describe("EthersV5TransactionMapper", () => {
// THEN
expect(result.isNothing()).toBeTruthy();
});

it("should return Nothing when the transaction is an EthersV6Transaction", () => {
// GIVEN
const transaction = new EthersV6Transaction();
transaction.chainId = 1n;
transaction.nonce = 0;
transaction.data = "0x";
transaction.to = "0x0123456789abcdef0123456789abcdef01234567";

// WHEN
const result = mapper.map(transaction);

// THEN
expect(result.isNothing()).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
BigNumber as EthersV5BigNumber,
type Transaction as EthersV5Transaction,
} from "ethers-v5";
import { Transaction as EthersV6Transaction } from "ethers-v6";
import { Just } from "purify-ts";

Expand Down Expand Up @@ -114,5 +118,30 @@ describe("EthersV6TransactionMapper", () => {
// THEN
expect(result.isNothing()).toBeTruthy();
});

it("should return Nothing when the transaction is not an EthersV5Transaction", () => {
// GIVEN
const transaction: EthersV5Transaction = {
chainId: 1,
nonce: 0,
gasLimit: EthersV5BigNumber.from(0),
gasPrice: EthersV5BigNumber.from(0),
value: EthersV5BigNumber.from(0),
data: "0x",
from: "0x",
r: "0x",
s: "0x",
v: 0,
type: 1,
maxFeePerGas: EthersV5BigNumber.from(0),
maxPriorityFeePerGas: EthersV5BigNumber.from(0),
};

// WHEN
const result = mapper.map(transaction);

// THEN
expect(result.isNothing()).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { getBytes, Transaction as EthersV6Transaction } from "ethers-v6";
import {
BigNumberish,
getBytes,
Transaction as EthersV6Transaction,
} from "ethers-v6";
import { injectable } from "inversify";
import { Just, Maybe, Nothing } from "purify-ts";

import { Transaction } from "@api/index";
import { Transaction } from "@api/model/Transaction";

import { TransactionMapperResult } from "./model/TransactionMapperResult";
import { TransactionMapper } from "./TransactionMapper";
Expand All @@ -29,6 +33,21 @@ export class EthersV6TransactionMapper implements TransactionMapper {
private isEthersV6Transaction(
transaction: Transaction,
): transaction is EthersV6Transaction {
return transaction instanceof EthersV6Transaction;
return (
typeof transaction === "object" &&
(transaction.hash === null || typeof transaction.hash === "string") &&
(transaction.to === null || typeof transaction.to === "string") &&
(transaction.gasLimit == null || isBigNumberish(transaction.gasLimit)) &&
(transaction.gasPrice === null || isBigNumberish(transaction.gasPrice)) &&
(transaction.value === null || isBigNumberish(transaction.value)) &&
(transaction.chainId === null ||
isBigNumberish(typeof transaction.chainId)) &&
(transaction.type === null || typeof transaction.type === "number")
);
}
}

const isBigNumberish = (number: unknown): number is BigNumberish =>
typeof number === "string" ||
typeof number === "number" ||
typeof number === "bigint";
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4b2aef5

Please sign in to comment.