@btc-vision/bitcoin 6.3.0 → 6.3.2

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 (260) hide show
  1. package/.babelrc +4 -0
  2. package/.gitattributes +2 -0
  3. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
  4. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
  5. package/.nyc_output/processinfo/index.json +1 -0
  6. package/.prettierrc.json +12 -0
  7. package/CHANGELOG.md +403 -0
  8. package/CONTRIBUTING.md +83 -0
  9. package/browser/address.d.ts +16 -0
  10. package/{src → browser}/bip66.d.ts +6 -7
  11. package/{src → browser}/block.d.ts +29 -30
  12. package/{src → browser}/bufferutils.d.ts +34 -54
  13. package/browser/crypto/crypto.d.ts +1 -0
  14. package/{src → browser}/crypto.d.ts +13 -18
  15. package/browser/ecc_lib.d.ts +3 -0
  16. package/browser/hooks/AdvancedSignatureManager.d.ts +16 -0
  17. package/{src → browser}/hooks/HookedSigner.d.ts +4 -4
  18. package/browser/hooks/SignatureManager.d.ts +13 -0
  19. package/browser/index.d.ts +58 -0
  20. package/browser/index.js +2 -0
  21. package/browser/index.js.LICENSE.txt +14 -0
  22. package/browser/merkle.d.ts +1 -0
  23. package/browser/networks.d.ts +23 -0
  24. package/{src → browser}/ops.d.ts +126 -126
  25. package/browser/payments/bip341.d.ts +23 -0
  26. package/browser/payments/embed.d.ts +2 -0
  27. package/browser/payments/index.d.ts +41 -0
  28. package/{src → browser}/payments/lazy.d.ts +2 -2
  29. package/browser/payments/p2ms.d.ts +2 -0
  30. package/browser/payments/p2pk.d.ts +2 -0
  31. package/browser/payments/p2pkh.d.ts +2 -0
  32. package/browser/payments/p2sh.d.ts +2 -0
  33. package/browser/payments/p2tr.d.ts +2 -0
  34. package/browser/payments/p2wpkh.d.ts +2 -0
  35. package/browser/payments/p2wsh.d.ts +2 -0
  36. package/browser/psbt/bip371.d.ts +16 -0
  37. package/browser/psbt/psbtutils.d.ts +26 -0
  38. package/{src → browser}/psbt.d.ts +167 -235
  39. package/browser/push_data.d.ts +7 -0
  40. package/browser/script.d.ts +17 -0
  41. package/browser/script_number.d.ts +2 -0
  42. package/browser/script_signature.d.ts +7 -0
  43. package/{src → browser}/transaction.d.ts +48 -60
  44. package/{src → browser}/types.d.ts +37 -54
  45. package/build/address.d.ts +16 -0
  46. package/build/address.js +148 -0
  47. package/build/bip66.d.ts +6 -0
  48. package/build/bip66.js +99 -0
  49. package/build/block.d.ts +29 -0
  50. package/build/block.js +181 -0
  51. package/build/bufferutils.d.ts +34 -0
  52. package/build/bufferutils.js +141 -0
  53. package/build/crypto/crypto.d.ts +1 -0
  54. package/build/crypto/crypto.js +1 -0
  55. package/build/crypto.d.ts +13 -0
  56. package/build/crypto.js +87 -0
  57. package/build/ecc_lib.d.ts +3 -0
  58. package/build/ecc_lib.js +61 -0
  59. package/build/hooks/AdvancedSignatureManager.d.ts +16 -0
  60. package/build/hooks/AdvancedSignatureManager.js +52 -0
  61. package/build/hooks/HookedSigner.d.ts +4 -0
  62. package/build/hooks/HookedSigner.js +64 -0
  63. package/build/hooks/SignatureManager.d.ts +13 -0
  64. package/build/hooks/SignatureManager.js +45 -0
  65. package/build/index.d.ts +58 -0
  66. package/build/index.js +32 -0
  67. package/build/merkle.d.ts +1 -0
  68. package/build/merkle.js +19 -0
  69. package/build/networks.d.ts +23 -0
  70. package/build/networks.js +121 -0
  71. package/build/ops.d.ts +126 -0
  72. package/{src → build}/ops.js +127 -131
  73. package/build/payments/bip341.d.ts +23 -0
  74. package/build/payments/bip341.js +82 -0
  75. package/build/payments/embed.d.ts +2 -0
  76. package/build/payments/embed.js +39 -0
  77. package/build/payments/index.d.ts +41 -0
  78. package/build/payments/index.js +10 -0
  79. package/build/payments/lazy.d.ts +2 -0
  80. package/{src → build}/payments/lazy.js +28 -32
  81. package/build/payments/p2ms.d.ts +2 -0
  82. package/{src → build}/payments/p2ms.js +128 -158
  83. package/build/payments/p2pk.d.ts +2 -0
  84. package/build/payments/p2pk.js +68 -0
  85. package/build/payments/p2pkh.d.ts +2 -0
  86. package/build/payments/p2pkh.js +135 -0
  87. package/build/payments/p2sh.d.ts +2 -0
  88. package/build/payments/p2sh.js +175 -0
  89. package/build/payments/p2tr.d.ts +2 -0
  90. package/build/payments/p2tr.js +254 -0
  91. package/build/payments/p2wpkh.d.ts +2 -0
  92. package/build/payments/p2wpkh.js +130 -0
  93. package/build/payments/p2wsh.d.ts +2 -0
  94. package/build/payments/p2wsh.js +180 -0
  95. package/build/psbt/bip371.d.ts +16 -0
  96. package/build/psbt/bip371.js +246 -0
  97. package/build/psbt/psbtutils.d.ts +26 -0
  98. package/build/psbt/psbtutils.js +170 -0
  99. package/build/psbt.d.ts +167 -0
  100. package/build/psbt.js +1305 -0
  101. package/build/push_data.d.ts +7 -0
  102. package/build/push_data.js +57 -0
  103. package/build/script.d.ts +17 -0
  104. package/build/script.js +167 -0
  105. package/build/script_number.d.ts +2 -0
  106. package/build/script_number.js +49 -0
  107. package/build/script_signature.d.ts +7 -0
  108. package/build/script_signature.js +49 -0
  109. package/build/transaction.d.ts +48 -0
  110. package/build/transaction.js +445 -0
  111. package/build/types.d.ts +37 -0
  112. package/build/types.js +73 -0
  113. package/cjs/package.json +3 -0
  114. package/eslint.config.js +56 -0
  115. package/gulpfile.js +42 -0
  116. package/package.json +105 -50
  117. package/src/{address.js → address.ts} +93 -73
  118. package/src/{bip66.js → bip66.ts} +23 -19
  119. package/src/{block.js → block.ts} +114 -105
  120. package/src/{bufferutils.js → bufferutils.ts} +65 -67
  121. package/src/crypto/crypto-browser.js +75 -0
  122. package/src/crypto/crypto.ts +1 -0
  123. package/src/crypto.ts +108 -0
  124. package/src/{ecc_lib.js → ecc_lib.ts} +25 -53
  125. package/src/hooks/{AdvancedSignatureManager.js → AdvancedSignatureManager.ts} +34 -18
  126. package/src/hooks/HookedSigner.ts +108 -0
  127. package/src/hooks/{SignatureManager.js → SignatureManager.ts} +26 -14
  128. package/src/index.ts +86 -0
  129. package/src/{merkle.js → merkle.ts} +8 -7
  130. package/src/networks.ts +235 -0
  131. package/src/ops.ts +282 -0
  132. package/src/payments/bip341.ts +140 -0
  133. package/src/payments/embed.ts +55 -0
  134. package/src/payments/{index.d.ts → index.ts} +20 -10
  135. package/src/payments/lazy.ts +28 -0
  136. package/src/payments/p2ms.ts +150 -0
  137. package/src/payments/{p2pk.js → p2pk.ts} +32 -29
  138. package/src/payments/{p2pkh.js → p2pkh.ts} +53 -47
  139. package/src/payments/{p2sh.js → p2sh.ts} +72 -71
  140. package/src/payments/{p2tr.js → p2tr.ts} +114 -125
  141. package/src/payments/{p2wpkh.js → p2wpkh.ts} +51 -56
  142. package/src/payments/{p2wsh.js → p2wsh.ts} +69 -81
  143. package/src/psbt/{bip371.js → bip371.ts} +195 -178
  144. package/src/psbt/psbtutils.ts +299 -0
  145. package/src/{psbt.js → psbt.ts} +1048 -699
  146. package/src/{push_data.js → push_data.ts} +35 -21
  147. package/src/{script.js → script.ts} +93 -77
  148. package/src/{script_number.js → script_number.ts} +15 -21
  149. package/src/{script_signature.js → script_signature.ts} +26 -14
  150. package/src/{transaction.js → transaction.ts} +247 -167
  151. package/src/types.ts +122 -0
  152. package/test/address.spec.js +124 -0
  153. package/test/address.spec.ts +177 -0
  154. package/test/bitcoin.core.spec.js +170 -0
  155. package/test/bitcoin.core.spec.ts +234 -0
  156. package/test/block.spec.js +141 -0
  157. package/test/block.spec.ts +194 -0
  158. package/test/bufferutils.spec.js +427 -0
  159. package/test/bufferutils.spec.ts +513 -0
  160. package/test/crypto.spec.js +41 -0
  161. package/test/crypto.spec.ts +55 -0
  162. package/test/fixtures/address.json +329 -0
  163. package/test/fixtures/block.json +148 -0
  164. package/test/fixtures/bufferutils.json +102 -0
  165. package/test/fixtures/core/README.md +26 -0
  166. package/test/fixtures/core/base58_encode_decode.json +50 -0
  167. package/test/fixtures/core/base58_keys_invalid.json +152 -0
  168. package/test/fixtures/core/base58_keys_valid.json +452 -0
  169. package/test/fixtures/core/blocks.json +27 -0
  170. package/test/fixtures/core/sig_canonical.json +7 -0
  171. package/test/fixtures/core/sig_noncanonical.json +33 -0
  172. package/test/fixtures/core/sighash.json +3505 -0
  173. package/test/fixtures/core/tx_valid.json +2023 -0
  174. package/test/fixtures/crypto.json +43 -0
  175. package/test/fixtures/ecdsa.json +217 -0
  176. package/test/fixtures/ecpair.json +141 -0
  177. package/test/fixtures/embed.json +108 -0
  178. package/test/fixtures/p2ms.json +434 -0
  179. package/test/fixtures/p2pk.json +179 -0
  180. package/test/fixtures/p2pkh.json +276 -0
  181. package/test/fixtures/p2sh.json +508 -0
  182. package/test/fixtures/p2tr.json +1198 -0
  183. package/test/fixtures/p2wpkh.json +290 -0
  184. package/test/fixtures/p2wsh.json +489 -0
  185. package/test/fixtures/psbt.json +924 -0
  186. package/test/fixtures/script.json +465 -0
  187. package/test/fixtures/script_number.json +225 -0
  188. package/test/fixtures/signature.json +140 -0
  189. package/test/fixtures/transaction.json +916 -0
  190. package/test/integration/_regtest.js +7 -0
  191. package/test/integration/_regtest.ts +6 -0
  192. package/test/integration/addresses.spec.js +116 -0
  193. package/test/integration/addresses.spec.ts +154 -0
  194. package/test/integration/bip32.spec.js +85 -0
  195. package/test/integration/bip32.spec.ts +151 -0
  196. package/test/integration/blocks.spec.js +26 -0
  197. package/test/integration/blocks.spec.ts +28 -0
  198. package/test/integration/cltv.spec.js +199 -0
  199. package/test/integration/cltv.spec.ts +283 -0
  200. package/test/integration/csv.spec.js +362 -0
  201. package/test/integration/csv.spec.ts +527 -0
  202. package/test/integration/payments.spec.js +98 -0
  203. package/test/integration/payments.spec.ts +135 -0
  204. package/test/integration/taproot.spec.js +532 -0
  205. package/test/integration/taproot.spec.ts +707 -0
  206. package/test/integration/transactions.spec.js +561 -0
  207. package/test/integration/transactions.spec.ts +769 -0
  208. package/test/payments.spec.js +97 -0
  209. package/test/payments.spec.ts +125 -0
  210. package/test/payments.utils.js +190 -0
  211. package/test/payments.utils.ts +208 -0
  212. package/test/psbt.spec.js +1044 -0
  213. package/test/psbt.spec.ts +1414 -0
  214. package/test/script.spec.js +151 -0
  215. package/test/script.spec.ts +210 -0
  216. package/test/script_number.spec.js +24 -0
  217. package/test/script_number.spec.ts +29 -0
  218. package/test/script_signature.spec.js +52 -0
  219. package/test/script_signature.spec.ts +66 -0
  220. package/test/transaction.spec.js +269 -0
  221. package/test/transaction.spec.ts +387 -0
  222. package/test/ts-node-register.js +5 -0
  223. package/test/tsconfig.json +45 -0
  224. package/test/types.spec.js +46 -0
  225. package/test/types.spec.ts +58 -0
  226. package/tsconfig.base.json +27 -0
  227. package/tsconfig.json +19 -0
  228. package/tsconfig.webpack.json +18 -0
  229. package/webpack.config.js +79 -0
  230. package/src/address.d.ts +0 -42
  231. package/src/crypto.js +0 -128
  232. package/src/ecc_lib.d.ts +0 -17
  233. package/src/hooks/AdvancedSignatureManager.d.ts +0 -44
  234. package/src/hooks/HookedSigner.js +0 -90
  235. package/src/hooks/SignatureManager.d.ts +0 -35
  236. package/src/index.d.ts +0 -42
  237. package/src/index.js +0 -87
  238. package/src/merkle.d.ts +0 -10
  239. package/src/networks.d.ts +0 -29
  240. package/src/networks.js +0 -71
  241. package/src/payments/bip341.d.ts +0 -49
  242. package/src/payments/bip341.js +0 -124
  243. package/src/payments/embed.d.ts +0 -9
  244. package/src/payments/embed.js +0 -54
  245. package/src/payments/index.js +0 -69
  246. package/src/payments/p2ms.d.ts +0 -9
  247. package/src/payments/p2pk.d.ts +0 -10
  248. package/src/payments/p2pkh.d.ts +0 -10
  249. package/src/payments/p2sh.d.ts +0 -10
  250. package/src/payments/p2tr.d.ts +0 -10
  251. package/src/payments/p2wpkh.d.ts +0 -10
  252. package/src/payments/p2wsh.d.ts +0 -10
  253. package/src/psbt/bip371.d.ts +0 -42
  254. package/src/psbt/psbtutils.d.ts +0 -64
  255. package/src/psbt/psbtutils.js +0 -191
  256. package/src/push_data.d.ts +0 -29
  257. package/src/script.d.ts +0 -42
  258. package/src/script_number.d.ts +0 -19
  259. package/src/script_signature.d.ts +0 -21
  260. package/src/types.js +0 -106
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * Represents a payment object, which is used to create a payment.
4
3
  *
