Skip to content

Commit

Permalink
Replace regex with 'isArray' call and handle elements exceeding array…
Browse files Browse the repository at this point in the history
… size

Also, split test cases into different 'describes' and
address lint issues.
  • Loading branch information
Alex Forshtat committed Aug 8, 2019
1 parent d1fdbd4 commit 7a0ac94
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
20 changes: 13 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,20 +450,26 @@ ABI.stringify = function (types, values) {
ABI.solidityHexValue = function (type, value, bitsize) {
// pass in bitsize = null if use default bitsize
var size, num
if (type.match('^[a-zA-Z0-9_]+(\\[[0-9]*\\])+$')) {
var sub_type = type.replace(/\[.*?\]/, '')
var array_values = value.map(function (v) {
return ABI.solidityHexValue(sub_type, v, 256)
if (isArray(type)) {
var subType = type.replace(/\[.*?\]/, '')
if (!isArray(subType)) {
var arraySize = parseTypeArray(type)
if (arraySize !== 'dynamic' && arraySize !== 0 && value.length > arraySize) {
throw new Error('Elements exceed array size: ' + arraySize)
}
}
var arrayValues = value.map(function (v) {
return ABI.solidityHexValue(subType, v, 256)
})
return Buffer.concat(array_values)
return Buffer.concat(arrayValues)
} else if (type === 'bytes') {
return value
} else if (type === 'string') {
return new Buffer(value, 'utf8')
return Buffer.from(value, 'utf8')
} else if (type === 'bool') {
bitsize = bitsize || 8
var padding = Array((bitsize) / 4).join('0')
return new Buffer(value ? padding + '1' : padding + '0', 'hex')
return Buffer.from(value ? padding + '1' : padding + '0', 'hex')
} else if (type === 'address') {
var bytesize = 20
if (bitsize) {
Expand Down
58 changes: 44 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,28 +511,58 @@ describe('solidity tight packing multiple arguments', function () {
})
})

describe('solidity tight packing arrays', function () {
describe('solidity tight packing uint32[]', function () {
it('should equal', function () {
var a = abi.solidityPack(
[ 'uint32[]' ],
[ [ 8, 9 ] ]
['uint32[]'],
[[8, 9]]
)
var b = '00000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009'
assert.equal(a.toString('hex'), b.toString('hex'))
assert.strict.equal(a.toString('hex'), b.toString('hex'))
})
})

a = abi.solidityPack(
[ 'bool[][]' ],
[ [[true, false], [false, true]] ]
describe('solidity tight packing bool[][]', function () {
it('should equal', function () {
let a = abi.solidityPack(
['bool[][]'],
[[[true, false], [false, true]]]
)
b = '0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'
assert.equal(a.toString('hex'), b.toString('hex'))
let b = '0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'
assert.strict.equal(a.toString('hex'), b.toString('hex'))
})
})

a = abi.solidityPack(
[ 'address[]' ],
[ [new BN('43989fb883ba8111221e89123897538475893837', 16)] ]
describe('solidity tight packing address[]', function () {
it('should equal', function () {
let a = abi.solidityPack(
['address[]'],
[[new BN('43989fb883ba8111221e89123897538475893837', 16)]]
)
b = '00000000000000000000000043989fb883ba8111221e89123897538475893837'
assert.equal(a.toString('hex'), b.toString('hex'))
let b = '00000000000000000000000043989fb883ba8111221e89123897538475893837'
assert.strict.equal(a.toString('hex'), b.toString('hex'))
})
})

describe('solidity tight packing uint32[2]', function () {
it('should equal', function () {
let a = abi.solidityPack(
['uint32[2]'],
[[11, 12]]
)
let b = '000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c'
assert.strict.equal(a.toString('hex'), b.toString('hex'))
})
})

describe('solidity tight packing uint32[2] with wrong array length', function () {
it('should throw', function () {
assert.throws(function () {
abi.solidityPack(
['uint32[2]'],
[[11, 12, 13]]
)
})
})
})

Expand Down

0 comments on commit 7a0ac94

Please sign in to comment.