Skip to content

Commit

Permalink
EIP 1559 (#13)
Browse files Browse the repository at this point in the history
* Bump Ethers

* Bump Ledger libs

* Add tests for Private Key and Mnemonics

* Simplify sanitize function

* Add WIP Ledger & Trezor tests

* Bump Ledger libs and fix test

* Bump version
  • Loading branch information
FrederikBolding authored Aug 3, 2021
1 parent c296128 commit 2e096ab
Show file tree
Hide file tree
Showing 8 changed files with 542 additions and 423 deletions.
17 changes: 16 additions & 1 deletion .jest/__fixtures__/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ export const fTransactionRequest = {
nonce: 6,
gasPrice: '0x012a05f200',
gasLimit: '0x5208',
to: '0xb2bb2b958AFa2e96dab3f3Ce7162b87daEa39017',
to: '0xB2BB2b958aFA2e96dAb3F3Ce7162B87dAea39017',
value: '0x2386f26fc10000',
data: '0x',
chainId: 3
};

export const fTransactionRequestEIP1559 = {
nonce: 6,
maxFeePerGas: '0x4a817c800',
maxPriorityFeePerGas: '0x3b9aca00',
gasLimit: '0x5208',
to: '0xB2BB2b958aFA2e96dAb3F3Ce7162B87dAea39017',
value: '0x2386f26fc10000',
data: '0x',
chainId: 3,
type: 2
};

export const fSignedTx =
'0xf86b0685012a05f20082520894b2bb2b958afa2e96dab3f3ce7162b87daea39017872386f26fc100008029a075b96c4423ea79037099e0f8a0fa7d8538f00c6aaddea26e151320aac65ae3bda05266d81476adedc28c5e769f8bf016de33bdaa49f341435df429e01fe5f9b16e';

export const fSignedTokenTx =
'0xf86b0685012a05f20082520894e41d2489571d322189246dafa5ebde1f4699f498872386f26fc100008026a086fdf8b0c4ba7e59b93f0a81a9d5083835caf56b17aac3fe251f276a10ef33d4a04ec0c25116937acdec4617182127d9f6a9431e837412babd150331d00d5e0ae9';

export const fSignedTxEIP1559 =
'0x02f8720306843b9aca008504a817c80082520894b2bb2b958afa2e96dab3f3ce7162b87daea39017872386f26fc1000080c001a0884850dc596eac6b74175d2c62deedd9295570808882b0cd9adf47e5ac8b3b3da068881b0ef002d48ef78374d6842ee4987a222a4726af47b5a0a4bcb8f38e2cf3';
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mycrypto/wallets",
"version": "1.2.3",
"version": "1.3.0",
"description": "Wallet abstractions to be used throughout the MyCrypto product suite.",
"repository": "MyCryptoHQ/wallets",
"author": "MyCrypto",
Expand Down Expand Up @@ -30,23 +30,23 @@
"prepack": "yarn build"
},
"dependencies": {
"@ethersproject/abstract-provider": "5.3.0",
"@ethersproject/address": "5.3.0",
"@ethersproject/basex": "5.3.0",
"@ethersproject/bytes": "5.3.0",
"@ethersproject/hdnode": "5.3.0",
"@ethersproject/json-wallets": "5.3.0",
"@ethersproject/sha2": "5.3.0",
"@ethersproject/signing-key": "5.3.0",
"@ethersproject/strings": "5.3.0",
"@ethersproject/transactions": "5.3.0",
"@ethersproject/wallet": "5.3.0",
"@ledgerhq/hw-app-eth": "6.0.2",
"@ledgerhq/hw-transport": "6.0.2",
"@ledgerhq/hw-transport-node-hid-noevents": "6.0.2",
"@ethersproject/abstract-provider": "5.4.0",
"@ethersproject/address": "5.4.0",
"@ethersproject/basex": "5.4.0",
"@ethersproject/bytes": "5.4.0",
"@ethersproject/hdnode": "5.4.0",
"@ethersproject/json-wallets": "5.4.0",
"@ethersproject/sha2": "5.4.0",
"@ethersproject/signing-key": "5.4.0",
"@ethersproject/strings": "5.4.0",
"@ethersproject/transactions": "5.4.0",
"@ethersproject/wallet": "5.4.0",
"@ledgerhq/hw-app-eth": "6.3.0",
"@ledgerhq/hw-transport": "6.3.0",
"@ledgerhq/hw-transport-node-hid-noevents": "6.3.0",
"@ledgerhq/hw-transport-u2f": "5.36.0-deprecated",
"@ledgerhq/hw-transport-webhid": "6.0.2",
"@ledgerhq/hw-transport-webusb": "6.0.2",
"@ledgerhq/hw-transport-webhid": "6.3.0",
"@ledgerhq/hw-transport-webusb": "6.3.0",
"ethereumjs-wallet": "1.0.1",
"superstruct": "0.15.1",
"trezor-connect": "8.1.25"
Expand Down
20 changes: 19 additions & 1 deletion src/implementations/deterministic/ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
fSignedMessage,
fSignedTokenTx,
fSignedTx,
fTransactionRequest
fSignedTxEIP1559,
fTransactionRequest,
fTransactionRequestEIP1559
} from '../../../.jest/__fixtures__';
import { DEFAULT_ETH, LEDGER_LIVE_ETH } from '../../dpaths';
import { getFullPath } from '../../utils';
Expand Down Expand Up @@ -44,6 +46,22 @@ describe('LedgerWalletInstance', () => {
await expect(instance.signTransaction(fTransactionRequest)).resolves.toBe(fSignedTx);
});

