Add name attribute to Payments

This commit is contained in:
junderw 2019-07-11 14:49:26 +09:00
parent 8bbe7c7178
commit c403757ce8
No known key found for this signature in database
GPG key ID: B256185D3A971908
23 changed files with 105 additions and 8 deletions

View file

@ -29,7 +29,7 @@ export function p2data(a: Payment, opts?: PaymentOpts): Payment {
);
const network = a.network || BITCOIN_NETWORK;
const o = { network } as Payment;
const o = { name: 'embed', network } as Payment;
lazy.prop(o, 'output', () => {
if (!a.data) return;

View file

@ -8,6 +8,7 @@ import { p2wpkh } from './p2wpkh';
import { p2wsh } from './p2wsh';
export interface Payment {
name?: string;
network?: Network;
output?: Buffer;
data?: Buffer[];

View file

@ -102,6 +102,10 @@ export function p2ms(a: Payment, opts?: PaymentOpts): Payment {
if (!o.input) return;
return [];
});
lazy.prop(o, 'name', () => {
if (!o.m || !o.n) return;
return `p2ms(${o.m} of ${o.n})`;
});
// extended validation
if (opts.validate) {

View file

@ -30,7 +30,7 @@ export function p2pk(a: Payment, opts?: PaymentOpts): Payment {
}) as StackFunction;
const network = a.network || BITCOIN_NETWORK;
const o: Payment = { network };
const o: Payment = { name: 'p2pk', network };
lazy.prop(o, 'output', () => {
if (!a.pubkey) return;

View file

@ -41,7 +41,7 @@ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
}) as StackFunction;
const network = a.network || BITCOIN_NETWORK;
const o: Payment = { network };
const o: Payment = { name: 'p2pkh', network };
lazy.prop(o, 'address', () => {
if (!o.hash) return;

View file

@ -116,6 +116,11 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
if (o.redeem && o.redeem.witness) return o.redeem.witness;
if (o.input) return [];
});
lazy.prop(o, 'name', () => {
const nameParts = ['p2sh'];
if (o.redeem !== undefined) nameParts.push(o.redeem.name!);
return nameParts.join('-');
});
if (opts.validate) {
let hash: Buffer = Buffer.from([]);

View file

@ -45,7 +45,7 @@ export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment {
});
const network = a.network || BITCOIN_NETWORK;
const o: Payment = { network };
const o: Payment = { name: 'p2wpkh', network };
lazy.prop(o, 'address', () => {
if (!o.hash) return;

View file

@ -116,6 +116,11 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
if (!a.redeem.witness) return;
return ([] as Buffer[]).concat(a.redeem.witness, a.redeem.output);
});
lazy.prop(o, 'name', () => {
const nameParts = ['p2wsh'];
if (o.redeem !== undefined) nameParts.push(o.redeem.name!);
return nameParts.join('-');
});
// extended validation
if (opts.validate) {