Files

982 lines
32 KiB
JavaScript
Raw Permalink Normal View History

// Public npm libraries.
import assert from 'assert'
// import { assert } from 'chai'
import Bitcoin from '@psf/bitcoincashjs-lib'
2020-07-27 21:32:51 -07:00
// 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/address.json')
let bchjs
2021-01-02 15:41:12 -08:00
function flatten (arrays) {
2020-07-27 21:32:51 -07:00
return [].concat.apply([], arrays)
}
const XPUBS = flatten([fixtures.mainnetXPub, fixtures.testnetXPub])
const LEGACY_ADDRESSES = flatten([
fixtures.legacyMainnetP2PKH,
fixtures.legacyMainnetP2SH,
fixtures.legacyTestnetP2PKH
])
2021-01-02 16:39:44 -08:00
const mainnetXpubs = []
2020-07-27 21:32:51 -07:00
fixtures.mainnetXPub.forEach((f, i) => {
2021-01-02 16:39:44 -08:00
mainnetXpubs.push(f.xpub)
2020-07-27 21:32:51 -07:00
})
const MAINNET_ADDRESSES = flatten([
2021-01-02 16:39:44 -08:00
mainnetXpubs,
2020-07-27 21:32:51 -07:00
fixtures.legacyMainnetP2PKH,
fixtures.legacyMainnetP2SH,
fixtures.cashaddrMainnetP2PKH
])
2021-01-02 16:39:44 -08:00
const testnetXpubs = []
2020-07-27 21:32:51 -07:00
fixtures.testnetXPub.forEach((f, i) => {
2021-01-02 16:39:44 -08:00
testnetXpubs.push(f.xpub)
2020-07-27 21:32:51 -07:00
})
const TESTNET_ADDRESSES = flatten([
2021-01-02 16:39:44 -08:00
testnetXpubs,
2020-07-27 21:32:51 -07:00
fixtures.legacyTestnetP2PKH,
fixtures.cashaddrTestnetP2PKH
])
const CASHADDR_ADDRESSES = flatten([
fixtures.cashaddrMainnetP2PKH,
fixtures.cashaddrMainnetP2SH,
fixtures.cashaddrTestnetP2PKH
])
const CASHADDR_ADDRESSES_NO_PREFIX = CASHADDR_ADDRESSES.map(address => {
2021-01-02 15:41:12 -08:00
const parts = address.split(':')
2020-07-27 21:32:51 -07:00
return parts[1]
})
const REGTEST_ADDRESSES = fixtures.cashaddrRegTestP2PKH
const REGTEST_ADDRESSES_NO_PREFIX = REGTEST_ADDRESSES.map(address => {
2021-01-02 15:41:12 -08:00
const parts = address.split(':')
2020-07-27 21:32:51 -07:00
return parts[1]
})
const HASH160_HASHES = flatten([
fixtures.hash160MainnetP2PKH,
fixtures.hash160MainnetP2SH,
fixtures.hash160TestnetP2PKH
])
const P2PKH_ADDRESSES = flatten([
fixtures.legacyMainnetP2PKH,
fixtures.legacyTestnetP2PKH,
fixtures.cashaddrMainnetP2PKH,
fixtures.cashaddrTestnetP2PKH,
fixtures.cashaddrRegTestP2PKH
])
const P2SH_ADDRESSES = flatten([
fixtures.legacyMainnetP2SH,
fixtures.cashaddrMainnetP2SH
])
2021-01-02 15:41:12 -08:00
describe('#address.js', () => {
beforeEach(() => {
bchjs = new BCHJS()
})
2021-01-02 15:41:12 -08:00
describe('#addressConversion', () => {
describe('#toLegacyAddress', () => {
it('should translate legacy address format to itself correctly', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
LEGACY_ADDRESSES.map(address =>
bchjs.Address.toLegacyAddress(address)
),
LEGACY_ADDRESSES
)
})
2021-01-02 15:41:12 -08:00
it('should convert cashaddr address to legacy base58Check', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.toLegacyAddress(address)
),
LEGACY_ADDRESSES
)
})
2021-01-02 15:41:12 -08:00
it('should convert cashaddr regtest address to legacy base58Check', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES.map(address =>
bchjs.Address.toLegacyAddress(address)
),
fixtures.legacyTestnetP2PKH
)
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.toLegacyAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.toLegacyAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
})
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
describe('#toCashAddress', () => {
it('should convert legacy base58Check address to cashaddr', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
LEGACY_ADDRESSES.map(address =>
bchjs.Address.toCashAddress(address, true)
),
CASHADDR_ADDRESSES
)
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should convert legacy base58Check address to regtest cashaddr', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.legacyTestnetP2PKH.map(address =>
bchjs.Address.toCashAddress(address, true, true)
),
REGTEST_ADDRESSES
)
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should translate cashaddr address format to itself correctly', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.toCashAddress(address, true)
),
CASHADDR_ADDRESSES
)
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should translate regtest cashaddr address format to itself correctly', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES.map(address =>
bchjs.Address.toCashAddress(address, true, true)
),
REGTEST_ADDRESSES
)
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should translate no-prefix cashaddr address format to itself correctly', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.toCashAddress(address, true)
),
CASHADDR_ADDRESSES
)
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should translate no-prefix regtest cashaddr address format to itself correctly', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.toCashAddress(address, true, true)
),
REGTEST_ADDRESSES
)
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should translate cashaddr address format to itself of no-prefix correctly', () => {
CASHADDR_ADDRESSES.forEach(address => {
const noPrefix = bchjs.Address.toCashAddress(address, false)
2021-01-02 16:39:44 -08:00
assert.strictEqual(address.split(':')[1], noPrefix)
})
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
it('should translate regtest cashaddr address format to itself of no-prefix correctly', () => {
REGTEST_ADDRESSES.forEach(address => {
const noPrefix = bchjs.Address.toCashAddress(address, false, true)
2021-01-02 16:39:44 -08:00
assert.strictEqual(address.split(':')[1], noPrefix)
})
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.BitcoinCash.Address.toCashAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.BitcoinCash.Address.toCashAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('#toHash160', () => {
it('should convert legacy base58check address to hash160', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
LEGACY_ADDRESSES.map(address => bchjs.Address.toHash160(address)),
HASH160_HASHES
)
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should convert cashaddr address to hash160', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES.map(address => bchjs.Address.toHash160(address)),
HASH160_HASHES
)
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should convert regtest cashaddr address to hash160', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES.map(address => bchjs.Address.toHash160(address)),
fixtures.hash160TestnetP2PKH
)
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.toHash160()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.toHash160('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
})
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
describe('#fromHash160', () => {
it('should convert hash160 to mainnet P2PKH legacy base58check address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160MainnetP2PKH.map(hash160 =>
bchjs.Address.hash160ToLegacy(hash160)
),
fixtures.legacyMainnetP2PKH
)
})
2021-01-02 15:41:12 -08:00
it('should convert hash160 to mainnet P2SH legacy base58check address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160MainnetP2SH.map(hash160 =>
bchjs.Address.hash160ToLegacy(
hash160,
Bitcoin.networks.bitcoin.scriptHash
)
),
fixtures.legacyMainnetP2SH
)
})
2021-01-02 15:41:12 -08:00
it('should convert hash160 to testnet P2PKH legacy base58check address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160TestnetP2PKH.map(hash160 =>
bchjs.Address.hash160ToLegacy(
hash160,
Bitcoin.networks.testnet.pubKeyHash
)
),
fixtures.legacyTestnetP2PKH
)
})
2021-01-02 15:41:12 -08:00
it('should convert hash160 to mainnet P2PKH cash address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160MainnetP2PKH.map(hash160 =>
bchjs.Address.hash160ToCash(hash160)
),
fixtures.cashaddrMainnetP2PKH
)
})
2021-01-02 15:41:12 -08:00
it('should convert hash160 to mainnet P2SH cash address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160MainnetP2SH.map(hash160 =>
bchjs.Address.hash160ToCash(
hash160,
Bitcoin.networks.bitcoin.scriptHash
)
),
fixtures.cashaddrMainnetP2SH
)
})
2021-01-02 15:41:12 -08:00
it('should convert hash160 to testnet P2PKH cash address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160TestnetP2PKH.map(hash160 =>
bchjs.Address.hash160ToCash(
hash160,
Bitcoin.networks.testnet.pubKeyHash
)
),
fixtures.cashaddrTestnetP2PKH
)
})
2021-01-02 15:41:12 -08:00
it('should convert hash160 to regtest P2PKH cash address', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
fixtures.hash160TestnetP2PKH.map(hash160 =>
bchjs.Address.hash160ToCash(
hash160,
Bitcoin.networks.testnet.pubKeyHash,
true
)
),
REGTEST_ADDRESSES
)
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.hash160ToLegacy()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.hash160ToLegacy('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
bchjs.Address.hash160ToCash()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.hash160ToCash('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
2020-07-27 21:32:51 -07:00
})
})
})
2020-12-06 15:41:39 -08:00
2021-01-02 15:41:12 -08:00
describe('address format detection', () => {
describe('#isLegacyAddress', () => {
describe('is legacy', () => {
LEGACY_ADDRESSES.forEach(address => {
it(`should detect ${address} is a legacy base58Check address`, () => {
const isBase58Check = bchjs.Address.isLegacyAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isBase58Check, true)
})
})
})
2021-01-02 15:41:12 -08:00
describe('is not legacy', () => {
CASHADDR_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a legacy address`, () => {
const isBase58Check = bchjs.Address.isLegacyAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isBase58Check, false)
})
})
2020-07-27 21:32:51 -07:00
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a legacy address`, () => {
const isBase58Check = bchjs.Address.isLegacyAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isBase58Check, false)
})
})
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isLegacyAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isLegacyAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
})
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
describe('#isCashAddress', () => {
describe('is cashaddr', () => {
CASHADDR_ADDRESSES.forEach(address => {
it(`should detect ${address} is a cashaddr address`, () => {
const isCashaddr = bchjs.Address.isCashAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isCashaddr, true)
})
})
2020-07-27 21:32:51 -07:00
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is a cashaddr address`, () => {
const isCashaddr = bchjs.Address.isCashAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isCashaddr, true)
})
})
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('is not cashaddr', () => {
LEGACY_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a cashaddr address`, () => {
const isCashaddr = bchjs.Address.isCashAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isCashaddr, false)
})
})
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isCashAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isCashAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
2020-07-27 21:32:51 -07:00
})
})
})
2021-01-02 15:41:12 -08:00
describe('#isHash160', () => {
describe('is hash160', () => {
HASH160_HASHES.forEach(address => {
it(`should detect ${address} is a hash160 hash`, () => {
const isHash160 = bchjs.Address.isHash160(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isHash160, true)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('is not hash160', () => {
LEGACY_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a hash160 hash`, () => {
const isHash160 = bchjs.Address.isHash160(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isHash160, false)
})
})
CASHADDR_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a hash160 hash`, () => {
const isHash160 = bchjs.Address.isHash160(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isHash160, false)
})
})
2020-07-27 21:32:51 -07:00
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a legacy address`, () => {
const isHash160 = bchjs.Address.isHash160(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isHash160, false)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isHash160()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isHash160('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
2020-07-27 21:32:51 -07:00
})
})
})
2021-01-02 15:41:12 -08:00
describe('network detection', () => {
describe('#isMainnetAddress', () => {
describe('is mainnet', () => {
MAINNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is a mainnet address`, () => {
const isMainnet = bchjs.Address.isMainnetAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isMainnet, true)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('is not mainnet', () => {
TESTNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a mainnet address`, () => {
const isMainnet = bchjs.Address.isMainnetAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isMainnet, false)
})
2020-07-27 21:32:51 -07:00
})
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a mainnet address`, () => {
const isMainnet = bchjs.Address.isMainnetAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isMainnet, false)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isMainnetAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isMainnetAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
2020-07-27 21:32:51 -07:00
})
})
})
2021-01-02 15:41:12 -08:00
describe('#isTestnetAddress', () => {
describe('is testnet', () => {
TESTNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is a testnet address`, () => {
const isTestnet = bchjs.Address.isTestnetAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isTestnet, true)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('is not testnet', () => {
MAINNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a testnet address`, () => {
const isTestnet = bchjs.Address.isTestnetAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isTestnet, false)
})
})
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a testnet address`, () => {
const isTestnet = bchjs.Address.isTestnetAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isTestnet, false)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isTestnetAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isTestnetAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
2020-07-27 21:32:51 -07:00
})
})
})
2021-01-02 15:41:12 -08:00
describe('#isRegTestAddress', () => {
describe('is testnet', () => {
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is a regtest address`, () => {
const isRegTest = bchjs.Address.isRegTestAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isRegTest, true)
})
})
})
2021-01-02 15:41:12 -08:00
describe('is not testnet', () => {
MAINNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a regtest address`, () => {
const isRegTest = bchjs.Address.isRegTestAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isRegTest, false)
})
})
TESTNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a regtest address`, () => {
const isRegTest = bchjs.Address.isRegTestAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isRegTest, false)
})
})
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isRegTestAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isRegTestAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
2020-07-27 21:32:51 -07:00
})
})
})
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('address type detection', () => {
describe('#isP2PKHAddress', () => {
describe('is P2PKH', () => {
P2PKH_ADDRESSES.forEach(address => {
it(`should detect ${address} is a P2PKH address`, () => {
const isP2PKH = bchjs.Address.isP2PKHAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isP2PKH, true)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('is not P2PKH', () => {
P2SH_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a P2PKH address`, () => {
const isP2PKH = bchjs.Address.isP2PKHAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isP2PKH, false)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isP2PKHAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isP2PKHAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('#isP2SHAddress', () => {
describe('is P2SH', () => {
P2SH_ADDRESSES.forEach(address => {
it(`should detect ${address} is a P2SH address`, () => {
const isP2SH = bchjs.Address.isP2SHAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isP2SH, true)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('is not P2SH', () => {
P2PKH_ADDRESSES.forEach(address => {
it(`should detect ${address} is not a P2SH address`, () => {
const isP2SH = bchjs.Address.isP2SHAddress(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isP2SH, false)
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
assert.throws(() => {
bchjs.Address.isP2SHAddress()
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.isP2SHAddress('some invalid address')
}, bchjs.BitcoinCash.InvalidAddressError)
2020-07-27 21:32:51 -07:00
})
})
})
})
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('cashaddr prefix detection', () => {
it('should return the same result for detectAddressFormat', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.detectAddressFormat(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.detectAddressFormat(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.detectAddressFormat(address)
),
REGTEST_ADDRESSES.map(address =>
bchjs.Address.detectAddressFormat(address)
)
)
})
2021-01-02 15:41:12 -08:00
it('should return the same result for detectAddressNetwork', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.detectAddressNetwork(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.detectAddressNetwork(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.detectAddressNetwork(address)
),
REGTEST_ADDRESSES.map(address =>
bchjs.Address.detectAddressNetwork(address)
)
)
})
2021-01-02 15:41:12 -08:00
it('should return the same result for detectAddressType', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.detectAddressType(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.detectAddressType(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.detectAddressType(address)
),
REGTEST_ADDRESSES.map(address =>
bchjs.Address.detectAddressType(address)
)
)
})
2021-01-02 15:41:12 -08:00
it('should return the same result for toLegacyAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.toLegacyAddress(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.toLegacyAddress(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.toLegacyAddress(address)
),
REGTEST_ADDRESSES.map(address => bchjs.Address.toLegacyAddress(address))
)
})
2021-01-02 15:41:12 -08:00
it('should return the same result for isLegacyAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isLegacyAddress(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.isLegacyAddress(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isLegacyAddress(address)
),
REGTEST_ADDRESSES.map(address => bchjs.Address.isLegacyAddress(address))
)
})
2021-01-02 15:41:12 -08:00
it('should return the same result for isCashAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isCashAddress(address)
),
CASHADDR_ADDRESSES.map(address => bchjs.Address.isCashAddress(address))
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isCashAddress(address)
),
REGTEST_ADDRESSES.map(address => bchjs.Address.isCashAddress(address))
)
})
2021-01-02 15:41:12 -08:00
it('should return the same result for isMainnetAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isMainnetAddress(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.isMainnetAddress(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isMainnetAddress(address)
),
REGTEST_ADDRESSES.map(address =>
bchjs.Address.isMainnetAddress(address)
)
)
})
2021-01-02 16:39:44 -08:00
2021-01-02 15:41:12 -08:00
it('should return the same result for isTestnetAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isTestnetAddress(address)
),
CASHADDR_ADDRESSES.map(address =>
bchjs.Address.isTestnetAddress(address)
)
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isTestnetAddress(address)
),
REGTEST_ADDRESSES.map(address =>
bchjs.Address.isTestnetAddress(address)
)
)
})
2021-01-02 16:39:44 -08:00
2021-01-02 15:41:12 -08:00
it('should return the same result for isP2PKHAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isP2PKHAddress(address)
),
CASHADDR_ADDRESSES.map(address => bchjs.Address.isP2PKHAddress(address))
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isP2PKHAddress(address)
),
REGTEST_ADDRESSES.map(address => bchjs.Address.isP2PKHAddress(address))
)
})
2021-01-02 16:39:44 -08:00
2021-01-02 15:41:12 -08:00
it('should return the same result for isP2SHAddress', () => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
CASHADDR_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isP2SHAddress(address)
),
CASHADDR_ADDRESSES.map(address => bchjs.Address.isP2SHAddress(address))
)
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(
REGTEST_ADDRESSES_NO_PREFIX.map(address =>
bchjs.Address.isP2SHAddress(address)
),
REGTEST_ADDRESSES.map(address => bchjs.Address.isP2SHAddress(address))
)
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('#detectAddressFormat', () => {
LEGACY_ADDRESSES.forEach(address => {
it(`should detect ${address} is a legacy base58Check address`, () => {
const isBase58Check = bchjs.Address.detectAddressFormat(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isBase58Check, 'legacy')
2020-07-27 21:32:51 -07:00
})
})
CASHADDR_ADDRESSES.forEach(address => {
it(`should detect ${address} is a legacy cashaddr address`, () => {
const isCashaddr = bchjs.Address.detectAddressFormat(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isCashaddr, 'cashaddr')
2020-07-27 21:32:51 -07:00
})
})
2020-07-27 21:32:51 -07:00
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is a legacy cashaddr address`, () => {
const isCashaddr = bchjs.Address.detectAddressFormat(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isCashaddr, 'cashaddr')
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
2020-07-27 21:32:51 -07:00
assert.throws(() => {
bchjs.Address.detectAddressFormat()
2020-07-27 21:32:51 -07:00
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.detectAddressFormat('some invalid address')
2020-07-27 21:32:51 -07:00
}, bchjs.BitcoinCash.InvalidAddressError)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#detectAddressNetwork', () => {
MAINNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is a mainnet address`, () => {
const isMainnet = bchjs.Address.detectAddressNetwork(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isMainnet, 'mainnet')
2020-07-27 21:32:51 -07:00
})
})
TESTNET_ADDRESSES.forEach(address => {
it(`should detect ${address} is a testnet address`, () => {
const isTestnet = bchjs.Address.detectAddressNetwork(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isTestnet, 'testnet')
})
})
REGTEST_ADDRESSES.forEach(address => {
it(`should detect ${address} is a testnet address`, () => {
const isTestnet = bchjs.Address.detectAddressNetwork(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isTestnet, 'regtest')
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
2020-07-27 21:32:51 -07:00
assert.throws(() => {
bchjs.Address.detectAddressNetwork()
2020-07-27 21:32:51 -07:00
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.detectAddressNetwork('some invalid address')
2020-07-27 21:32:51 -07:00
}, bchjs.BitcoinCash.InvalidAddressError)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#detectAddressType', () => {
P2PKH_ADDRESSES.forEach(address => {
it(`should detect ${address} is a P2PKH address`, () => {
const isP2PKH = bchjs.Address.detectAddressType(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isP2PKH, 'p2pkh')
2020-07-27 21:32:51 -07:00
})
})
P2SH_ADDRESSES.forEach(address => {
it(`should detect ${address} is a P2SH address`, () => {
const isP2SH = bchjs.Address.detectAddressType(address)
2021-01-02 16:39:44 -08:00
assert.strictEqual(isP2SH, 'p2sh')
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('errors', () => {
it('should fail when called with an invalid address', () => {
2020-07-27 21:32:51 -07:00
assert.throws(() => {
bchjs.Address.detectAddressType()
2020-07-27 21:32:51 -07:00
}, bchjs.BitcoinCash.InvalidAddressError)
assert.throws(() => {
2021-01-02 15:41:12 -08:00
bchjs.Address.detectAddressType('some invalid address')
2020-07-27 21:32:51 -07:00
}, bchjs.BitcoinCash.InvalidAddressError)
})
})
})
2021-01-02 15:41:12 -08:00
describe('#fromXPub', () => {
XPUBS.forEach((xpub, i) => {
xpub.addresses.forEach((address, j) => {
it(`generate public external change address ${j} for ${xpub.xpub}`, () => {
2021-01-02 16:39:44 -08:00
assert.strictEqual(bchjs.Address.fromXPub(xpub.xpub, `0/${j}`), address)
})
})
2020-07-27 21:32:51 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('#fromOutputScript', () => {
it('generate address from output script', () => {
const script = bchjs.Script.encode([
2021-01-02 15:41:12 -08:00
Buffer.from('BOX', 'ascii'),
bchjs.Script.opcodes.OP_CAT,
2021-01-02 15:41:12 -08:00
Buffer.from('BITBOX', 'ascii'),
bchjs.Script.opcodes.OP_EQUAL
])
2020-07-27 21:32:51 -07:00
// hash160 script buffer
2021-01-02 16:39:44 -08:00
const p2shHash160 = bchjs.Crypto.hash160(script)
2020-07-27 21:32:51 -07:00
// encode hash160 as P2SH output
2021-01-02 16:39:44 -08:00
const scriptPubKey = bchjs.Script.scriptHash.output.encode(p2shHash160)
const p2shAddress = bchjs.Address.fromOutputScript(scriptPubKey)
2020-07-27 21:32:51 -07:00
fixtures.p2shMainnet.forEach((address, i) => {
2021-01-02 16:39:44 -08:00
assert.strictEqual(p2shAddress, address)
2020-07-27 21:32:51 -07:00
})
})
})
describe('#toEcashAddress', () => {
it('should convert a BCH address to an eCash address', () => {
const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
const result = bchjs.Address.toEcashAddress(cashAddr)
// console.log('result: ', result)
assert.equal(result, 'ecash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s8f6wu02p')
})
it('should convert a BCH address to an eCash address without a prefix', () => {
const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
const result = bchjs.Address.toEcashAddress(cashAddr, false)
// console.log('result: ', result)
assert.equal(result, 'qr24z7q6s26d9wr078lr2pxsmxg22cyn9s8f6wu02p')
})
})
describe('#toEtokenAddress', () => {
it('should convert a BCH address to an eToken address', () => {
const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
const result = bchjs.Address.toEtokenAddress(cashAddr)
// console.log('result: ', result)
assert.equal(result, 'etoken:qr24z7q6s26d9wr078lr2pxsmxg22cyn9sfhnv2gwk')
})
it('should convert a BCH address to an eToken address without a prefix', () => {
const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
const result = bchjs.Address.toEtokenAddress(cashAddr, false)
// console.log('result: ', result)
assert.equal(result, 'qr24z7q6s26d9wr078lr2pxsmxg22cyn9sfhnv2gwk')
})
})
2020-07-27 21:32:51 -07:00
})