Skip to content

Commit

Permalink
Check for Buffer in rawDecode
Browse files Browse the repository at this point in the history
  • Loading branch information
chikeichan committed Mar 5, 2019
1 parent d84a967 commit ff783d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# IDE
.idea
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ ABI.rawEncode = function (types, values) {

ABI.rawDecode = function (types, data) {
var ret = []

if (!Buffer.isBuffer(data)) {
throw new Error('expect data to be Buffer')
}

data = new Buffer(data)
var offset = 0
for (var i = 0; i < types.length; i++) {
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ it('decoding address with leading 0', function () {

// Homebrew decoding tests

describe('decoding non-Buffer', function () {
it('should throw error', function () {
try {
abi.rawDecode([ 'uint32' ], 'hello')
} catch (error) {
assert.equal(error.message, 'expect data to be Buffer')
}
})
})

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

0 comments on commit ff783d8

Please sign in to comment.