Files
bch-js/test/unit/ecpairs.js
T

156 lines
5.2 KiB
JavaScript
Raw Normal View History

// Public npm libraries.
import assert from 'assert'
import { Buffer } from 'safe-buffer'
// Mocks
2025-11-16 13:47:24 -08:00
import { createRequire } from 'module'
// Unit under test (uut)
import BCHJS from '../../src/bch-js.js'
2025-11-16 13:47:24 -08:00
const require = createRequire(import.meta.url)
const fixtures = require('./fixtures/ecpair.json')
// const bchjs = new BCHJS()
let bchjs
2021-01-02 15:41:12 -08:00
describe('#ECPair', () => {
beforeEach(() => {
bchjs = new BCHJS()
})
2021-01-02 15:41:12 -08:00
describe('#fromWIF', () => {
fixtures.fromWIF.forEach(fixture => {
it(`should create ECPair from WIF ${fixture.privateKeyWIF}`, () => {
const ecpair = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
2021-01-02 15:41:12 -08:00
assert.equal(typeof ecpair, 'object')
})
it(`should get ${fixture.legacy} legacy address`, () => {
const legacy = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
assert.equal(bchjs.HDNode.toLegacyAddress(legacy), fixture.legacy)
})
it(`should get ${fixture.cashAddr} cash address`, () => {
const cashAddr = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
assert.equal(bchjs.HDNode.toCashAddress(cashAddr), fixture.cashAddr)
})
it(`should get ${fixture.regtestAddr} cash address`, () => {
const cashAddr = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
assert.equal(
bchjs.HDNode.toCashAddress(cashAddr, true),
fixture.regtestAddr
)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#toWIF', () => {
fixtures.toWIF.forEach(fixture => {
it(`should get WIF ${fixture.privateKeyWIF} from ECPair`, () => {
const ecpair = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
const wif = bchjs.ECPair.toWIF(ecpair)
assert.equal(wif, fixture.privateKeyWIF)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#fromPublicKey', () => {
fixtures.fromPublicKey.forEach(fixture => {
2021-01-02 15:41:12 -08:00
it('should create ECPair from public key buffer', () => {
const ecpair = bchjs.ECPair.fromPublicKey(
2021-01-02 15:41:12 -08:00
Buffer.from(fixture.pubkeyHex, 'hex')
)
2021-01-02 15:41:12 -08:00
assert.equal(typeof ecpair, 'object')
})
it(`should get ${fixture.legacy} legacy address`, () => {
const ecpair = bchjs.ECPair.fromPublicKey(
2021-01-02 15:41:12 -08:00
Buffer.from(fixture.pubkeyHex, 'hex')
)
assert.equal(bchjs.HDNode.toLegacyAddress(ecpair), fixture.legacy)
})
it(`should get ${fixture.cashAddr} cash address`, () => {
const ecpair = bchjs.ECPair.fromPublicKey(
2021-01-02 15:41:12 -08:00
Buffer.from(fixture.pubkeyHex, 'hex')
)
assert.equal(bchjs.HDNode.toCashAddress(ecpair), fixture.cashAddr)
})
it(`should get ${fixture.regtestAddr} cash address`, () => {
const ecpair = bchjs.ECPair.fromPublicKey(
2021-01-02 15:41:12 -08:00
Buffer.from(fixture.pubkeyHex, 'hex')
)
assert.equal(
bchjs.HDNode.toCashAddress(ecpair, true),
fixture.regtestAddr
)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#toPublicKey', () => {
fixtures.toPublicKey.forEach(fixture => {
2021-01-02 15:41:12 -08:00
it('should create a public key buffer from an ECPair', () => {
const ecpair = bchjs.ECPair.fromPublicKey(
2021-01-02 15:41:12 -08:00
Buffer.from(fixture.pubkeyHex, 'hex')
)
const pubkeyBuffer = bchjs.ECPair.toPublicKey(ecpair)
2021-01-02 15:41:12 -08:00
assert.equal(typeof pubkeyBuffer, 'object')
})
})
})
2021-01-02 15:41:12 -08:00
describe('#toLegacyAddress', () => {
fixtures.toLegacyAddress.forEach(fixture => {
it(`should create legacy address ${fixture.legacy} from an ECPair`, () => {
const ecpair = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
const legacyAddress = bchjs.ECPair.toLegacyAddress(ecpair)
assert.equal(legacyAddress, fixture.legacy)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#toCashAddress', () => {
fixtures.toCashAddress.forEach(fixture => {
it(`should create cash address ${fixture.cashAddr} from an ECPair`, () => {
const ecpair = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
const cashAddr = bchjs.ECPair.toCashAddress(ecpair)
assert.equal(cashAddr, fixture.cashAddr)
})
})
fixtures.toCashAddress.forEach(fixture => {
it(`should create regtest cash address ${fixture.regtestAddr} from an ECPair`, () => {
const ecpair = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
const regtestAddr = bchjs.ECPair.toCashAddress(ecpair, true)
assert.equal(regtestAddr, fixture.regtestAddr)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#sign', () => {
fixtures.sign.forEach(fixture => {
2021-01-02 15:41:12 -08:00
it('should sign 32 byte hash buffer', () => {
const ecpair = bchjs.ECPair.fromWIF(fixture.privateKeyWIF)
2021-01-02 15:41:12 -08:00
const buf = Buffer.from(bchjs.Crypto.sha256(fixture.data), 'hex')
const signatureBuf = bchjs.ECPair.sign(ecpair, buf)
2021-01-02 15:41:12 -08:00
assert.equal(typeof signatureBuf, 'object')
})
})
})
2021-01-02 15:41:12 -08:00
describe('#verify', () => {
fixtures.verify.forEach(fixture => {
2021-01-02 15:41:12 -08:00
it('should verify signed 32 byte hash buffer', () => {
const ecpair1 = bchjs.ECPair.fromWIF(fixture.privateKeyWIF1)
2021-01-02 15:41:12 -08:00
// const ecpair2 = bchjs.ECPair.fromWIF(fixture.privateKeyWIF2)
const buf = Buffer.from(bchjs.Crypto.sha256(fixture.data), 'hex')
const signature = bchjs.ECPair.sign(ecpair1, buf)
const verify = bchjs.ECPair.verify(ecpair1, buf, signature)
assert.equal(verify, true)
})
})
})
})