diff --git a/.changeset/gorgeous-tables-cheer.md b/.changeset/gorgeous-tables-cheer.md new file mode 100644 index 000000000..f8789f96e --- /dev/null +++ b/.changeset/gorgeous-tables-cheer.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/device-signer-kit-btc": patch +--- + +Set bitcoin-js as peer dep diff --git a/.changeset/rich-pumpkins-laugh.md b/.changeset/rich-pumpkins-laugh.md new file mode 100644 index 000000000..648f829c0 --- /dev/null +++ b/.changeset/rich-pumpkins-laugh.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/device-signer-kit-ethereum": patch +--- + +Improve transaction v6 typeguard diff --git a/packages/signer/signer-btc/package.json b/packages/signer/signer-btc/package.json index bcd8c0704..d306f8b35 100644 --- a/packages/signer/signer-btc/package.json +++ b/packages/signer/signer-btc/package.json @@ -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", @@ -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" } } diff --git a/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV5TransactionMapper.test.ts b/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV5TransactionMapper.test.ts index db663d410..9b75b9161 100644 --- a/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV5TransactionMapper.test.ts +++ b/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV5TransactionMapper.test.ts @@ -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"; @@ -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(); + }); }); }); diff --git a/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.test.ts b/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.test.ts index 88d510b3a..4147b98db 100644 --- a/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.test.ts +++ b/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.test.ts @@ -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"; @@ -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(); + }); }); }); diff --git a/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.ts b/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.ts index f0c4c5b7b..220af0d29 100644 --- a/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.ts +++ b/packages/signer/signer-eth/src/internal/transaction/service/mapper/EthersV6TransactionMapper.ts @@ -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"; @@ -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"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5727776a6..e927847e9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -397,9 +397,6 @@ importers: '@types/crypto-js': specifier: ^4.2.2 version: 4.2.2 - bitcoinjs-lib: - specifier: ^6.1.6 - version: 6.1.6 crypto-js: specifier: ^4.2.0 version: 4.2.0 @@ -440,6 +437,9 @@ importers: '@ledgerhq/tsconfig-dsdk': specifier: workspace:* version: link:../../config/typescript + bitcoinjs-lib: + specifier: ^6.1.6 + version: 6.1.6 ts-node: specifier: ^10.9.2 version: 10.9.2(@types/node@22.9.0)(typescript@5.6.3)