diff --git a/test/index.js b/test/index.js index e0866dbc..4c634100 100644 --- a/test/index.js +++ b/test/index.js @@ -1,189 +1,190 @@ -var assert = require('assert') -var ethUtils = require('../dist/index.js') -var BN = require('bn.js') +const assert = require('assert') +const ethUtils = require('../dist/index.js') +const BN = require('bn.js') +const eip1014Testdata = require('./testdata/eip1014Examples.json') describe('zeros function', function () { it('should produce lots of 0s', function () { - var z60 = ethUtils.zeros(30) - var zs60 = '000000000000000000000000000000000000000000000000000000000000' + const z60 = ethUtils.zeros(30) + const zs60 = '000000000000000000000000000000000000000000000000000000000000' assert.equal(z60.toString('hex'), zs60) }) }) describe('zero address', function () { it('should generate a zero address', function () { - var zeroAddress = ethUtils.zeroAddress() + const zeroAddress = ethUtils.zeroAddress() assert.equal(zeroAddress, '0x0000000000000000000000000000000000000000') }) }) describe('is zero address', function () { it('should return true when a zero address is passed', function () { - var isZeroAddress = ethUtils.isZeroAddress('0x0000000000000000000000000000000000000000') + const isZeroAddress = ethUtils.isZeroAddress('0x0000000000000000000000000000000000000000') assert.equal(isZeroAddress, true) }) it('should return false when the address is not equal to zero', function () { - var nonZeroAddress = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' + const nonZeroAddress = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' assert.equal(ethUtils.isZeroAddress(nonZeroAddress), false) }) }) describe('keccak', function () { it('should produce a hash', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' - var hash = ethUtils.keccak(msg) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' + const hash = ethUtils.keccak(msg) assert.equal(hash.toString('hex'), r) }) }) describe('keccak256', function () { it('should produce a hash (keccak(a, 256) alias)', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' - var hash = ethUtils.keccak256(msg) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' + const hash = ethUtils.keccak256(msg) assert.equal(hash.toString('hex'), r) }) }) describe('keccak without hexprefix', function () { it('should produce a hash', function () { - var msg = '3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '22ae1937ff93ec72c4d46ff3e854661e3363440acd6f6e4adf8f1a8978382251' - var hash = ethUtils.keccak(msg) + const msg = '3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '22ae1937ff93ec72c4d46ff3e854661e3363440acd6f6e4adf8f1a8978382251' + const hash = ethUtils.keccak(msg) assert.equal(hash.toString('hex'), r) }) }) describe('keccak-512', function () { it('should produce a hash', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '36fdacd0339307068e9ed191773a6f11f6f9f99016bd50f87fd529ab7c87e1385f2b7ef1ac257cc78a12dcb3e5804254c6a7b404a6484966b831eadc721c3d24' - var hash = ethUtils.keccak(msg, 512) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '36fdacd0339307068e9ed191773a6f11f6f9f99016bd50f87fd529ab7c87e1385f2b7ef1ac257cc78a12dcb3e5804254c6a7b404a6484966b831eadc721c3d24' + const hash = ethUtils.keccak(msg, 512) assert.equal(hash.toString('hex'), r) }) }) describe('sha256', function () { it('should produce a sha256', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '58bbda5e10bc11a32d808e40f9da2161a64f00b5557762a161626afe19137445' - var hash = ethUtils.sha256(msg) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '58bbda5e10bc11a32d808e40f9da2161a64f00b5557762a161626afe19137445' + const hash = ethUtils.sha256(msg) assert.equal(hash.toString('hex'), r) }) }) describe('ripemd160', function () { it('should produce a ripemd160', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '4bb0246cbfdfddbe605a374f1187204c896fabfd' - var hash = ethUtils.ripemd160(msg) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '4bb0246cbfdfddbe605a374f1187204c896fabfd' + const hash = ethUtils.ripemd160(msg) assert.equal(hash.toString('hex'), r) }) it('should produce a padded ripemd160', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '0000000000000000000000004bb0246cbfdfddbe605a374f1187204c896fabfd' - var hash = ethUtils.ripemd160(msg, true) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '0000000000000000000000004bb0246cbfdfddbe605a374f1187204c896fabfd' + const hash = ethUtils.ripemd160(msg, true) assert.equal(hash.toString('hex'), r) }) }) describe('rlphash', function () { it('should produce a keccak-256 hash of the rlp data', function () { - var msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - var r = '33f491f24abdbdbf175e812b94e7ede338d1c7f01efb68574acd279a15a39cbe' - var hash = ethUtils.rlphash(msg) + const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' + const r = '33f491f24abdbdbf175e812b94e7ede338d1c7f01efb68574acd279a15a39cbe' + const hash = ethUtils.rlphash(msg) assert.equal(hash.toString('hex'), r) }) }) describe('unpad', function () { it('should unpad a string', function () { - var str = '0000000006600' - var r = ethUtils.unpad(str) + const str = '0000000006600' + const r = ethUtils.unpad(str) assert.equal(r, '6600') }) }) describe('unpad a hex string', function () { it('should unpad a string', function () { - var str = '0x0000000006600' - var r = ethUtils.unpad(str) + const str = '0x0000000006600' + const r = ethUtils.unpad(str) assert.equal(r, '6600') }) }) describe('pad', function () { it('should left pad a Buffer', function () { - var buf = Buffer.from([9, 9]) - var padded = ethUtils.setLength(buf, 3) + const buf = Buffer.from([9, 9]) + const padded = ethUtils.setLength(buf, 3) assert.equal(padded.toString('hex'), '000909') }) it('should left truncate a Buffer', function () { - var buf = Buffer.from([9, 0, 9]) - var padded = ethUtils.setLength(buf, 2) + const buf = Buffer.from([9, 0, 9]) + const padded = ethUtils.setLength(buf, 2) assert.equal(padded.toString('hex'), '0009') }) it('should left pad a Buffer - alias', function () { - var buf = Buffer.from([9, 9]) - var padded = ethUtils.setLengthLeft(buf, 3) + const buf = Buffer.from([9, 9]) + const padded = ethUtils.setLengthLeft(buf, 3) assert.equal(padded.toString('hex'), '000909') }) }) describe('rpad', function () { it('should right pad a Buffer', function () { - var buf = Buffer.from([9, 9]) - var padded = ethUtils.setLength(buf, 3, true) + const buf = Buffer.from([9, 9]) + const padded = ethUtils.setLength(buf, 3, true) assert.equal(padded.toString('hex'), '090900') }) it('should right truncate a Buffer', function () { - var buf = Buffer.from([9, 0, 9]) - var padded = ethUtils.setLength(buf, 2, true) + const buf = Buffer.from([9, 0, 9]) + const padded = ethUtils.setLength(buf, 2, true) assert.equal(padded.toString('hex'), '0900') }) it('should right pad a Buffer - alias', function () { - var buf = Buffer.from([9, 9]) - var padded = ethUtils.setLengthRight(buf, 3) + const buf = Buffer.from([9, 9]) + const padded = ethUtils.setLengthRight(buf, 3) assert.equal(padded.toString('hex'), '090900') }) }) describe('bufferToHex', function () { it('should convert a buffer to hex', function () { - var buf = Buffer.from('5b9ac8', 'hex') - var hex = ethUtils.bufferToHex(buf) + const buf = Buffer.from('5b9ac8', 'hex') + const hex = ethUtils.bufferToHex(buf) assert.equal(hex, '0x5b9ac8') }) it('empty buffer', function () { - var buf = Buffer.alloc(0) - var hex = ethUtils.bufferToHex(buf) + const buf = Buffer.alloc(0) + const hex = ethUtils.bufferToHex(buf) assert.strictEqual(hex, '0x') }) }) describe('intToHex', function () { it('should convert a int to hex', function () { - var i = 6003400 - var hex = ethUtils.intToHex(i) + const i = 6003400 + const hex = ethUtils.intToHex(i) assert.equal(hex, '0x5b9ac8') }) }) describe('intToBuffer', function () { it('should convert a int to a buffer', function () { - var i = 6003400 - var buf = ethUtils.intToBuffer(i) + const i = 6003400 + const buf = ethUtils.intToBuffer(i) assert.equal(buf.toString('hex'), '5b9ac8') }) }) describe('bufferToInt', function () { it('should convert a int to hex', function () { - var buf = Buffer.from('5b9ac8', 'hex') - var i = ethUtils.bufferToInt(buf) + const buf = Buffer.from('5b9ac8', 'hex') + const i = ethUtils.bufferToInt(buf) assert.equal(i, 6003400) assert.equal(ethUtils.bufferToInt(Buffer.allocUnsafe(0)), 0) }) @@ -194,15 +195,15 @@ describe('bufferToInt', function () { describe('fromSigned', function () { it('should convert an unsigned (negative) buffer to a singed number', function () { - var neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656' - var buf = Buffer.allocUnsafe(32).fill(0) + const neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656' + const buf = Buffer.allocUnsafe(32).fill(0) buf[0] = 255 assert.equal(ethUtils.fromSigned(buf), neg) }) it('should convert an unsigned (positive) buffer to a singed number', function () { - var neg = '452312848583266388373324160190187140051835877600158453279131187530910662656' - var buf = Buffer.allocUnsafe(32).fill(0) + const neg = '452312848583266388373324160190187140051835877600158453279131187530910662656' + const buf = Buffer.allocUnsafe(32).fill(0) buf[0] = 1 assert.equal(ethUtils.fromSigned(buf), neg) @@ -211,134 +212,122 @@ describe('fromSigned', function () { describe('toUnsigned', function () { it('should convert a signed (negative) number to unsigned', function () { - var neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656' - var hex = 'ff00000000000000000000000000000000000000000000000000000000000000' - var num = new BN(neg) + const neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656' + const hex = 'ff00000000000000000000000000000000000000000000000000000000000000' + const num = new BN(neg) assert.equal(ethUtils.toUnsigned(num).toString('hex'), hex) }) it('should convert a signed (positive) number to unsigned', function () { - var neg = '452312848583266388373324160190187140051835877600158453279131187530910662656' - var hex = '0100000000000000000000000000000000000000000000000000000000000000' - var num = new BN(neg) + const neg = '452312848583266388373324160190187140051835877600158453279131187530910662656' + const hex = '0100000000000000000000000000000000000000000000000000000000000000' + const num = new BN(neg) assert.equal(ethUtils.toUnsigned(num).toString('hex'), hex) }) }) describe('isValidPrivate', function () { - var SECP256K1_N = new ethUtils.BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16) + const SECP256K1_N = new ethUtils.BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16) it('should fail on short input', function () { - var tmp = '0011223344' + const tmp = '0011223344' assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), false) }) it('should fail on too big input', function () { - var tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), false) }) it('should fail on invalid curve (zero)', function () { - var tmp = '0000000000000000000000000000000000000000000000000000000000000000' + const tmp = '0000000000000000000000000000000000000000000000000000000000000000' assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), false) }) it('should fail on invalid curve (== N)', function () { - var tmp = SECP256K1_N.toString(16) + const tmp = SECP256K1_N.toString(16) assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), false) }) it('should fail on invalid curve (>= N)', function () { - var tmp = SECP256K1_N.addn(1).toString(16) + const tmp = SECP256K1_N.addn(1).toString(16) assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), false) }) it('should work otherwise (< N)', function () { - var tmp = SECP256K1_N.subn(1).toString(16) + const tmp = SECP256K1_N.subn(1).toString(16) assert.equal(ethUtils.isValidPrivate(Buffer.from(tmp, 'hex')), true) }) }) describe('isValidPublic', function () { it('should fail on too short input', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), false) }) it('should fail on too big input', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d00' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d00', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), false) }) it('should fail on SEC1 key', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), false) }) it('shouldn\'t fail on SEC1 key with sanitize enabled', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), true) }) it('should fail with an invalid SEC1 public key', function () { - var pubKey = '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), false) }) it('should work with compressed keys with sanitize enabled', function () { - var pubKey = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), true) }) it('should work with sanitize enabled', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey, true), true) }) it('should work otherwise', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.equal(ethUtils.isValidPublic(pubKey), true) }) }) describe('importPublic', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' it('should work with an Ethereum public key', function () { - var tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' assert.equal(ethUtils.importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) }) it('should work with uncompressed SEC1 keys', function () { - var tmp = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const tmp = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' assert.equal(ethUtils.importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) }) it('should work with compressed SEC1 keys', function () { - var tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' + const tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' assert.equal(ethUtils.importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) }) }) describe('publicToAddress', function () { it('should produce an address given a public key', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - pubKey = Buffer.from(pubKey, 'hex') - var r = ethUtils.publicToAddress(pubKey) + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const r = ethUtils.publicToAddress(pubKey) assert.equal(r.toString('hex'), address) }) it('should produce an address given a SEC1 public key', function () { - var pubKey = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - pubKey = Buffer.from(pubKey, 'hex') - var r = ethUtils.publicToAddress(pubKey, true) + const pubKey = Buffer.from('043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const r = ethUtils.publicToAddress(pubKey, true) assert.equal(r.toString('hex'), address) }) it('shouldn\'t produce an address given an invalid SEC1 public key', function () { - var pubKey = '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex') assert.throws(function () { ethUtils.publicToAddress(pubKey, true) }) }) it('shouldn\'t produce an address given an invalid public key', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744' - pubKey = Buffer.from(pubKey, 'hex') + const pubKey = Buffer.from('3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex') assert.throws(function () { ethUtils.publicToAddress(pubKey) }) @@ -347,23 +336,23 @@ describe('publicToAddress', function () { describe('publicToAddress 0x', function () { it('should produce an address given a public key', function () { - var pubKey = '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - var r = ethUtils.publicToAddress(pubKey) + const pubKey = '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const r = ethUtils.publicToAddress(pubKey) assert.equal(r.toString('hex'), address) }) }) describe('privateToPublic', function () { it('should produce a public key given a private key', function () { - var pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - var privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) - var r = ethUtils.privateToPublic(privateKey).toString('hex') + const pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + const privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) + const r = ethUtils.privateToPublic(privateKey).toString('hex') assert.equal(r.toString('hex'), pubKey) }) it('shouldn\'t produce a public key given an invalid private key', function () { - var privateKey1 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95, 42]) - var privateKey2 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28]) + const privateKey1 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95, 42]) + const privateKey2 = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28]) assert.throws(function () { ethUtils.privateToPublic(privateKey1) }) @@ -375,37 +364,47 @@ describe('privateToPublic', function () { describe('privateToAddress', function () { it('should produce an address given a private key', function () { - var address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' // Our private key - var privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) - var r = ethUtils.privateToAddress(privateKey).toString('hex') + const privateKey = Buffer.from([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95]) + const r = ethUtils.privateToAddress(privateKey).toString('hex') assert.equal(r.toString('hex'), address) }) }) describe('generateAddress', function () { it('should produce an address given a public key', function () { - var add = ethUtils.generateAddress('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') + const add = ethUtils.generateAddress('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') assert.equal(add.toString('hex'), '936a4295d8d74e310c0c95f0a63e53737b998d12') }) }) describe('generateAddress with hex prefix', function () { it('should produce an address given a public key', function () { - var add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') + const add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 14).toString('hex') assert.equal(add.toString('hex'), 'd658a4b8247c14868f3c512fa5cbb6e458e4a989') }) }) describe('generateAddress with nonce 0 (special case)', function () { it('should produce an address given a public key', function () { - var add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 0).toString('hex') + const add = ethUtils.generateAddress('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39', 0).toString('hex') assert.equal(add.toString('hex'), 'bfa69ba91385206bfdd2d8b9c1a5d6c10097a85b') }) }) +describe('generateAddress2: EIP-1014 testdata examples', function () { + for (let i = 0; i <= 6; i++) { + let e = eip1014Testdata[i] + it(`${e['comment']}: should generate the addresses provided`, function () { + let result = ethUtils.generateAddress2(e['address'], e['salt'], e['initCode']) + assert.equal('0x' + result.toString('hex'), e['result']) + }) + } +}) + describe('hex prefix', function () { - var string = 'd658a4b8247c14868f3c512fa5cbb6e458e4a989' + const string = 'd658a4b8247c14868f3c512fa5cbb6e458e4a989' it('should add', function () { assert.equal(ethUtils.addHexPrefix(string), '0x' + string) }) @@ -465,7 +464,7 @@ describe('toBuffer', function () { describe('baToJSON', function () { it('should turn a array of buffers into a pure json object', function () { - var ba = [Buffer.from([0]), Buffer.from([1]), [Buffer.from([2])]] + const ba = [Buffer.from([0]), Buffer.from([1]), [Buffer.from([2])]] assert.deepEqual(ethUtils.baToJSON(ba), ['0x00', '0x01', ['0x02']]) }) it('should turn a buffers into string', function () { @@ -473,12 +472,12 @@ describe('baToJSON', function () { }) }) -var echash = Buffer.from('82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', 'hex') -var ecprivkey = Buffer.from('3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', 'hex') +const echash = Buffer.from('82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', 'hex') +const ecprivkey = Buffer.from('3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', 'hex') describe('ecsign', function () { it('should produce a signature', function () { - var sig = ethUtils.ecsign(echash, ecprivkey) + const sig = ethUtils.ecsign(echash, ecprivkey) assert.deepEqual(sig.r, Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')) assert.deepEqual(sig.s, Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')) assert.equal(sig.v, 27) @@ -487,28 +486,28 @@ describe('ecsign', function () { describe('ecrecover', function () { it('should recover a public key', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - var pubkey = ethUtils.ecrecover(echash, 27, r, s) + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const pubkey = ethUtils.ecrecover(echash, 27, r, s) assert.deepEqual(pubkey, ethUtils.privateToPublic(ecprivkey)) }) it('should fail on an invalid signature (v = 21)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.throws(function () { ethUtils.ecrecover(echash, 21, r, s) }) }) it('should fail on an invalid signature (v = 29)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.throws(function () { ethUtils.ecrecover(echash, 29, r, s) }) }) it('should fail on an invalid signature (swapped points)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.throws(function () { ethUtils.ecrecover(echash, 27, s, r) }) @@ -517,35 +516,35 @@ describe('ecrecover', function () { describe('hashPersonalMessage', function () { it('should produce a deterministic hash', function () { - var h = ethUtils.hashPersonalMessage(Buffer.from('Hello world')) + const h = ethUtils.hashPersonalMessage(Buffer.from('Hello world')) assert.deepEqual(h, Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex')) }) }) describe('isValidSignature', function () { it('should fail on an invalid signature (shorter r))', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(27, r, s), false) }) it('should fail on an invalid signature (shorter s))', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca', 'hex') assert.equal(ethUtils.isValidSignature(27, r, s), false) }) it('should fail on an invalid signature (v = 21)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(21, r, s), false) }) it('should fail on an invalid signature (v = 29)', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(29, r, s), false) }) it('should work otherwise', function () { - var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') - var s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') assert.equal(ethUtils.isValidSignature(27, r, s), true) }) // FIXME: add homestead test @@ -567,8 +566,8 @@ var checksumAddresses = [ describe('.toChecksumAddress()', function () { it('should work', function () { - for (var i = 0; i < checksumAddresses.length; i++) { - var tmp = checksumAddresses[i] + for (let i = 0; i < checksumAddresses.length; i++) { + let tmp = checksumAddresses[i] assert.equal(ethUtils.toChecksumAddress(tmp.toLowerCase()), tmp) } }) @@ -576,7 +575,7 @@ describe('.toChecksumAddress()', function () { describe('.isValidChecksumAddress()', function () { it('should return true', function () { - for (var i = 0; i < checksumAddresses.length; i++) { + for (let i = 0; i < checksumAddresses.length; i++) { assert.equal(ethUtils.isValidChecksumAddress(checksumAddresses[i]), true) } }) diff --git a/test/testdata/eip1014Examples.json b/test/testdata/eip1014Examples.json new file mode 100644 index 00000000..a503c140 --- /dev/null +++ b/test/testdata/eip1014Examples.json @@ -0,0 +1,51 @@ +[ + { + "comment": "Example 0", + "address": "0x0000000000000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0x00", + "result": "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38" + }, + { + "comment": "Example 1", + "address": "0xdeadbeef00000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0x00", + "result": "0xb928f69bb1d91cd65274e3c79d8986362984fda3" + }, + { + "comment": "Example 2", + "address": "0xdeadbeef00000000000000000000000000000000", + "salt": "0x000000000000000000000000feed000000000000000000000000000000000000", + "initCode": "0x00", + "result": "0xd04116cdd17bebe565eb2422f2497e06cc1c9833" + }, + { + "comment": "Example 3", + "address": "0x0000000000000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0xdeadbeef", + "result": "0x70f2b2914a2a4b783faefb75f459a580616fcb5e" + }, + { + "comment": "Example 4", + "address": "0x00000000000000000000000000000000deadbeef", + "salt": "0x00000000000000000000000000000000000000000000000000000000cafebabe", + "initCode": "0xdeadbeef", + "result": "0x60f3f640a8508fc6a86d45df051962668e1e8ac7" + }, + { + "comment": "Example 5", + "address": "0x00000000000000000000000000000000deadbeef", + "salt": "0x00000000000000000000000000000000000000000000000000000000cafebabe", + "initCode": "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", + "result": "0x1d8bfdc5d46dc4f61d6b6115972536ebe6a8854c" + }, + { + "comment": "Example 6", + "address": "0x0000000000000000000000000000000000000000", + "salt": "0x0000000000000000000000000000000000000000000000000000000000000000", + "initCode": "0x", + "result": "0xe33c0c7f7df4809055c3eba6c09cfe4baf1bd9e0" + } +] \ No newline at end of file