@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.
- package/browser/address.d.ts +1 -0
- package/browser/index.js +1 -1
- package/browser/payments/index.d.ts +4 -0
- package/browser/payments/p2op.d.ts +2 -0
- package/browser/psbt/psbtutils.d.ts +1 -0
- package/build/address.d.ts +1 -0
- package/build/address.js +56 -8
- package/build/networks.js +6 -6
- package/build/payments/index.d.ts +4 -0
- package/build/payments/index.js +1 -0
- package/build/payments/p2op.d.ts +2 -0
- package/build/payments/p2op.js +108 -0
- package/build/psbt/psbtutils.d.ts +1 -0
- package/build/psbt/psbtutils.js +2 -0
- package/package.json +1 -1
- package/src/address.ts +68 -9
- package/src/block.ts +233 -233
- package/src/bufferutils.ts +188 -188
- package/src/networks.ts +6 -6
- package/src/payments/index.ts +68 -62
- package/src/payments/p2op.ts +136 -0
- package/src/psbt/psbtutils.ts +2 -0
- package/src/psbt.ts +2187 -2187
- package/test/address.spec.ts +155 -155
- package/test/bitcoin.core.spec.ts +212 -212
- package/test/block.spec.ts +171 -171
- package/test/bufferutils.spec.ts +450 -450
- package/test/crypto.spec.ts +49 -49
- package/test/integration/addresses.spec.ts +142 -142
- package/test/integration/bip32.spec.ts +130 -130
- package/test/integration/blocks.spec.ts +28 -28
- package/test/integration/cltv.spec.ts +241 -241
- package/test/integration/csv.spec.ts +452 -452
- package/test/integration/payments.spec.ts +110 -110
- package/test/integration/taproot.spec.ts +663 -663
- package/test/integration/transactions.spec.ts +668 -668
- package/test/payments.spec.ts +114 -114
- package/test/payments.utils.ts +165 -165
- package/test/psbt.spec.ts +1285 -1285
- package/test/script.spec.ts +186 -186
- package/test/script_number.spec.ts +26 -26
- package/test/script_signature.spec.ts +66 -66
- package/test/transaction.spec.ts +337 -337
- package/test/ts-node-register.js +7 -7
- 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
|
+
}
|
package/src/psbt/psbtutils.ts
CHANGED
|
@@ -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.
|