2025-11-16 13:47:24 -08:00
|
|
|
import { createRequire } from 'module'
|
2025-11-16 13:37:04 -08:00
|
|
|
import assert from 'assert'
|
|
|
|
|
import BCHJS from '../../src/bch-js.js'
|
|
|
|
|
import { Buffer } from 'safe-buffer'
|
2025-11-16 13:47:24 -08:00
|
|
|
const require = createRequire(import.meta.url)
|
|
|
|
|
const fixtures = require('./fixtures/crypto.json')
|
|
|
|
|
const bchjs = new BCHJS()
|
2020-07-27 21:32:51 -07:00
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#Crypto', () => {
|
|
|
|
|
describe('#sha256', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
fixtures.sha256.forEach(fixture => {
|
|
|
|
|
it(`should create SHA256Hash hex encoded ${fixture.hash} from ${fixture.hex}`, () => {
|
2021-01-02 15:41:12 -08:00
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const sha256Hash = bchjs.Crypto.sha256(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(sha256Hash, fixture.hash)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should create 64 character SHA256Hash hex encoded', () => {
|
|
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const sha256Hash = bchjs.Crypto.sha256(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(sha256Hash.length, 64)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#ripemd160', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
fixtures.ripemd160.forEach(fixture => {
|
|
|
|
|
it(`should create RIPEMD160Hash hex encoded ${fixture.hash} from ${fixture.hex}`, () => {
|
2021-01-02 15:41:12 -08:00
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const ripemd160 = bchjs.Crypto.ripemd160(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(ripemd160, fixture.hash)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should create 64 character RIPEMD160Hash hex encoded', () => {
|
|
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const ripemd160 = bchjs.Crypto.ripemd160(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(ripemd160.length, 40)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#hash256', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
fixtures.hash256.forEach(fixture => {
|
|
|
|
|
it(`should create double SHA256 Hash hex encoded ${fixture.hash} from ${fixture.hex}`, () => {
|
2021-01-02 15:41:12 -08:00
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const hash256 = bchjs.Crypto.hash256(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(hash256, fixture.hash)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should create 64 character SHA256 Hash hex encoded', () => {
|
|
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const hash256 = bchjs.Crypto.hash256(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(hash256.length, 64)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#hash160', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
fixtures.hash160.forEach(fixture => {
|
|
|
|
|
it(`should create RIPEMD160(SHA256()) hex encoded ${fixture.hash} from ${fixture.hex}`, () => {
|
2021-01-02 15:41:12 -08:00
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const hash160 = bchjs.Crypto.hash160(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(hash160, fixture.hash)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should create 64 character SHA256Hash hex encoded', () => {
|
|
|
|
|
const data = Buffer.from(fixture.hex, 'hex')
|
|
|
|
|
const hash160 = bchjs.Crypto.hash160(data).toString('hex')
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(hash160.length, 40)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#randomBytes', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
for (let i = 0; i < 6; i++) {
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should return 16 bytes of entropy hex encoded', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const entropy = bchjs.Crypto.randomBytes(16)
|
|
|
|
|
assert.equal(Buffer.byteLength(entropy), 16)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should return 20 bytes of entropy hex encoded', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const entropy = bchjs.Crypto.randomBytes(20)
|
|
|
|
|
assert.equal(Buffer.byteLength(entropy), 20)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should return 24 bytes of entropy hex encoded', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const entropy = bchjs.Crypto.randomBytes(24)
|
|
|
|
|
assert.equal(Buffer.byteLength(entropy), 24)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should return 28 bytes of entropy hex encoded', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const entropy = bchjs.Crypto.randomBytes(28)
|
|
|
|
|
assert.equal(Buffer.byteLength(entropy), 28)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should return 32 bytes of entropy hex encoded', () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const entropy = bchjs.Crypto.randomBytes(32)
|
|
|
|
|
assert.equal(Buffer.byteLength(entropy), 32)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|