From f91723fdd58ddb95d5087f0ebd719c6ae94ef99c Mon Sep 17 00:00:00 2001 From: Alex Miller Date: Tue, 15 Jun 2021 14:00:22 -0500 Subject: [PATCH] Fixes issue introduced in v0.7.22 In some cases we want hex buffers to represent `0` with a 0-length buffer, and in others we want it represented as a 1-byte buffer `00` --- package-lock.json | 2 +- package.json | 2 +- src/ethereum.js | 12 ++++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 79fe6e2a..2126483a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gridplus-sdk", - "version": "0.7.22", + "version": "0.7.23", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7d943370..2ba4d39c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gridplus-sdk", - "version": "0.7.22", + "version": "0.7.23", "description": "SDK to interact with GridPlus Lattice1 device", "scripts": { "commit": "git-cz", diff --git a/src/ethereum.js b/src/ethereum.js index 9dad053b..67d73655 100644 --- a/src/ethereum.js +++ b/src/ethereum.js @@ -428,10 +428,13 @@ function isBase10NumStr(x) { // Ensure a param is represented by a buffer // TODO: Remove circular dependency in util.js so that we can put this function there -function ensureHexBuffer(x) { +function ensureHexBuffer(x, zeroIsNull=true) { try { - // For null values, return a 0-sized buffer - if (x === null) return Buffer.alloc(0); + // For null values, return a 0-sized buffer. For most situations we assume + // 0 should be represented with a zero-length buffer (e.g. for RLP-building + // txs), but it can also be treated as a 1-byte buffer (`00`) if needed + if (x === null || (x === 0 && zeroIsNull === true)) + return Buffer.alloc(0); const isNumber = typeof x === 'number' || isBase10NumStr(x); // Otherwise try to get this converted to a hex string if (isNumber) { @@ -646,7 +649,8 @@ function parseEIP712Item(data, type, isEthers=false) { data = `0x${data.toString('hex')}` } } else if (type === 'uint8' || type === 'uint16' || type === 'uint32' || type === 'uint64') { - data = parseInt(ensureHexBuffer(data).toString('hex'), 16) + // In this case we want the hex buffer to represent `0` as a 1-byte buffer (`00`) + data = parseInt(ensureHexBuffer(data, false).toString('hex'), 16) } else if (type === 'uint256') { let b = ensureHexBuffer(data); // Edge case to handle 0-value bignums