bitcoinjs-lib/ts_src/payments/p2wsh.ts

204 lines
5.8 KiB
TypeScript
Raw Normal View History

2019-03-07 05:05:04 +01:00
import * as bcrypto from '../crypto';
2019-03-05 07:11:20 +01:00
import { bitcoin as BITCOIN_NETWORK } from '../networks';
2019-03-04 14:17:29 +01:00
import * as bscript from '../script';
2019-03-07 05:05:04 +01:00
import { Payment, PaymentOpts, StackFunction } from './index';
2019-03-04 14:17:29 +01:00
import * as lazy from './lazy';
const typef = require('typeforce');
const OPS = bscript.OPS;
2019-03-03 15:07:49 +01:00
const bech32 = require('bech32');
2019-03-03 15:07:49 +01:00
const EMPTY_BUFFER = Buffer.alloc(0);
2019-03-07 05:05:04 +01:00
function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
2019-03-03 15:07:49 +01:00
if (a.length !== b.length) return false;
2019-03-07 05:05:04 +01:00
return a.every((x, i) => {
2019-03-03 15:07:49 +01:00
return x.equals(b[i]);
});
}
// input: <>
// witness: [redeemScriptSig ...] {redeemScript}
// output: OP_0 {sha256(redeemScript)}
2019-03-03 15:07:49 +01:00
export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
if (!a.address && !a.hash && !a.output && !a.redeem && !a.witness)
throw new TypeError('Not enough data');
opts = Object.assign({ validate: true }, opts || {});
typef(
{
network: typef.maybe(typef.Object),
2019-03-03 15:07:49 +01:00
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(32)),
output: typef.maybe(typef.BufferN(34)),
redeem: typef.maybe({
input: typef.maybe(typef.Buffer),
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
}),
input: typef.maybe(typef.BufferN(0)),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
},
a,
);
2019-03-07 05:05:04 +01:00
const _address = lazy.value(() => {
2019-03-03 15:07:49 +01:00
const result = bech32.decode(a.address);
const version = result.words.shift();
const data = bech32.fromWords(result.words);
return {
version,
prefix: result.prefix,
2019-03-03 15:07:49 +01:00
data: Buffer.from(data),
};
});
2019-03-07 05:05:04 +01:00
const _rchunks = lazy.value(() => {
2019-03-03 15:07:49 +01:00
return bscript.decompile(a.redeem!.input!);
2019-03-07 05:05:04 +01:00
}) as StackFunction;
2019-03-03 15:07:49 +01:00
let network = a.network;
if (!network) {
2019-03-03 15:07:49 +01:00
network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
}
2019-03-03 15:07:49 +01:00
const o: Payment = { network };
2019-03-07 05:05:04 +01:00
lazy.prop(o, 'address', () => {
2019-03-03 15:07:49 +01:00
if (!o.hash) return;
const words = bech32.toWords(o.hash);
words.unshift(0x00);
return bech32.encode(network!.bech32, words);
});
2019-03-07 05:05:04 +01:00
lazy.prop(o, 'hash', () => {
2019-03-03 15:07:49 +01:00
if (a.output) return a.output.slice(2);
if (a.address) return _address().data;
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output);
});
2019-03-07 05:05:04 +01:00
lazy.prop(o, 'output', () => {
2019-03-03 15:07:49 +01:00
if (!o.hash) return;
return bscript.compile([OPS.OP_0, o.hash]);
});
2019-03-07 05:05:04 +01:00
lazy.prop(o, 'redeem', () => {
2019-03-03 15:07:49 +01:00
if (!a.witness) return;
return {
output: a.witness[a.witness.length - 1],
input: EMPTY_BUFFER,
2019-03-03 15:07:49 +01:00
witness: a.witness.slice(0, -1),
};
});
2019-03-07 05:05:04 +01:00
lazy.prop(o, 'input', () => {
2019-03-03 15:07:49 +01:00
if (!o.witness) return;
return EMPTY_BUFFER;
});
2019-03-07 05:05:04 +01:00
lazy.prop(o, 'witness', () => {
// transform redeem input to witness stack?
if (
a.redeem &&
a.redeem.input &&
a.redeem.input.length > 0 &&
a.redeem.output &&
a.redeem.output.length > 0
) {
2019-03-03 15:07:49 +01:00
const stack = bscript.toStack(_rchunks());
// assign, and blank the existing input
2019-03-03 15:07:49 +01:00
o.redeem = Object.assign({ witness: stack }, a.redeem);
o.redeem.input = EMPTY_BUFFER;
2019-03-07 05:05:04 +01:00
return ([] as Buffer[]).concat(stack, a.redeem.output);
}
2019-03-03 15:07:49 +01:00
if (!a.redeem) return;
if (!a.redeem.output) return;
if (!a.redeem.witness) return;
2019-03-07 05:05:04 +01:00
return ([] as Buffer[]).concat(a.redeem.witness, a.redeem.output);
2019-03-03 15:07:49 +01:00
});
2019-07-11 07:49:26 +02:00
lazy.prop(o, 'name', () => {
const nameParts = ['p2wsh'];
if (o.redeem !== undefined) nameParts.push(o.redeem.name!);
return nameParts.join('-');
});
// extended validation
if (opts.validate) {
2019-03-03 15:07:49 +01:00
let hash: Buffer = Buffer.from([]);
if (a.address) {
2019-03-03 15:07:49 +01:00
if (_address().prefix !== network.bech32)
throw new TypeError('Invalid prefix or Network mismatch');
if (_address().version !== 0x00)
throw new TypeError('Invalid address version');
if (_address().data.length !== 32)
throw new TypeError('Invalid address data');
hash = _address().data;
}
if (a.hash) {
2019-03-03 15:07:49 +01:00
if (hash.length > 0 && !hash.equals(a.hash))
throw new TypeError('Hash mismatch');
else hash = a.hash;
}
if (a.output) {
if (
a.output.length !== 34 ||
a.output[0] !== OPS.OP_0 ||
2019-03-03 15:07:49 +01:00
a.output[1] !== 0x20
)
throw new TypeError('Output is invalid');
const hash2 = a.output.slice(2);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
if (a.redeem) {
2019-03-03 15:07:49 +01:00
if (a.redeem.network && a.redeem.network !== network)
throw new TypeError('Network mismatch');
// is there two redeem sources?
if (
a.redeem.input &&
a.redeem.input.length > 0 &&
a.redeem.witness &&
a.redeem.witness.length > 0
2019-03-03 15:07:49 +01:00
)
throw new TypeError('Ambiguous witness source');
// is the redeem output non-empty?
if (a.redeem.output) {
2019-03-03 15:07:49 +01:00
if (bscript.decompile(a.redeem.output)!.length === 0)
throw new TypeError('Redeem.output is invalid');
// match hash against other sources
2019-03-03 15:07:49 +01:00
const hash2 = bcrypto.sha256(a.redeem.output);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
2019-03-03 15:07:49 +01:00
if (a.redeem.input && !bscript.isPushOnly(_rchunks()))
throw new TypeError('Non push-only scriptSig');
if (
a.witness &&
a.redeem.witness &&
!stacksEqual(a.witness, a.redeem.witness)
)
throw new TypeError('Witness and redeem.witness mismatch');
}
if (a.witness) {
2019-03-03 15:07:49 +01:00
if (
a.redeem &&
a.redeem.output &&
!a.redeem.output.equals(a.witness[a.witness.length - 1])
)
throw new TypeError('Witness and redeem.output mismatch');
}
}
2019-03-03 15:07:49 +01:00
return Object.assign(o, a);
}