bitcoinjs-lib/test/payments.spec.ts

114 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as assert from 'assert';
import { describe, it } from 'mocha';
import { PaymentCreator } from '../src/payments';
2019-09-12 10:35:08 +02:00
import * as u from './payments.utils';
['embed', 'p2ms', 'p2pk', 'p2pkh', 'p2sh', 'p2wpkh', 'p2wsh'].forEach(p => {
describe(p, () => {
let fn: PaymentCreator;
2019-09-12 10:35:08 +02:00
const payment = require('../src/payments/' + p);
2018-12-28 16:00:52 +01:00
if (p === 'embed') {
fn = payment.p2data;
2018-12-28 16:00:52 +01:00
} else {
fn = payment[p];
2018-12-28 16:00:52 +01:00
}
const fixtures = require('./fixtures/' + p);
fixtures.valid.forEach((f: any) => {
it(f.description + ' as expected', () => {
const args = u.preform(f.arguments);
const actual = fn(args, f.options);
2018-07-03 14:33:04 +02:00
u.equate(actual, f.expected, f.arguments);
});
it(f.description + ' as expected (no validation)', () => {
const args = u.preform(f.arguments);
const actual = fn(
args,
Object.assign({}, f.options, {
validate: false,
}),
);
u.equate(actual, f.expected, f.arguments);
});
});
fixtures.invalid.forEach((f: any) => {
it(
'throws ' + f.exception + (f.description ? 'for ' + f.description : ''),
() => {
const args = u.preform(f.arguments);
assert.throws(() => {
fn(args, f.options);
}, new RegExp(f.exception));
},
);
});
2019-07-11 07:49:26 +02:00
if (p === 'p2sh') {
const p2wsh = require('../src/payments/p2wsh').p2wsh;
const p2pk = require('../src/payments/p2pk').p2pk;
2019-07-11 07:49:26 +02:00
it('properly assembles nested p2wsh with names', () => {
const actual = fn({
redeem: p2wsh({
redeem: p2pk({
pubkey: Buffer.from(
'03e15819590382a9dd878f01e2f0cbce541564eb415e43b440472d883ecd283058',
'hex',
),
}),
}),
});
assert.strictEqual(
actual.address,
'3MGbrbye4ttNUXM8WAvBFRKry4fkS9fjuw',
);
assert.strictEqual(actual.name, 'p2sh-p2wsh-p2pk');
assert.strictEqual(actual.redeem!.name, 'p2wsh-p2pk');
assert.strictEqual(actual.redeem!.redeem!.name, 'p2pk');
});
2019-07-11 07:49:26 +02:00
}
// cross-verify dynamically too
if (!fixtures.dynamic) return;
const { depends, details } = fixtures.dynamic;
details.forEach((f: any) => {
const detail = u.preform(f);
const disabled: any = {};
if (f.disabled)
f.disabled.forEach((k: string) => {
disabled[k] = true;
});
2019-09-12 10:35:08 +02:00
for (const key in depends) {
if (key in disabled) continue;
const dependencies = depends[key];
dependencies.forEach((dependency: any) => {
if (!Array.isArray(dependency)) dependency = [dependency];
const args = {};
dependency.forEach((d: any) => {
u.from(d, detail, args);
});
const expected = u.from(key, detail);
it(
f.description +
', ' +
key +
' derives from ' +
JSON.stringify(dependency),
() => {
u.equate(fn(args), expected);
},
);
});
}
});
});
});