From f6e0e054ec010e53f0be1538ce5f4920e37a5eea Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Tue, 12 Nov 2024 12:29:02 +0100 Subject: [PATCH] fix: cip64 --- .../wallet-ledger/src/ledger-signer.ts | 11 +--- .../wallet-ledger/src/ledger-wallet.test.ts | 51 +++++++++++++++++-- .../wallet-ledger/src/ledger-wallet.ts | 32 ++++++------ 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/packages/sdk/wallets/wallet-ledger/src/ledger-signer.ts b/packages/sdk/wallets/wallet-ledger/src/ledger-signer.ts index 3fd8e775e..8e3bb37b7 100644 --- a/packages/sdk/wallets/wallet-ledger/src/ledger-signer.ts +++ b/packages/sdk/wallets/wallet-ledger/src/ledger-signer.ts @@ -193,15 +193,6 @@ export class LedgerSigner implements Signer { } private provideERC20TokenInformation(tokenInfoData: Buffer) { - // it looks like legacy might need it WITHOUT 0x prefix - const isModern = meetsVersionRequirements(this.appConfiguration.version, { - minimum: LedgerWallet.MIN_VERSION_EIP1559, - }) - - const hexStringTokenInfo = isModern - ? ensureLeading0x(tokenInfoData.toString('hex')) - : trimLeading0x(tokenInfoData.toString('hex')) - - return this.ledger!.provideERC20TokenInformation(hexStringTokenInfo) + return this.ledger!.provideERC20TokenInformation(trimLeading0x(tokenInfoData.toString('hex'))) } } diff --git a/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.test.ts b/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.test.ts index a0db2d497..42edfd2c1 100644 --- a/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.test.ts +++ b/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.test.ts @@ -827,7 +827,7 @@ describe('LedgerWallet class', () => { }) }) - describe('[cip64]', () => { + syntheticDescribe('[cip64] synthetic', () => { const kit = newKit('https://alfajores-forno.celo-testnet.org') beforeEach(async () => { celoTransaction = { @@ -846,9 +846,8 @@ describe('LedgerWallet class', () => { test( 'succeeds', async () => { - jest - .spyOn(wallet.ledger!, 'provideERC20TokenInformation') - .mockImplementationOnce(async () => true) + jest.spyOn(wallet.ledger!, 'provideERC20TokenInformation') + await expect(wallet.signTransaction(celoTransaction)).resolves.toMatchInlineSnapshot(` { "raw": "0x7bf87f82aef38063636394588e4b68193001e4d10928660ab4165b813717c0880de0b6b3a764000080c094874069fa1eb16d44d622f2e0ca25eea172369bc101a0254f952c5223c30039f7f845778d7aac558464ce2971fd09883df34913eb6dfca037a78571ae1a44d86bac7269e3a845990a49ad5fb60a5ec1fcaba428693558c0", @@ -878,6 +877,50 @@ describe('LedgerWallet class', () => { TEST_TIMEOUT_IN_MS ) }) + + hardwareDescribe('[cip64] device', () => { + const kit = newKit('https://alfajores-forno.celo-testnet.org') + beforeEach(async () => { + celoTransaction = { + from: knownAddress, + to: otherAddress, + chainId: CHAIN_ID, + value: Web3.utils.toWei('1', 'ether'), + nonce: 0, + gas: 99, + maxFeePerGas: 99, + maxPriorityFeePerGas: 99, + feeCurrency: (await kit.contracts.getStableToken(StableToken.cUSD)).address, + } + }) + + test( + 'succeeds', + async () => { + jest.spyOn(wallet.ledger!, 'provideERC20TokenInformation') + const tx = await wallet.signTransaction(celoTransaction) + // @ts-expect-error + expect(tx.type).toBe('cip64') + expect(tx.tx.nonce).toBe('0') + expect(BigInt(tx.tx.value)).toBe(BigInt(celoTransaction.value as string)) + const v = parseInt(tx.tx.v, 16) + expect(v === 0 || v === 1).toBe(true) + // @ts-expect-error + expect(tx.tx.feeCurrency.toLowerCase()).toBe( + celoTransaction.feeCurrency?.toLowerCase() + ) + expect(recoverTransaction(tx.raw)[1].toLowerCase()).toBe( + wallet.getAccounts()[0].toLowerCase() + ) + + expect(wallet.ledger!.provideERC20TokenInformation).toHaveBeenCalledWith( + `06612063555344874069fa1eb16d44d622f2e0ca25eea172369bc1000000120000aef33045022100a885480c357fd6ec64ed532656a7e988198fdf4e2cf4632408f2d65561189872022009fd78725055fc68af16e151516ba29625e3e1c74ceab3da1bcabd6015e3f6e8` + ) + }, + TEST_TIMEOUT_IN_MS + ) + }) + describe.skip('[cip66]', () => { const kit = newKit('https://alfajores-forno.celo-testnet.org') beforeEach(async () => { diff --git a/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.ts b/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.ts index 1fa49baf9..58bbc8f98 100644 --- a/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.ts +++ b/packages/sdk/wallets/wallet-ledger/src/ledger-wallet.ts @@ -127,23 +127,25 @@ export class LedgerWallet extends RemoteWallet implements ReadOnly ) } // but if not celo as layer 2 and as layer 1 are different - } else if (this.isCel2) { - throw new Error( - `celo ledger app version must be at least ${LedgerWallet.MIN_VERSION_EIP1559} to sign transactions supported on celo after the L2 upgrade` - ) } else { - // the l1 legacy case - console.warn( - `Upgrade your celo ledger app to at least ${LedgerWallet.MIN_VERSION_EIP1559} before cel2 transition` - ) - if (!txParams.gasPrice) { - // this version of app only supports legacy so must have gasPrice - txParams.gasPrice = txParams.maxFeePerGas - delete txParams.maxFeePerGas - delete txParams.maxPriorityFeePerGas - console.info('automatically converting to legacy transaction') + if (this.isCel2) { + throw new Error( + `celo ledger app version must be at least ${LedgerWallet.MIN_VERSION_EIP1559} to sign transactions supported on celo after the L2 upgrade` + ) + } else { + // the l1 legacy case + console.warn( + `Upgrade your celo ledger app to at least ${LedgerWallet.MIN_VERSION_EIP1559} before cel2 transition` + ) + if (!txParams.gasPrice) { + // this version of app only supports legacy so must have gasPrice + txParams.gasPrice = txParams.maxFeePerGas + delete txParams.maxFeePerGas + delete txParams.maxPriorityFeePerGas + console.info('automatically converting to legacy transaction') + } + return encode_deprecated_celo_legacy_type_only_for_temporary_ledger_compat(txParams) } - return encode_deprecated_celo_legacy_type_only_for_temporary_ledger_compat(txParams) } }