2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { describe, it } from 'mocha';
|
|
|
|
import * as baddress from '../src/address';
|
|
|
|
import * as bscript from '../src/script';
|
|
|
|
import * as fixtures from './fixtures/address.json';
|
|
|
|
|
|
|
|
const NETWORKS = Object.assign(
|
|
|
|
{
|
|
|
|
litecoin: {
|
|
|
|
messagePrefix: '\x19Litecoin Signed Message:\n',
|
|
|
|
bip32: {
|
|
|
|
public: 0x019da462,
|
|
|
|
private: 0x019d9cfe,
|
|
|
|
},
|
|
|
|
pubKeyHash: 0x30,
|
|
|
|
scriptHash: 0x32,
|
|
|
|
wif: 0xb0,
|
2018-06-05 09:21:43 +02:00
|
|
|
},
|
2019-09-07 06:42:03 +02:00
|
|
|
},
|
|
|
|
require('../src/networks'),
|
|
|
|
);
|
2014-03-07 00:21:42 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('address', () => {
|
|
|
|
describe('fromBase58Check', () => {
|
|
|
|
fixtures.standard.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (!f.base58check) return;
|
2017-08-17 06:12:43 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('decodes ' + f.base58check, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const decode = baddress.fromBase58Check(f.base58check);
|
2014-03-31 05:47:47 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(decode.version, f.version);
|
|
|
|
assert.strictEqual(decode.hash.toString('hex'), f.hash);
|
|
|
|
});
|
|
|
|
});
|
2014-05-08 05:28:20 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.fromBase58Check.forEach(f => {
|
|
|
|
it('throws on ' + f.exception, () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
baddress.fromBase58Check(f.address);
|
|
|
|
}, new RegExp(f.address + ' ' + f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-03-31 05:47:47 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('fromBech32', () => {
|
|
|
|
fixtures.standard.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (!f.bech32) return;
|
2017-08-17 06:12:43 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('decodes ' + f.bech32, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const actual = baddress.fromBech32(f.bech32);
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(actual.version, f.version);
|
|
|
|
assert.strictEqual(actual.prefix, NETWORKS[f.network].bech32);
|
|
|
|
assert.strictEqual(actual.data.toString('hex'), f.data);
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
fixtures.invalid.bech32.forEach(f => {
|
|
|
|
it('decode fails for ' + f.address + '(' + f.exception + ')', () => {
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
baddress.fromBech32(f.address);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('fromOutputScript', () => {
|
|
|
|
fixtures.standard.forEach(f => {
|
|
|
|
it('encodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const script = bscript.fromASM(f.script);
|
|
|
|
const address = baddress.fromOutputScript(script, NETWORKS[f.network]);
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, f.base58check || f.bech32!.toLowerCase());
|
|
|
|
});
|
|
|
|
});
|
2014-05-28 05:17:04 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.fromOutputScript.forEach(f => {
|
|
|
|
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const script = bscript.fromASM(f.script);
|
2014-05-28 05:17:04 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
baddress.fromOutputScript(script);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('toBase58Check', () => {
|
|
|
|
fixtures.standard.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (!f.base58check) return;
|
2017-08-17 06:12:43 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('encodes ' + f.hash + ' (' + f.network + ')', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const address = baddress.toBase58Check(
|
|
|
|
Buffer.from(f.hash, 'hex'),
|
|
|
|
f.version,
|
|
|
|
);
|
2014-03-06 02:45:56 +01:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, f.base58check);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('toBech32', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
fixtures.bech32.forEach(f => {
|
|
|
|
if (!f.address) return;
|
|
|
|
const data = Buffer.from(f.data, 'hex');
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('encode ' + f.address, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.deepStrictEqual(
|
|
|
|
baddress.toBech32(data, f.version, f.prefix),
|
|
|
|
f.address.toLowerCase(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
// TODO: These fixtures (according to TypeScript) have none of the data used below
|
|
|
|
fixtures.invalid.bech32.forEach((f: any) => {
|
|
|
|
if (!f.prefix || f.version === undefined || f.data === undefined) return;
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('encode fails (' + f.exception, () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
baddress.toBech32(Buffer.from(f.data, 'hex'), f.version, f.prefix);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('toOutputScript', () => {
|
|
|
|
fixtures.standard.forEach(f => {
|
|
|
|
it('decodes ' + f.script.slice(0, 30) + '... (' + f.network + ')', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const script = baddress.toOutputScript(
|
|
|
|
(f.base58check || f.bech32)!,
|
|
|
|
NETWORKS[f.network],
|
|
|
|
);
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(bscript.toASM(script), f.script);
|
|
|
|
});
|
|
|
|
});
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.toOutputScript.forEach(f => {
|
|
|
|
it('throws when ' + f.exception, () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
baddress.toOutputScript(f.address, f.network as any);
|
|
|
|
}, new RegExp(f.address + ' ' + f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|