@@ -6,16 +5,21 @@
6
5
  *
7
6
  * @packageDocumentation
8
7
  */
8
+
9
9
  import { Network } from '../networks';
10
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';
11
+
12
+ export * from './bip341.js';
13
+ export * from './embed.js';
14
+ export * from './lazy.js';
15
+ export * from './p2ms.js';
16
+ export * from './p2pk.js';
17
+ export * from './p2pkh.js';
18
+ export * from './p2sh.js';
19
+ export * from './p2tr.js';
20
+ export * from './p2wpkh.js';
21
+ export * from './p2wsh.js';
22
+
19
23
  export interface Payment {
20
24
  name?: string;
21
25
  network?: Network;
@@ -36,13 +40,19 @@ export interface Payment {
36
40
  scriptTree?: Taptree;
37
41
  witness?: Buffer[];
38
42
  }
43
+
39
44
  export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
45
+
40
46
  export type PaymentFunction = () => Payment;
47
+
41
48
  export interface PaymentOpts {
42
49
  validate?: boolean;
43
50
  allowIncomplete?: boolean;
44
51
  }
52
+
45
53
  export type StackElement = Buffer | number;
46
54
  export type Stack = StackElement[];
47
55
  export type StackFunction = () => Stack;
48
- export { embed, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh, p2tr };
56
+
57
+ // TODO
58
+ // witness commitment
@@ -0,0 +1,28 @@
1
+ export function prop(object: {}, name: string, f: () => any): void {
2
+ Object.defineProperty(object, name, {
3
+ configurable: true,
4
+ enumerable: true,
5
+ get(): any {
6
+ const _value = f.call(this);
7
+ this[name] = _value;
8
+ return _value;
9
+ },
10
+ set(_value: any): void {
11
+ Object.defineProperty(this, name, {
12
+ configurable: true,
13
+ enumerable: true,
14
+ value: _value,
15
+ writable: true,
16
+ });
17
+ },
18
+ });
19
+ }
20
+
21
+ export function value<T>(f: () => T): () => T {
22
+ let _value: T;
23
+ return (): T => {
24
+ if (_value !== undefined) return _value;
25
+ _value = f();
26
+ return _value;
27
+ };
28
+ }
@@ -0,0 +1,150 @@
1
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
2
+ import * as bscript from '../script.js';
3
+ import { isPoint, stacksEqual, typeforce as typef } from '../types.js';
4
+ import { Payment, PaymentOpts, Stack } from './index.js';
5
+ import * as lazy from './lazy.js';
6
+
7
+ const OPS = bscript.OPS;
8
+
9
+ const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
10
+
11
+ // input: OP_0 [signatures ...]
12
+ // output: m [pubKeys ...] n OP_CHECKMULTISIG
13
+ /**
14
+ * Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
15
+ * @param a - The payment object.
16
+ * @param opts - Optional payment options.
17
+ * @returns The created payment object.
18
+ * @throws {TypeError} If the provided data is not valid.
19
+ */
20
+ export function p2ms(a: Payment, opts?: PaymentOpts): Payment {
21
+ if (!a.input && !a.output && !(a.pubkeys && a.m !== undefined) && !a.signatures)
22
+ throw new TypeError('Not enough data');
23
+ opts = Object.assign({ validate: true }, opts || {});
24
+
25
+ function isAcceptableSignature(x: Buffer | number): boolean {
26
+ return (
27
+ bscript.isCanonicalScriptSignature(x as Buffer) ||
28
+ (opts!.allowIncomplete && (x as number) === OPS.OP_0) !== undefined
29
+ );
30
+ }
31
+
32
+ typef(
33
+ {
34
+ network: typef.maybe(typef.Object),
35
+ m: typef.maybe(typef.Number),
36
+ n: typef.maybe(typef.Number),
37
+ output: typef.maybe(typef.Buffer),
38
+ pubkeys: typef.maybe(typef.arrayOf(isPoint)),
39
+
40
+ signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)),
41
+ input: typef.maybe(typef.Buffer),
42
+ },
43
+ a,
44
+ );
45
+
46
+ const network = a.network || BITCOIN_NETWORK;
47
+ const o: Payment = { network };
48
+
49
+ let chunks: Stack = [];
50
+ let decoded = false;
51
+
52
+ function decode(output: Buffer | Stack): void {
53
+ if (decoded) return;
54
+ decoded = true;
55
+ chunks = bscript.decompile(output) as Stack;
56
+ o.m = (chunks[0] as number) - OP_INT_BASE;
57
+ o.n = (chunks[chunks.length - 2] as number) - OP_INT_BASE;
58
+ o.pubkeys = chunks.slice(1, -2) as Buffer[];
59
+ }
60
+
61
+ lazy.prop(o, 'output', () => {
62
+ if (!a.m) return;
63
+ if (!o.n) return;
64
+ if (!a.pubkeys) return;
65
+ return bscript.compile(
66
+ ([] as Stack).concat(
67
+ OP_INT_BASE + a.m,
68
+ a.pubkeys,
69
+ OP_INT_BASE + o.n,
70
+ OPS.OP_CHECKMULTISIG,
71
+ ),
72
+ );
73
+ });
74
+ lazy.prop(o, 'm', () => {
75
+ if (!o.output) return;
76
+ decode(o.output);
77
+ return o.m;
78
+ });
79
+ lazy.prop(o, 'n', () => {
80
+ if (!o.pubkeys) return;
81
+ return o.pubkeys.length;
82
+ });
83
+ lazy.prop(o, 'pubkeys', () => {
84
+ if (!a.output) return;
85
+ decode(a.output);
86
+ return o.pubkeys;
87
+ });
88
+ lazy.prop(o, 'signatures', () => {
89
+ if (!a.input) return;
90
+ return bscript.decompile(a.input)!.slice(1);
91
+ });
92
+ lazy.prop(o, 'input', () => {
93
+ if (!a.signatures) return;
94
+ return bscript.compile(([OPS.OP_0] as Stack).concat(a.signatures));
95
+ });
96
+ lazy.prop(o, 'witness', () => {
97
+ if (!o.input) return;
98
+ return [];
99
+ });
100
+ lazy.prop(o, 'name', () => {
101
+ if (!o.m || !o.n) return;
102
+ return `p2ms(${o.m} of ${o.n})`;
103
+ });
104
+
105
+ // extended validation
106
+ if (opts.validate) {
107
+ if (a.output) {
108
+ decode(a.output);
109
+ if (!typef.Number(chunks[0])) throw new TypeError('Output is invalid');
110
+ if (!typef.Number(chunks[chunks.length - 2])) throw new TypeError('Output is invalid');
111
+ if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG)
112
+ throw new TypeError('Output is invalid');
113
+
114
+ if (o.m! <= 0 || o.n! > 16 || o.m! > o.n! || o.n !== chunks.length - 3)
115
+ throw new TypeError('Output is invalid');
116
+ if (!o.pubkeys!.every((x) => isPoint(x))) throw new TypeError('Output is invalid');
117
+
118
+ if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');
119
+ if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch');
120
+ if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys!))
121
+ throw new TypeError('Pubkeys mismatch');
122
+ }
123
+
124
+ if (a.pubkeys) {
125
+ if (a.n !== undefined && a.n !== a.pubkeys.length)
126
+ throw new TypeError('Pubkey count mismatch');
127
+ o.n = a.pubkeys.length;
128
+
129
+ if (o.n < o.m!) throw new TypeError('Pubkey count cannot be less than m');
130
+ }
131
+
132
+ if (a.signatures) {
133
+ if (a.signatures.length < o.m!) throw new TypeError('Not enough signatures provided');
134
+ if (a.signatures.length > o.m!) throw new TypeError('Too many signatures provided');
135
+ }
136
+
137
+ if (a.input) {
138
+ if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid');
139
+ if (o.signatures!.length === 0 || !o.signatures!.every(isAcceptableSignature))
140
+ throw new TypeError('Input has invalid signature(s)');
141
+
142
+ if (a.signatures && !stacksEqual(a.signatures, o.signatures!))
143
+ throw new TypeError('Signature mismatch');
144
+ if (a.m !== undefined && a.m !== a.signatures!.length)
145
+ throw new TypeError('Signature count mismatch');
146
+ }
147
+ }
148
+
149
+ return Object.assign(o, a);
150
+ }
@@ -1,11 +1,11 @@
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');
1
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
2
+ import * as bscript from '../script.js';
3
+ import { isPoint, typeforce as typef } from '../types.js';
4
+ import { Payment, PaymentOpts, StackFunction } from './index.js';
5
+ import * as lazy from './lazy.js';
6
+
8
7
  const OPS = bscript.OPS;
