Skip to content

Commit

Permalink
Merge pull request #159 from GridPlus/eip712-fix
Browse files Browse the repository at this point in the history
Fixes some edge cases in parsing EIP712 messages
  • Loading branch information
alex-miller-0 authored Jun 14, 2021
2 parents 4270119 + 4da1e36 commit 16314d8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.21",
"version": "0.7.22",
"description": "SDK to interact with GridPlus Lattice1 device",
"scripts": {
"commit": "git-cz",
Expand Down
9 changes: 4 additions & 5 deletions src/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,10 @@ function isBase10NumStr(x) {
function ensureHexBuffer(x) {
try {
// For null values, return a 0-sized buffer
if (x === null || x === 0) return Buffer.alloc(0);
if (x === null) return Buffer.alloc(0);
const isNumber = typeof x === 'number' || isBase10NumStr(x);
// Otherwise try to get this converted to a hex string
if (typeof x === 'number' || isBase10NumStr(x)) {
if (isNumber) {
// If this is a number or a base-10 number string, convert it to hex
x = `${new BN(x).toString(16)}`;
} else if (typeof x === 'string' && x.slice(0, 2) === '0x') {
Expand All @@ -442,7 +443,7 @@ function ensureHexBuffer(x) {
x = x.toString('hex')
}
if (x.length % 2 > 0) x = `0${x}`;
if (x === '00')
if (x === '00' && !isNumber)
return Buffer.alloc(0);
return Buffer.from(x, 'hex');
} catch (err) {
Expand Down Expand Up @@ -625,8 +626,6 @@ function parseEIP712Item(data, type, isEthers=false) {
if (type === 'bytes') {
// Variable sized bytes need to be buffer type
data = ensureHexBuffer(data);
if (data.length === 0)
throw new Error('"bytes" type must contain at least one byte in value');
} else if (type.slice(0, 5) === 'bytes') {
// Fixed sizes bytes need to be buffer type. We also add some sanity checks.
const nBytes = parseInt(type.slice(5));
Expand Down

0 comments on commit 16314d8

Please sign in to comment.