From f2efc50b61f1b2534bbd0f411d47c54d2cf1ac7b Mon Sep 17 00:00:00 2001 From: dcb9 Date: Tue, 6 Nov 2018 22:53:35 +0800 Subject: [PATCH] converts bytes[N] with hex value --- package.json | 1 + src/formatters/encoder.js | 7 ++- src/formatters/tests/encoder.tests.js | 71 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3abfb87..c9e26f6 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "ethjs-abi": "^0.2.1", "lodash": "^4.17.10", "utf8": "^3.0.0", + "web3-eth-abi": "^1.0.0-beta.36", "web3-utils": "^1.0.0-beta.36" }, "devDependencies": { diff --git a/src/formatters/encoder.js b/src/formatters/encoder.js index b236cc2..fffd271 100644 --- a/src/formatters/encoder.js +++ b/src/formatters/encoder.js @@ -1,5 +1,6 @@ const _ = require('lodash'); const Web3Utils = require('web3-utils'); +const Web3ETHAbi = require('web3-eth-abi'); const BigNumber = require('bignumber.js'); const bs58 = require('bs58'); @@ -236,7 +237,11 @@ class Encoder { hex = this.uintToHex(value); } } else if (type.match(Constants.REGEX_BYTES)) { // fixed bytes, ie. bytes32 - hex = this.stringToHex(value, Constants.MAX_HEX_CHARS_PER_BYTE); + if (Web3Utils.isHexStrict(value)) { + hex = Web3ETHAbi.encodeParameter(type, value).substr(2); + } else { + hex = this.stringToHex(value, Constants.MAX_HEX_CHARS_PER_BYTE); + } } else if (type.match(Constants.REGEX_STATIC_BYTES_ARRAY)) { // fixed bytes array, ie. bytes32[10] const arrCapacity = _.toNumber(type.match(Constants.REGEX_NUMBER)[1]); if (value instanceof Array) { diff --git a/src/formatters/tests/encoder.tests.js b/src/formatters/tests/encoder.tests.js index 8fdad24..8030308 100644 --- a/src/formatters/tests/encoder.tests.js +++ b/src/formatters/tests/encoder.tests.js @@ -844,6 +844,50 @@ describe('Encoder', () => { .toLowerCase()); }); + it('converts bytes types with hex value', () => { + let methodObj = { + constant: true, + inputs: [ + { + name: '', + type: 'bytes32', + }, + ], + name: 'didWithdraw', + outputs: [], + payable: false, + stateMutability: 'view', + type: 'function', + }; + let args = ['0x68656c6c6f']; + let dataHex = Encoder.constructData([methodObj], 'didWithdraw', args); + + let funcHash = Encoder.objToHash(methodObj, true); + let param = '68656c6c6f000000000000000000000000000000000000000000000000000000'; + assert.equal(dataHex, funcHash.concat(param)); + + methodObj = { + constant: true, + inputs: [ + { + name: '', + type: 'bytes8', + }, + ], + name: 'didWithdraw', + outputs: [], + payable: false, + stateMutability: 'view', + type: 'function', + }; + args = ['0x68656c6c6f']; + dataHex = Encoder.constructData([methodObj], 'didWithdraw', args); + + funcHash = Encoder.objToHash(methodObj, true); + param = '68656c6c6f000000000000000000000000000000000000000000000000000000'; + assert.equal(dataHex, funcHash.concat(param)); + }); + it('converts bytes types', () => { let methodObj = { constant: true, @@ -973,6 +1017,33 @@ describe('Encoder', () => { assert.equal(dataHex, funcHash); }); + it('converts bytes32 types', () => { + const methodObj = { + constant: true, + inputs: [ + { + name: 'v', + type: 'uint8', + }, { + name: 'r', + type: 'bytes32', + }, { + name: 's', + type: 'bytes32', + }, + ], + name: 'verify', + outputs: [], + payable: false, + stateMutability: 'view', + type: 'function', + }; + const args = ['0x1c', '0x9e72ad2e50c8cff774ce8984e9cbd47d3edb658f72a4938e8e2e475390a8e57e', '0x3685b2ee64860d800428efa29b4da13453455159a9cd2448c7ce15844f6d564d']; + const dataHex = Encoder.constructData([methodObj], 'verify', args); + + assert.equal('e2454522000000000000000000000000000000000000000000000000000000000000001c9e72ad2e50c8cff774ce8984e9cbd47d3edb658f72a4938e8e2e475390a8e57e3685b2ee64860d800428efa29b4da13453455159a9cd2448c7ce15844f6d564d', dataHex); + }); + it('throws if abi is undefined', () => { assert.throws(() => Encoder.constructData(undefined, 'test', ['qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy', 'Hello World', ['a', 'b', 'c'], 'c350', 'c738']), Error);