Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #34 #42

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('ethereumjs-util')
const BN = require('bn.js')
const util = require('util')

var ABI = function () {
}
Expand Down Expand Up @@ -28,8 +29,7 @@ function elementaryName (name) {
}

ABI.eventID = function (name, types) {
// FIXME: use node.js util.format?
var sig = name + '(' + types.map(elementaryName).join(',') + ')'
var sig = util.format('%s(%s)', name, types.map(elementaryName).join(','))
return utils.sha3(new Buffer(sig))
}

Expand Down Expand Up @@ -106,7 +106,8 @@ function encodeSingle (type, arg) {
var size, num, ret, i

if (type === 'address') {
return encodeSingle('uint160', parseNumber(arg))
arg = new Buffer(utils.stripHexPrefix(arg), 'hex')
return Buffer.concat([ utils.zeros(32 - arg.length), arg ])
} else if (type === 'bool') {
return encodeSingle('uint8', arg ? 1 : 0)
} else if (type === 'string') {
Expand Down Expand Up @@ -217,7 +218,7 @@ function decodeSingle (type, arg) {
var size, num, ret, i

if (type === 'address') {
return decodeSingle('uint160', arg)
return utils.addHexPrefix(arg.slice(12).toString('hex'))
} else if (type === 'bool') {
return decodeSingle('uint8', arg).toString() === new BN(1).toString()
} else if (type === 'string') {
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ describe('encoding', function () {
var b = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
assert.equal(a, b)
})
it('should work for address', function () {
var a = abi.rawEncode([ 'address' ], [ '0xce85ce682f7193991c9460403007a29badca198d' ]).toString('hex')
var b = '000000000000000000000000ce85ce682f7193991c9460403007a29badca198d'
assert.equal(a, b)
})
})

describe('encoding bytes33', function () {
Expand Down Expand Up @@ -229,6 +234,14 @@ describe('decoding string', function () {
})
})

describe('decoding address', function () {
it('should equal', function () {
var a = abi.rawDecode([ 'address' ], new Buffer('000000000000000000000000ce85ce682f7193991c9460403007a29badca198d', 'hex'))
var b = '0xce85ce682f7193991c9460403007a29badca198d'
assert.equal(a, b)
})
})

describe('decoding int32', function () {
it('should equal', function () {
var a = abi.rawDecode([ 'int32' ], new Buffer('fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe', 'hex'))
Expand Down