UNPKG

4.67 kBJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, '__esModule', { value: true });
3exports.p2wpkh = void 0;
4const bcrypto = require('../crypto');
5const networks_1 = require('../networks');
6const bscript = require('../script');
7const types_1 = require('../types');
8const lazy = require('./lazy');
9const bech32_1 = require('bech32');
10const OPS = bscript.OPS;
11const EMPTY_BUFFER = Buffer.alloc(0);
12// witness: {signature} {pubKey}
13// input: <>
14// output: OP_0 {pubKeyHash}
15function p2wpkh(a, opts) {
16 if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
17 throw new TypeError('Not enough data');
18 opts = Object.assign({ validate: true }, opts || {});
19 (0, types_1.typeforce)(
20 {
21 address: types_1.typeforce.maybe(types_1.typeforce.String),
22 hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
23 input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
24 network: types_1.typeforce.maybe(types_1.typeforce.Object),
25 output: types_1.typeforce.maybe(types_1.typeforce.BufferN(22)),
26 pubkey: types_1.typeforce.maybe(types_1.isPoint),
27 signature: types_1.typeforce.maybe(bscript.isCanonicalScriptSignature),
28 witness: types_1.typeforce.maybe(
29 types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
30 ),
31 },
32 a,
33 );
34 const _address = lazy.value(() => {
35 const result = bech32_1.bech32.decode(a.address);
36 const version = result.words.shift();
37 const data = bech32_1.bech32.fromWords(result.words);
38 return {
39 version,
40 prefix: result.prefix,
41 data: Buffer.from(data),
42 };
43 });
44 const network = a.network || networks_1.bitcoin;
45 const o = { name: 'p2wpkh', network };
46 lazy.prop(o, 'address', () => {
47 if (!o.hash) return;
48 const words = bech32_1.bech32.toWords(o.hash);
49 words.unshift(0x00);
50 return bech32_1.bech32.encode(network.bech32, words);
51 });
52 lazy.prop(o, 'hash', () => {
53 if (a.output) return a.output.slice(2, 22);
54 if (a.address) return _address().data;
55 if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
56 });
57 lazy.prop(o, 'output', () => {
58 if (!o.hash) return;
59 return bscript.compile([OPS.OP_0, o.hash]);
60 });
61 lazy.prop(o, 'pubkey', () => {
62 if (a.pubkey) return a.pubkey;
63 if (!a.witness) return;
64 return a.witness[1];
65 });
66 lazy.prop(o, 'signature', () => {
67 if (!a.witness) return;
68 return a.witness[0];
69 });
70 lazy.prop(o, 'input', () => {
71 if (!o.witness) return;
72 return EMPTY_BUFFER;
73 });
74 lazy.prop(o, 'witness', () => {
75 if (!a.pubkey) return;
76 if (!a.signature) return;
77 return [a.signature, a.pubkey];
78 });
79 // extended validation
80 if (opts.validate) {
81 let hash = Buffer.from([]);
82 if (a.address) {
83 if (network && network.bech32 !== _address().prefix)
84 throw new TypeError('Invalid prefix or Network mismatch');
85 if (_address().version !== 0x00)
86 throw new TypeError('Invalid address version');
87 if (_address().data.length !== 20)
88 throw new TypeError('Invalid address data');
89 hash = _address().data;
90 }
91 if (a.hash) {
92 if (hash.length > 0 && !hash.equals(a.hash))
93 throw new TypeError('Hash mismatch');
94 else hash = a.hash;
95 }
96 if (a.output) {
97 if (
98 a.output.length !== 22 ||
99 a.output[0] !== OPS.OP_0 ||
100 a.output[1] !== 0x14
101 )
102 throw new TypeError('Output is invalid');
103 if (hash.length > 0 && !hash.equals(a.output.slice(2)))
104 throw new TypeError('Hash mismatch');
105 else hash = a.output.slice(2);
106 }
107 if (a.pubkey) {
108 const pkh = bcrypto.hash160(a.pubkey);
109 if (hash.length > 0 && !hash.equals(pkh))
110 throw new TypeError('Hash mismatch');
111 else hash = pkh;
112 if (!(0, types_1.isPoint)(a.pubkey) || a.pubkey.length !== 33)
113 throw new TypeError('Invalid pubkey for p2wpkh');
114 }
115 if (a.witness) {
116 if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
117 if (!bscript.isCanonicalScriptSignature(a.witness[0]))
118 throw new TypeError('Witness has invalid signature');
119 if (!(0, types_1.isPoint)(a.witness[1]) || a.witness[1].length !== 33)
120 throw new TypeError('Witness has invalid pubkey');
121 if (a.signature && !a.signature.equals(a.witness[0]))
122 throw new TypeError('Signature mismatch');
123 if (a.pubkey && !a.pubkey.equals(a.witness[1]))
124 throw new TypeError('Pubkey mismatch');
125 const pkh = bcrypto.hash160(a.witness[1]);
126 if (hash.length > 0 && !hash.equals(pkh))
127 throw new TypeError('Hash mismatch');
128 }
129 }
130 return Object.assign(o, a);
131}
132exports.p2wpkh = p2wpkh;