2020-12-06 14:51:48 -08:00
|
|
|
// Public npm libraries.
|
2025-11-16 13:37:04 -08:00
|
|
|
import assert from 'assert'
|
|
|
|
|
import { Buffer } from 'safe-buffer'
|
2020-12-06 14:51:48 -08:00
|
|
|
|
|
|
|
|
// Mocks
|
2025-11-16 13:47:24 -08:00
|
|
|
import { createRequire } from 'module'
|
2020-12-06 14:51:48 -08:00
|
|
|
|
|
|
|
|
// Unit under test (uut)
|
2025-11-16 13:37:04 -08:00
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
// const bchjs = new BCHJS()
|
|
|
|
|
let bchjs
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#ECPair', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
beforeEach(() => {
|
|
|
|
|
bchjs = new BCHJS()
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#fromWIF', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
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', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
fixtures.fromPublicKey.forEach(fixture => {
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should create ECPair from public key buffer', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
const ecpair = bchjs.ECPair.fromPublicKey(
|
2021-01-02 15:41:12 -08:00
|
|
|
Buffer.from(fixture.pubkeyHex, 'hex')
|
2020-12-06 14:51:48 -08:00
|
|
|
)
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.equal(typeof ecpair, 'object')
|
2020-12-06 14:51:48 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
)
|
|
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
)
|
|
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
)
|
|
|
|
|
assert.equal(
|
|
|
|
|
bchjs.HDNode.toCashAddress(ecpair, true),
|
|
|
|
|
fixture.regtestAddr
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#toPublicKey', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
fixtures.toPublicKey.forEach(fixture => {
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should create a public key buffer from an ECPair', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
const ecpair = bchjs.ECPair.fromPublicKey(
|
2021-01-02 15:41:12 -08:00
|
|
|
Buffer.from(fixture.pubkeyHex, 'hex')
|
2020-12-06 14:51:48 -08:00
|
|
|
)
|
|
|
|
|
const pubkeyBuffer = bchjs.ECPair.toPublicKey(ecpair)
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.equal(typeof pubkeyBuffer, 'object')
|
2020-12-06 14:51:48 -08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#toLegacyAddress', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
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', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
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', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
fixtures.sign.forEach(fixture => {
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should sign 32 byte hash buffer', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
const signatureBuf = bchjs.ECPair.sign(ecpair, buf)
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.equal(typeof signatureBuf, 'object')
|
2020-12-06 14:51:48 -08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#verify', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
fixtures.verify.forEach(fixture => {
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should verify signed 32 byte hash buffer', () => {
|
2020-12-06 14:51:48 -08:00
|
|
|
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')
|
2020-12-06 14:51:48 -08:00
|
|
|
const signature = bchjs.ECPair.sign(ecpair1, buf)
|
|
|
|
|
const verify = bchjs.ECPair.verify(ecpair1, buf, signature)
|
|
|
|
|
assert.equal(verify, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|