feat: Jest Test Runner (#2170)

This commit is contained in:
justinr1234
2023-02-03 17:03:07 -06:00
committed by GitHub
parent 5a63f18faf
commit 5fe480ece4
229 changed files with 13497 additions and 17033 deletions

View File

@@ -5,7 +5,7 @@ module.exports = {
parserOptions: {
// Enable linting rules with type information from our tsconfig
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
project: ['./tsconfig.json', './tsconfig.eslint.json'],
sourceType: 'module', // Allow the use of imports / ES modules
@@ -19,11 +19,11 @@ module.exports = {
browser: true, // Enable browser global variables
node: true, // Enable node global variables & Node.js scoping
es2020: true, // Add all ECMAScript 2020 globals and automatically set the ecmaVersion parser option to ES2020
mocha: true, // Add Mocha testing global variables
jest: true, // Add Jest testing global variables
},
plugins: [],
extends: ['@xrplf/eslint-config/base', 'plugin:mocha/recommended'],
extends: ['@xrplf/eslint-config/base'],
rules: {
// ** TODO **
@@ -56,5 +56,6 @@ module.exports = {
'eslint-comments/require-description': 'off',
'no-shadow': 'off',
'multiline-comment-style': 'off',
'@typescript-eslint/no-require-imports': 'off',
},
}

View File

@@ -1,6 +1,8 @@
# ripple-keypairs Release History
## Unreleased
### Changed
- All tests now use the Jest test runner and have been refactored for consistency across all packages
## 1.1.4 (2022-05-02)
- `hexToBytes` now produces empty output for empty input, rather than `[0]`.

View File

@@ -0,0 +1,8 @@
// Jest configuration for api
const base = require('../../jest.config.base.js')
module.exports = {
...base,
roots: [...base.roots, '<rootDir>/test'],
displayName: 'ripple-keypairs',
}

View File

@@ -4,7 +4,7 @@
"description": "Cryptographic key pairs for the XRP Ledger",
"scripts": {
"build": "tsc -b",
"test": "tsc -b && nyc mocha",
"test": "jest --verbose false --silent=false ./test/*.test.ts",
"clean": "rm -rf ./dist && rm -rf tsconfig.tsbuildinfo",
"lint": "eslint . --ext .ts",
"prepublish": "npm run lint && npm test"

View File

@@ -1,7 +1,7 @@
/* eslint-disable no-bitwise --
* lots of bitwise operators necessary for this */
import * as hashjs from 'hash.js'
import * as BigNum from 'bn.js'
import BigNum = require('bn.js')
export default class Sha512 {
// TODO: type of `hash`?

View File

@@ -1,5 +1,5 @@
import * as assert from 'assert'
import * as brorand from 'brorand'
import brorand = require('brorand')
import * as hashjs from 'hash.js'
import * as elliptic from 'elliptic'
@@ -157,7 +157,7 @@ function deriveNodeAddress(publicKey): string {
const { decodeSeed } = addressCodec
export = {
export {
generateSeed,
deriveKeypair,
sign,

View File

@@ -1,6 +1,6 @@
import * as assert from 'assert'
import * as hashjs from 'hash.js'
import * as BN from 'bn.js'
import BN = require('bn.js')
function bytesToHex(a: Iterable<number> | ArrayLike<number>): string {
return Array.from(a, (byteValue) => {

View File

@@ -1,33 +1,36 @@
'use strict' // eslint-disable-line strict
import assert from 'assert'
import fixtures from './fixtures/api.json'
import * as api from '../src'
const assert = require('assert')
const fixtures = require('./fixtures/api.json')
const api = require('../dist')
const decodeSeed = api.decodeSeed
const entropy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
const entropy = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
])
describe('api', () => {
it('generateSeed - secp256k1', () => {
assert.strictEqual(api.generateSeed({entropy}), fixtures.secp256k1.seed)
assert.strictEqual(api.generateSeed({ entropy }), fixtures.secp256k1.seed)
})
it('generateSeed - secp256k1, random', () => {
const seed = api.generateSeed()
assert(seed.charAt(0) === 's')
const {type, bytes} = decodeSeed(seed)
assert(seed.startsWith('s'))
const { type, bytes } = decodeSeed(seed)
assert(type === 'secp256k1')
assert(bytes.length === 16)
})
it('generateSeed - ed25519', () => {
assert.strictEqual(api.generateSeed({entropy, algorithm: 'ed25519'}),
fixtures.ed25519.seed)
assert.strictEqual(
api.generateSeed({ entropy, algorithm: 'ed25519' }),
fixtures.ed25519.seed,
)
})
it('generateSeed - ed25519, random', () => {
const seed = api.generateSeed({algorithm: 'ed25519'})
assert(seed.slice(0, 3) === 'sEd')
const {type, bytes} = decodeSeed(seed)
const seed = api.generateSeed({ algorithm: 'ed25519' })
assert(seed.startsWith('sEd'))
const { type, bytes } = decodeSeed(seed)
assert(type === 'ed25519')
assert(bytes.length === 16)
})
@@ -43,12 +46,16 @@ describe('api', () => {
})
it('deriveKeypair - secp256k1 - validator', () => {
const keypair = api.deriveKeypair(fixtures.secp256k1.seed, {validator: true})
const keypair = api.deriveKeypair(fixtures.secp256k1.seed, {
validator: true,
})
assert.deepEqual(keypair, fixtures.secp256k1.validatorKeypair)
})
it('deriveKeypair - ed25519 - validator', () => {
const keypair = api.deriveKeypair(fixtures.ed25519.seed, {validator: true})
const keypair = api.deriveKeypair(fixtures.ed25519.seed, {
validator: true,
})
assert.deepEqual(keypair, fixtures.ed25519.validatorKeypair)
})
@@ -65,7 +72,7 @@ describe('api', () => {
it('sign - secp256k1', () => {
const privateKey = fixtures.secp256k1.keypair.privateKey
const message = fixtures.secp256k1.message
const messageHex = (Buffer.from(message, 'utf8')).toString('hex')
const messageHex = Buffer.from(message, 'utf8').toString('hex')
const signature = api.sign(messageHex, privateKey)
assert.strictEqual(signature, fixtures.secp256k1.signature)
})
@@ -74,14 +81,14 @@ describe('api', () => {
const signature = fixtures.secp256k1.signature
const publicKey = fixtures.secp256k1.keypair.publicKey
const message = fixtures.secp256k1.message
const messageHex = (Buffer.from(message, 'utf8')).toString('hex')
const messageHex = Buffer.from(message, 'utf8').toString('hex')
assert(api.verify(messageHex, signature, publicKey))
})
it('sign - ed25519', () => {
const privateKey = fixtures.ed25519.keypair.privateKey
const message = fixtures.ed25519.message
const messageHex = (Buffer.from(message, 'utf8')).toString('hex')
const messageHex = Buffer.from(message, 'utf8').toString('hex')
const signature = api.sign(messageHex, privateKey)
assert.strictEqual(signature, fixtures.ed25519.signature)
})
@@ -90,20 +97,20 @@ describe('api', () => {
const signature = fixtures.ed25519.signature
const publicKey = fixtures.ed25519.keypair.publicKey
const message = fixtures.ed25519.message
const messageHex = (Buffer.from(message, 'utf8')).toString('hex')
const messageHex = Buffer.from(message, 'utf8').toString('hex')
assert(api.verify(messageHex, signature, publicKey))
})
it('deriveNodeAddress', () => {
const x = 'n9KHn8NfbBsZV5q8bLfS72XyGqwFt5mgoPbcTV4c6qKiuPTAtXYk'
const y = 'rU7bM9ENDkybaxNrefAVjdLTyNLuue1KaJ'
assert.strictEqual(api.deriveNodeAddress(x), y)
const addrX = 'n9KHn8NfbBsZV5q8bLfS72XyGqwFt5mgoPbcTV4c6qKiuPTAtXYk'
const addrY = 'rU7bM9ENDkybaxNrefAVjdLTyNLuue1KaJ'
assert.strictEqual(api.deriveNodeAddress(addrX), addrY)
})
it('Random Address', () => {
const seed = api.generateSeed()
const keypair = api.deriveKeypair(seed)
const address = api.deriveAddress(keypair.publicKey)
assert(address[0] === 'r')
assert(address.startsWith('r'))
})
})

View File

@@ -1,9 +1,5 @@
/* eslint-disable no-unused-expressions/no-unused-expressions */
'use strict'
const assert = require('assert')
const api = require('ripple-address-codec')
import assert from 'assert'
import * as api from 'ripple-address-codec'
function toHex(bytes) {
return Buffer.from(bytes).toString('hex').toUpperCase()
@@ -13,27 +9,31 @@ function toBytes(hex) {
return Buffer.from(hex, 'hex').toJSON().data
}
describe('ripple-address-codec', function() {
describe('ripple-address-codec', function () {
function makeTest(type, base58, hex) {
it('can translate between ' + hex + ' and ' + base58 + ' (encode ' + type + ')', function() {
const actual = api['encode' + type](toBytes(hex))
it(`can translate between ${hex} and ${base58} (encode ${type})`, () => {
const actual = api[`encode${type}`](toBytes(hex))
assert.equal(actual, base58)
})
it('can translate between ' + base58 + ' and ' + hex + ' (decode ' + type + ')', function() {
const buf = api['decode' + type](base58)
it(`can translate between ${base58} and ${hex} (decode ${type})`, () => {
const buf = api[`decode${type}`](base58)
assert.equal(toHex(buf), hex)
})
}
makeTest('AccountID', 'rJrRMgiRgrU6hDF4pgu5DXQdWyPbY35ErN',
'BA8E78626EE42C41B46D46C3048DF3A1C3C87072')
makeTest(
'AccountID',
'rJrRMgiRgrU6hDF4pgu5DXQdWyPbY35ErN',
'BA8E78626EE42C41B46D46C3048DF3A1C3C87072',
)
makeTest(
'NodePublic',
'n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH',
'0388E5BA87A000CB807240DF8C848EB0B5FFA5C8E5A521BC8E105C0F0A44217828')
'0388E5BA87A000CB807240DF8C848EB0B5FFA5C8E5A521BC8E105C0F0A44217828',
)
it('can decode arbitrary seeds', function() {
it('can decode arbitrary seeds', () => {
const decoded = api.decodeSeed('sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
assert.equal(toHex(decoded.bytes), '4C3A1D213FBDFB14C7C28D609469B341')
assert.equal(decoded.type, 'ed25519')
@@ -43,11 +43,16 @@ describe('ripple-address-codec', function() {
assert.equal(decoded2.type, 'secp256k1')
})
it('can pass a type as second arg to encodeSeed', function() {
it('can pass a type as second arg to encodeSeed', () => {
const edSeed = 'sEdTM1uX8pu2do5XvTnutH6HsouMaM2'
const decoded = api.decodeSeed(edSeed)
assert.equal(toHex(decoded.bytes), '4C3A1D213FBDFB14C7C28D609469B341')
assert.equal(decoded.type, 'ed25519')
if (decoded.type === null) {
assert.fail('decoded.type should not be null')
}
assert.equal(api.encodeSeed(decoded.bytes, decoded.type), edSeed)
})
})
export {}

View File

@@ -1,9 +1,7 @@
'use strict' // eslint-disable-line strict
import assert from 'assert'
import * as utils from '../src/utils'
const assert = require('assert')
const utils = require('../dist/utils')
describe('utils', () => {
describe('utils', function () {
it('hexToBytes - empty', () => {
assert.deepEqual(utils.hexToBytes(''), [])
})
@@ -18,9 +16,14 @@ describe('utils', () => {
it('bytesToHex - DEADBEEF', () => {
assert.deepEqual(utils.bytesToHex([222, 173, 190, 239]), 'DEADBEEF')
});
})
it('bytesToHex - DEADBEEF (Uint8Array)', () => {
assert.deepEqual(utils.bytesToHex(new Uint8Array([222, 173, 190, 239])), 'DEADBEEF')
});
assert.deepEqual(
utils.bytesToHex(new Uint8Array([222, 173, 190, 239])),
'DEADBEEF',
)
})
})
export {}

View File

@@ -1,81 +0,0 @@
/* eslint-disable no-unused-expressions/no-unused-expressions */
'use strict'
const assert = require('assert')
const api = require('ripple-address-codec')
function toHex(bytes) {
return Buffer.from(bytes).toString('hex').toUpperCase()
}
function toBytes(hex) {
return Buffer.from(hex, 'hex').toJSON().data
}
describe('ripple-address-codec', function() {
describe('encodeSeed', function() {
it('encodes a secp256k1 seed', function() {
const result = api.encodeSeed(toBytes('CF2DE378FBDD7E2EE87D486DFB5A7BFF'), 'secp256k1')
assert.equal(result, 'sn259rEFXrQrWyx3Q7XneWcwV6dfL')
})
it('encodes low secp256k1 seed', function() {
const result = api.encodeSeed(toBytes('00000000000000000000000000000000'), 'secp256k1')
assert.equal(result, 'sp6JS7f14BuwFY8Mw6bTtLKWauoUs')
})
it('encodes high secp256k1 seed', function() {
const result = api.encodeSeed(toBytes('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'), 'secp256k1')
assert.equal(result, 'saGwBRReqUNKuWNLpUAq8i8NkXEPN')
})
it('encodes an ed25519 seed', function() {
const result = api.encodeSeed(toBytes('4C3A1D213FBDFB14C7C28D609469B341'), 'ed25519')
assert.equal(result, 'sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
})
it('encodes low ed25519 seed', function() {
const result = api.encodeSeed(toBytes('00000000000000000000000000000000'), 'ed25519')
assert.equal(result, 'sEdSJHS4oiAdz7w2X2ni1gFiqtbJHqE')
})
it('encodes high ed25519 seed', function() {
const result = api.encodeSeed(toBytes('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'), 'ed25519')
assert.equal(result, 'sEdV19BLfeQeKdEXyYA4NhjPJe6XBfG')
})
})
describe('decodeSeed', function() {
it('can decode an Ed25519 seed', function() {
const decoded = api.decodeSeed('sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
assert.equal(toHex(decoded.bytes), '4C3A1D213FBDFB14C7C28D609469B341')
assert.equal(decoded.type, 'ed25519')
})
it('can decode a secp256k1 seed', function() {
const decoded = api.decodeSeed('sn259rEFXrQrWyx3Q7XneWcwV6dfL')
assert.equal(toHex(decoded.bytes), 'CF2DE378FBDD7E2EE87D486DFB5A7BFF')
assert.equal(decoded.type, 'secp256k1')
})
})
describe('encodeAccountID', function() {
it('can encode an AccountID', function() {
const encoded = api.encodeAccountID(toBytes('BA8E78626EE42C41B46D46C3048DF3A1C3C87072'))
assert.equal(encoded, 'rJrRMgiRgrU6hDF4pgu5DXQdWyPbY35ErN')
})
})
describe('decodeNodePublic', function() {
it('can decode a NodePublic', function() {
const decoded = api.decodeNodePublic('n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH')
assert.equal(toHex(decoded), '0388E5BA87A000CB807240DF8C848EB0B5FFA5C8E5A521BC8E105C0F0A44217828')
})
})
})

View File

@@ -0,0 +1,99 @@
import assert from 'assert'
import * as api from 'ripple-address-codec'
function toHex(bytes: Buffer) {
return Buffer.from(bytes).toString('hex').toUpperCase()
}
function toBytes(hex: string) {
return Buffer.from(hex, 'hex').toJSON().data
}
describe('ripple-address-codec', function () {
describe('encodeSeed', function () {
it('encodes a secp256k1 seed', () => {
const result = api.encodeSeed(
Buffer.from(toBytes('CF2DE378FBDD7E2EE87D486DFB5A7BFF')),
'secp256k1',
)
assert.equal(result, 'sn259rEFXrQrWyx3Q7XneWcwV6dfL')
})
it('encodes low secp256k1 seed', () => {
const result = api.encodeSeed(
Buffer.from(toBytes('00000000000000000000000000000000')),
'secp256k1',
)
assert.equal(result, 'sp6JS7f14BuwFY8Mw6bTtLKWauoUs')
})
it('encodes high secp256k1 seed', () => {
const result = api.encodeSeed(
Buffer.from(toBytes('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')),
'secp256k1',
)
assert.equal(result, 'saGwBRReqUNKuWNLpUAq8i8NkXEPN')
})
it('encodes an ed25519 seed', () => {
const result = api.encodeSeed(
Buffer.from(toBytes('4C3A1D213FBDFB14C7C28D609469B341')),
'ed25519',
)
assert.equal(result, 'sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
})
it('encodes low ed25519 seed', () => {
const result = api.encodeSeed(
Buffer.from(toBytes('00000000000000000000000000000000')),
'ed25519',
)
assert.equal(result, 'sEdSJHS4oiAdz7w2X2ni1gFiqtbJHqE')
})
it('encodes high ed25519 seed', () => {
const result = api.encodeSeed(
Buffer.from(toBytes('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')),
'ed25519',
)
assert.equal(result, 'sEdV19BLfeQeKdEXyYA4NhjPJe6XBfG')
})
})
describe('decodeSeed', function () {
it('can decode an Ed25519 seed', () => {
const decoded = api.decodeSeed('sEdTM1uX8pu2do5XvTnutH6HsouMaM2')
assert.equal(toHex(decoded.bytes), '4C3A1D213FBDFB14C7C28D609469B341')
assert.equal(decoded.type, 'ed25519')
})
it('can decode a secp256k1 seed', () => {
const decoded = api.decodeSeed('sn259rEFXrQrWyx3Q7XneWcwV6dfL')
assert.equal(toHex(decoded.bytes), 'CF2DE378FBDD7E2EE87D486DFB5A7BFF')
assert.equal(decoded.type, 'secp256k1')
})
})
describe('encodeAccountID', function () {
it('can encode an AccountID', () => {
const encoded = api.encodeAccountID(
Buffer.from(toBytes('BA8E78626EE42C41B46D46C3048DF3A1C3C87072')),
)
assert.equal(encoded, 'rJrRMgiRgrU6hDF4pgu5DXQdWyPbY35ErN')
})
})
describe('decodeNodePublic', function () {
it('can decode a NodePublic', () => {
const decoded = api.decodeNodePublic(
'n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH',
)
assert.equal(
toHex(decoded),
'0388E5BA87A000CB807240DF8C848EB0B5FFA5C8E5A521BC8E105C0F0A44217828',
)
})
})
})
export {}

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts", "test/**/*.ts"]
}

View File

@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "ES2017",
"target": "es6",
"declaration": true,
"declarationMap": true,
"outDir": "./dist",
@@ -11,7 +11,9 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true,
"resolveJsonModule": true
},
"references": [{
"path": "../ripple-address-codec/tsconfig.json"