Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Fix an issue with ERC20 info when using EIP 1559 transactions (#654)
Browse files Browse the repository at this point in the history
* Fix an issue with ERC20 info when using EIP 1559 transactions

* Add default chainid for non EIP 155 txs

* Fix linting issue
  • Loading branch information
FrederikBolding authored Sep 6, 2021
1 parent 65723e8 commit 6ba1f9a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
57 changes: 32 additions & 25 deletions packages/hw-app-eth/src/Eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,41 @@ export default class Eth {
const toSend: Buffer[] = [];
let response;
// Check if the TX is encoded following EIP 155
let rlpTx = ethers.utils.RLP.decode(rlpData).map((hex) =>
const rlpTx = ethers.utils.RLP.decode(rlpData).map((hex) =>
Buffer.from(hex.slice(2), "hex")
);

let vrsOffset = 0;
let chainId = new BigNumber(0);
let chainIdTruncated = 0;

const rlpDecoded = ethers.utils.RLP.decode(rlpData);

let decodedTx;
if (txType === 2) {
// EIP1559
decodedTx = {
data: rlpDecoded[7],
to: rlpDecoded[5],
chainId: rlpTx[0],
};
} else if (txType === 1) {
// EIP2930
decodedTx = {
data: rlpDecoded[6],
to: rlpDecoded[4],
chainId: rlpTx[0],
};
} else {
// Legacy tx
decodedTx = {
data: rlpDecoded[5],
to: rlpDecoded[3],
// Default to 1 for non EIP 155 txs
chainId: rlpTx.length > 6 ? rlpTx[6] : Buffer.from("0x01", "hex"),
};
}

if (txType === null && rlpTx.length > 6) {
const rlpVrs = Buffer.from(
ethers.utils.RLP.encode(rlpTx.slice(-3)).slice(2),
Expand All @@ -224,10 +251,12 @@ export default class Eth {
// Increase rlpOffset by the size of the list length.
vrsOffset += sizeOfListLen - 1;
}
}

const chainIdSrc = decodedTx.chainId;
if (chainIdSrc) {
// Using BigNumber because chainID could be any uint256.
chainId = new BigNumber(rlpTx[6].toString("hex"), 16);
const chainIdSrc = rlpTx[6];
chainId = new BigNumber(chainIdSrc.toString("hex"), 16);
const chainIdTruncatedBuf = Buffer.alloc(4);
if (chainIdSrc.length > 4) {
chainIdSrc.copy(chainIdTruncatedBuf);
Expand Down Expand Up @@ -267,28 +296,6 @@ export default class Eth {
offset += chunkSize;
}

rlpTx = ethers.utils.RLP.decode(rlpData);

let decodedTx;
if (txType === 2) {
// EIP1559
decodedTx = {
data: rlpTx[7],
to: rlpTx[5],
};
} else if (txType === 1) {
// EIP2930
decodedTx = {
data: rlpTx[6],
to: rlpTx[4],
};
} else {
// Legacy tx
decodedTx = {
data: rlpTx[5],
to: rlpTx[3],
};
}
const provideForContract = async (address) => {
const erc20Info = byContractAddressAndChainId(address, chainIdTruncated);
if (erc20Info) {
Expand Down
21 changes: 21 additions & 0 deletions packages/hw-app-eth/tests/Eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ test("signTransaction supports EIP1559", async () => {
});
});

test("signTransaction supports EIP1559 with tokens", async () => {
const transport = await openTransportReplayer(
RecordStore.fromString(`
=> e00a000066035a5258e41d2489571d322189246dafa5ebde1f4699f4980000001200000001304402200ae8634c22762a8ba41d2acb1e068dcce947337c6dd984f13b820d396176952302203306a49d8a6c35b11a61088e1570b3928ca3a0db6bd36f577b5ef87628561ff7
<= 9000
=> e00400008c058000002c8000003c80000000000000000000000002f8740106843b9aca008504a817c80082520894e41d2489571d322189246dafa5ebde1f4699f498872386f26fc10000b844095ea7b3000000000000000000000000221657776846890989a759ba2973e427dff5c9bb0000000000000000000000000000000000000000000000004563918244f40000c0
<= 00d6814aa5db69de910824b14462af006fde864224c616ab93e30f646e7309a93f0312ac6e580e918ce6e39e5f910cb95ba7b68167f4d71e581dec2495a198ecc09000
`)
);
const eth = new Eth(transport);
const result = await eth.signTransaction(
"44'/60'/0'/0/0",
"02f8740106843b9aca008504a817c80082520894e41d2489571d322189246dafa5ebde1f4699f498872386f26fc10000b844095ea7b3000000000000000000000000221657776846890989a759ba2973e427dff5c9bb0000000000000000000000000000000000000000000000004563918244f40000c0"
);
expect(result).toEqual({
r: "d6814aa5db69de910824b14462af006fde864224c616ab93e30f646e7309a93f",
s: "0312ac6e580e918ce6e39e5f910cb95ba7b68167f4d71e581dec2495a198ecc0",
v: "00",
});
});

test("signTransaction supports EIP2930", async () => {
const transport = await openTransportReplayer(
RecordStore.fromString(`
Expand Down

0 comments on commit 6ba1f9a

Please sign in to comment.