1 | 'use strict';
|
2 | Object.defineProperty(exports, '__esModule', { value: true });
|
3 | exports.p2pk = void 0;
|
4 | const networks_1 = require('../networks');
|
5 | const bscript = require('../script');
|
6 | const types_1 = require('../types');
|
7 | const lazy = require('./lazy');
|
8 | const OPS = bscript.OPS;
|
9 |
|
10 |
|
11 | function p2pk(a, opts) {
|
12 | if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
|
13 | throw new TypeError('Not enough data');
|
14 | opts = Object.assign({ validate: true }, opts || {});
|
15 | (0, types_1.typeforce)(
|
16 | {
|
17 | network: types_1.typeforce.maybe(types_1.typeforce.Object),
|
18 | output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
|
19 | pubkey: types_1.typeforce.maybe(types_1.isPoint),
|
20 | signature: types_1.typeforce.maybe(bscript.isCanonicalScriptSignature),
|
21 | input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
|
22 | },
|
23 | a,
|
24 | );
|
25 | const _chunks = lazy.value(() => {
|
26 | return bscript.decompile(a.input);
|
27 | });
|
28 | const network = a.network || networks_1.bitcoin;
|
29 | const o = { name: 'p2pk', network };
|
30 | lazy.prop(o, 'output', () => {
|
31 | if (!a.pubkey) return;
|
32 | return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
|
33 | });
|
34 | lazy.prop(o, 'pubkey', () => {
|
35 | if (!a.output) return;
|
36 | return a.output.slice(1, -1);
|
37 | });
|
38 | lazy.prop(o, 'signature', () => {
|
39 | if (!a.input) return;
|
40 | return _chunks()[0];
|
41 | });
|
42 | lazy.prop(o, 'input', () => {
|
43 | if (!a.signature) return;
|
44 | return bscript.compile([a.signature]);
|
45 | });
|
46 | lazy.prop(o, 'witness', () => {
|
47 | if (!o.input) return;
|
48 | return [];
|
49 | });
|
50 |
|
51 | if (opts.validate) {
|
52 | if (a.output) {
|
53 | if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
|
54 | throw new TypeError('Output is invalid');
|
55 | if (!(0, types_1.isPoint)(o.pubkey))
|
56 | throw new TypeError('Output pubkey is invalid');
|
57 | if (a.pubkey && !a.pubkey.equals(o.pubkey))
|
58 | throw new TypeError('Pubkey mismatch');
|
59 | }
|
60 | if (a.signature) {
|
61 | if (a.input && !a.input.equals(o.input))
|
62 | throw new TypeError('Signature mismatch');
|
63 | }
|
64 | if (a.input) {
|
65 | if (_chunks().length !== 1) throw new TypeError('Input is invalid');
|
66 | if (!bscript.isCanonicalScriptSignature(o.signature))
|
67 | throw new TypeError('Input has invalid signature');
|
68 | }
|
69 | }
|
70 | return Object.assign(o, a);
|
71 | }
|
72 | exports.p2pk = p2pk;
|