Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

toBuffer now throws if strings aren't 0x-prefixed hex values #197

Merged
merged 10 commits into from
May 14, 2019
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/

### App ###

.cachedb
Expand Down
90 changes: 45 additions & 45 deletions docs/README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/ecdsasignature.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

**● r**: *`Buffer`*

*Defined in [index.ts:13](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/index.ts#L13)*
*Defined in [signature.ts:8](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/signature.ts#L8)*

___
<a id="s"></a>
Expand All @@ -33,7 +33,7 @@ ___

**● s**: *`Buffer`*

*Defined in [index.ts:14](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/index.ts#L14)*
*Defined in [signature.ts:9](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/signature.ts#L9)*

___
<a id="v"></a>
Expand All @@ -42,7 +42,7 @@ ___

**● v**: *`number`*

*Defined in [index.ts:12](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/index.ts#L12)*
*Defined in [signature.ts:7](https://github.com/ethereumjs/ethereumjs-util/blob/master/src/signature.ts#L7)*

___

4 changes: 3 additions & 1 deletion src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export const toBuffer = function(v: any): Buffer {
if (ethjsUtil.isHexString(v)) {
v = Buffer.from(ethjsUtil.padToEven(ethjsUtil.stripHexPrefix(v)), 'hex')
} else {
v = Buffer.from(v)
throw new Error(
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`,
)
}
} else if (typeof v === 'number') {
v = ethjsUtil.intToBuffer(v)
Expand Down
11 changes: 9 additions & 2 deletions src/hash.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const createKeccakHash = require('keccak')
const createHash = require('create-hash')
const ethjsUtil = require('ethjs-util')
import rlp = require('rlp')
import { toBuffer, setLength } from './bytes'

/**
* Creates Keccak hash of the input
* @param a The input data (Buffer|Array|String|Number)
* @param a The input data (Buffer|Array|String|Number) If the string is a 0x-prefixed hex value
* it's interpreted as hexadecimal, otherwise as utf8.
* @param bits The Keccak width
*/
export const keccak = function(a: any, bits: number = 256): Buffer {
a = toBuffer(a)
if (typeof a === 'string' && !ethjsUtil.isHexString(a)) {
a = Buffer.from(a, 'utf8')
} else {
a = toBuffer(a)
}

if (!bits) bits = 256

return createKeccakHash(`keccak${bits}`)
Expand Down
7 changes: 5 additions & 2 deletions src/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ export const isValidSignature = function(
* call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
* used to produce the signature.
*/
export const hashPersonalMessage = function(message: any): Buffer {
const prefix = toBuffer(`\u0019Ethereum Signed Message:\n${message.length.toString()}`)
export const hashPersonalMessage = function(message: Buffer): Buffer {
const prefix = Buffer.from(
`\u0019Ethereum Signed Message:\n${message.length.toString()}`,
'utf-8',
)
return keccak(Buffer.concat([prefix, message]))
}

Expand Down
40 changes: 20 additions & 20 deletions test/defineFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ describe('define', function () {
it('it should accept rlp encoded intial data', function () {
var someOb = {}
var data = {
aword: 'test',
cannotBeZero: 'not zero',
value: 'a value',
r: 'rrr'
aword: '0x01',
cannotBeZero: '0x02',
value: '0x03',
r: '0x04'
}

var expected = {
aword: '0x74657374',
aword: '0x01',
empty: '0x',
cannotBeZero: '0x6e6f74207a65726f',
value: '0x612076616c7565',
r: '0x727272'
cannotBeZero: '0x02',
value: '0x03',
r: '0x04'
}

var expectedArray = [
'0x74657374', '0x', '0x6e6f74207a65726f', '0x612076616c7565', '0x727272'
'0x01', '0x', '0x02', '0x03', '0x04'
]

ethUtil.defineProperties(someOb, fields, data)
Expand Down Expand Up @@ -100,25 +100,25 @@ describe('define', function () {
it('alias should work ', function () {
var someOb = {}
var data = {
aword: 'test',
cannotBeZero: 'not zero',
value: 'a value',
r: 'rrr'
aword: '0x01',
cannotBeZero: '0x02',
value: '0x03',
r: '0x04'
}

ethUtil.defineProperties(someOb, fields, data)
assert.equal(someOb.blah.toString(), 'test')
someOb.blah = 'lol'
assert.equal(someOb.blah.toString(), 'lol')
assert.equal(someOb.aword.toString(), 'lol')
assert.equal(someOb.blah.toString('hex'), '01')
someOb.blah = '0x09'
assert.equal(someOb.blah.toString('hex'), '09')
assert.equal(someOb.aword.toString('hex'), '09')
})

it('alias should work #2', function () {
var someOb = {}
var data = { blah: '42' }
var data = { blah: '0x1' }

ethUtil.defineProperties(someOb, fields, data)
assert.equal(someOb.blah, '42')
assert.equal(someOb.aword, '42')
assert.equal(someOb.blah.toString('hex'), '01')
assert.equal(someOb.aword.toString('hex'), '01')
})
})
17 changes: 12 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,11 @@ describe('privateToAddress', function () {
})
})

describe('generateAddress', function () {
it('should produce an address given a public key', function () {
const add = ethUtils.generateAddress('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex')
describe('generateAddress', function() {
it('should produce an address given a public key', function() {
const add = ethUtils
.generateAddress(Buffer.from('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 'utf8'), 14)
.toString('hex')
assert.equal(add.toString('hex'), '936a4295d8d74e310c0c95f0a63e53737b998d12')
})
})
Expand Down Expand Up @@ -484,10 +486,9 @@ describe('toBuffer', function () {
// Array
assert.deepEqual(ethUtils.toBuffer([]), Buffer.allocUnsafe(0))
// String
assert.deepEqual(ethUtils.toBuffer('11'), Buffer.from([49, 49]))
assert.deepEqual(ethUtils.toBuffer('0x11'), Buffer.from([17]))
assert.deepEqual(ethUtils.toBuffer('1234').toString('hex'), '31323334')
assert.deepEqual(ethUtils.toBuffer('0x1234').toString('hex'), '1234')
assert.deepEqual(ethUtils.toBuffer('0x'), Buffer.from([]))
// Number
assert.deepEqual(ethUtils.toBuffer(1), Buffer.from([1]))
// null
Expand All @@ -504,6 +505,12 @@ describe('toBuffer', function () {
ethUtils.toBuffer({ test: 1 })
})
})

it('should fail with non 0x-prefixed hex strings', function() {
assert.throws(() => ethUtils.toBuffer('11'), '11')
assert.throws(() => ethUtils.toBuffer(''))
assert.throws(() => ethUtils.toBuffer('0xR'), '0xR')
})
})

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