// @todo Make sure this ACTUALLY works on a device
it('signs a EIP 1559 transaction', async () => {
const store = RecordStore.fromString(`
=> e004000046058000002c8000003c80000000000000000000000002ef0306843b9aca008504a817c80082520894b2bb2b958afa2e96dab3f3ce7162b87daea39017872386f26fc1000080c0
<= 01884850dc596eac6b74175d2c62deedd9295570808882b0cd9adf47e5ac8b3b3d68881b0ef002d48ef78374d6842ee4987a222a4726af47b5a0a4bcb8f38e2cf39000
`);

const transport = await openTransportReplayer(store);
const wallet = new LedgerWallet(transport);
const instance = await wallet.getWallet(DEFAULT_ETH, 0);

await expect(instance.signTransaction(fTransactionRequestEIP1559)).resolves.toBe(
fSignedTxEIP1559
);
});

it('signs a transaction with token information', async () => {
const store = RecordStore.fromString(`
=> e00a000066035a5258e41d2489571d322189246dafa5ebde1f4699f4980000001200000001304402200ae8634c22762a8ba41d2acb1e068dcce947337c6dd984f13b820d396176952302203306a49d8a6c35b11a61088e1570b3928ca3a0db6bd36f577b5ef87628561ff7
Expand Down
11 changes: 10 additions & 1 deletion src/implementations/deterministic/mnemonic-phrase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
fMnemonicPhrase,
fPrivateKey,
fSignedTx,
fTransactionRequest
fSignedTxEIP1559,
fTransactionRequest,
fTransactionRequestEIP1559
} from '../../../.jest/__fixtures__';
import { DEFAULT_ETH, LEDGER_LIVE_ETH } from '../../dpaths';
import { MnemonicPhrase } from './mnemonic-phrase';
Expand Down Expand Up @@ -180,6 +182,13 @@ describe('Mnemonic Phrase', () => {
return expect(wallet.signTransaction(fTransactionRequest)).resolves.toBe(fSignedTx);
});

it('wallet signs EIP 1559 transaction correctly', async () => {
const wallet = await new MnemonicPhrase(fMnemonicPhrase).getWallet(DEFAULT_ETH, 0);
return expect(wallet.signTransaction(fTransactionRequestEIP1559)).resolves.toBe(
fSignedTxEIP1559
);
});

it('wallet derives address correctly', async () => {
const wallet = await new MnemonicPhrase(fMnemonicPhrase).getWallet(DEFAULT_ETH, 0);
return expect(wallet.getAddress()).resolves.toBe(fAddress);
Expand Down
14 changes: 13 additions & 1 deletion src/implementations/deterministic/trezor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
fMessageToSign,
fSignedMessage,
fSignedTx,
fTransactionRequest
fSignedTxEIP1559,
fTransactionRequest,
fTransactionRequestEIP1559
} from '../../../.jest/__fixtures__';
import { DEFAULT_ETH, LEDGER_LIVE_ETH } from '../../dpaths';
import { getFullPath } from '../../utils';
Expand All @@ -22,6 +24,16 @@ describe('TrezorWalletInstance', () => {
await expect(instance.signTransaction(fTransactionRequest)).resolves.toBe(fSignedTx);
});

// @todo Make sure this ACTUALLY works
it('signs a EIP 1559 transaction', async () => {
const wallet = new TrezorWallet(manifest);
const instance = await wallet.getWallet(DEFAULT_ETH, 0);

await expect(instance.signTransaction(fTransactionRequestEIP1559)).resolves.toBe(
fSignedTxEIP1559
);
});

it('throws if the call to TrezorConnect fails', async () => {
(TrezorConnect.ethereumSignTransaction as jest.MockedFunction<
typeof TrezorConnect.ethereumSignTransaction
Expand Down
10 changes: 9 additions & 1 deletion src/implementations/non-deterministic/private-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
fPrivateKey,
fSignedMessage,
fSignedTx,
fTransactionRequest
fSignedTxEIP1559,
fTransactionRequest,
fTransactionRequestEIP1559
} from '../../../.jest/__fixtures__';
import { PrivateKey } from './private-key';

Expand All @@ -15,6 +17,12 @@ describe('Private Key', () => {
);
});

it('signs EIP 1559 transaction correctly', () => {
return expect(
new PrivateKey(fPrivateKey).signTransaction(fTransactionRequestEIP1559)
).resolves.toBe(fSignedTxEIP1559);
});

it('signs messages correctly', () => {
return expect(new PrivateKey(fPrivateKey).signMessage(fMessageToSign)).resolves.toBe(
fSignedMessage
Expand Down
9 changes: 2 additions & 7 deletions src/utils/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { BigNumber } from '@ethersproject/bignumber';
import type { UnsignedTransaction } from '@ethersproject/transactions';

export const sanitizeTx = (tx: TransactionRequest): UnsignedTransaction => ({
chainId: tx.chainId || undefined,
data: tx.data || undefined,
gasLimit: tx.gasLimit || undefined,
gasPrice: tx.gasPrice || undefined,
nonce: tx.nonce ? BigNumber.from(tx.nonce).toNumber() : undefined,
to: tx.to || undefined,
value: tx.value || undefined
...tx,
nonce: tx.nonce ? BigNumber.from(tx.nonce).toNumber() : undefined
});
Loading

0 comments on commit 2e096ab

Please sign in to comment.