Skip to content

Commit

Permalink
Merge pull request #538 from GridPlus/fix-bignumber-decode
Browse files Browse the repository at this point in the history
fixes bignumber cbor decoding
  • Loading branch information
netbonus authored Jul 9, 2024
2 parents 6e93426 + 3e04678 commit e6b0fe4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
13 changes: 5 additions & 8 deletions src/__test__/e2e/eth.msg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
import { HARDENED_OFFSET } from '../../constants';
import { randomBytes } from '../../util';
import { buildEthMsgReq, buildRandomMsg } from '../utils/builders';
import { getN } from '../utils/getters';
import { initializeClient } from '../utils/initializeClient';
import { runEthMsg } from '../utils/runners';

const numRandom = getN() ? getN() : 20; // Number of random tests to conduct

describe('ETH Messages', () => {
const client = initializeClient();

Expand Down Expand Up @@ -101,8 +98,8 @@ describe('ETH Messages', () => {
).rejects.toThrow(/Invalid Request/);
});

describe(`Test ${numRandom} random payloads`, () => {
for (let i = 0; i < numRandom; i++) {
describe(`Test ${5} random payloads`, () => {
for (let i = 0; i < 5; i++) {
it(`Payload: ${i}`, async () => {
await runEthMsg(
buildEthMsgReq(
Expand Down Expand Up @@ -284,7 +281,7 @@ describe('ETH Messages', () => {
],
Order: [
{ name: 'sender', type: 'bytes32' },
{ name: 'priceX18', type: 'uint256' },
{ name: 'priceX18', type: 'int128' },
{ name: 'amount', type: 'int128' },
{ name: 'expiration', type: 'uint64' },
{ name: 'nonce', type: 'uint64' },
Expand Down Expand Up @@ -1296,8 +1293,8 @@ describe('ETH Messages', () => {
await runEthMsg(buildEthMsgReq(msg, 'eip712'), client);
});

describe(`test ${numRandom} random payloads`, () => {
for (let i = 0; i < numRandom; i++) {
describe('test 5 random payloads', () => {
for (let i = 0; i < 5; i++) {
it(`Payload #: ${i}`, async () => {
await runEthMsg(
buildEthMsgReq(buildRandomMsg('eip712', client), 'eip712'),
Expand Down
21 changes: 19 additions & 2 deletions src/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const validateEthereumMsgResponse = function (res, req) {
useEIP155: false,
});
} else if (input.protocol === 'eip712') {
req = convertBigNumbers(req);
const encoded = TypedDataUtils.eip712Hash(
req.input.payload,
SignTypedDataVersion.V4,
Expand All @@ -81,6 +82,22 @@ const validateEthereumMsgResponse = function (res, req) {
}
};

function convertBigNumbers(obj) {
if (BN.isBigNumber(obj)) {
return obj.toFixed();
} else if (Array.isArray(obj)) {
return obj.map(convertBigNumbers);
} else if (typeof obj === 'object' && obj !== null) {
const newObj = {};
for (const [key, value] of Object.entries(obj)) {
newObj[key] = convertBigNumbers(value);
}
return newObj;
} else {
return obj;
}
}

const buildEthereumTxRequest = function (data) {
try {
let { chainId = 1 } = data;
Expand Down Expand Up @@ -883,7 +900,7 @@ function parseEIP712Item(data, type, forJSParser = false) {
// TODO: Find another cbor lib that is compataible with the firmware's lib in a browser
// context. This is surprisingly difficult - I tried several libs and only cbor/borc have
// worked (borc is a supposedly "browser compatible" version of cbor)
data = new cbor.Encoder().semanticTypes[1][0](data);
data = new BN(data);
} else if (
ethMsgProtocol.TYPED_DATA.typeCodes[type] &&
(type.indexOf('uint') > -1 || type.indexOf('int') > -1)
Expand All @@ -902,7 +919,7 @@ function parseEIP712Item(data, type, forJSParser = false) {
data = `0x${b.toString('hex')}`;
} else {
// Load into bignumber.js used by cbor lib
data = new cbor.Encoder().semanticTypes[1][0](b.toString('hex'), 16);
data = new BN(b.toString('hex'), 16);
}
} else if (type === 'bool') {
// Booleans need to be cast to a u8
Expand Down

0 comments on commit e6b0fe4

Please sign in to comment.