8
+
9
9
  // input: {signature}
10
10
  // output: {pubKey} OP_CHECKSIG
11
11
  /**
@@ -16,27 +16,30 @@ const OPS = bscript.OPS;
16
16
  * @returns The P2PK payment object.
17
17
  * @throws {TypeError} If the required data is not provided or if the data is invalid.
18
18
  */
19
- function p2pk(a, opts) {
19
+ export function p2pk(a: Payment, opts?: PaymentOpts): Payment {
20
20
  if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
21
21
  throw new TypeError('Not enough data');
22
22
  opts = Object.assign({ validate: true }, opts || {});
23
- (0, types_1.typeforce)(
23
+
24
+ typef(
24
25
  {
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),
26
+ network: typef.maybe(typef.Object),
27
+ output: typef.maybe(typef.Buffer),
28
+ pubkey: typef.maybe(isPoint),
29
+
30
+ signature: typef.maybe(bscript.isCanonicalScriptSignature),
31
+ input: typef.maybe(typef.Buffer),
32
32
  },
33
33
  a,
34
34
  );
35
+
35
36
  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 };
37
+ return bscript.decompile(a.input!);
38
+ }) as StackFunction;
39
+
40
+ const network = a.network || BITCOIN_NETWORK;
41
+ const o: Payment = { name: 'p2pk', network };
42
+
40
43
  lazy.prop(o, 'output', () => {
41
44
  if (!a.pubkey) return;
42
45
  return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
@@ -47,7 +50,7 @@ function p2pk(a, opts) {
47
50
  });
48
51
  lazy.prop(o, 'signature', () => {
49
52
  if (!a.input) return;
50
- return _chunks()[0];
53
+ return _chunks()[0] as Buffer;
51
54
  });
52
55
  lazy.prop(o, 'input', () => {
53
56
  if (!a.signature) return;
@@ -57,26 +60,26 @@ function p2pk(a, opts) {
57
60
  if (!o.input) return;
58
61
  return [];
59
62
  });
63
+
60
64
  // extended validation
61
65
  if (opts.validate) {
62
66
  if (a.output) {
63
67
  if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
64
68
  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
+ if (!isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid');
70
+ if (a.pubkey && !a.pubkey.equals(o.pubkey!)) throw new TypeError('Pubkey mismatch');
69
71
  }
72
+
70
73
  if (a.signature) {
71
- if (a.input && !a.input.equals(o.input))
72
- throw new TypeError('Signature mismatch');
74
+ if (a.input && !a.input.equals(o.input!)) throw new TypeError('Signature mismatch');
73
75
  }
76
+
74
77
  if (a.input) {
75
78
  if (_chunks().length !== 1) throw new TypeError('Input is invalid');
76
- if (!bscript.isCanonicalScriptSignature(o.signature))
79
+ if (!bscript.isCanonicalScriptSignature(o.signature!))
77
80
  throw new TypeError('Input has invalid signature');
78
81
  }
79
82
  }
83
+
80
84
  return Object.assign(o, a);
81
85
  }
82
- exports.p2pk = p2pk;
@@ -1,13 +1,13 @@
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');
1
+ import * as bs58check from 'bs58check';
2
+ import * as bcrypto from '../crypto.js';
3
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
+ import * as bscript from '../script.js';
5
+ import { isPoint, typeforce as typef } from '../types.js';
6
+ import { Payment, PaymentOpts, StackFunction } from './index.js';
7
+ import * as lazy from './lazy.js';
8
+
10
9
  const OPS = bscript.OPS;
10
+
11
11
  // input: {signature} {pubkey}
12
12
  // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
13
13
  /**
@@ -18,46 +18,50 @@ const OPS = bscript.OPS;
18
18
  * @returns The P2PKH payment object.
19
19
  * @throws {TypeError} If the required data is not provided or if the data is invalid.
20
20
  */
21
- function p2pkh(a, opts) {
21
+ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
22
22
  if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input)
23
23
  throw new TypeError('Not enough data');
24
24
  opts = Object.assign({ validate: true }, opts || {});
25
- (0, types_1.typeforce)(
25
+
26
+ typef(
26
27
  {
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),
28
+ network: typef.maybe(typef.Object),
29
+ address: typef.maybe(typef.String),
30
+ hash: typef.maybe(typef.BufferN(20)),
31
+ output: typef.maybe(typef.BufferN(25)),
32
+
33
+ pubkey: typef.maybe(isPoint),
34
+ signature: typef.maybe(bscript.isCanonicalScriptSignature),
35
+ input: typef.maybe(typef.Buffer),
36
36
  },
37
37
  a,
38
38
  );
39
+
39
40
  const _address = lazy.value(() => {
40
- const payload = Buffer.from(bs58check.decode(a.address));
41
+ const payload = Buffer.from(bs58check.default.decode(a.address!));
41
42
  const version = payload.readUInt8(0);
42
43
  const hash = payload.slice(1);
43
44
  return { version, hash };
44
45
  });
45
46
  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 };
47
+ return bscript.decompile(a.input!);
48
+ }) as StackFunction;
49
+
50
+ const network = a.network || BITCOIN_NETWORK;
51
+ const o: Payment = { name: 'p2pkh', network };
52
+
50
53
  lazy.prop(o, 'address', () => {
51
54
  if (!o.hash) return;
55
+
52
56
  const payload = Buffer.allocUnsafe(21);
53
57
  payload.writeUInt8(network.pubKeyHash, 0);
54
58
  o.hash.copy(payload, 1);
55
- return bs58check.encode(payload);
59
+ return bs58check.default.encode(payload);
56
60
  });
