@btc-vision/bitcoin 6.4.0 → 6.4.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 (45) hide show
  1. package/browser/address.d.ts +1 -0
  2. package/browser/index.js +1 -1
  3. package/browser/payments/index.d.ts +4 -0
  4. package/browser/payments/p2op.d.ts +2 -0
  5. package/browser/psbt/psbtutils.d.ts +1 -0
  6. package/build/address.d.ts +1 -0
  7. package/build/address.js +56 -8
  8. package/build/networks.js +6 -6
  9. package/build/payments/index.d.ts +4 -0
  10. package/build/payments/index.js +1 -0
  11. package/build/payments/p2op.d.ts +2 -0
  12. package/build/payments/p2op.js +108 -0
  13. package/build/psbt/psbtutils.d.ts +1 -0
  14. package/build/psbt/psbtutils.js +2 -0
  15. package/package.json +1 -1
  16. package/src/address.ts +68 -9
  17. package/src/block.ts +233 -233
  18. package/src/bufferutils.ts +188 -188
  19. package/src/networks.ts +6 -6
  20. package/src/payments/index.ts +68 -62
  21. package/src/payments/p2op.ts +136 -0
  22. package/src/psbt/psbtutils.ts +2 -0
  23. package/src/psbt.ts +2187 -2187
  24. package/test/address.spec.ts +155 -155
  25. package/test/bitcoin.core.spec.ts +212 -212
  26. package/test/block.spec.ts +171 -171
  27. package/test/bufferutils.spec.ts +450 -450
  28. package/test/crypto.spec.ts +49 -49
  29. package/test/integration/addresses.spec.ts +142 -142
  30. package/test/integration/bip32.spec.ts +130 -130
  31. package/test/integration/blocks.spec.ts +28 -28
  32. package/test/integration/cltv.spec.ts +241 -241
  33. package/test/integration/csv.spec.ts +452 -452
  34. package/test/integration/payments.spec.ts +110 -110
  35. package/test/integration/taproot.spec.ts +663 -663
  36. package/test/integration/transactions.spec.ts +668 -668
  37. package/test/payments.spec.ts +114 -114
  38. package/test/payments.utils.ts +165 -165
  39. package/test/psbt.spec.ts +1285 -1285
  40. package/test/script.spec.ts +186 -186
  41. package/test/script_number.spec.ts +26 -26
  42. package/test/script_signature.spec.ts +66 -66
  43. package/test/transaction.spec.ts +337 -337
  44. package/test/ts-node-register.js +7 -7
  45. package/test/types.spec.ts +53 -53
