Skip to content

Commit

Permalink
Merge pull request #152 from GridPlus/eip712-edge-cases
Browse files Browse the repository at this point in the history
Adds support for EIP712 edge cases
  • Loading branch information
alex-miller-0 authored May 5, 2021
2 parents aaf2d58 + c814eba commit f9cf6aa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gridplus-sdk",
"version": "0.7.12",
"version": "0.7.13",
"description": "SDK to interact with GridPlus Lattice1 device",
"scripts": {
"commit": "git-cz",
Expand Down
13 changes: 11 additions & 2 deletions src/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,10 @@ function parseEIP712Item(data, type, isEthers=false) {
} else if (type === 'address') {
// Address must be a 20 byte buffer
data = ensureHexBuffer(data);
// Edge case to handle the 0-address
if (data.length === 0) {
data = Buffer.alloc(20);
}
if (data.length !== 20)
throw new Error(`Address type must be 20 bytes, but got ${data.length} bytes`);
// Ethers wants addresses as hex strings
Expand All @@ -615,10 +619,15 @@ function parseEIP712Item(data, type, isEthers=false) {
} else if (type === 'uint8' || type === 'uint16' || type === 'uint32' || type === 'uint64') {
data = parseInt(ensureHexBuffer(data).toString('hex'), 16)
} else if (type === 'uint256') {
let b = ensureHexBuffer(data);
// Edge case to handle 0-value bignums
if (b.length === 0) {
b = Buffer.from('00', 'hex');
}
// Uint256s should be encoded as bignums.
if (isEthers === true) {
// `ethers` uses their own BigNumber lib
data = ethers.BigNumber.from(`0x${ensureHexBuffer(data).toString('hex')}`)
data = ethers.BigNumber.from(`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.
Expand All @@ -628,7 +637,7 @@ function parseEIP712Item(data, type, isEthers=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](ensureHexBuffer(data).toString('hex'), 16)
data = new cbor.Encoder().semanticTypes[1][0](b.toString('hex'), 16)
}
} else if (type === 'bool') {
// Booleans need to be cast to a u8
Expand Down
41 changes: 41 additions & 0 deletions test/testEthMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,47 @@ describe('Test ETH EIP712', function() {
}
})

it('Should test an example with 0 values', async () => {
const msg = {
'types': {
'EIP712Domain': [
{ name: 'name', type: 'string' },
{ name: 'host', type: 'string'},
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
'Test': [
{ name: 'owner', type: 'string' },
]
},
'domain':{
name: 'Opensea on Matic',
verifyingContract: '0x0',
version: '1',
chainId: '',
host: '',
},
'primaryType': 'Test',
'message': {
'owner': '0x56626bd0d646ce9da4a12403b2c1ba00fb9e1c43',
}
}
const req = {
currency: 'ETH_MSG',
data: {
signerPath: [helpers.BTC_LEGACY_PURPOSE, helpers.ETH_COIN, HARDENED_OFFSET, 0, 0],
protocol: 'eip712',
payload: msg,
}
}
try {
await helpers.sign(client, req);
} catch (err) {
expect(err).to.equal(null)
}
})

it('Should test canonical EIP712 example', async () => {
const msg = {
types: {
Expand Down

0 comments on commit f9cf6aa

Please sign in to comment.