Skip to content

Commit

Permalink
fix: decoded bool always 1
Browse files Browse the repository at this point in the history
fix #35
  • Loading branch information
osdio committed Jun 30, 2021
1 parent 682230c commit 05eb008
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/abi/coder/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getRawData({ type, typeStr, actualByteLen }, params, _params) {
case 'address':
return getAddressFromOriginalAddress(params);
case 'bool':
return showNumber(params ? '1' : '0', 'uint');
return showNumber(params === '01' ? '1' : '0', 'uint');
case 'number':
return showNumber(params, typeStr, actualByteLen, _params);
case 'gid':
Expand Down
27 changes: 26 additions & 1 deletion test/packages/abi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require('assert');

import { type } from 'os';
import * as abi from '../../src/abi/index';

describe('encodeParameter', function () {
Expand Down Expand Up @@ -294,10 +295,14 @@ describe('encodeParameter', function () {
const encodeParameterResult5666 = abi.encodeParameter('gid', '01000000000000000000');
assert.equal('0000000000000000000000000000000000000000000001000000000000000000', encodeParameterResult5666);
});
it('bool', function () {
it('bool: 0000000000000000000000000000000000000000000000000000000000000001 is true', function () {
const encodeParameterResult5667 = abi.encodeParameter('bool', true);
assert.equal('0000000000000000000000000000000000000000000000000000000000000001', encodeParameterResult5667);
});
it('bool: 0000000000000000000000000000000000000000000000000000000000000000 is false', function () {
const encodeParameterResult5667 = abi.encodeParameter('bool', false);
assert.equal('0000000000000000000000000000000000000000000000000000000000000000', encodeParameterResult5667);
});
it('bytes32[]', function () {
const encodeParameterResult58 = abi.encodeParameter('bytes32[]', [ '0x0100000000000000000000000000000000000000000000000000000000000000', '0x0200000000000000000000000000000000000000000000000000000000000000' ]);
assert.equal('000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000', encodeParameterResult58);
Expand Down Expand Up @@ -355,6 +360,14 @@ describe('decodeParameter', function () {
const encodeParameterResult1 = abi.decodeParameter('uint8', '0000000000000000000000000000000000000000000000000000000000000002');
assert.equal('2', encodeParameterResult1);
});
it('bool: 0000000000000000000000000000000000000000000000000000000000000001 is decode to 1', function () {
const encodeParameterResult1 = abi.decodeParameter('bool', '0000000000000000000000000000000000000000000000000000000000000001');
assert.equal('1', encodeParameterResult1);
});
it('bool: 0000000000000000000000000000000000000000000000000000000000000000 is decode to 0', function () {
const encodeParameterResult1 = abi.decodeParameter('bool', '0000000000000000000000000000000000000000000000000000000000000000');
assert.equal('0', encodeParameterResult1);
});
it('uint16', function () {
const encodeParameterResult3 = abi.decodeParameter('uint16', '0000000000000000000000000000000000000000000000000000000000000002');
assert.equal('2', encodeParameterResult3);
Expand Down Expand Up @@ -762,6 +775,18 @@ describe('decodeParameters', function () {
const encodeParametersResult6 = abi.decodeParameters([ 'int64', 'int32[]', 'int8' ], 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000060fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff70000000000000000000000000000000000000000000000000000000000000002ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9dfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb');
assert.deepEqual([ -1, [ -99, -5 ], -9 ], encodeParametersResult6);
});

it('case 9: decode for bool', function () {
const types = [
{ name: 'success', type: 'bool' },
{ name: 'successStr', type: 'string' }
];
const result = abi.decodeParameters(types, Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEdHJ1ZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=', 'base64').toString('hex'));
assert.deepEqual([ '1', 'true' ], result);

const result2 = abi.decodeParameters(types, Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFZmFsc2UAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=', 'base64').toString('hex'));
assert.deepEqual([ '0', 'false' ], result2);
});
});

describe('encodeFunctionSignature', function () {
Expand Down

0 comments on commit 05eb008

Please sign in to comment.