@@ -0,0 +1,136 @@
1
+ import { bech32m } from 'bech32';
2
+ import { Buffer as NBuffer } from 'buffer';
3
+ import { fromBech32 } from '../address.js';
4
+ import { bitcoin as BITCOIN_NETWORK, Network } from '../networks.js';
5
+ import * as bscript from '../script.js';
6
+ import { typeforce as typef } from '../types.js';
7
+ import * as lazy from './lazy.js';
8
+ import { Payment, PaymentOpts } from './index.js';
9
+
10
+ const OPS = bscript.OPS;
11
+ const P2OP_WITNESS_VERSION = 0x10;
12
+ const MIN_SIZE = 2;
13
+ const MAX_SIZE = 40;
14
+
15
+ /**
16
+ * Pay-to-OPNet (P2OP) decoder / encoder.
17
+ *
18
+ * ▪ witness program = <deploymentVersion:uint8><hash160:20-bytes|...>
19
+ * ▪ scriptPubKey = OP_16 <program>
20
+ * ▪ address HRP = network.bech32Opnet, encoded with Bech32m
21
+ *
22
+ * Accepts any combination of { address, output, program } and returns a
23
+ * fully lazy payment object, mirroring the style of `p2tr`.
24
+ */
25
+ export function p2op(a: Payment, opts?: PaymentOpts): Payment {
26
+ if (!a.address && !a.output && !a.program) throw new TypeError('Not enough data for p2op');
27
+
28
+ opts = Object.assign({ validate: true }, opts || {});
29
+
30
+ typef(
31
+ {
32
+ address: typef.maybe(typef.String),
33
+ output: typef.maybe(typef.Buffer),
34
+ program: typef.maybe(typef.Buffer),
35
+ network: typef.maybe(typef.Object),
36
+ deploymentVersion: typef.maybe(typef.Number),
37
+ hash160: typef.maybe(typef.BufferN(20)),
38
+ },
39
+ a,
40
+ );
41
+
42
+ const _address = lazy.value(() => fromBech32(a.address!));
43
+
44
+ const network: Network = a.network || BITCOIN_NETWORK;
45
+ const o: Payment = { name: 'p2op', network };
46
+
47
+ lazy.prop(o, 'program', () => {
48
+ if (a.program) return a.program;
49
+
50
+ if (a.output) {
51
+ if (a.output[0] !== OPS.OP_16) throw new TypeError('Invalid P2OP script');
52
+ let pushPos = 1,
53
+ progLen: number;
54
+ if (a.output[1] < 0x4c) {
55
+ progLen = a.output[1];
56
+ pushPos = 2;
57
+ } else if (a.output[1] === 0x4c) {
58
+ progLen = a.output[2];
59
+ pushPos = 3;
60
+ } else {
61
+ throw new TypeError('Unsupported push opcode in P2OP script');
62
+ }
63
+ return a.output.slice(pushPos, pushPos + progLen);
64
+ }
65
+
66
+ if (a.address) {
67
+ const dec = _address();
68
+ return dec.data;
69
+ }
70
+ });
71
+
72
+ lazy.prop(o, 'deploymentVersion', () => {
73
+ if (!o.program) return;
74
+ return o.program[0];
75
+ });
76
+
77
+ lazy.prop(o, 'hash160', () => {
78
+ if (!o.program) return;
79
+ return o.program.slice(1);
80
+ });
81
+
82
+ lazy.prop(o, 'output', () => {
83
+ if (!o.program) return;
84
+ return bscript.compile([OPS.OP_16, o.program]);
85
+ });
86
+
87
+ lazy.prop(o, 'address', () => {
88
+ if (!o.program) return;
89
+ if (!network.bech32Opnet) {
90
+ throw new TypeError('Network does not support opnet');
91
+ }
92
+
93
+ const words = bech32m.toWords(o.program);
94
+ words.unshift(P2OP_WITNESS_VERSION);
95
+
96
+ return bech32m.encode(network.bech32Opnet, words);
97
+ });
98
+
99
+ // extended validation
100
+ if (opts.validate) {
101
+ let prog: Buffer = NBuffer.alloc(0);
102
+
103
+ if (a.address) {
104
+ const dec = _address();
105
+ if (network.bech32Opnet !== dec.prefix)
106
+ throw new TypeError('Invalid prefix or network mismatch');
107
+ if (dec.version !== P2OP_WITNESS_VERSION)
108
+ throw new TypeError('Invalid witness version for p2op');
109
+ if (dec.data.length < MIN_SIZE || dec.data.length > MAX_SIZE)
110
+ throw new TypeError('Invalid witness program length');
111
+ prog = dec.data;
112
+ }
113
+
114
+ if (a.program) {
115
+ if (prog.length && !prog.equals(a.program)) throw new TypeError('Program mismatch');
116
+ prog = a.program;
117
+ }
118
+
119
+ if (a.output) {
120
+ const outProg = o.program!;
121
+ if (prog.length && !prog.equals(outProg))
122
+ throw new TypeError('Program mismatch (output vs other source)');
123
+ prog = outProg;
124
+ }
125
+
126
+ if (prog.length < MIN_SIZE || prog.length > MAX_SIZE)
127
+ throw new TypeError('Witness program must be 2–40 bytes');
128
+
129
+ if (a.deploymentVersion !== undefined && a.deploymentVersion !== prog[0])
130
+ throw new TypeError('deploymentVersion mismatch');
131
+
132
+ if (a.hash160 && !a.hash160.equals(prog.slice(1))) throw new TypeError('hash160 mismatch');
133
+ }
134
+
135
+ return Object.assign(o, a);
136
+ }
@@ -12,6 +12,7 @@ import { p2wsh } from '../payments/p2wsh.js';
12
12
  import * as bscript from '../script.js';
13
13
  import { Transaction } from '../transaction.js';
14
14
  import { toXOnly } from './bip371.js';
15
+ import { p2op } from '../payments/p2op.js';
15
16
 
16
17
  function isPaymentFactory(payment: any): (script: Buffer) => boolean {
17
18
  return (script: Buffer): boolean => {
@@ -31,6 +32,7 @@ export const isP2WPKH = isPaymentFactory(p2wpkh);
31
32
  export const isP2WSHScript = isPaymentFactory(p2wsh);
32
33
  export const isP2SHScript = isPaymentFactory(p2sh);
33
34
  export const isP2TR = isPaymentFactory(p2tr);
35
+ export const isP2OP = isPaymentFactory(p2op);
34
36
 
35
37
  /**
36
38
  * Converts a witness stack to a script witness.