diff --git a/package-lock.json b/package-lock.json index a16f71a1..f7654034 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gridplus-sdk", - "version": "2.5.1", + "version": "2.5.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gridplus-sdk", - "version": "2.5.1", + "version": "2.5.2", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 48c22567..7514a662 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gridplus-sdk", - "version": "2.5.1", + "version": "2.5.2", "description": "SDK to interact with GridPlus Lattice1 device", "scripts": { "build": "NODE_ENV=production tsc -p tsconfig.json", @@ -109,4 +109,4 @@ "vitest": "^0.15.2" }, "license": "MIT" -} +} \ No newline at end of file diff --git a/src/__test__/e2e/eth.msg.test.ts b/src/__test__/e2e/eth.msg.test.ts index b13789cf..515a379f 100644 --- a/src/__test__/e2e/eth.msg.test.ts +++ b/src/__test__/e2e/eth.msg.test.ts @@ -273,6 +273,41 @@ describe('ETH Messages', () => { await runEthMsg(buildEthMsgReq(msg, 'eip712'), client); }); + it('Should test Vertex message', async () => { + const msg = { + 'types': { + 'EIP712Domain': [ + {'name': 'name', 'type': 'string'}, + {'name': 'version', 'type': 'string'}, + {'name': 'chainId', 'type': 'uint256'}, + {'name': 'verifyingContract', 'type': 'address'} + ], + 'Order': [ + {'name': 'sender', 'type': 'bytes32'}, + {'name': 'priceX18', 'type': 'int128'}, + {'name': 'amount', 'type': 'int128'}, + {'name': 'expiration', 'type': 'uint64'}, + {'name': 'nonce', 'type': 'uint64'}, + ], + }, + 'primaryType': 'Order', + 'domain': { + 'name': 'Vertex', + 'version': '0.0.1', + 'chainId': '42161', + 'verifyingContract': '0xf03f457a30e598d5020164a339727ef40f2b8fbc' + }, + 'message': { + 'sender': '0x841fe4876763357975d60da128d8a54bb045d76a64656661756c740000000000', + 'priceX18': '28898000000000000000000', + 'amount': '-10000000000000000', + 'expiration': '4611687701117784255', + 'nonce': '1764428860167815857', + }, + }; + await runEthMsg(buildEthMsgReq(msg, 'eip712'), client); + }) + it('Should test a large 1inch transaction', async () => { const msg = { domain: { diff --git a/src/ethereum.ts b/src/ethereum.ts index 2dc1d902..1c9db291 100644 --- a/src/ethereum.ts +++ b/src/ethereum.ts @@ -868,10 +868,27 @@ function parseEIP712Item(data, type, forJSParser = false) { if (forJSParser) { data = `0x${data.toString('hex')}`; } + } else if ( + ethMsgProtocol.TYPED_DATA.typeCodes[type] && + (type.indexOf('uint') === -1 && type.indexOf('int') > -1) + ) { + // Handle signed integers using bignumber.js directly + // `bignumber.js` is needed for `cbor` encoding, which gets sent to the Lattice and plays + // nicely with its firmware cbor lib. + // NOTE: If we instantiate a `bignumber.js` object, it will not match what `borc` creates + // when run inside of the browser (i.e. MetaMask). Thus we introduce this hack to make sure + // we are creating a compatible type. + // 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); } else if ( ethMsgProtocol.TYPED_DATA.typeCodes[type] && (type.indexOf('uint') > -1 || type.indexOf('int') > -1) ) { + // For uints, convert to a buffer and do some sanity checking. + // Note that we could probably just use bignumber.js directly as we do with + // signed ints, but this code is battle tested and we don't want to change it. let b = ensureHexBuffer(data); // Edge case to handle 0-value bignums if (b.length === 0) { @@ -882,14 +899,7 @@ function parseEIP712Item(data, type, forJSParser = false) { // For EIP712 encoding in this module we need strings to represent the numbers data = `0x${b.toString('hex')}`; } else { - // `bignumber.js` is needed for `cbor` encoding, which gets sent to the Lattice and plays - // nicely with its firmware cbor lib. - // NOTE: If we instantiate a `bignumber.js` object, it will not match what `borc` creates - // when run inside of the browser (i.e. MetaMask). Thus we introduce this hack to make sure - // we are creating a compatible type. - // 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) + // Load into bignumber.js used by cbor lib data = new cbor.Encoder().semanticTypes[1][0](b.toString('hex'), 16); } } else if (type === 'bool') {