@btc-vision/bitcoin 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/package.json +95 -0
  4. package/src/address.d.ts +42 -0
  5. package/src/address.js +191 -0
  6. package/src/bip66.d.ts +7 -0
  7. package/src/bip66.js +103 -0
  8. package/src/block.d.ts +30 -0
  9. package/src/block.js +224 -0
  10. package/src/bufferutils.d.ts +54 -0
  11. package/src/bufferutils.js +182 -0
  12. package/src/crypto.d.ts +18 -0
  13. package/src/crypto.js +128 -0
  14. package/src/ecc_lib.d.ts +17 -0
  15. package/src/ecc_lib.js +122 -0
  16. package/src/hooks/AdvancedSignatureManager.d.ts +44 -0
  17. package/src/hooks/AdvancedSignatureManager.js +88 -0
  18. package/src/hooks/HookedSigner.d.ts +4 -0
  19. package/src/hooks/HookedSigner.js +90 -0
  20. package/src/hooks/SignatureManager.d.ts +35 -0
  21. package/src/hooks/SignatureManager.js +72 -0
  22. package/src/index.d.ts +42 -0
  23. package/src/index.js +87 -0
  24. package/src/merkle.d.ts +10 -0
  25. package/src/merkle.js +30 -0
  26. package/src/networks.d.ts +29 -0
  27. package/src/networks.js +71 -0
  28. package/src/ops.d.ts +126 -0
  29. package/src/ops.js +131 -0
  30. package/src/payments/bip341.d.ts +49 -0
  31. package/src/payments/bip341.js +124 -0
  32. package/src/payments/embed.d.ts +9 -0
  33. package/src/payments/embed.js +54 -0
  34. package/src/payments/index.d.ts +48 -0
  35. package/src/payments/index.js +69 -0
  36. package/src/payments/lazy.d.ts +2 -0
  37. package/src/payments/lazy.js +32 -0
  38. package/src/payments/p2ms.d.ts +9 -0
  39. package/src/payments/p2ms.js +158 -0
  40. package/src/payments/p2pk.d.ts +10 -0
  41. package/src/payments/p2pk.js +82 -0
  42. package/src/payments/p2pkh.d.ts +10 -0
  43. package/src/payments/p2pkh.js +143 -0
  44. package/src/payments/p2sh.d.ts +10 -0
  45. package/src/payments/p2sh.js +204 -0
  46. package/src/payments/p2tr.d.ts +10 -0
  47. package/src/payments/p2tr.js +315 -0
  48. package/src/payments/p2wpkh.d.ts +10 -0
  49. package/src/payments/p2wpkh.js +146 -0
  50. package/src/payments/p2wsh.d.ts +10 -0
  51. package/src/payments/p2wsh.js +226 -0
  52. package/src/psbt/bip371.d.ts +42 -0
  53. package/src/psbt/bip371.js +424 -0
  54. package/src/psbt/psbtutils.d.ts +64 -0
  55. package/src/psbt/psbtutils.js +191 -0
  56. package/src/psbt.d.ts +235 -0
  57. package/src/psbt.js +1825 -0
  58. package/src/push_data.d.ts +29 -0
  59. package/src/push_data.js +83 -0
  60. package/src/script.d.ts +42 -0
  61. package/src/script.js +231 -0
  62. package/src/script_number.d.ts +19 -0
  63. package/src/script_number.js +78 -0
  64. package/src/script_signature.d.ts +21 -0
  65. package/src/script_signature.js +79 -0
  66. package/src/transaction.d.ts +60 -0
  67. package/src/transaction.js +571 -0
  68. package/src/types.d.ts +54 -0
  69. package/src/types.js +106 -0
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2data = 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
+ // output: OP_RETURN ...
10
+ /**
11
+ * Embeds data in a Bitcoin payment.
12
+ * @param a - The payment object.
13
+ * @param opts - Optional payment options.
14
+ * @returns The modified payment object.
15
+ * @throws {TypeError} If there is not enough data or if the output is invalid.
16
+ */
17
+ function p2data(a, opts) {
18
+ if (!a.data && !a.output) throw new TypeError('Not enough data');
19
+ opts = Object.assign({ validate: true }, opts || {});
20
+ (0, types_1.typeforce)(
21
+ {
22
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
23
+ output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
24
+ data: types_1.typeforce.maybe(
25
+ types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
26
+ ),
27
+ },
28
+ a,
29
+ );
30
+ const network = a.network || networks_1.bitcoin;
31
+ const o = { name: 'embed', network };
32
+ lazy.prop(o, 'output', () => {
33
+ if (!a.data) return;
34
+ return bscript.compile([OPS.OP_RETURN].concat(a.data));
35
+ });
36
+ lazy.prop(o, 'data', () => {
37
+ if (!a.output) return;
38
+ return bscript.decompile(a.output).slice(1);
39
+ });
40
+ // extended validation
41
+ if (opts.validate) {
42
+ if (a.output) {
43
+ const chunks = bscript.decompile(a.output);
44
+ if (chunks[0] !== OPS.OP_RETURN)
45
+ throw new TypeError('Output is invalid');
46
+ if (!chunks.slice(1).every(types_1.typeforce.Buffer))
47
+ throw new TypeError('Output is invalid');
48
+ if (a.data && !(0, types_1.stacksEqual)(a.data, o.data))
49
+ throw new TypeError('Data mismatch');
50
+ }
51
+ }
52
+ return Object.assign(o, a);
53
+ }
54
+ exports.p2data = p2data;
@@ -0,0 +1,48 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Represents a payment object, which is used to create a payment.
4
+ *
5
+ * Supports P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ import { Network } from '../networks';
10
+ import { Taptree } from '../types';
11
+ import { p2data as embed } from './embed';
12
+ import { p2ms } from './p2ms';
13
+ import { p2pk } from './p2pk';
14
+ import { p2pkh } from './p2pkh';
15
+ import { p2sh } from './p2sh';
16
+ import { p2wpkh } from './p2wpkh';
17
+ import { p2wsh } from './p2wsh';
18
+ import { p2tr } from './p2tr';
19
+ export interface Payment {
20
+ name?: string;
21
+ network?: Network;
22
+ output?: Buffer;
23
+ data?: Buffer[];
24
+ m?: number;
25
+ n?: number;
26
+ pubkeys?: Buffer[];
27
+ input?: Buffer;
28
+ signatures?: Buffer[];
29
+ internalPubkey?: Buffer;
30
+ pubkey?: Buffer;
31
+ signature?: Buffer;
32
+ address?: string;
33
+ hash?: Buffer;
34
+ redeem?: Payment;
35
+ redeemVersion?: number;
36
+ scriptTree?: Taptree;
37
+ witness?: Buffer[];
38
+ }
39
+ export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
40
+ export type PaymentFunction = () => Payment;
41
+ export interface PaymentOpts {
42
+ validate?: boolean;
43
+ allowIncomplete?: boolean;
44
+ }
45
+ export type StackElement = Buffer | number;
46
+ export type Stack = StackElement[];
47
+ export type StackFunction = () => Stack;
48
+ export { embed, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh, p2tr };
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2tr =
4
+ exports.p2wsh =
5
+ exports.p2wpkh =
6
+ exports.p2sh =
7
+ exports.p2pkh =
8
+ exports.p2pk =
9
+ exports.p2ms =
10
+ exports.embed =
11
+ void 0;
12
+ const embed_1 = require('./embed');
13
+ Object.defineProperty(exports, 'embed', {
14
+ enumerable: true,
15
+ get: function () {
16
+ return embed_1.p2data;
17
+ },
18
+ });
19
+ const p2ms_1 = require('./p2ms');
20
+ Object.defineProperty(exports, 'p2ms', {
21
+ enumerable: true,
22
+ get: function () {
23
+ return p2ms_1.p2ms;
24
+ },
25
+ });
26
+ const p2pk_1 = require('./p2pk');
27
+ Object.defineProperty(exports, 'p2pk', {
28
+ enumerable: true,
29
+ get: function () {
30
+ return p2pk_1.p2pk;
31
+ },
32
+ });
33
+ const p2pkh_1 = require('./p2pkh');
34
+ Object.defineProperty(exports, 'p2pkh', {
35
+ enumerable: true,
36
+ get: function () {
37
+ return p2pkh_1.p2pkh;
38
+ },
39
+ });
40
+ const p2sh_1 = require('./p2sh');
41
+ Object.defineProperty(exports, 'p2sh', {
42
+ enumerable: true,
43
+ get: function () {
44
+ return p2sh_1.p2sh;
45
+ },
46
+ });
47
+ const p2wpkh_1 = require('./p2wpkh');
48
+ Object.defineProperty(exports, 'p2wpkh', {
49
+ enumerable: true,
50
+ get: function () {
51
+ return p2wpkh_1.p2wpkh;
52
+ },
53
+ });
54
+ const p2wsh_1 = require('./p2wsh');
55
+ Object.defineProperty(exports, 'p2wsh', {
56
+ enumerable: true,
57
+ get: function () {
58
+ return p2wsh_1.p2wsh;
59
+ },
60
+ });
61
+ const p2tr_1 = require('./p2tr');
62
+ Object.defineProperty(exports, 'p2tr', {
63
+ enumerable: true,
64
+ get: function () {
65
+ return p2tr_1.p2tr;
66
+ },
67
+ });
68
+ // TODO
69
+ // witness commitment
@@ -0,0 +1,2 @@
1
+ export declare function prop(object: {}, name: string, f: () => any): void;
2
+ export declare function value<T>(f: () => T): () => T;
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.value = exports.prop = void 0;
4
+ function prop(object, name, f) {
5
+ Object.defineProperty(object, name, {
6
+ configurable: true,
7
+ enumerable: true,
8
+ get() {
9
+ const _value = f.call(this);
10
+ this[name] = _value;
11
+ return _value;
12
+ },
13
+ set(_value) {
14
+ Object.defineProperty(this, name, {
15
+ configurable: true,
16
+ enumerable: true,
17
+ value: _value,
18
+ writable: true,
19
+ });
20
+ },
21
+ });
22
+ }
23
+ exports.prop = prop;
24
+ function value(f) {
25
+ let _value;
26
+ return () => {
27
+ if (_value !== undefined) return _value;
28
+ _value = f();
29
+ return _value;
30
+ };
31
+ }
32
+ exports.value = value;
@@ -0,0 +1,9 @@
1
+ import { Payment, PaymentOpts } from './index';
2
+ /**
3
+ * Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
4
+ * @param a - The payment object.
5
+ * @param opts - Optional payment options.
6
+ * @returns The created payment object.
7
+ * @throws {TypeError} If the provided data is not valid.
8
+ */
9
+ export declare function p2ms(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,158 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2ms = 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
+ const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
10
+ // input: OP_0 [signatures ...]
11
+ // output: m [pubKeys ...] n OP_CHECKMULTISIG
12
+ /**
13
+ * Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
14
+ * @param a - The payment object.
15
+ * @param opts - Optional payment options.
16
+ * @returns The created payment object.
17
+ * @throws {TypeError} If the provided data is not valid.
18
+ */
19
+ function p2ms(a, opts) {
20
+ if (
21
+ !a.input &&
22
+ !a.output &&
23
+ !(a.pubkeys && a.m !== undefined) &&
24
+ !a.signatures
25
+ )
26
+ throw new TypeError('Not enough data');
27
+ opts = Object.assign({ validate: true }, opts || {});
28
+ function isAcceptableSignature(x) {
29
+ return (
30
+ bscript.isCanonicalScriptSignature(x) ||
31
+ (opts.allowIncomplete && x === OPS.OP_0) !== undefined
32
+ );
33
+ }
34
+ (0, types_1.typeforce)(
35
+ {
36
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
37
+ m: types_1.typeforce.maybe(types_1.typeforce.Number),
38
+ n: types_1.typeforce.maybe(types_1.typeforce.Number),
39
+ output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
40
+ pubkeys: types_1.typeforce.maybe(
41
+ types_1.typeforce.arrayOf(types_1.isPoint),
42
+ ),
43
+ signatures: types_1.typeforce.maybe(
44
+ types_1.typeforce.arrayOf(isAcceptableSignature),
45
+ ),
46
+ input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
47
+ },
48
+ a,
49
+ );
50
+ const network = a.network || networks_1.bitcoin;
51
+ const o = { network };
52
+ let chunks = [];
53
+ let decoded = false;
54
+ function decode(output) {
55
+ if (decoded) return;
56
+ decoded = true;
57
+ chunks = bscript.decompile(output);
58
+ o.m = chunks[0] - OP_INT_BASE;
59
+ o.n = chunks[chunks.length - 2] - OP_INT_BASE;
60
+ o.pubkeys = chunks.slice(1, -2);
61
+ }
62
+ lazy.prop(o, 'output', () => {
63
+ if (!a.m) return;
64
+ if (!o.n) return;
65
+ if (!a.pubkeys) return;
66
+ return bscript.compile(
67
+ [].concat(
68
+ OP_INT_BASE + a.m,
69
+ a.pubkeys,
70
+ OP_INT_BASE + o.n,
71
+ OPS.OP_CHECKMULTISIG,
72
+ ),
73
+ );
74
+ });
75
+ lazy.prop(o, 'm', () => {
76
+ if (!o.output) return;
77
+ decode(o.output);
78
+ return o.m;
79
+ });
80
+ lazy.prop(o, 'n', () => {
81
+ if (!o.pubkeys) return;
82
+ return o.pubkeys.length;
83
+ });
84
+ lazy.prop(o, 'pubkeys', () => {
85
+ if (!a.output) return;
86
+ decode(a.output);
87
+ return o.pubkeys;
88
+ });
89
+ lazy.prop(o, 'signatures', () => {
90
+ if (!a.input) return;
91
+ return bscript.decompile(a.input).slice(1);
92
+ });
93
+ lazy.prop(o, 'input', () => {
94
+ if (!a.signatures) return;
95
+ return bscript.compile([OPS.OP_0].concat(a.signatures));
96
+ });
97
+ lazy.prop(o, 'witness', () => {
98
+ if (!o.input) return;
99
+ return [];
100
+ });
101
+ lazy.prop(o, 'name', () => {
102
+ if (!o.m || !o.n) return;
103
+ return `p2ms(${o.m} of ${o.n})`;
104
+ });
105
+ // extended validation
106
+ if (opts.validate) {
107
+ if (a.output) {
108
+ decode(a.output);
109
+ if (!types_1.typeforce.Number(chunks[0]))
110
+ throw new TypeError('Output is invalid');
111
+ if (!types_1.typeforce.Number(chunks[chunks.length - 2]))
112
+ throw new TypeError('Output is invalid');
113
+ if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG)
114
+ throw new TypeError('Output is invalid');
115
+ if (o.m <= 0 || o.n > 16 || o.m > o.n || o.n !== chunks.length - 3)
116
+ throw new TypeError('Output is invalid');
117
+ if (!o.pubkeys.every(x => (0, types_1.isPoint)(x)))
118
+ throw new TypeError('Output is invalid');
119
+ if (a.m !== undefined && a.m !== o.m)
120
+ throw new TypeError('m mismatch');
121
+ if (a.n !== undefined && a.n !== o.n)
122
+ throw new TypeError('n mismatch');
123
+ if (a.pubkeys && !(0, types_1.stacksEqual)(a.pubkeys, o.pubkeys))
124
+ throw new TypeError('Pubkeys mismatch');
125
+ }
126
+ if (a.pubkeys) {
127
+ if (a.n !== undefined && a.n !== a.pubkeys.length)
128
+ throw new TypeError('Pubkey count mismatch');
129
+ o.n = a.pubkeys.length;
130
+ if (o.n < o.m)
131
+ throw new TypeError('Pubkey count cannot be less than m');
132
+ }
133
+ if (a.signatures) {
134
+ if (a.signatures.length < o.m)
135
+ throw new TypeError('Not enough signatures provided');
136
+ if (a.signatures.length > o.m)
137
+ throw new TypeError('Too many signatures provided');
138
+ }
139
+ if (a.input) {
140
+ if (a.input[0] !== OPS.OP_0)
141
+ throw new TypeError('Input is invalid');
142
+ if (
143
+ o.signatures.length === 0 ||
144
+ !o.signatures.every(isAcceptableSignature)
145
+ )
146
+ throw new TypeError('Input has invalid signature(s)');
147
+ if (
148
+ a.signatures &&
149
+ !(0, types_1.stacksEqual)(a.signatures, o.signatures)
150
+ )
151
+ throw new TypeError('Signature mismatch');
152
+ if (a.m !== undefined && a.m !== a.signatures.length)
153
+ throw new TypeError('Signature count mismatch');
154
+ }
155
+ }
156
+ return Object.assign(o, a);
157
+ }
158
+ exports.p2ms = p2ms;
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index';
2
+ /**
3
+ * Creates a pay-to-public-key (P2PK) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data.
6
+ * @param opts - Optional payment options.
7
+ * @returns The P2PK payment object.
8
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
9
+ */
10
+ export declare function p2pk(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,82 @@
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
+ // input: {signature}
10
+ // output: {pubKey} OP_CHECKSIG
11
+ /**
12
+ * Creates a pay-to-public-key (P2PK) payment object.
13
+ *
14
+ * @param a - The payment object containing the necessary data.
15
+ * @param opts - Optional payment options.
16
+ * @returns The P2PK payment object.
17
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
18
+ */
19
+ function p2pk(a, opts) {
20
+ if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
21
+ throw new TypeError('Not enough data');
22
+ opts = Object.assign({ validate: true }, opts || {});
23
+ (0, types_1.typeforce)(
24
+ {
25
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
26
+ output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
27
+ pubkey: types_1.typeforce.maybe(types_1.isPoint),
28
+ signature: types_1.typeforce.maybe(
29
+ bscript.isCanonicalScriptSignature,
30
+ ),
31
+ input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
32
+ },
33
+ a,
34
+ );
35
+ const _chunks = lazy.value(() => {
36
+ return bscript.decompile(a.input);
37
+ });
38
+ const network = a.network || networks_1.bitcoin;
39
+ const o = { name: 'p2pk', network };
40
+ lazy.prop(o, 'output', () => {
41
+ if (!a.pubkey) return;
42
+ return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
43
+ });
44
+ lazy.prop(o, 'pubkey', () => {
45
+ if (!a.output) return;
46
+ return a.output.slice(1, -1);
47
+ });
48
+ lazy.prop(o, 'signature', () => {
49
+ if (!a.input) return;
50
+ return _chunks()[0];
51
+ });
52
+ lazy.prop(o, 'input', () => {
53
+ if (!a.signature) return;
54
+ return bscript.compile([a.signature]);
55
+ });
56
+ lazy.prop(o, 'witness', () => {
57
+ if (!o.input) return;
58
+ return [];
59
+ });
60
+ // extended validation
61
+ if (opts.validate) {
62
+ if (a.output) {
63
+ if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
64
+ throw new TypeError('Output is invalid');
65
+ if (!(0, types_1.isPoint)(o.pubkey))
66
+ throw new TypeError('Output pubkey is invalid');
67
+ if (a.pubkey && !a.pubkey.equals(o.pubkey))
68
+ throw new TypeError('Pubkey mismatch');
69
+ }
70
+ if (a.signature) {
71
+ if (a.input && !a.input.equals(o.input))
72
+ throw new TypeError('Signature mismatch');
73
+ }
74
+ if (a.input) {
75
+ if (_chunks().length !== 1) throw new TypeError('Input is invalid');
76
+ if (!bscript.isCanonicalScriptSignature(o.signature))
77
+ throw new TypeError('Input has invalid signature');
78
+ }
79
+ }
80
+ return Object.assign(o, a);
81
+ }
82
+ exports.p2pk = p2pk;
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index';
2
+ /**
3
+ * Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data.
6
+ * @param opts - Optional payment options.
7
+ * @returns The P2PKH payment object.
8
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
9
+ */
10
+ export declare function p2pkh(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,143 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2pkh = void 0;
4
+ const bcrypto = require('../crypto');
5
+ const networks_1 = require('../networks');
6
+ const bscript = require('../script');
7
+ const types_1 = require('../types');
8
+ const lazy = require('./lazy');
9
+ const bs58check = require('bs58check');
10
+ const OPS = bscript.OPS;
11
+ // input: {signature} {pubkey}
12
+ // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
13
+ /**
14
+ * Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
15
+ *
16
+ * @param a - The payment object containing the necessary data.
17
+ * @param opts - Optional payment options.
18
+ * @returns The P2PKH payment object.
19
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
20
+ */
21
+ function p2pkh(a, opts) {
22
+ if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input)
23
+ throw new TypeError('Not enough data');
24
+ opts = Object.assign({ validate: true }, opts || {});
25
+ (0, types_1.typeforce)(
26
+ {
27
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
28
+ address: types_1.typeforce.maybe(types_1.typeforce.String),
29
+ hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
30
+ output: types_1.typeforce.maybe(types_1.typeforce.BufferN(25)),
31
+ pubkey: types_1.typeforce.maybe(types_1.isPoint),
32
+ signature: types_1.typeforce.maybe(
33
+ bscript.isCanonicalScriptSignature,
34
+ ),
35
+ input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
36
+ },
37
+ a,
38
+ );
39
+ const _address = lazy.value(() => {
40
+ const payload = Buffer.from(bs58check.decode(a.address));
41
+ const version = payload.readUInt8(0);
42
+ const hash = payload.slice(1);
43
+ return { version, hash };
44
+ });
45
+ const _chunks = lazy.value(() => {
46
+ return bscript.decompile(a.input);
47
+ });
48
+ const network = a.network || networks_1.bitcoin;
49
+ const o = { name: 'p2pkh', network };
50
+ lazy.prop(o, 'address', () => {
51
+ if (!o.hash) return;
52
+ const payload = Buffer.allocUnsafe(21);
53
+ payload.writeUInt8(network.pubKeyHash, 0);
54
+ o.hash.copy(payload, 1);
55
+ return bs58check.encode(payload);
56
+ });
57
+ lazy.prop(o, 'hash', () => {
58
+ if (a.output) return a.output.slice(3, 23);
59
+ if (a.address) return _address().hash;
60
+ if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
61
+ });
62
+ lazy.prop(o, 'output', () => {
63
+ if (!o.hash) return;
64
+ return bscript.compile([
65
+ OPS.OP_DUP,
66
+ OPS.OP_HASH160,
67
+ o.hash,
68
+ OPS.OP_EQUALVERIFY,
69
+ OPS.OP_CHECKSIG,
70
+ ]);
71
+ });
72
+ lazy.prop(o, 'pubkey', () => {
73
+ if (!a.input) return;
74
+ return _chunks()[1];
75
+ });
76
+ lazy.prop(o, 'signature', () => {
77
+ if (!a.input) return;
78
+ return _chunks()[0];
79
+ });
80
+ lazy.prop(o, 'input', () => {
81
+ if (!a.pubkey) return;
82
+ if (!a.signature) return;
83
+ return bscript.compile([a.signature, a.pubkey]);
84
+ });
85
+ lazy.prop(o, 'witness', () => {
86
+ if (!o.input) return;
87
+ return [];
88
+ });
89
+ // extended validation
90
+ if (opts.validate) {
91
+ let hash = Buffer.from([]);
92
+ if (a.address) {
93
+ if (_address().version !== network.pubKeyHash)
94
+ throw new TypeError('Invalid version or Network mismatch');
95
+ if (_address().hash.length !== 20)
96
+ throw new TypeError('Invalid address');
97
+ hash = _address().hash;
98
+ }
99
+ if (a.hash) {
100
+ if (hash.length > 0 && !hash.equals(a.hash))
101
+ throw new TypeError('Hash mismatch');
102
+ else hash = a.hash;
103
+ }
104
+ if (a.output) {
105
+ if (
106
+ a.output.length !== 25 ||
107
+ a.output[0] !== OPS.OP_DUP ||
108
+ a.output[1] !== OPS.OP_HASH160 ||
109
+ a.output[2] !== 0x14 ||
110
+ a.output[23] !== OPS.OP_EQUALVERIFY ||
111
+ a.output[24] !== OPS.OP_CHECKSIG
112
+ )
113
+ throw new TypeError('Output is invalid');
114
+ const hash2 = a.output.slice(3, 23);
115
+ if (hash.length > 0 && !hash.equals(hash2))
116
+ throw new TypeError('Hash mismatch');
117
+ else hash = hash2;
118
+ }
119
+ if (a.pubkey) {
120
+ const pkh = bcrypto.hash160(a.pubkey);
121
+ if (hash.length > 0 && !hash.equals(pkh))
122
+ throw new TypeError('Hash mismatch');
123
+ else hash = pkh;
124
+ }
125
+ if (a.input) {
126
+ const chunks = _chunks();
127
+ if (chunks.length !== 2) throw new TypeError('Input is invalid');
128
+ if (!bscript.isCanonicalScriptSignature(chunks[0]))
129
+ throw new TypeError('Input has invalid signature');
130
+ if (!(0, types_1.isPoint)(chunks[1]))
131
+ throw new TypeError('Input has invalid pubkey');
132
+ if (a.signature && !a.signature.equals(chunks[0]))
133
+ throw new TypeError('Signature mismatch');
134
+ if (a.pubkey && !a.pubkey.equals(chunks[1]))
135
+ throw new TypeError('Pubkey mismatch');
136
+ const pkh = bcrypto.hash160(chunks[1]);
137
+ if (hash.length > 0 && !hash.equals(pkh))
138
+ throw new TypeError('Hash mismatch');
139
+ }
140
+ }
141
+ return Object.assign(o, a);
142
+ }
143
+ exports.p2pkh = p2pkh;
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index';
2
+ /**
3
+ * Creates a Pay-to-Script-Hash (P2SH) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data.
6
+ * @param opts - Optional payment options.
7
+ * @returns The P2SH payment object.
8
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
9
+ */
10
+ export declare function p2sh(a: Payment, opts?: PaymentOpts): Payment;