57
61
  lazy.prop(o, 'hash', () => {
58
62
  if (a.output) return a.output.slice(3, 23);
59
63
  if (a.address) return _address().hash;
60
- if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
64
+ if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
61
65
  });
62
66
  lazy.prop(o, 'output', () => {
63
67
  if (!o.hash) return;
@@ -71,11 +75,11 @@ function p2pkh(a, opts) {
71
75
  });
72
76
  lazy.prop(o, 'pubkey', () => {
73
77
  if (!a.input) return;
74
- return _chunks()[1];
78
+ return _chunks()[1] as Buffer;
75
79
  });
76
80
  lazy.prop(o, 'signature', () => {
77
81
  if (!a.input) return;
78
- return _chunks()[0];
82
+ return _chunks()[0] as Buffer;
79
83
  });
80
84
  lazy.prop(o, 'input', () => {
81
85
  if (!a.pubkey) return;
@@ -86,21 +90,22 @@ function p2pkh(a, opts) {
86
90
  if (!o.input) return;
87
91
  return [];
88
92
  });
93
+
89
94
  // extended validation
90
95
  if (opts.validate) {
91
- let hash = Buffer.from([]);
96
+ let hash: Buffer = Buffer.from([]);
92
97
  if (a.address) {
93
98
  if (_address().version !== network.pubKeyHash)
94
99
  throw new TypeError('Invalid version or Network mismatch');
95
- if (_address().hash.length !== 20)
96
- throw new TypeError('Invalid address');
100
+ if (_address().hash.length !== 20) throw new TypeError('Invalid address');
97
101
  hash = _address().hash;
98
102
  }
103
+
99
104
  if (a.hash) {
100
- if (hash.length > 0 && !hash.equals(a.hash))
101
- throw new TypeError('Hash mismatch');
105
+ if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
102
106
  else hash = a.hash;
103
107
  }
108
+
104
109
  if (a.output) {
105
110
  if (
106
111
  a.output.length !== 25 ||
@@ -111,33 +116,34 @@ function p2pkh(a, opts) {
111
116
  a.output[24] !== OPS.OP_CHECKSIG
112
117
  )
113
118
  throw new TypeError('Output is invalid');
119
+
114
120
  const hash2 = a.output.slice(3, 23);
115
- if (hash.length > 0 && !hash.equals(hash2))
116
- throw new TypeError('Hash mismatch');
121
+ if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
117
122
  else hash = hash2;
118
123
  }
124
+
119
125
  if (a.pubkey) {
120
126
  const pkh = bcrypto.hash160(a.pubkey);
121
- if (hash.length > 0 && !hash.equals(pkh))
122
- throw new TypeError('Hash mismatch');
127
+ if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
123
128
  else hash = pkh;
124
129
  }
130
+
125
131
  if (a.input) {
126
132
  const chunks = _chunks();
127
133
  if (chunks.length !== 2) throw new TypeError('Input is invalid');
128
- if (!bscript.isCanonicalScriptSignature(chunks[0]))
134
+ if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
129
135
  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]))
136
+ if (!isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey');
137
+
138
+ if (a.signature && !a.signature.equals(chunks[0] as Buffer))
133
139
  throw new TypeError('Signature mismatch');
134
- if (a.pubkey && !a.pubkey.equals(chunks[1]))
140
+ if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
135
141
  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');
142
+
143
+ const pkh = bcrypto.hash160(chunks[1] as Buffer);
144
+ if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
139
145
  }
140
146
  }
147
+
141
148
  return Object.assign(o, a);
142
149
  }
143
- exports.p2pkh = p2pkh;