From 35f1747f5782ebc6c62bbec1c339db377877087a Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Thu, 4 Mar 2021 13:47:28 -0800 Subject: [PATCH] convert mocha to tape --- .eslintrc.js | 5 +- .github/workflows/build.yml | 2 +- karma.conf.js | 5 +- package.json | 9 +- test/account.spec.ts | 1008 +++++++++++++++++------------------ test/address.spec.ts | 80 +-- test/bytes.spec.ts | 240 +++++---- test/constants.spec.ts | 30 +- test/externals.spec.ts | 85 +-- test/hash.spec.ts | 183 ++++--- test/object.spec.ts | 48 +- test/signature.spec.ts | 338 ++++++------ test/types.spec.ts | 156 +++--- 13 files changed, 1136 insertions(+), 1053 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index aa0843b6..fd92c294 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,3 @@ module.exports = { - extends: '@ethereumjs/eslint-config-defaults', - env: { - mocha: true - } + extends: '@ethereumjs/eslint-config-defaults' } diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 98993c3c..b33cc6ee 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -24,7 +24,7 @@ jobs: - run: npm install - run: npm run lint - - run: npm run test + - run: npm run coverage - uses: codecov/codecov-action@v1 if: matrix.node-version == '12.x' diff --git a/karma.conf.js b/karma.conf.js index a64ca12c..f9617f27 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,11 +1,10 @@ module.exports = function(config) { config.set({ - frameworks: ['mocha', 'karma-typescript'], + frameworks: ['tap', 'karma-typescript'], files: ['src/**/*.ts', 'test/**/*.ts'], preprocessors: { '**/*.ts': ['karma-typescript'] }, - plugins: ['karma-mocha', 'karma-typescript', 'karma-chrome-launcher', 'karma-firefox-launcher'], karmaTypescriptConfig: { bundlerOptions: { entrypoints: /\.spec\.ts$/ @@ -16,7 +15,7 @@ module.exports = function(config) { reporters: ['progress', 'karma-typescript'], browsers: ['FirefoxHeadless', 'ChromeHeadless'], singleRun: true, - concurrency: Infinity, + concurrency: 1, // Fail after timeout browserDisconnectTimeout: 100000, browserNoActivityTimeout: 100000 diff --git a/package.json b/package.json index 752310cc..93430636 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,13 @@ "scripts": { "build": "ethereumjs-config-ts-build", "prepublishOnly": "npm run test && npm run build", + "coverage": "ethereumjs-config-coverage", "docs:build": "npx typedoc --options typedoc.js", "lint": "ethereumjs-config-lint", "lint:fix": "ethereumjs-config-lint-fix", "test": "npm run test:node && npm run test:browser", "test:browser": "karma start karma.conf.js", - "test:node": "nyc --reporter=lcov mocha --require ts-node/register 'test/*.spec.ts'", + "test:node": "tape -r ts-node/register test/*.spec.ts", "tsc": "ethereumjs-config-tsc" }, "husky": { @@ -97,18 +98,18 @@ "@ethereumjs/config-typescript": "^2.0.0", "@ethereumjs/eslint-config-defaults": "^2.0.0", "@types/assert": "^1.5.4", - "@types/mocha": "^8.2.0", "@types/node": "^11.9.0", "@types/secp256k1": "^4.0.1", + "@types/tape": "^4.13.0", "husky": "^2.1.0", "karma": "^5.0.2", "karma-chrome-launcher": "^2.0.0", "karma-firefox-launcher": "^1.0.0", - "karma-mocha": "^2.0.0", + "karma-tap": "^4.2.0", "karma-typescript": "^4.1.1", - "mocha": "^8.2.1", "nyc": "^15.0.0", "prettier": "^1.15.3", + "tape": "^5.2.2", "ts-node": "^8.6.2", "typedoc": "next", "typedoc-plugin-markdown": "^2.2.16", diff --git a/test/account.spec.ts b/test/account.spec.ts index 469ef129..483f88de 100644 --- a/test/account.spec.ts +++ b/test/account.spec.ts @@ -1,8 +1,8 @@ -import assert from 'assert' -import BN from 'bn.js' -import * as rlp from 'rlp' +import tape from 'tape' +import { encode } from 'rlp' import { Account, + BN, isValidPrivate, isValidPublic, importPublic, @@ -18,30 +18,25 @@ import { } from '../src' const eip1014Testdata = require('./testdata/eip1014Examples.json') -describe('Account', function() { - describe('empty constructor', function() { +tape('Account', function(t) { + t.test('empty constructor', function(st) { const account = new Account() - it('should have zero nonce', function() { - assert.ok(account.nonce.isZero()) - }) - it('should have zero balance', function() { - assert.ok(account.balance.isZero()) - }) - it('should have stateRoot equal to KECCAK256_RLP', function() { - assert.ok( - account.stateRoot.toString('hex'), - '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' - ) - }) - it('should have codeHash equal to KECCAK256_NULL', function() { - assert.equal( - account.codeHash.toString('hex'), - 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' - ) - }) + st.ok(account.nonce.isZero(), 'should have zero nonce') + st.ok(account.balance.isZero(), 'should have zero balance') + st.equal( + account.stateRoot.toString('hex'), + '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + 'should have stateRoot equal to KECCAK256_RLP' + ) + st.equal( + account.codeHash.toString('hex'), + 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + 'should have codeHash equal to KECCAK256_NULL' + ) + st.end() }) - describe('from Array data', function() { + t.test('from Array data', function(st) { const raw = [ '0x02', // nonce '0x0384', // balance @@ -49,27 +44,22 @@ describe('Account', function() { '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' // codeHash ] const account = Account.fromValuesArray(raw.map(toBuffer)) - it('should have correct nonce', function() { - assert.ok(account.nonce.eqn(2)) - }) - it('should have correct balance', function() { - assert.ok(account.balance.eqn(900)) - }) - it('should have correct stateRoot', function() { - assert.equal( - account.stateRoot.toString('hex'), - '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' - ) - }) - it('should have correct codeHash', function() { - assert.equal( - account.codeHash.toString('hex'), - 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' - ) - }) + st.ok(account.nonce.eqn(2), 'should have correct nonce') + st.ok(account.balance.eqn(900), 'should have correct balance') + st.equal( + account.stateRoot.toString('hex'), + '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + 'should have correct stateRoot' + ) + st.equal( + account.codeHash.toString('hex'), + 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + 'should have correct codeHash' + ) + st.end() }) - describe('from Object data', function() { + t.test('from Object data', function(st) { const raw = { nonce: '0x02', balance: '0x0384', @@ -77,53 +67,43 @@ describe('Account', function() { codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' } const account = Account.fromAccountData(raw) - it('should have correct nonce', function() { - assert.ok(account.nonce.eqn(2)) - }) - it('should have correct balance', function() { - assert.ok(account.balance.eqn(900)) - }) - it('should have correct stateRoot', function() { - assert.equal( - account.stateRoot.toString('hex'), - '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' - ) - }) - it('should have correct codeHash', function() { - assert.equal( - account.codeHash.toString('hex'), - 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' - ) - }) + st.ok(account.nonce.eqn(2), 'should have correct nonce') + st.ok(account.balance.eqn(900), 'should have correct balance') + st.equal( + account.stateRoot.toString('hex'), + '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + 'should have correct stateRoot' + ) + st.equal( + account.codeHash.toString('hex'), + 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + 'should have correct codeHash' + ) + st.end() }) - describe('from RLP data', function() { + t.test('from RLP data', function(st) { const accountRlp = Buffer.from( 'f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', 'hex' ) const account = Account.fromRlpSerializedAccount(accountRlp) - it('should have correct nonce', function() { - assert.ok(account.nonce.eqn(2)) - }) - it('should have correct balance', function() { - assert.ok(account.balance.eqn(900)) - }) - it('should have correct stateRoot', function() { - assert.equal( - account.stateRoot.toString('hex'), - '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' - ) - }) - it('should have correct codeHash', function() { - assert.equal( - account.codeHash.toString('hex'), - 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' - ) - }) + st.ok(account.nonce.eqn(2), 'should have correct nonce') + st.ok(account.balance.eqn(900), 'should have correct balance') + st.equal( + account.stateRoot.toString('hex'), + '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + 'should have correct stateRoot' + ) + st.equal( + account.codeHash.toString('hex'), + 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + 'should have correct codeHash' + ) + st.end() }) - describe('serialize', function() { + t.test('serialize', function(st) { const raw = { nonce: '0x01', balance: '0x42', @@ -131,603 +111,571 @@ describe('Account', function() { codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' } const account = Account.fromAccountData(raw) - const accountRlp = rlp.encode([raw.nonce, raw.balance, raw.stateRoot, raw.codeHash]) - it('should serialize correctly', function() { - assert.ok(account.serialize().equals(accountRlp)) - }) + const accountRlp = encode([raw.nonce, raw.balance, raw.stateRoot, raw.codeHash]) + st.ok(account.serialize().equals(accountRlp), 'should serialize correctly') + st.end() }) - describe('isContract', function() { - it('should return false for a non-contract account', function() { - const accountRlp = Buffer.from( - 'f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', - 'hex' - ) - const account = Account.fromRlpSerializedAccount(accountRlp) - assert.equal(account.isContract(), false) - }) + t.test('isContract', function(st) { + const accountRlp = Buffer.from( + 'f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + 'hex' + ) + let account = Account.fromRlpSerializedAccount(accountRlp) + st.notOk(account.isContract(), 'should return false for a non-contract account') - it('should return true for a contract account', function() { - const raw = { - nonce: '0x01', - balance: '0x0042', - stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', - codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' - } - const account = Account.fromAccountData(raw) - assert.ok(account.isContract()) - }) + const raw = { + nonce: '0x01', + balance: '0x0042', + stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' + } + account = Account.fromAccountData(raw) + st.ok(account.isContract(), 'should return true for a contract account') + st.end() }) - describe('isEmpty', function() { - it('should return true for an empty account', function() { - const account = new Account() - assert.ok(account.isEmpty()) - }) + t.test('isEmpty', function(st) { + let account = new Account() + st.ok(account.isEmpty(), 'should return true for an empty account') - it('should return false for a non-empty account', function() { - const raw = { - nonce: '0x01', - balance: '0x0042', - stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', - codeHash: '0xd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b' - } - const account = Account.fromAccountData(raw) - assert.equal(account.isEmpty(), false) - }) + const raw = { + nonce: '0x01', + balance: '0x0042', + stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + codeHash: '0xd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b' + } + account = Account.fromAccountData(raw) + st.notOk(account.isEmpty(), 'should return false for a non-empty account') + st.end() }) - describe('validation', function() { - it('should only accept length 32 buffer for stateRoot', function() { - assert.throws(() => { - new Account(undefined, undefined, Buffer.from('hey'), undefined) - }) - }) + t.test('validation', function(st) { + st.throws(() => { + new Account(undefined, undefined, Buffer.from('hey'), undefined) + }, 'should only accept length 32 buffer for stateRoot') - it('should only accept length 32 buffer for codeHash', function() { - assert.throws(() => { - new Account(undefined, undefined, undefined, Buffer.from('hey')) - }) - }) + st.throws(() => { + new Account(undefined, undefined, undefined, Buffer.from('hey')) + }, 'should only accept length 32 buffer for codeHash') - it('should only accept an array in fromRlpSerializedAccount', function() { - const data = { balance: new BN(5) } - assert.throws(() => { - Account.fromRlpSerializedAccount(data as any) - }) - }) + const data = { balance: new BN(5) } + st.throws(() => { + Account.fromRlpSerializedAccount(data as any) + }, 'should only accept an array in fromRlpSerializedAccount') - it('should not accept nonce less than 0', function() { - assert.throws(() => { - new Account(new BN(-5)) - }) - }) + st.throws(() => { + new Account(new BN(-5)) + }, 'should not accept nonce less than 0') - it('should not accept balance less than 0', function() { - assert.throws(() => { - new Account(undefined, new BN(-5)) - }) - }) + st.throws(() => { + new Account(undefined, new BN(-5)) + }, 'should not accept balance less than 0') + st.end() }) }) -describe('isValidPrivate', function() { - const SECP256K1_N = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16) - it('should fail on short input', function() { - const tmp = '0011223344' - assert.throws(function() { +tape('Utility Functions', function(t) { + t.test('isValidPrivate', function(st) { + const SECP256K1_N = new BN( + 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', + 16 + ) + + let tmp = '0011223344' + st.throws(function() { isValidPrivate(Buffer.from(tmp, 'hex')) - }) - }) - it('should fail on too big input', function() { - const tmp = + }, 'should fail on short input') + + tmp = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - assert.throws(function() { + st.throws(function() { isValidPrivate(Buffer.from(tmp, 'hex')) - }) - }) - it('should fail on wrong input type', function() { - assert.throws(function() { + }, 'should fail on too big input') + + st.throws(function() { isValidPrivate(('WRONG_INPUT_TYPE') as Buffer) - }) - }) - it('should fail on invalid curve (zero)', function() { - const tmp = '0000000000000000000000000000000000000000000000000000000000000000' - assert.equal(isValidPrivate(Buffer.from(tmp, 'hex')), false) - }) - it('should fail on invalid curve (== N)', function() { - const tmp = SECP256K1_N.toString(16) - assert.equal(isValidPrivate(Buffer.from(tmp, 'hex')), false) - }) - it('should fail on invalid curve (>= N)', function() { - const tmp = SECP256K1_N.addn(1).toString(16) - assert.equal(isValidPrivate(Buffer.from(tmp, 'hex')), false) - }) - it('should work otherwise (< N)', function() { - const tmp = SECP256K1_N.subn(1).toString(16) - assert.equal(isValidPrivate(Buffer.from(tmp, 'hex')), true) + }, 'should fail on wrong input type') + + tmp = '0000000000000000000000000000000000000000000000000000000000000000' + st.notOk(isValidPrivate(Buffer.from(tmp, 'hex')), 'should fail on invalid curve (zero)') + + tmp = SECP256K1_N.toString(16) + st.notOk(isValidPrivate(Buffer.from(tmp, 'hex')), 'should fail on invalid curve (== N)') + + tmp = SECP256K1_N.addn(1).toString(16) + st.notOk(isValidPrivate(Buffer.from(tmp, 'hex')), 'should fail on invalid curve (>= N)') + + tmp = SECP256K1_N.subn(1).toString(16) + st.ok(isValidPrivate(Buffer.from(tmp, 'hex')), 'should work otherwise (< N)') + st.end() }) -}) -describe('isValidPublic', function() { - it('should fail on too short input', function() { - const pubKey = Buffer.from( + t.test('isValidPublic', function(st) { + let pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex' ) - assert.equal(isValidPublic(pubKey), false) - }) - it('should fail on too big input', function() { - const pubKey = Buffer.from( + st.notOk(isValidPublic(pubKey), 'should fail on too short input') + + pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d00', 'hex' ) - assert.equal(isValidPublic(pubKey), false) - }) - it('should fail on SEC1 key', function() { - const pubKey = Buffer.from( + st.notOk(isValidPublic(pubKey), 'should fail on too big input') + + pubKey = Buffer.from( '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - assert.equal(isValidPublic(pubKey), false) - }) - it("shouldn't fail on SEC1 key with sanitize enabled", function() { - const pubKey = Buffer.from( + st.notOk(isValidPublic(pubKey), 'should fail on SEC1 key') + + pubKey = Buffer.from( '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - assert.equal(isValidPublic(pubKey, true), true) - }) - it('should fail with an invalid SEC1 public key', function() { - const pubKey = Buffer.from( + st.ok(isValidPublic(pubKey, true), "shouldn't fail on SEC1 key wt.testh sant.testize enabled") + + pubKey = Buffer.from( '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - assert.equal(isValidPublic(pubKey, true), false) - }) - it('should work with compressed keys with sanitize enabled', function() { - const pubKey = Buffer.from( + st.notOk(isValidPublic(pubKey), 'should fail wt.testh an invalid SEC1 public key') + + pubKey = Buffer.from( '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a', 'hex' ) - assert.equal(isValidPublic(pubKey, true), true) - }) - it('should work with sanitize enabled', function() { - const pubKey = Buffer.from( + st.ok( + isValidPublic(pubKey, true), + 'should work wt.testh compressed keys wt.testh sant.testize enabled' + ) + + pubKey = Buffer.from( '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - assert.equal(isValidPublic(pubKey, true), true) - }) - it('should work otherwise', function() { - const pubKey = Buffer.from( + st.ok(isValidPublic(pubKey, true), 'should work wt.testh sant.testize enabled') + + pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - assert.equal(isValidPublic(pubKey), true) - }) - it('should throw if input is not Buffer', function() { - const pubKey = - '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + st.ok(isValidPublic(pubKey), 'should work otherwise') + + pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' as any try { isValidPublic((pubKey) as Buffer) } catch (err) { - assert(err.message.includes('This method only supports Buffer')) + st.ok( + err.message.includes('This method only supports Buffer'), + 'should throw if input is not Buffer' + ) } + st.end() }) -}) -describe('importPublic', function() { - const pubKey = - '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - it('should work with an Ethereum public key', function() { - const tmp = + t.test('importPublic', function(st) { + const pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - assert.equal(importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) - }) - it('should work with uncompressed SEC1 keys', function() { - const tmp = + + let tmp = + '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' + st.equal( + importPublic(Buffer.from(tmp, 'hex')).toString('hex'), + pubKey, + 'should work wt.testh an Ethereum public key' + ) + + tmp = '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - assert.equal(importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) - }) - it('should work with compressed SEC1 keys', function() { - const tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' - assert.equal(importPublic(Buffer.from(tmp, 'hex')).toString('hex'), pubKey) - }) - it('should throw if input is not Buffer', function() { - assert.throws(function() { + st.equal( + importPublic(Buffer.from(tmp, 'hex')).toString('hex'), + pubKey, + 'should work wt.testh uncompressed SEC1 keys' + ) + + tmp = '033a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a' + st.equal( + importPublic(Buffer.from(tmp, 'hex')).toString('hex'), + pubKey, + 'should work wt.testh compressed SEC1 keys' + ) + + st.throws(function() { importPublic((pubKey) as Buffer) - }) + }, 'should throw if input is not Buffer') + st.end() }) -}) -describe('publicToAddress', function() { - it('should produce an address given a public key', function() { - const pubKey = Buffer.from( + t.test('publicToAddress', function(st) { + let pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - const r = publicToAddress(pubKey) - assert.equal(r.toString('hex'), address) - }) - it('should produce an address given a SEC1 public key', function() { - const pubKey = Buffer.from( + let address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + let r = publicToAddress(pubKey) + st.equal(r.toString('hex'), address, 'should produce an address given a public key') + + pubKey = Buffer.from( '043a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - const r = publicToAddress(pubKey, true) - assert.equal(r.toString('hex'), address) - }) - it("shouldn't produce an address given an invalid SEC1 public key", function() { - const pubKey = Buffer.from( + address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + r = publicToAddress(pubKey, true) + st.equal(r.toString('hex'), address, 'should produce an address given a SEC1 public key') + + pubKey = Buffer.from( '023a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) - assert.throws(function() { + st.throws(function() { publicToAddress(pubKey, true) - }) - }) - it("shouldn't produce an address given an invalid public key", function() { - const pubKey = Buffer.from( + }, "shouldn't produce an address given an invalid SEC1 public key") + + pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex' ) - assert.throws(function() { + st.throws(function() { publicToAddress(pubKey) - }) - }) - it('should throw if input is not a buffer', function() { - const pubKey: any = - '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - assert.throws(function() { + }, "shouldn't produce an address given an invalid public key") + + pubKey = '0x3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' as any + st.throws(function() { publicToAddress(pubKey) - }) + }, 'should throw if input is not a buffer') + st.end() }) -}) -describe('privateToPublic', function() { - it('should produce a public key given a private key', function() { + t.test('privateToPublic', function(st) { const pubKey = '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d' - const privateKey = Buffer.from( + let privateKey = Buffer.from( 'ea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c5f', 'hex' ) - const r: any = privateToPublic(privateKey).toString('hex') - assert.equal(r.toString('hex'), pubKey) - }) - it("shouldn't produce a public key given an invalid private key", function() { - const privateKey1 = Buffer.from( + const r = privateToPublic(privateKey) + st.equal(r.toString('hex'), pubKey, 'should produce a public key given a private key') + + privateKey = Buffer.from( 'ea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c5f2a', 'hex' ) - const privateKey2 = Buffer.from( + st.throws(function() { + privateToPublic(privateKey) + }, "shouldn't produce a public key given an invalid private key") + + privateKey = Buffer.from( 'ea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c', 'hex' ) - assert.throws(function() { - privateToPublic(privateKey1) - }) - assert.throws(function() { - privateToPublic(privateKey2) - }) - }) + st.throws(function() { + privateToPublic(privateKey) + }, "shouldn't produce a public key given an invalid private key") - it('should throw if private key is not Buffer', function() { - const privateKey = '0xea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c5f' + privateKey = '0xea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c5f' as any try { privateToPublic((privateKey) as Buffer) } catch (err) { - assert(err.message.includes('This method only supports Buffer')) - assert(err.message.includes(privateKey)) + st.ok( + err.message.includes('This method only supports Buffer'), + 'should throw if private key is not Buffer' + ) + st.ok(err.message.includes(privateKey), 'should throw if private key is not Buffer') } + st.end() }) -}) -describe('privateToAddress', function() { - it('should produce an address given a private key', function() { + t.test('privateToAddress', function(st) { const address = '2f015c60e0be116b1f0cd534704db9c92118fb6a' // Our private key const privateKey = Buffer.from( 'ea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c5f', 'hex' ) - const r: any = privateToAddress(privateKey).toString('hex') - assert.equal(r.toString('hex'), address) + const r = privateToAddress(privateKey) + st.equal(r.toString('hex'), address, 'should produce an address given a private key') + st.end() }) -}) -describe('generateAddress', function() { - it('should produce an address given a public key', function() { - const add: any = generateAddress( + t.test('generateAddress', function(st) { + const addr = generateAddress( Buffer.from('990ccf8a0de58091c028d6ff76bb235ee67c1c39', 'utf8'), toBuffer(14) - ).toString('hex') - assert.equal(add.toString('hex'), '936a4295d8d74e310c0c95f0a63e53737b998d12') + ) + st.equal( + addr.toString('hex'), + '936a4295d8d74e310c0c95f0a63e53737b998d12', + 'should produce an address given a public key' + ) + st.end() }) -}) -describe('generateAddress with hex prefix', function() { - it('should produce an address given a public key', function() { - const add: any = generateAddress( + t.test('generateAddress wt.testh hex prefix', function(st) { + const addr = generateAddress( toBuffer('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39'), toBuffer(14) - ).toString('hex') - assert.equal(add.toString('hex'), 'd658a4b8247c14868f3c512fa5cbb6e458e4a989') + ) + st.equal( + addr.toString('hex'), + 'd658a4b8247c14868f3c512fa5cbb6e458e4a989', + 'should produce an address given a public key' + ) + st.end() }) -}) -describe('generateAddress with nonce 0 (special case)', function() { - it('should produce an address given a public key', function() { - const add: any = generateAddress( + t.test('generateAddress wt.testh nonce 0 (special case)', function(st) { + const addr = generateAddress( toBuffer('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39'), toBuffer(0) - ).toString('hex') - assert.equal(add.toString('hex'), 'bfa69ba91385206bfdd2d8b9c1a5d6c10097a85b') + ) + st.equal( + addr.toString('hex'), + 'bfa69ba91385206bfdd2d8b9c1a5d6c10097a85b', + 'should produce an address given a public key' + ) + st.end() }) -}) -describe('generateAddress with non-buffer inputs', function() { - it('should throw if address is not Buffer', function() { - assert.throws(function() { + t.test('generateAddress wt.testh non-buffer inputs', function(st) { + st.throws(function() { generateAddress( ('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39') as Buffer, toBuffer(0) ) - }) - }) - it('should throw if nonce is not Buffer', function() { - assert.throws(function() { + }, 'should throw if address is not Buffer') + + st.throws(function() { generateAddress( toBuffer('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39'), (0) as Buffer ) - }) - }) -}) - -describe('generateAddress2: EIP-1014 testdata examples', function() { - for (let i = 0; i <= 6; i++) { - const e = eip1014Testdata[i] - it(`${e['comment']}: should generate the addresses provided`, function() { - const result = generateAddress2( - toBuffer(e['address']), - toBuffer(e['salt']), - toBuffer(e['initCode']) - ) - assert.equal('0x' + result.toString('hex'), e['result']) - }) - } -}) - -describe('generateAddress2: non-buffer inputs', function() { - const e = eip1014Testdata[0] - - it('should throw if address is not Buffer', function() { - assert.throws(function() { - generateAddress2( - (e['address']) as Buffer, - toBuffer(e['salt']), - toBuffer(e['initCode']) - ) - }) - }) - it('should throw if salt is not Buffer', function() { - assert.throws(function() { - generateAddress2( - toBuffer(e['address']), - (e['salt']) as Buffer, - toBuffer(e['initCode']) - ) - }) - }) - it('should throw if initCode is not Buffer', function() { - assert.throws(function() { - generateAddress2( - toBuffer(e['address']), - toBuffer(e['salt']), - (e['initCode']) as Buffer + }, 'should throw if nonce is not Buffer') + st.end() + }) + + t.test('generateAddress2: EIP-1014 testdata examples', function(st) { + for (const testdata of eip1014Testdata) { + const { address, comment, result, salt, initCode } = testdata + const addr = generateAddress2(toBuffer(address), toBuffer(salt), toBuffer(initCode)) + st.equal( + '0x' + addr.toString('hex'), + result, + `${comment}: should generate the addresses provided` ) - }) - }) -}) - -const eip55ChecksumAddresses = [ - // All caps - '0x52908400098527886E0F7030069857D2E4169EE7', - '0x8617E340B3D01FA5F11F306F4090FD50E238070D', - // All Lower - '0xde709f2102306220921060314715629080e2fb77', - '0x27b1fdb04752bbc536007a920d24acb045561c26', - // Normal - '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed', - '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', - '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB', - '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb' -] - -const eip1191ChecksummAddresses = { - 1: [ - '0x88021160c5C792225E4E5452585947470010289d', - '0x27b1FdB04752bBc536007a920D24ACB045561c26', - '0x52908400098527886e0f7030069857D2e4169EE7', - '0x5aaeB6053f3E94C9b9A09f33669435e7Ef1bEAed', - '0x8617E340b3d01FA5F11F306f4090FD50E238070d', - '0xd1220a0CF47C7B9Be7A2E6ba89F429762E7B9Adb', - '0xdBf03b407c01e7cD3CBea99509d93f8dDDC8C6fB', - '0xDe709F2102306220921060314715629080E2fb77', - '0xfb6916095Ca1dF60bB79cE92ce3ea74C37c5D359' - ], - 30: [ - '0x6549F4939460DE12611948B3F82B88C3C8975323', - '0x27b1FdB04752BBc536007A920D24ACB045561c26', - '0x3599689E6292B81B2D85451025146515070129Bb', - '0x52908400098527886E0F7030069857D2E4169ee7', - '0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD', - '0x8617E340b3D01Fa5f11f306f4090fd50E238070D', - '0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB', - '0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB', - '0xDe709F2102306220921060314715629080e2FB77', - '0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359' - ], - 31: [ - '0x42712D45473476B98452F434E72461577D686318', - '0x27B1FdB04752BbC536007a920D24acB045561C26', - '0x3599689e6292b81b2D85451025146515070129Bb', - '0x52908400098527886E0F7030069857D2e4169EE7', - '0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd', - '0x66f9664F97F2b50f62d13eA064982F936DE76657', - '0x8617e340b3D01fa5F11f306F4090Fd50e238070d', - '0xDE709F2102306220921060314715629080e2Fb77', - '0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359', - '0xd1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB', - '0xdbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB' + } + st.end() + }) + + t.test('generateAddress2: non-buffer inputs', function(st) { + const { address, salt, initCode } = eip1014Testdata[0] + + st.throws(function() { + generateAddress2((address) as Buffer, toBuffer(salt), toBuffer(initCode)) + }, 'should throw if address is not Buffer') + + st.throws(function() { + generateAddress2(toBuffer(address), (salt) as Buffer, toBuffer(initCode)) + }, 'should throw if salt is not Buffer') + + st.throws(function() { + generateAddress2(toBuffer(address), toBuffer(salt), (initCode) as Buffer) + }, 'should throw if initCode is not Buffer') + st.end() + }) + + const eip55ChecksumAddresses = [ + // All caps + '0x52908400098527886E0F7030069857D2E4169EE7', + '0x8617E340B3D01FA5F11F306F4090FD50E238070D', + // All Lower + '0xde709f2102306220921060314715629080e2fb77', + '0x27b1fdb04752bbc536007a920d24acb045561c26', + // Normal + '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed', + '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', + '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB', + '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb' ] -} -describe('.toChecksumAddress()', function() { - describe('EIP55', function() { - it('should work', function() { + const eip1191ChecksummAddresses = { + 1: [ + '0x88021160c5C792225E4E5452585947470010289d', + '0x27b1FdB04752bBc536007a920D24ACB045561c26', + '0x52908400098527886e0f7030069857D2e4169EE7', + '0x5aaeB6053f3E94C9b9A09f33669435e7Ef1bEAed', + '0x8617E340b3d01FA5F11F306f4090FD50E238070d', + '0xd1220a0CF47C7B9Be7A2E6ba89F429762E7B9Adb', + '0xdBf03b407c01e7cD3CBea99509d93f8dDDC8C6fB', + '0xDe709F2102306220921060314715629080E2fb77', + '0xfb6916095Ca1dF60bB79cE92ce3ea74C37c5D359' + ], + 30: [ + '0x6549F4939460DE12611948B3F82B88C3C8975323', + '0x27b1FdB04752BBc536007A920D24ACB045561c26', + '0x3599689E6292B81B2D85451025146515070129Bb', + '0x52908400098527886E0F7030069857D2E4169ee7', + '0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD', + '0x8617E340b3D01Fa5f11f306f4090fd50E238070D', + '0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB', + '0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB', + '0xDe709F2102306220921060314715629080e2FB77', + '0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359' + ], + 31: [ + '0x42712D45473476B98452F434E72461577D686318', + '0x27B1FdB04752BbC536007a920D24acB045561C26', + '0x3599689e6292b81b2D85451025146515070129Bb', + '0x52908400098527886E0F7030069857D2e4169EE7', + '0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd', + '0x66f9664F97F2b50f62d13eA064982F936DE76657', + '0x8617e340b3D01fa5F11f306F4090Fd50e238070d', + '0xDE709F2102306220921060314715629080e2Fb77', + '0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359', + '0xd1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB', + '0xdbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB' + ] + } + + t.test('toChecksumAddress()', function(st) { + st.test('EIP55', function(st) { for (let i = 0; i < eip55ChecksumAddresses.length; i++) { const tmp = eip55ChecksumAddresses[i] - assert.equal(toChecksumAddress(tmp.toLowerCase()), tmp) + st.equal(toChecksumAddress(tmp.toLowerCase()), tmp) } - }) - }) - - describe('EIP1191', function() { - it('Should encode the example addresses correctly', function() { - for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) { - for (const addr of addresses) { - assert.equal(toChecksumAddress(addr.toLowerCase(), Number(chainId)), addr) - assert.equal(toChecksumAddress(addr.toLowerCase(), Buffer.from([chainId])), addr) - assert.equal(toChecksumAddress(addr.toLowerCase(), new BN(chainId)), addr) - assert.equal( - toChecksumAddress(addr.toLowerCase(), '0x' + Buffer.from([chainId]).toString('hex')), - addr - ) + st.end() + }) + + st.test('EIP1191', function(st) { + st.test('Should encode the example addresses correctly', function(st) { + for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) { + for (const addr of addresses) { + st.equal(toChecksumAddress(addr.toLowerCase(), Number(chainId)), addr) + st.equal(toChecksumAddress(addr.toLowerCase(), Buffer.from([chainId])), addr) + st.equal(toChecksumAddress(addr.toLowerCase(), new BN(chainId)), addr) + st.equal( + toChecksumAddress(addr.toLowerCase(), '0x' + Buffer.from([chainId]).toString('hex')), + addr + ) + } } - } - }) - it('Should encode large chain ids greater than MAX_INTEGER correctly', function() { - const addr = '0x88021160C5C792225E4E5452585947470010289D' - const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') - assert.equal(toChecksumAddress(addr.toLowerCase(), chainIDBuffer), addr) - assert.equal(toChecksumAddress(addr.toLowerCase(), new BN(chainIDBuffer)), addr) - assert.equal( - toChecksumAddress(addr.toLowerCase(), '0x' + chainIDBuffer.toString('hex')), - addr - ) - const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) - assert.throws( - () => { + st.end() + }) + st.test('Should encode large chain ids greater than MAX_INTEGER correctly', function(st) { + const addr = '0x88021160C5C792225E4E5452585947470010289D' + const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') + st.equal(toChecksumAddress(addr.toLowerCase(), chainIDBuffer), addr) + st.equal(toChecksumAddress(addr.toLowerCase(), new BN(chainIDBuffer)), addr) + st.equal(toChecksumAddress(addr.toLowerCase(), '0x' + chainIDBuffer.toString('hex')), addr) + const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) + st.throws(() => { toChecksumAddress(addr.toLowerCase(), chainIDNumber) - }, - { - message: - 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)' - } - ) + }) + st.end() + }) + st.end() }) - }) - describe('input format', function() { - it('Should throw when the address is not hex-prefixed', function() { - assert.throws(function() { + st.test('input format', function(st) { + st.throws(function() { toChecksumAddress('52908400098527886E0F7030069857D2E4169EE7'.toLowerCase()) - }) - }) - it('Should throw when the chainId is not hex-prefixed', function() { - assert.throws(function() { + }, 'Should throw when the address is not hex-prefixed') + + st.throws(function() { toChecksumAddress('0xde709f2102306220921060314715629080e2fb77', '1234') - }) + }, 'Should throw when the chainId is not hex-prefixed') + st.end() }) }) -}) -describe('.isValidChecksumAddress()', function() { - describe('EIP55', function() { - it('should return true', function() { + t.test('isValidChecksumAddress()', function(st) { + st.test('EIP55', function(st) { for (let i = 0; i < eip55ChecksumAddresses.length; i++) { - assert.equal(isValidChecksumAddress(eip55ChecksumAddresses[i]), true) + st.ok(isValidChecksumAddress(eip55ChecksumAddresses[i])) } - }) - it('should validate', function() { - assert.equal(isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a'), false) - }) - }) - - describe('EIP1191', function() { - it('Should return true for the example addresses', function() { - for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) { - for (const addr of addresses) { - assert.equal(isValidChecksumAddress(addr, Number(chainId)), true) - assert.equal(isValidChecksumAddress(addr, Buffer.from([chainId])), true) - assert.equal(isValidChecksumAddress(addr, new BN(chainId)), true) - assert.equal( - isValidChecksumAddress(addr, '0x' + Buffer.from([chainId]).toString('hex')), - true - ) + st.notOk(isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a')) + st.end() + }) + + st.test('EIP1191', function(st) { + st.test('Should return true for the example addresses', function(st) { + for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) { + for (const addr of addresses) { + st.ok(isValidChecksumAddress(addr, Number(chainId))) + st.ok(isValidChecksumAddress(addr, Buffer.from([chainId]))) + st.ok(isValidChecksumAddress(addr, new BN(chainId))) + st.equal( + isValidChecksumAddress(addr, '0x' + Buffer.from([chainId]).toString('hex')), + true + ) + } } - } - }) + st.end() + }) - it('Should return false for invalid cases', function() { - // If we set the chain id, an EIP55 encoded address should be invalid - for (let i = 0; i < eip55ChecksumAddresses.length; i++) { - assert.equal(isValidChecksumAddress(eip55ChecksumAddresses[i], 1), false) - } + st.test('Should return false for invalid cases', function(st) { + // If we set the chain id, an EIP55 encoded address should be invalid + for (let i = 0; i < eip55ChecksumAddresses.length; i++) { + st.notOk(isValidChecksumAddress(eip55ChecksumAddresses[i], 1)) + } - assert.equal(isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a', 1), false) - }) + st.notOk(isValidChecksumAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a', 1)) + st.end() + }) - it('Should return false if the wrong chain id is used', function() { - for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) { - for (const addr of addresses) { - assert.equal(isValidChecksumAddress(addr, Number(chainId) + 1), false) + st.test('Should return false if the wrong chain id is used', function(st) { + for (const [chainId, addresses] of Object.entries(eip1191ChecksummAddresses)) { + for (const addr of addresses) { + st.notOk(isValidChecksumAddress(addr, Number(chainId) + 1)) + } } - } - }) - }) + st.end() + }) - describe('input format', function() { - it('Should throw when the address is not hex-prefixed', function() { - assert.throws(function() { - isValidChecksumAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a') + st.test('input format', function(st) { + st.throws(() => { + isValidChecksumAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a') + }, 'Should throw when the address is not hex-prefixed') + st.end() }) + + st.end() }) }) -}) -describe('.isValidAddress()', function() { - it('should return true', function() { - assert.equal(isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a'), true) - assert.equal(isValidAddress('0x52908400098527886E0F7030069857D2E4169EE7'), true) - }) - it('should return false', function() { - assert.equal(isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6'), false) - assert.equal(isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6aa'), false) - }) - it('should throw when input is not hex prefixed', function() { - assert.throws(function() { - isValidAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a') + t.test('isValidAddress()', function(st) { + st.test('should return true', function(st) { + st.ok(isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6a')) + st.ok(isValidAddress('0x52908400098527886E0F7030069857D2E4169EE7')) + st.end() }) - assert.throws(function() { - isValidAddress('x2f015c60e0be116b1f0cd534704db9c92118fb6a') + st.test('should return false', function(st) { + st.notOk(isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6')) + st.notOk(isValidAddress('0x2f015c60e0be116b1f0cd534704db9c92118fb6aa')) + st.end() }) - assert.throws(function() { - isValidAddress('0X52908400098527886E0F7030069857D2E4169EE7') + st.test('should throw when input is not hex prefixed', function(st) { + st.throws(function() { + isValidAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a') + }) + st.throws(function() { + isValidAddress('x2f015c60e0be116b1f0cd534704db9c92118fb6a') + }) + st.throws(function() { + isValidAddress('0X52908400098527886E0F7030069857D2E4169EE7') + }) + st.end() + }) + st.test('error message should have correct format', function(st) { + const input = '2f015c60e0be116b1f0cd534704db9c92118fb6a' + try { + isValidAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a') + } catch (err) { + st.ok(err.message.includes('only supports 0x-prefixed hex strings')) + st.ok(err.message.includes(input)) + } + st.end() }) }) - it('error message should have correct format', function() { - const input = '2f015c60e0be116b1f0cd534704db9c92118fb6a' - try { - isValidAddress('2f015c60e0be116b1f0cd534704db9c92118fb6a') - } catch (err) { - assert(err.message.includes('only supports 0x-prefixed hex strings')) - assert(err.message.includes(input)) - } - }) }) diff --git a/test/address.spec.ts b/test/address.spec.ts index ae92f17e..804084b9 100644 --- a/test/address.spec.ts +++ b/test/address.spec.ts @@ -1,105 +1,115 @@ -import assert from 'assert' -import { BN, toBuffer } from '../src' -import { Address } from '../src' +import tape from 'tape' +import { Address, BN, toBuffer } from '../src' const eip1014Testdata = require('./testdata/eip1014Examples.json') -describe('Address', () => { +tape('Address', t => { const ZERO_ADDR_S = '0x0000000000000000000000000000000000000000' - it('should validate address length', () => { + t.test('should validate address length', st => { const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a11' - assert.throws(() => Address.fromString(str)) + st.throws(() => Address.fromString(str)) const shortStr = '0x2f015c60e0be116b1f0cd534704db9c92118fb' - assert.throws(() => Address.fromString(shortStr)) + st.throws(() => Address.fromString(shortStr)) const buf = toBuffer(str) - assert.throws(() => new Address(buf)) + st.throws(() => new Address(buf)) + st.end() }) - it('should generate a zero address', () => { + t.test('should generate a zero address', st => { const addr = Address.zero() - assert.deepEqual(addr.buf, toBuffer(ZERO_ADDR_S)) - assert.equal(addr.toString(), ZERO_ADDR_S) + st.deepEqual(addr.buf, toBuffer(ZERO_ADDR_S)) + st.equal(addr.toString(), ZERO_ADDR_S) + st.end() }) - it('should instantiate address from zero address string', () => { + t.test('should instantiate address from zero address string', st => { const addr = Address.fromString(ZERO_ADDR_S) - assert.deepEqual(addr.toString(), ZERO_ADDR_S) - assert.equal(addr.isZero(), true) + st.deepEqual(addr.toString(), ZERO_ADDR_S) + st.ok(addr.isZero()) + st.end() }) - it('should detect non-zero address', () => { + t.test('should detect non-zero address', st => { const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' const addr = Address.fromString(str) - assert.equal(addr.isZero(), false) + st.notOk(addr.isZero()) + st.end() }) - it('should instantiate from public key', () => { + t.test('should instantiate from public key', st => { const pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae7441e1d', 'hex' ) const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' const addr = Address.fromPublicKey(pubKey) - assert.equal(addr.toString(), str) + st.equal(addr.toString(), str) + st.end() }) - it('should fail to instantiate from invalid public key', () => { + t.test('should fail to instantiate from invalid public key', st => { const pubKey = Buffer.from( '3a443d8381a6798a70c6ff9304bdc8cb0163c23211d11628fae52ef9e0dca11a001cf066d56a8156fc201cd5df8a36ef694eecd258903fca7086c1fae744', 'hex' ) - assert.throws(() => Address.fromPublicKey(pubKey)) + st.throws(() => Address.fromPublicKey(pubKey)) + st.end() }) - it('should instantiate from private key', () => { + t.test('should instantiate from private key', st => { // prettier-ignore 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 str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' const addr = Address.fromPrivateKey(privateKey) - assert.equal(addr.toString(), str) + st.equal(addr.toString(), str) + st.end() }) - it('should generate address for created contract', () => { + t.test('should generate address for created contract', st => { const from = Address.fromString('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39') const addr = Address.generate(from, new BN(14)) - assert.equal(addr.toString(), '0xd658a4b8247c14868f3c512fa5cbb6e458e4a989') + st.equal(addr.toString(), '0xd658a4b8247c14868f3c512fa5cbb6e458e4a989') const addr2 = Address.generate(from, new BN(0)) - assert.equal(addr2.toString(), '0xbfa69ba91385206bfdd2d8b9c1a5d6c10097a85b') + st.equal(addr2.toString(), '0xbfa69ba91385206bfdd2d8b9c1a5d6c10097a85b') + st.end() }) - it('should generate address for CREATE2', () => { + t.test('should generate address for CREATE2', st => { for (const testdata of eip1014Testdata) { const { address, salt, initCode, result } = testdata const from = Address.fromString(address) const addr = Address.generate2(from, toBuffer(salt), toBuffer(initCode)) - assert.equal(addr.toString(), result) + st.equal(addr.toString(), result) } + st.end() }) - it('should provide a buffer that does not mutate the original address', () => { + t.test('should provide a buffer that does not mutate the original address', st => { const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' const address = Address.fromString(str) const addressBuf = address.toBuffer() addressBuf.fill(0) - assert.equal(address.toString(), str) + st.equal(address.toString(), str) + st.end() }) - it('should compare equality properly', () => { + t.test('should compare equality properly', st => { const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' const address1 = Address.fromString(str) const address2 = new Address(Buffer.from(str.slice(2), 'hex')) - assert.ok(address1.equals(address2)) - assert.ok(address1.buf.equals(address2.buf)) + st.ok(address1.equals(address2)) + st.ok(address1.buf.equals(address2.buf)) const str2 = '0xcd4EC7b66fbc029C116BA9Ffb3e59351c20B5B06' const address3 = Address.fromString(str2) - assert.ok(!address1.equals(address3)) + st.ok(!address1.equals(address3)) const address3LowerCase = Address.fromString(str2.toLowerCase()) - assert.ok(address3.equals(address3LowerCase)) + st.ok(address3.equals(address3LowerCase)) const address4 = Address.zero() - assert.ok(!address1.equals(address4)) + st.ok(!address1.equals(address4)) + st.end() }) }) diff --git a/test/bytes.spec.ts b/test/bytes.spec.ts index b95c3f90..7ee5cf8e 100644 --- a/test/bytes.spec.ts +++ b/test/bytes.spec.ts @@ -1,7 +1,7 @@ -import assert from 'assert' -import BN from 'bn.js' +import tape from 'tape' import { Address, + BN, zeros, zeroAddress, isZeroAddress, @@ -19,237 +19,271 @@ import { baToJSON } from '../src' -describe('zeros function', function() { - it('should produce lots of 0s', function() { +tape('zeros function', function(t) { + t.test('should produce lots of 0s', function(st) { const z60 = zeros(30) const zs60 = '000000000000000000000000000000000000000000000000000000000000' - assert.equal(z60.toString('hex'), zs60) + st.equal(z60.toString('hex'), zs60) + st.end() }) }) -describe('zero address', function() { - it('should generate a zero address', function() { - assert.equal(zeroAddress(), '0x0000000000000000000000000000000000000000') +tape('zero address', function(t) { + t.test('should generate a zero address', function(st) { + st.equal(zeroAddress(), '0x0000000000000000000000000000000000000000') + st.end() }) }) -describe('is zero address', function() { - it('should return true when a zero address is passed', function() { - assert.equal(isZeroAddress('0x0000000000000000000000000000000000000000'), true) +tape('is zero address', function(t) { + t.test('should return true when a zero address is passed', function(st) { + st.equal(isZeroAddress('0x0000000000000000000000000000000000000000'), true) + st.end() }) - it('should return false when the address is not equal to zero', function() { + t.test('should return false when the address is not equal to zero', function(st) { const nonZeroAddress = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' - assert.equal(isZeroAddress(nonZeroAddress), false) + st.equal(isZeroAddress(nonZeroAddress), false) + st.end() }) - it('should throw when address is not hex-prefixed', function() { - assert.throws(function() { + t.test('should throw when address is not hex-prefixed', function(st) { + st.throws(function() { isZeroAddress('0000000000000000000000000000000000000000') }) + st.end() }) }) -describe('unpadBuffer', function() { - it('should unpad a Buffer', function() { +tape('unpadBuffer', function(t) { + t.test('should unpad a Buffer', function(st) { const buf = toBuffer('0x0000000006600') const r = unpadBuffer(buf) - assert.deepEqual(r, toBuffer('0x6600')) + st.ok(r.equals(toBuffer('0x6600'))) + st.end() }) - it('should throw if input is not a Buffer', function() { - assert.throws(function() { + t.test('should throw if input is not a Buffer', function(st) { + st.throws(function() { unpadBuffer(('0000000006600') as Buffer) }) + st.end() }) }) -describe('unpadArray', function() { - it('should unpad an Array', function() { +tape('unpadArray', function(t) { + t.test('should unpad an Array', function(st) { const arr = [0, 0, 0, 1] const r = unpadArray(arr) - assert.deepEqual(r, [1]) + st.deepEqual(r, [1]) + st.end() }) - it('should throw if input is not an Array', function() { - assert.throws(function() { + t.test('should throw if input is not an Array', function(st) { + st.throws(function() { unpadArray((toBuffer([0, 0, 0, 1])) as number[]) }) + st.end() }) }) -describe('unpadHexString', function() { - it('should unpad a hex prefixed string', function() { +tape('unpadHexString', function(t) { + t.test('should unpad a hex prefixed string', function(st) { const str = '0x0000000006600' const r = unpadHexString(str) - assert.equal(r, '6600') + st.equal(r, '6600') + st.end() }) - it('should throw if input is not hex-prefixed', function() { - assert.throws(function() { + t.test('should throw if input is not hex-prefixed', function(st) { + st.throws(function() { unpadHexString('0000000006600') }) + st.end() }) }) -describe('setLengthLeft', function() { - it('should left pad a Buffer', function() { +tape('setLengthLeft', function(t) { + t.test('should left pad a Buffer', function(st) { const buf = Buffer.from([9, 9]) const padded = setLengthLeft(buf, 3) - assert.equal(padded.toString('hex'), '000909') + st.equal(padded.toString('hex'), '000909') + st.end() }) - it('should left truncate a Buffer', function() { + t.test('should left truncate a Buffer', function(st) { const buf = Buffer.from([9, 0, 9]) const padded = setLengthLeft(buf, 2) - assert.equal(padded.toString('hex'), '0009') + st.equal(padded.toString('hex'), '0009') + st.end() }) - it('should throw if input is not a Buffer', function() { - assert.throws(function() { + t.test('should throw if input is not a Buffer', function(st) { + st.throws(function() { setLengthLeft(([9, 9]) as Buffer, 3) }) + st.end() }) }) -describe('setLengthRight', function() { - it('should right pad a Buffer', function() { +tape('setLengthRight', function(t) { + t.test('should right pad a Buffer', function(st) { const buf = Buffer.from([9, 9]) const padded = setLengthRight(buf, 3) - assert.equal(padded.toString('hex'), '090900') + st.equal(padded.toString('hex'), '090900') + st.end() }) - it('should right truncate a Buffer', function() { + t.test('should right truncate a Buffer', function(st) { const buf = Buffer.from([9, 0, 9]) const padded = setLengthRight(buf, 2) - assert.equal(padded.toString('hex'), '0900') + st.equal(padded.toString('hex'), '0900') + st.end() }) - it('should throw if input is not a Buffer', function() { - assert.throws(function() { + t.test('should throw if input is not a Buffer', function(st) { + st.throws(function() { setLengthRight(([9, 9]) as Buffer, 3) }) + st.end() }) }) -describe('bufferToHex', function() { - it('should convert a buffer to hex', function() { +tape('bufferToHex', function(t) { + t.test('should convert a buffer to hex', function(st) { const buf = Buffer.from('5b9ac8', 'hex') const hex = bufferToHex(buf) - assert.equal(hex, '0x5b9ac8') + st.equal(hex, '0x5b9ac8') + st.end() }) - it('empty buffer', function() { + t.test('empty buffer', function(st) { const buf = Buffer.alloc(0) const hex = bufferToHex(buf) - assert.strictEqual(hex, '0x') + st.strictEqual(hex, '0x') + st.end() }) }) -describe('bufferToInt', function() { - it('should convert a int to hex', function() { +tape('bufferToInt', function(t) { + t.test('should convert an int to hex', function(st) { const buf = Buffer.from('5b9ac8', 'hex') const i = bufferToInt(buf) - assert.equal(i, 6003400) - assert.equal(bufferToInt(Buffer.allocUnsafe(0)), 0) + st.equal(i, 6003400) + st.equal(bufferToInt(Buffer.allocUnsafe(0)), 0) + st.end() }) - it('should convert empty input to 0', function() { - assert.equal(bufferToInt(Buffer.allocUnsafe(0)), 0) + t.test('should convert empty input to 0', function(st) { + st.equal(bufferToInt(Buffer.allocUnsafe(0)), 0) + st.end() }) }) -describe('fromSigned', function() { - it('should convert an unsigned (negative) buffer to a singed number', function() { +tape('fromSigned', function(t) { + t.test('should convert an unsigned (negative) buffer to a signed number', function(st) { const neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656' const buf = Buffer.allocUnsafe(32).fill(0) buf[0] = 255 - assert.equal(fromSigned(buf), neg) + st.equal(fromSigned(buf).toString(), neg) + st.end() }) - it('should convert an unsigned (positive) buffer to a singed number', function() { + t.test('should convert an unsigned (positive) buffer to a signed number', function(st) { const neg = '452312848583266388373324160190187140051835877600158453279131187530910662656' const buf = Buffer.allocUnsafe(32).fill(0) buf[0] = 1 - assert.equal(fromSigned(buf), neg) + st.equal(fromSigned(buf).toString(), neg) + st.end() }) }) -describe('toUnsigned', function() { - it('should convert a signed (negative) number to unsigned', function() { +tape('toUnsigned', function(t) { + t.test('should convert a signed (negative) number to unsigned', function(st) { const neg = '-452312848583266388373324160190187140051835877600158453279131187530910662656' const hex = 'ff00000000000000000000000000000000000000000000000000000000000000' const num = new BN(neg) - assert.equal(toUnsigned(num).toString('hex'), hex) + st.equal(toUnsigned(num).toString('hex'), hex) + st.end() }) - it('should convert a signed (positive) number to unsigned', function() { + t.test('should convert a signed (positive) number to unsigned', function(st) { const neg = '452312848583266388373324160190187140051835877600158453279131187530910662656' const hex = '0100000000000000000000000000000000000000000000000000000000000000' const num = new BN(neg) - assert.equal(toUnsigned(num).toString('hex'), hex) + st.equal(toUnsigned(num).toString('hex'), hex) + st.end() }) }) -describe('hex prefix', function() { +tape('hex prefix', function(t) { const string = 'd658a4b8247c14868f3c512fa5cbb6e458e4a989' - it('should add', function() { - assert.equal(addHexPrefix(string), '0x' + string) + t.test('should add', function(st) { + st.equal(addHexPrefix(string), '0x' + string) + st.end() }) - it('should return on non-string input', function() { - assert.equal(addHexPrefix(1 as any), 1) + t.test('should return on non-string input', function(st) { + st.equal(addHexPrefix(1 as any), 1) + st.end() }) }) -describe('toBuffer', function() { - it('should work', function() { +tape('toBuffer', function(t) { + t.test('should work', function(st) { // Buffer - assert.deepEqual(toBuffer(Buffer.allocUnsafe(0)), Buffer.allocUnsafe(0)) + st.ok(toBuffer(Buffer.allocUnsafe(0)).equals(Buffer.allocUnsafe(0))) // Array - assert.deepEqual(toBuffer([]), Buffer.allocUnsafe(0)) + st.ok(toBuffer([]).equals(Buffer.allocUnsafe(0))) // String - assert.deepEqual(toBuffer('0x11'), Buffer.from([17])) - assert.deepEqual(toBuffer('0x1234').toString('hex'), '1234') - assert.deepEqual(toBuffer('0x'), Buffer.from([])) + st.ok(toBuffer('0x11').equals(Buffer.from([17]))) + st.equal(toBuffer('0x1234').toString('hex'), '1234') + st.ok(toBuffer('0x').equals(Buffer.from([]))) // Number - assert.deepEqual(toBuffer(1), Buffer.from([1])) + st.ok(toBuffer(1).equals(Buffer.from([1]))) // null - assert.deepEqual(toBuffer(null), Buffer.allocUnsafe(0)) + st.ok(toBuffer(null).equals(Buffer.allocUnsafe(0))) // undefined - assert.deepEqual(toBuffer(undefined), Buffer.allocUnsafe(0)) + st.ok(toBuffer(undefined).equals(Buffer.allocUnsafe(0))) // 'toBN' - assert.deepEqual(toBuffer(new BN(1)), Buffer.from([1])) + st.ok(toBuffer(new BN(1)).equals(Buffer.from([1]))) // 'toArray' - assert.deepEqual( + st.ok( toBuffer({ toArray: function(): any { return [1] } - }), - Buffer.from([1]) + }).equals(Buffer.from([1])) ) + st.end() }) - it('should fail', function() { - assert.throws(function() { - // @ts-ignore - toBuffer({ test: 1 }) + t.test('should fail', function(st) { + st.throws(function() { + toBuffer({ test: 1 } as any) }) + st.end() }) - it('should fail with non 0x-prefixed hex strings', function() { - assert.throws(() => toBuffer('11'), '11') - assert.throws(() => toBuffer('')) - assert.throws(() => toBuffer('0xR'), '0xR') + t.test('should fail with non 0x-prefixed hex strings', function(st) { + st.throws(() => toBuffer('11'), '11') + st.throws(() => toBuffer('')) + st.throws(() => toBuffer('0xR'), '0xR') + st.end() }) - it('should convert a TransformableToBuffer like the Address class (i.e. provides a toBuffer method)', function() { - const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' - const address = Address.fromString(str) - const addressBuf = toBuffer(address) - assert.ok(addressBuf.equals(address.toBuffer())) - }) + t.test( + 'should convert a TransformableToBuffer like the Address class (i.e. provides a toBuffer method)', + function(st) { + const str = '0x2f015c60e0be116b1f0cd534704db9c92118fb6a' + const address = Address.fromString(str) + const addressBuf = toBuffer(address) + st.ok(addressBuf.equals(address.toBuffer())) + st.end() + } + ) }) -describe('baToJSON', function() { - it('should turn a array of buffers into a pure json object', function() { +tape('baToJSON', function(t) { + t.test('should turn a array of buffers into a pure json object', function(st) { const ba = [Buffer.from([0]), Buffer.from([1]), [Buffer.from([2])]] - assert.deepEqual(baToJSON(ba), ['0x00', '0x01', ['0x02']]) + st.deepEqual(baToJSON(ba), ['0x00', '0x01', ['0x02']]) + st.end() }) - it('should turn a buffers into string', function() { - assert.deepEqual(baToJSON(Buffer.from([0])), '0x00') + t.test('should turn a buffers into string', function(st) { + st.deepEqual(baToJSON(Buffer.from([0])), '0x00') + st.end() }) }) diff --git a/test/constants.spec.ts b/test/constants.spec.ts index c985c2b9..c8dd28b7 100644 --- a/test/constants.spec.ts +++ b/test/constants.spec.ts @@ -1,4 +1,4 @@ -import assert from 'assert' +import tape from 'tape' import { MAX_INTEGER, TWO_POW256, @@ -10,46 +10,42 @@ import { KECCAK256_RLP } from '../src' -describe('constants', function() { - it('should match constants', function() { - assert.equal( +tape('constants', function(t) { + t.test('should match constants', function(st) { + st.equal( MAX_INTEGER.toString('hex'), 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' ) - assert.equal( + st.equal( TWO_POW256.toString('hex'), '10000000000000000000000000000000000000000000000000000000000000000' ) - assert.equal( - KECCAK256_NULL_S, - 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' - ) + st.equal(KECCAK256_NULL_S, 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470') - assert.equal( + st.equal( KECCAK256_NULL.toString('hex'), 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' ) - assert.equal( + st.equal( KECCAK256_RLP_ARRAY_S, '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347' ) - assert.equal( + st.equal( KECCAK256_RLP_ARRAY.toString('hex'), '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347' ) - assert.equal( - KECCAK256_RLP_S, - '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' - ) + st.equal(KECCAK256_RLP_S, '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421') - assert.equal( + st.equal( KECCAK256_RLP.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' ) + + st.end() }) }) diff --git a/test/externals.spec.ts b/test/externals.spec.ts index b6b25130..37fbad46 100644 --- a/test/externals.spec.ts +++ b/test/externals.spec.ts @@ -1,54 +1,61 @@ -import assert from 'assert' - +import tape from 'tape' import BN_export from 'bn.js' import * as rlp_export from 'rlp' - import * as src from '../src' -describe('External BN export', () => { - it('should export `BN`', () => { - assert.equal(src.BN, BN_export) +tape('External BN export', t => { + t.test('should export `BN`', st => { + st.equal(src.BN, BN_export) + st.end() }) - it('should use a BN function correctly', () => { + t.test('should use a BN function correctly', st => { const a = new src.BN('dead', 16) const b = new src.BN('101010', 2) const result = a.add(b) - assert.equal(result.toString(10), 57047) + st.equal(result.toString(10), '57047') + st.end() }) - it('should throw on exceptions', () => { + t.test('should throw on exceptions', st => { // should not allow 0 input - assert.throws(() => { + st.throws(() => { new src.BN(1).egcd(new src.BN('0')) }, /^Error: Assertion failed$/) + st.end() }) - // should not accept an unsafe integer - const num = Math.pow(2, 53) - assert.throws(() => { - return new src.BN(num, 10) - }, /^Error: Assertion failed$/) + t.test('should not accept an unsafe integer', st => { + const num = Math.pow(2, 53) + st.throws(() => { + return new src.BN(num, 10) + }, /^Error: Assertion failed$/) + st.end() + }) - // should throw error with num eq 0x4000000 - assert.throws(function() { - new src.BN(0).iaddn(0x4000000) - }, /^Error: Assertion failed$/) + t.test('should throw error with num eq 0x4000000', st => { + st.throws(function() { + new src.BN(0).iaddn(0x4000000) + }, /^Error: Assertion failed$/) + st.end() + }) }) -describe('External rlp export', () => { - it('should export `rlp`', () => { - assert.equal(src.rlp, rlp_export) +tape('External rlp export', t => { + t.test('should export `rlp`', st => { + st.equal(src.rlp, rlp_export) + st.end() }) - it('should use a rlp function correctly', () => { + t.test('should use a rlp function correctly', st => { const nestedList = [[], [[]], [[], [[]]]] const encoded = src.rlp.encode(nestedList) const decoded = src.rlp.decode(encoded) - assert.deepEqual(nestedList, decoded) + st.deepEqual(nestedList, decoded) + st.end() }) - it('should throw on exceptions', () => { + t.test('should throw on exceptions', st => { // bad values: wrong encoded a zero const val = Buffer.from( 'f9005f030182520894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3', @@ -60,7 +67,7 @@ describe('External rlp export', () => { } catch (e) { // pass } - assert.equal(result, undefined) + st.equal(result, undefined) // bad values: invalid length const a = Buffer.from( @@ -74,12 +81,13 @@ describe('External rlp export', () => { } catch (e) { // pass } - assert.equal(res, undefined) + st.equal(res, undefined) + st.end() }) }) -describe('External ethjsUtil export', () => { - it('should have all ethjsUtil methods', () => { +tape('External ethjsUtil export', t => { + t.test('should have all ethjsUtil methods', st => { const expected = [ 'arrayContainsArray', 'toBuffer', @@ -98,30 +106,33 @@ describe('External ethjsUtil export', () => { ] expected.forEach(prop => { - assert.ok(prop in src) + st.ok(prop in src) }) + st.end() }) - it('should use ethjsUtil functions correctly', () => { + t.test('should use ethjsUtil functions correctly', st => { // should convert intToHex - assert.equal(src.intToHex(new src.BN(0).toNumber()), '0x0') + st.equal(src.intToHex(new src.BN(0).toNumber()), '0x0') // should convert intToHex const i = 6003400 const hex = src.intToHex(i) - assert.equal(hex, '0x5b9ac8') + st.equal(hex, '0x5b9ac8') // should convert a int to a buffer const j = 6003400 const buf = src.intToBuffer(j) - assert.equal(buf.toString('hex'), '5b9ac8') + st.equal(buf.toString('hex'), '5b9ac8') + st.end() }) - it('should handle exceptions and invalid inputs', () => { + t.test('should handle exceptions and invalid inputs', st => { // should throw when invalid abi - assert.throws(() => src.getKeys([], (3289) as string), Error) + st.throws(() => src.getKeys([], (3289) as string), Error) // should detect invalid length hex string - assert.equal(src.isHexString('0x0', 2), false) + st.equal(src.isHexString('0x0', 2), false) + st.end() }) }) diff --git a/test/hash.spec.ts b/test/hash.spec.ts index e9a3fadf..9e28cb78 100644 --- a/test/hash.spec.ts +++ b/test/hash.spec.ts @@ -1,4 +1,4 @@ -import assert from 'assert' +import tape from 'tape' import { keccak, keccak256, @@ -15,236 +15,267 @@ import { toBuffer } from '../src' -describe('keccak', function() { - it('should produce a keccak224 hash', function() { +tape('keccak', function(t) { + t.test('should produce a keccak224 hash', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '9e66938bd8f32c8610444bb524630db496bd58b689f9733182df63ba' const hash = keccak(toBuffer(msg), 224) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should produce a keccak256 hash', function() { + t.test('should produce a keccak256 hash', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' const hash = keccak(toBuffer(msg)) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should produce a keccak384 hash', function() { + t.test('should produce a keccak384 hash', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '923e0f6a1c324a698139c3f3abbe88ac70bf2e7c02b26192c6124732555a32cef18e81ac91d5d97ce969745409c5bbc6' const hash = keccak(toBuffer(msg), 384) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should produce a keccak512 hash', function() { + t.test('should produce a keccak512 hash', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '36fdacd0339307068e9ed191773a6f11f6f9f99016bd50f87fd529ab7c87e1385f2b7ef1ac257cc78a12dcb3e5804254c6a7b404a6484966b831eadc721c3d24' const hash = keccak(toBuffer(msg), 512) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not Buffer', function() { + t.test('should error if input is not Buffer', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { keccak((msg) as Buffer) }) + st.end() }) - it('should error if provided incorrect bits', function() { + t.test('should error if provided incorrect bits', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { keccak(toBuffer(msg), 1024) }) + st.end() }) }) -describe('keccak256', function() { - it('should produce a hash (keccak(a, 256) alias)', function() { +tape('keccak256', function(t) { + t.test('should produce a hash (keccak(a, 256) alias)', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' const hash = keccak256(toBuffer(msg)) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) }) -describe('keccakFromString', function() { - it('should produce a hash', function() { +tape('keccakFromString', function(t) { + t.test('should produce a hash', function(st) { const msg = '3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '22ae1937ff93ec72c4d46ff3e854661e3363440acd6f6e4adf8f1a8978382251' const hash = keccakFromString(msg) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should throw if input is not a string', function() { + t.test('should throw if input is not a string', function(st) { const buf = toBuffer('0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1') - assert.throws(function() { + st.throws(function() { keccakFromString((buf) as string) }) + st.end() }) }) -describe('keccakFromHexString', function() { - it('should produce a hash', function() { +tape('keccakFromHexString', function(t) { + t.test('should produce a hash', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28' const hash = keccakFromHexString(msg) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should throw if input is not hex-prefixed', function() { + t.test('should throw if input is not hex-prefixed', function(st) { const msg = '3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { keccakFromHexString(msg) }) + st.end() }) - it('should throw if input is not a string', function() { + t.test('should throw if input is not a string', function(st) { const buf = toBuffer('0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1') - assert.throws(function() { + st.throws(function() { keccakFromHexString((buf) as string) }) + st.end() }) }) -describe('keccakFromArray', function() { - it('should produce a hash', function() { +tape('keccakFromArray', function(t) { + t.test('should produce a hash', function(st) { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] const r = 'fba8669bd39e3257e64752758f3a0d3218865a15757c6b0bc48b8ef95bc8bfd5' const hash = keccakFromArray(arr) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should throw if input is not an array', function() { + t.test('should throw if input is not an array', function(st) { const buf = toBuffer([0, 1, 2, 3, 4, 5, 6, 7, 8, 0]) - assert.throws(function() { + st.throws(function() { keccakFromArray((buf) as number[]) }) + st.end() }) }) -describe('keccak-512', function() { - it('should produce a hash', function() { +tape('keccak-512', function(t) { + t.test('should produce a hash', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '36fdacd0339307068e9ed191773a6f11f6f9f99016bd50f87fd529ab7c87e1385f2b7ef1ac257cc78a12dcb3e5804254c6a7b404a6484966b831eadc721c3d24' const hash = keccak(toBuffer(msg), 512) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) }) -describe('sha256', function() { - it('should produce a sha256', function() { +tape('sha256', function(t) { + t.test('should produce a sha256', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '58bbda5e10bc11a32d808e40f9da2161a64f00b5557762a161626afe19137445' const hash = sha256(toBuffer(msg)) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not Buffer', function() { + t.test('should error if input is not Buffer', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { sha256((msg) as Buffer) }) + st.end() }) }) -describe('sha256FromString', function() { - it('should produce a sha256', function() { +tape('sha256FromString', function(t) { + t.test('should produce a sha256', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '58bbda5e10bc11a32d808e40f9da2161a64f00b5557762a161626afe19137445' const hash = sha256FromString(msg) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not Buffer', function() { + t.test('should error if input is not Buffer', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { sha256FromString((toBuffer(msg)) as string) }) + st.end() }) }) -describe('sha256FromArray', function() { - it('should produce a sha256', function() { +tape('sha256FromArray', function(t) { + t.test('should produce a sha256', function(st) { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] const r = '5443c487d45d01c56150d91e7a071c69a97939b1c57874b73989a9ff7875e86b' const hash = sha256FromArray(arr) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not Buffer', function() { + t.test('should error if input is not Buffer', function(st) { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] - assert.throws(function() { + st.throws(function() { sha256FromArray((toBuffer(arr)) as number[]) }) + st.end() }) }) -describe('ripemd160', function() { - it('should produce a ripemd160', function() { +tape('ripemd160', function(t) { + t.test('should produce a ripemd160', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '4bb0246cbfdfddbe605a374f1187204c896fabfd' const hash = ripemd160(toBuffer(msg), false) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should produce a padded ripemd160', function() { + t.test('should produce a padded ripemd160', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '0000000000000000000000004bb0246cbfdfddbe605a374f1187204c896fabfd' const hash = ripemd160(toBuffer(msg), true) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not Buffer', function() { + t.test('should error if input is not Buffer', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { ripemd160((msg) as Buffer, false) }) + st.end() }) }) -describe('ripemd160FromString', function() { - it('should produce a ripemd160', function() { +tape('ripemd160FromString', function(t) { + t.test('should produce a ripemd160', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '4bb0246cbfdfddbe605a374f1187204c896fabfd' const hash = ripemd160FromString(msg, false) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should produce a padded ripemd160', function() { + t.test('should produce a padded ripemd160', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '0000000000000000000000004bb0246cbfdfddbe605a374f1187204c896fabfd' const hash = ripemd160FromString(msg, true) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not a string', function() { + t.test('should error if input is not a string', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' - assert.throws(function() { + st.throws(function() { ripemd160FromString((toBuffer(msg)) as string, false) }) + st.end() }) }) -describe('ripemd160FromArray', function() { - it('should produce a ripemd160', function() { +tape('ripemd160FromArray', function(t) { + t.test('should produce a ripemd160', function(st) { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] const r = 'ddbb5062318b209e3dbfc389fe61840363050071' const hash = ripemd160FromArray(arr, false) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should produce a padded ripemd160', function() { + t.test('should produce a padded ripemd160', function(st) { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] const r = '000000000000000000000000ddbb5062318b209e3dbfc389fe61840363050071' const hash = ripemd160FromArray(arr, true) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) - it('should error if input is not an array', function() { + t.test('should error if input is not an array', function(st) { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0] - assert.throws(function() { + st.throws(function() { ripemd160FromArray((toBuffer(arr)) as number[], false) }) + st.end() }) }) -describe('rlphash', function() { - it('should produce a keccak-256 hash of the rlp data', function() { +tape('rlphash', function(t) { + t.test('should produce a keccak-256 hash of the rlp data', function(st) { const msg = '0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1' const r = '33f491f24abdbdbf175e812b94e7ede338d1c7f01efb68574acd279a15a39cbe' const hash = rlphash(msg) - assert.equal(hash.toString('hex'), r) + st.equal(hash.toString('hex'), r) + st.end() }) }) diff --git a/test/object.spec.ts b/test/object.spec.ts index 5e700910..e99ad11b 100644 --- a/test/object.spec.ts +++ b/test/object.spec.ts @@ -1,7 +1,7 @@ -import assert from 'assert' +import tape from 'tape' import { zeros, defineProperties } from '../src' -describe('define', function() { +tape('define', function(t) { const fields = [ { name: 'aword', @@ -32,22 +32,23 @@ describe('define', function() { } ] - it('should trim zeros', function() { + t.test('should trim zeros', function(st) { const someOb: any = {} defineProperties(someOb, fields) // Define Properties someOb.r = '0x00004' - assert.equal(someOb.r.toString('hex'), '04') + st.equal(someOb.r.toString('hex'), '04') someOb.r = Buffer.from([0, 0, 0, 0, 4]) - assert.equal(someOb.r.toString('hex'), '04') + st.equal(someOb.r.toString('hex'), '04') + st.end() }) - it("shouldn't allow wrong size for exact size requirements", function() { + t.test("shouldn't allow wrong size for exact size requirements", function(st) { const someOb = {} defineProperties(someOb, fields) - assert.throws(function() { + st.throws(function() { const tmp = [ { name: 'mustBeExactSize', @@ -58,9 +59,10 @@ describe('define', function() { ] defineProperties(someOb, tmp) }) + st.end() }) - it('it should accept rlp encoded intial data', function() { + t.test('it should accept rlp encoded intial data', function(st) { const someOb: any = {} const data = { aword: '0x01', @@ -80,12 +82,12 @@ describe('define', function() { const expectedArray = ['0x01', '0x', '0x02', '0x03', '0x04'] defineProperties(someOb, fields, data) - assert.deepEqual(someOb.toJSON(true), expected, 'should produce the correctly labeled object') + st.deepEqual(someOb.toJSON(true), expected, 'should produce the correctly labeled object') const someOb2: any = {} const rlpEncoded = someOb.serialize().toString('hex') defineProperties(someOb2, fields, rlpEncoded) - assert.equal( + st.equal( someOb2.serialize().toString('hex'), rlpEncoded, 'the constuctor should accept rlp encoded buffers' @@ -93,21 +95,23 @@ describe('define', function() { const someOb3 = {} defineProperties(someOb3, fields, expectedArray) - assert.deepEqual(someOb.toJSON(), expectedArray, 'should produce the correctly object') + st.deepEqual(someOb.toJSON(), expectedArray, 'should produce the correctly object') + st.end() }) - it('it should not accept invalid values in the constuctor', function() { + t.test('it should not accept invalid values in the constuctor', function(st) { const someOb = {} - assert.throws(function() { + st.throws(function() { defineProperties(someOb, fields, 5) }, 'should throw on nonsensical data') - assert.throws(function() { + st.throws(function() { defineProperties(someOb, fields, Array(6)) }, 'should throw on invalid arrays') + st.end() }) - it('alias should work ', function() { + t.test('alias should work ', function(st) { const someOb: any = {} const data = { aword: '0x01', @@ -117,18 +121,20 @@ describe('define', function() { } defineProperties(someOb, fields, data) - assert.equal(someOb.blah.toString('hex'), '01') + st.equal(someOb.blah.toString('hex'), '01') someOb.blah = '0x09' - assert.equal(someOb.blah.toString('hex'), '09') - assert.equal(someOb.aword.toString('hex'), '09') + st.equal(someOb.blah.toString('hex'), '09') + st.equal(someOb.aword.toString('hex'), '09') + st.end() }) - it('alias should work #2', function() { + t.test('alias should work #2', function(st) { const someOb: any = {} const data = { blah: '0x1' } defineProperties(someOb, fields, data) - assert.equal(someOb.blah.toString('hex'), '01') - assert.equal(someOb.aword.toString('hex'), '01') + st.equal(someOb.blah.toString('hex'), '01') + st.equal(someOb.aword.toString('hex'), '01') + st.end() }) }) diff --git a/test/signature.spec.ts b/test/signature.spec.ts index 09dd25d8..302d0097 100644 --- a/test/signature.spec.ts +++ b/test/signature.spec.ts @@ -1,6 +1,6 @@ -import assert from 'assert' -import BN from 'bn.js' +import tape from 'tape' import { + BN, ecsign, ecrecover, privateToPublic, @@ -21,34 +21,40 @@ const ecprivkey = Buffer.from( ) const chainId = 3 // ropsten -describe('ecsign', function() { - it('should produce a signature', function() { +tape('ecsign', function(t) { + t.test('should produce a signature', function(st) { const sig = ecsign(echash, ecprivkey) - assert.deepEqual( - sig.r, - Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + st.ok( + sig.r.equals( + Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + ) ) - assert.deepEqual( - sig.s, - Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + st.ok( + sig.s.equals( + Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + ) ) - assert.equal(sig.v, 27) + st.equal(sig.v, 27) + st.end() }) - it('should produce a signature for Ropsten testnet', function() { + t.test('should produce a signature for Ropsten testnet', function(st) { const sig = ecsign(echash, ecprivkey, chainId) - assert.deepEqual( - sig.r, - Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + st.ok( + sig.r.equals( + Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') + ) ) - assert.deepEqual( - sig.s, - Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + st.ok( + sig.s.equals( + Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') + ) ) - assert.equal(sig.v, 41) + st.equal(sig.v, 41) + st.end() }) - it('should produce a signature for chainId=150', function() { + t.test('should produce a signature for chainId=150', function(st) { const expectedSigR = Buffer.from( '99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex' @@ -60,103 +66,114 @@ describe('ecsign', function() { const expectedSigV = Buffer.from('014f', 'hex') const sig = ecsign(echash, ecprivkey, 150) - assert.deepEqual(sig.r, expectedSigR) - assert.deepEqual(sig.s, expectedSigS) - assert.equal(sig.v, 150 * 2 + 35) + st.ok(sig.r.equals(expectedSigR)) + st.ok(sig.s.equals(expectedSigS)) + st.equal(sig.v, 150 * 2 + 35) let sigBuffer = ecsign(echash, ecprivkey, new BN(150)) - assert.deepEqual(sigBuffer.r, expectedSigR) - assert.deepEqual(sigBuffer.s, expectedSigS) - assert.deepEqual(sigBuffer.v, expectedSigV) + st.ok(sigBuffer.r.equals(expectedSigR)) + st.ok(sigBuffer.s.equals(expectedSigS)) + st.ok(sigBuffer.v.equals(expectedSigV)) sigBuffer = ecsign(echash, ecprivkey, Buffer.from([150])) - assert.deepEqual(sigBuffer.v, expectedSigV) + st.ok(sigBuffer.v.equals(expectedSigV)) sigBuffer = ecsign(echash, ecprivkey, '0x96') - assert.deepEqual(sigBuffer.v, expectedSigV) + st.ok(sigBuffer.v.equals(expectedSigV)) - assert.throws(function() { + st.throws(function() { ecsign(echash, ecprivkey, '96') }) + st.end() }) -}) -it('should produce a signature for a high number chainId greater than MAX_SAFE_INTEGER', function() { - const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') - const expectedSigR = Buffer.from( - '99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', - 'hex' - ) - const expectedSigS = Buffer.from( - '129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', - 'hex' + t.test( + 'should produce a signature for a high number chainId greater than MAX_SAFE_INTEGER', + function(st) { + const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') + const expectedSigR = Buffer.from( + '99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', + 'hex' + ) + const expectedSigS = Buffer.from( + '129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', + 'hex' + ) + const expectedSigV = Buffer.from('f2ded8deec6713', 'hex') + + let sigBuffer = ecsign(echash, ecprivkey, new BN(chainIDBuffer)) + st.ok(sigBuffer.r.equals(expectedSigR)) + st.ok(sigBuffer.s.equals(expectedSigS)) + st.ok(sigBuffer.v.equals(expectedSigV)) + + sigBuffer = ecsign(echash, ecprivkey, chainIDBuffer) + st.ok(sigBuffer.v.equals(expectedSigV)) + + sigBuffer = ecsign(echash, ecprivkey, '0x' + chainIDBuffer.toString('hex')) + st.ok(sigBuffer.v.equals(expectedSigV)) + + const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) + st.throws(() => { + // If we would use a number for the `chainId` parameter then it should throw. + // (The numbers are too high to perform arithmetic on) + ecsign(echash, ecprivkey, chainIDNumber) + }) + st.end() + } ) - const expectedSigV = Buffer.from('f2ded8deec6713', 'hex') - - let sigBuffer = ecsign(echash, ecprivkey, new BN(chainIDBuffer)) - assert.deepEqual(sigBuffer.r, expectedSigR) - assert.deepEqual(sigBuffer.s, expectedSigS) - assert.deepEqual(sigBuffer.v, expectedSigV) - - sigBuffer = ecsign(echash, ecprivkey, chainIDBuffer) - assert.deepEqual(sigBuffer.v, expectedSigV) - - sigBuffer = ecsign(echash, ecprivkey, '0x' + chainIDBuffer.toString('hex')) - assert.deepEqual(sigBuffer.v, expectedSigV) - - const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) - assert.throws(() => { - // If we would use a number for the `chainId` parameter then it should throw. - // (The numbers are too high to perform arithmetic on) - ecsign(echash, ecprivkey, chainIDNumber) - }) }) -describe('ecrecover', function() { - it('should recover a public key', function() { +tape('ecrecover', function(t) { + t.test('should recover a public key', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') const v = 27 const pubkey = ecrecover(echash, v, r, s) - assert.deepEqual(pubkey, privateToPublic(ecprivkey)) + st.ok(pubkey.equals(privateToPublic(ecprivkey))) + st.end() }) - it('should recover a public key (chainId = 3)', function() { + t.test('should recover a public key (chainId = 3)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') const v = 41 const pubkey = ecrecover(echash, v, r, s, chainId) - assert.deepEqual(pubkey, privateToPublic(ecprivkey)) + st.ok(pubkey.equals(privateToPublic(ecprivkey))) + st.end() }) - it('should recover a public key (chainId = 150)', function() { + t.test('should recover a public key (chainId = 150)', function(st) { const chainId = 150 const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') const v = chainId * 2 + 35 const pubkey = ecrecover(echash, v, r, s, chainId) - assert.deepEqual(pubkey, privateToPublic(ecprivkey)) + st.ok(pubkey.equals(privateToPublic(ecprivkey))) + st.end() }) - it('should fail on an invalid signature (v = 21)', function() { + t.test('should fail on an invalid signature (v = 21)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - assert.throws(function() { + st.throws(function() { ecrecover(echash, 21, r, s) }) + st.end() }) - it('should fail on an invalid signature (v = 29)', function() { + t.test('should fail on an invalid signature (v = 29)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - assert.throws(function() { + st.throws(function() { ecrecover(echash, 29, r, s) }) + st.end() }) - it('should fail on an invalid signature (swapped points)', function() { + t.test('should fail on an invalid signature (swapped points)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - assert.throws(function() { + st.throws(function() { ecrecover(echash, 27, s, r) }) + st.end() }) - it('should return the right sender when using very high chain id / v values', function() { + t.test('should return the right sender when using very high chain id / v values', function(st) { // This data is from a transaction of the YoloV3 network, block 77, txhash c6121a23ca17b8ff70d4706c7d134920c1da43c8329444c96b4c63a55af1c760 /* { @@ -186,74 +203,82 @@ describe('ecrecover', function() { const vBuffer = Buffer.from('f2ded8deec6714', 'hex') const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') let sender = ecrecover(msgHash, vBuffer, r, s, chainIDBuffer) - assert.ok(sender.equals(senderPubKey), 'sender pubkey correct (Buffer)') + st.ok(sender.equals(senderPubKey), 'sender pubkey correct (Buffer)') const vBN = new BN(vBuffer) const chainIDBN = new BN(chainIDBuffer) sender = ecrecover(msgHash, vBN, r, s, chainIDBN) - assert.ok(sender.equals(senderPubKey), 'sender pubkey correct (BN)') + st.ok(sender.equals(senderPubKey), 'sender pubkey correct (BN)') const vHexString = '0xf2ded8deec6714' const chainIDHexString = '0x796f6c6f763378' sender = ecrecover(msgHash, vHexString, r, s, chainIDHexString) - assert.ok(sender.equals(senderPubKey), 'sender pubkey correct (HexString)') + st.ok(sender.equals(senderPubKey), 'sender pubkey correct (HexString)') - assert.throws(function() { + st.throws(function() { ecrecover(msgHash, 'f2ded8deec6714', r, s, chainIDHexString) }) - assert.throws(function() { + st.throws(function() { ecrecover(msgHash, vHexString, r, s, '796f6c6f763378') }) const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) const vNumber = parseInt(vBuffer.toString('hex'), 16) - assert.throws(() => { + st.throws(() => { // If we would use numbers for the `v` and `chainId` parameters, then it should throw. // (The numbers are too high to perform arithmetic on) ecrecover(msgHash, vNumber, r, s, chainIDNumber) }) + st.end() }) }) -describe('hashPersonalMessage', function() { - it('should produce a deterministic hash', function() { +tape('hashPersonalMessage', function(t) { + t.test('should produce a deterministic hash', function(st) { const h = hashPersonalMessage(Buffer.from('Hello world')) - assert.deepEqual( - h, - Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex') + st.ok( + h.equals( + Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex') + ) ) + st.end() }) - it('should throw if input is not a buffer', function() { + t.test('should throw if input is not a buffer', function(st) { try { hashPersonalMessage(([0, 1, 2, 3, 4]) as Buffer) } catch (err) { - assert(err.message.includes('This method only supports Buffer')) + st.ok(err.message.includes('This method only supports Buffer')) } + st.end() }) }) -describe('isValidSignature', function() { - it('should fail on an invalid signature (shorter r))', function() { +tape('isValidSignature', function(t) { + t.test('should fail on an invalid signature (shorter r))', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - assert.equal(isValidSignature(27, r, s), false) + st.notOk(isValidSignature(27, r, s)) + st.end() }) - it('should fail on an invalid signature (shorter s))', function() { + t.test('should fail on an invalid signature (shorter s))', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca', 'hex') - assert.equal(isValidSignature(27, r, s), false) + st.notOk(isValidSignature(27, r, s)) + st.end() }) - it('should fail on an invalid signature (v = 21)', function() { + t.test('should fail on an invalid signature (v = 21)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - assert.equal(isValidSignature(21, r, s), false) + st.notOk(isValidSignature(21, r, s)) + st.end() }) - it('should fail on an invalid signature (v = 29)', function() { + t.test('should fail on an invalid signature (v = 29)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - assert.equal(isValidSignature(29, r, s), false) + st.notOk(isValidSignature(29, r, s)) + st.end() }) - it('should fail when on homestead and s > secp256k1n/2', function() { + t.test('should fail when on homestead and s > secp256k1n/2', function(st) { const SECP256K1_N_DIV_2 = new BN( '7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16 @@ -263,9 +288,10 @@ describe('isValidSignature', function() { const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex') const v = 27 - assert.equal(isValidSignature(v, r, s, true), false) + st.notOk(isValidSignature(v, r, s, true)) + st.end() }) - it('should not fail when not on homestead but s > secp256k1n/2', function() { + t.test('should not fail when not on homestead but s > secp256k1n/2', function(st) { const SECP256K1_N_DIV_2 = new BN( '7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16 @@ -275,93 +301,97 @@ describe('isValidSignature', function() { const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex') const v = 27 - assert.equal(isValidSignature(v, r, s, false), true) + st.ok(isValidSignature(v, r, s, false)) + st.end() }) - it('should work otherwise', function() { + t.test('should work otherwise', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') const v = 27 - assert.equal(isValidSignature(v, r, s), true) + st.ok(isValidSignature(v, r, s)) + st.end() }) - it('should work otherwise(chainId=3)', function() { + t.test('should work otherwise (chainId=3)', function(st) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') const v = 41 - assert.equal(isValidSignature(v, r, s, false, chainId), true) + st.ok(isValidSignature(v, r, s, false, chainId)) + st.end() }) - it('should work otherwise(chainId=150)', function() { + t.test('should work otherwise (chainId=150)', function(st) { const chainId = 150 const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') const v = chainId * 2 + 35 - assert.equal(isValidSignature(v, r, s, false, chainId), true) - assert.equal(isValidSignature(intToBuffer(v), r, s, false, intToBuffer(chainId)), true) - assert.equal(isValidSignature(new BN(v), r, s, false, new BN(chainId)), true) - assert.equal( + st.ok(isValidSignature(v, r, s, false, chainId)) + st.ok(isValidSignature(intToBuffer(v), r, s, false, intToBuffer(chainId))) + st.ok(isValidSignature(new BN(v), r, s, false, new BN(chainId))) + st.ok( isValidSignature( '0x' + intToBuffer(v).toString('hex'), r, s, false, '0x' + intToBuffer(chainId).toString('hex') - ), - true + ) ) + st.end() }) - it('should work otherwise(chainId larger than MAX_INTEGER)', function() { + t.test('should work otherwise (chainId larger than MAX_INTEGER)', function(st) { const r = Buffer.from('ec212841e0b7aaffc3b3e33a08adf32fa07159e856ef23db85175a4f6d71dc0f', 'hex') const s = Buffer.from('4b8e02b96b94064a5aa2f8d72bd0040616ba8e482a5dd96422e38c9a4611f8d5', 'hex') const vBuffer = Buffer.from('f2ded8deec6714', 'hex') const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') - assert.equal(isValidSignature(vBuffer, r, s, false, chainIDBuffer), true) - assert.equal(isValidSignature(new BN(vBuffer), r, s, false, new BN(chainIDBuffer)), true) - assert.equal( + st.ok(isValidSignature(vBuffer, r, s, false, chainIDBuffer)) + st.ok(isValidSignature(new BN(vBuffer), r, s, false, new BN(chainIDBuffer))) + st.ok( isValidSignature( '0x' + vBuffer.toString('hex'), r, s, false, '0x' + chainIDBuffer.toString('hex') - ), - true + ) ) const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) const vNumber = parseInt(vBuffer.toString('hex'), 16) - assert.throws(() => { + st.throws(() => { // If we would use numbers for the `v` and `chainId` parameters, then it should throw. // (The numbers are too high to perform arithmetic on) isValidSignature(vNumber, r, s, false, chainIDNumber) }) + st.end() }) // FIXME: add homestead test }) -describe('message sig', function() { +tape('message sig', function(t) { const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex') const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex') - it('should return hex strings that the RPC can use', function() { + t.test('should return hex strings that the RPC can use', function(st) { const sig = '0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca661b' - assert.equal(toRpcSig(27, r, s), sig) - assert.deepEqual(fromRpcSig(sig), { + st.equal(toRpcSig(27, r, s), sig) + st.deepEqual(fromRpcSig(sig), { v: 27, r, s }) + st.end() }) - it('should return hex strings that the RPC can use (chainId=150)', function() { + t.test('should return hex strings that the RPC can use (chainId=150)', function(st) { const sig = '0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66014f' const chainId = 150 const v = chainId * 2 + 35 - assert.equal(toRpcSig(v, r, s, chainId), sig) - assert.equal(toRpcSig(intToBuffer(v), r, s, intToBuffer(chainId)), sig) - assert.equal(toRpcSig(new BN(v), r, s, new BN(chainId)), sig) - assert.equal( + st.equal(toRpcSig(v, r, s, chainId), sig) + st.equal(toRpcSig(intToBuffer(v), r, s, intToBuffer(chainId)), sig) + st.equal(toRpcSig(new BN(v), r, s, new BN(chainId)), sig) + st.equal( toRpcSig( '0x' + intToBuffer(v).toString('hex'), r, @@ -370,53 +400,61 @@ describe('message sig', function() { ), sig ) - assert.deepEqual(fromRpcSig(sig), { + st.deepEqual(fromRpcSig(sig), { v, r, s }) + st.end() }) - it('should return hex strings that the RPC can use (chainId larger than MAX_SAFE_INTEGER)', function() { - const sig = - '0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66f2ded8deec6714' - const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') - const vBuffer = Buffer.from('f2ded8deec6714', 'hex') - assert.equal(toRpcSig(vBuffer, r, s, chainIDBuffer), sig) - assert.equal(toRpcSig(new BN(vBuffer), r, s, new BN(chainIDBuffer)), sig) - assert.equal( - toRpcSig('0x' + vBuffer.toString('hex'), r, s, '0x' + chainIDBuffer.toString('hex')), - sig - ) + t.test( + 'should return hex strings that the RPC can use (chainId larger than MAX_SAFE_INTEGER)', + function(st) { + const sig = + '0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66f2ded8deec6714' + const chainIDBuffer = Buffer.from('796f6c6f763378', 'hex') + const vBuffer = Buffer.from('f2ded8deec6714', 'hex') + st.equal(toRpcSig(vBuffer, r, s, chainIDBuffer), sig) + st.equal(toRpcSig(new BN(vBuffer), r, s, new BN(chainIDBuffer)), sig) + st.equal( + toRpcSig('0x' + vBuffer.toString('hex'), r, s, '0x' + chainIDBuffer.toString('hex')), + sig + ) - const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) - const vNumber = parseInt(vBuffer.toString('hex'), 16) - assert.throws(function() { - toRpcSig(vNumber, r, s, chainIDNumber) - }) - }) + const chainIDNumber = parseInt(chainIDBuffer.toString('hex'), 16) + const vNumber = parseInt(vBuffer.toString('hex'), 16) + st.throws(function() { + toRpcSig(vNumber, r, s, chainIDNumber) + }) + st.end() + } + ) - it('should throw on shorter length', function() { - assert.throws(function() { + t.test('should throw on shorter length', function(st) { + st.throws(function() { fromRpcSig('') }) - assert.throws(function() { + st.throws(function() { fromRpcSig( '0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca' ) }) + st.end() }) - it('pad short r and s values', function() { - assert.equal( + t.test('pad short r and s values', function(st) { + st.equal( toRpcSig(27, r.slice(20), s.slice(20)), '0x00000000000000000000000000000000000000004a1579cf389ef88b20a1abe90000000000000000000000000000000000000000326fa689f228040429e3ca661b' ) + st.end() }) - it('should throw on invalid v value', function() { - assert.throws(function() { + t.test('should throw on invalid v value', function(st) { + st.throws(function() { toRpcSig(1, r, s) }) + st.end() }) }) diff --git a/test/types.spec.ts b/test/types.spec.ts index 16ae5f31..0b2322ed 100644 --- a/test/types.spec.ts +++ b/test/types.spec.ts @@ -1,112 +1,124 @@ -import assert from 'assert' -import BN from 'bn.js' -import { toType, TypeOutput, intToBuffer, bufferToHex, intToHex, bnToHex, toBuffer } from '../src' +import tape from 'tape' +import { + BN, + toType, + TypeOutput, + intToBuffer, + bufferToHex, + intToHex, + bnToHex, + toBuffer +} from '../src' -describe('toType', function() { - describe('from Number', function() { +tape('toType', function(t) { + t.test('from Number', function(st) { const num = 1000 - it('should convert to Number', function() { + st.test('should convert to Number', function(st) { const result = toType(num, TypeOutput.Number) - assert.strictEqual(result, num) + st.strictEqual(result, num) + st.end() }) - it('should convert to BN', function() { + st.test('should convert to BN', function(st) { const result = toType(num, TypeOutput.BN) - assert.ok(result.eq(new BN(num))) + st.ok(result.eq(new BN(num))) + st.end() }) - it('should convert to Buffer', function() { + st.test('should convert to Buffer', function(st) { const result = toType(num, TypeOutput.Buffer) - assert.ok(result.equals(intToBuffer(num))) + st.ok(result.equals(intToBuffer(num))) + st.end() }) - it('should convert to PrefixedHexString', function() { + st.test('should convert to PrefixedHexString', function(st) { const result = toType(num, TypeOutput.PrefixedHexString) - assert.strictEqual(result, bufferToHex(new BN(num).toArrayLike(Buffer))) - }) - it('should throw an error if greater than MAX_SAFE_INTEGER', function() { - assert.throws( - () => { - const num = Number.MAX_SAFE_INTEGER + 1 - toType(num, TypeOutput.BN) - }, - { - message: - 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)' - } - ) + st.strictEqual(result, bufferToHex(new BN(num).toArrayLike(Buffer))) + st.end() + }) + st.test('should throw an error if greater than MAX_SAFE_INTEGER', function(st) { + st.throws(() => { + const num = Number.MAX_SAFE_INTEGER + 1 + toType(num, TypeOutput.BN) + }, /^Error: The provided number is greater than MAX_SAFE_INTEGER \(please use an alternative input type\)$/) + st.end() }) }) - describe('from BN', function() { + t.test('from BN', function(st) { const num = new BN(1000) - it('should convert to Number', function() { + st.test('should convert to Number', function(st) { const result = toType(num, TypeOutput.Number) - assert.strictEqual(result, num.toNumber()) + st.strictEqual(result, num.toNumber()) + st.end() }) - it('should convert to BN', function() { + st.test('should convert to BN', function(st) { const result = toType(num, TypeOutput.BN) - assert.ok(result.eq(num)) + st.ok(result.eq(num)) + st.end() }) - it('should convert to Buffer', function() { + st.test('should convert to Buffer', function(st) { const result = toType(num, TypeOutput.Buffer) - assert.ok(result.equals(num.toArrayLike(Buffer))) + st.ok(result.equals(num.toArrayLike(Buffer))) + st.end() }) - it('should convert to PrefixedHexString', function() { + st.test('should convert to PrefixedHexString', function(st) { const result = toType(num, TypeOutput.PrefixedHexString) - assert.strictEqual(result, bufferToHex(num.toArrayLike(Buffer))) - }) - it('should throw an error if converting to Number and greater than MAX_SAFE_INTEGER', function() { - const num = new BN(Number.MAX_SAFE_INTEGER).addn(1) - assert.throws( - () => { + st.strictEqual(result, bufferToHex(num.toArrayLike(Buffer))) + st.end() + }) + st.test( + 'should throw an error if converting to Number and greater than MAX_SAFE_INTEGER', + function(st) { + const num = new BN(Number.MAX_SAFE_INTEGER).addn(1) + st.throws(() => { toType(num, TypeOutput.Number) - }, - { - message: - 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)' - } - ) - }) + }, /^Error: The provided number is greater than MAX_SAFE_INTEGER \(please use an alternative output type\)$/) + st.end() + } + ) }) - describe('from Buffer', function() { + t.test('from Buffer', function(st) { const num = intToBuffer(1000) - it('should convert to Number', function() { + st.test('should convert to Number', function(st) { const result = toType(num, TypeOutput.Number) - assert.ok(intToBuffer(result).equals(num)) + st.ok(intToBuffer(result).equals(num)) + st.end() }) - it('should convert to BN', function() { + st.test('should convert to BN', function(st) { const result = toType(num, TypeOutput.BN) - assert.ok(result.eq(new BN(num))) + st.ok(result.eq(new BN(num))) + st.end() }) - it('should convert to Buffer', function() { + st.test('should convert to Buffer', function(st) { const result = toType(num, TypeOutput.Buffer) - assert.ok(result.equals(num)) + st.ok(result.equals(num)) + st.end() }) - it('should convert to PrefixedHexString', function() { + st.test('should convert to PrefixedHexString', function(st) { const result = toType(num, TypeOutput.PrefixedHexString) - assert.strictEqual(result, bufferToHex(num)) + st.strictEqual(result, bufferToHex(num)) + st.end() }) }) - describe('from HexPrefixedString', function() { + t.test('from HexPrefixedString', function(st) { const num = intToHex(1000) - it('should convert to Number', function() { + st.test('should convert to Number', function(st) { const result = toType(num, TypeOutput.Number) - assert.strictEqual(intToHex(result), num) + st.strictEqual(intToHex(result), num) + st.end() }) - it('should convert to BN', function() { + st.test('should convert to BN', function(st) { const result = toType(num, TypeOutput.BN) - assert.strictEqual(bnToHex(result), num) + st.strictEqual(bnToHex(result), num) + st.end() }) - it('should convert to Buffer', function() { + st.test('should convert to Buffer', function(st) { const result = toType(num, TypeOutput.Buffer) - assert.ok(result.equals(toBuffer(num))) - }) - it('should throw an error if is not 0x-prefixed', function() { - assert.throws( - () => { - toType('1', TypeOutput.Number) - }, - { - message: 'A string must be provided with a 0x-prefix, given: 1' - } - ) + st.ok(result.equals(toBuffer(num))) + st.end() + }) + st.test('should throw an error if is not 0x-prefixed', function(st) { + st.throws(() => { + toType('1', TypeOutput.Number) + }, /^Error: A string must be provided with a 0x-prefix, given: 1$/) + st.end() }) }) })