@btc-vision/bitcoin 6.4.1 → 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.
@@ -10,6 +10,7 @@ export * from './p2sh.js';
10
10
  export * from './p2tr.js';
11
11
  export * from './p2wpkh.js';
12
12
  export * from './p2wsh.js';
13
+ export * from './p2op.js';
13
14
  export interface Payment {
14
15
  name?: string;
15
16
  network?: Network;
@@ -31,6 +32,9 @@ export interface Payment {
31
32
  witness?: Buffer[];
32
33
  useHybrid?: boolean;
33
34
  useUncompressed?: boolean;
35
+ program?: Buffer;
36
+ deploymentVersion?: number;
37
+ hash160?: Buffer;
34
38
  }
35
39
  export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
36
40
  export type PaymentFunction = () => Payment;
@@ -0,0 +1,2 @@
1
+ import { Payment, PaymentOpts } from './index.js';
2
+ export declare function p2op(a: Payment, opts?: PaymentOpts): Payment;
@@ -6,6 +6,7 @@ export declare const isP2WPKH: (script: Buffer) => boolean;
6
6
  export declare const isP2WSHScript: (script: Buffer) => boolean;
7
7
  export declare const isP2SHScript: (script: Buffer) => boolean;
8
8
  export declare const isP2TR: (script: Buffer) => boolean;
9
+ export declare const isP2OP: (script: Buffer) => boolean;
9
10
  export declare function witnessStackToScriptWitness(witness: Buffer[]): Buffer;
10
11
  export interface UncompressedPublicKey {
11
12
  hybrid: Buffer;
package/build/address.js CHANGED
@@ -179,11 +179,11 @@ export function toOutputScript(address, network) {
179
179
  else if (decodeBech32.version === FUTURE_OPNET_VERSION) {
180
180
  if (!network.bech32Opnet)
181
181
  throw new Error(address + ' has an invalid prefix');
182
- if (decodeBech32.data.length < FUTURE_SEGWIT_MIN_SIZE ||
183
- decodeBech32.data.length > FUTURE_SEGWIT_MAX_SIZE) {
184
- throw new Error('Invalid program length for opnet address');
185
- }
186
- return bscript.compile([opcodes.OP_16, decodeBech32.data]);
182
+ return payments.p2op({
183
+ program: decodeBech32.data,
184
+ deploymentVersion: decodeBech32.version,
185
+ network,
186
+ }).output;
187
187
  }
188
188
  else if (decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
189
189
  decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
@@ -10,6 +10,7 @@ export * from './p2sh.js';
10
10
  export * from './p2tr.js';
11
11
  export * from './p2wpkh.js';
12
12
  export * from './p2wsh.js';
13
+ export * from './p2op.js';
13
14
  export interface Payment {
14
15
  name?: string;
15
16
  network?: Network;
@@ -31,6 +32,9 @@ export interface Payment {
31
32
  witness?: Buffer[];
32
33
  useHybrid?: boolean;
33
34
  useUncompressed?: boolean;
35
+ program?: Buffer;
36
+ deploymentVersion?: number;
37
+ hash160?: Buffer;
34
38
  }
35
39
  export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
36
40
  export type PaymentFunction = () => Payment;
@@ -8,3 +8,4 @@ export * from './p2sh.js';
8
8
  export * from './p2tr.js';
9
9
  export * from './p2wpkh.js';
10
10
  export * from './p2wsh.js';
11
+ export * from './p2op.js';
@@ -0,0 +1,2 @@
1
+ import { Payment, PaymentOpts } from './index.js';
2
+ export declare function p2op(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,108 @@
1
+ import { bech32m } from 'bech32';
2
+ import { Buffer as NBuffer } from 'buffer';
3
+ import { fromBech32 } from '../address.js';
4
+ import { bitcoin as BITCOIN_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
+ const OPS = bscript.OPS;
9
+ const P2OP_WITNESS_VERSION = 0x10;
10
+ const MIN_SIZE = 2;
11
+ const MAX_SIZE = 40;
12
+ export function p2op(a, opts) {
13
+ if (!a.address && !a.output && !a.program)
14
+ throw new TypeError('Not enough data for p2op');
15
+ opts = Object.assign({ validate: true }, opts || {});
16
+ typef({
17
+ address: typef.maybe(typef.String),
18
+ output: typef.maybe(typef.Buffer),
19
+ program: typef.maybe(typef.Buffer),
20
+ network: typef.maybe(typef.Object),
21
+ deploymentVersion: typef.maybe(typef.Number),
22
+ hash160: typef.maybe(typef.BufferN(20)),
23
+ }, a);
24
+ const _address = lazy.value(() => fromBech32(a.address));
25
+ const network = a.network || BITCOIN_NETWORK;
26
+ const o = { name: 'p2op', network };
27
+ lazy.prop(o, 'program', () => {
28
+ if (a.program)
29
+ return a.program;
30
+ if (a.output) {
31
+ if (a.output[0] !== OPS.OP_16)
32
+ throw new TypeError('Invalid P2OP script');
33
+ let pushPos = 1, progLen;
34
+ if (a.output[1] < 0x4c) {
35
+ progLen = a.output[1];
36
+ pushPos = 2;
37
+ }
38
+ else if (a.output[1] === 0x4c) {
39
+ progLen = a.output[2];
40
+ pushPos = 3;
41
+ }
42
+ else {
43
+ throw new TypeError('Unsupported push opcode in P2OP script');
44
+ }
45
+ return a.output.slice(pushPos, pushPos + progLen);
46
+ }
47
+ if (a.address) {
48
+ const dec = _address();
49
+ return dec.data;
50
+ }
51
+ });
52
+ lazy.prop(o, 'deploymentVersion', () => {
53
+ if (!o.program)
54
+ return;
55
+ return o.program[0];
56
+ });
57
+ lazy.prop(o, 'hash160', () => {
58
+ if (!o.program)
59
+ return;
60
+ return o.program.slice(1);
61
+ });
62
+ lazy.prop(o, 'output', () => {
63
+ if (!o.program)
64
+ return;
65
+ return bscript.compile([OPS.OP_16, o.program]);
66
+ });
67
+ lazy.prop(o, 'address', () => {
68
+ if (!o.program)
69
+ return;
70
+ if (!network.bech32Opnet) {
71
+ throw new TypeError('Network does not support opnet');
72
+ }
73
+ const words = bech32m.toWords(o.program);
74
+ words.unshift(P2OP_WITNESS_VERSION);
75
+ return bech32m.encode(network.bech32Opnet, words);
76
+ });
77
+ if (opts.validate) {
78
+ let prog = NBuffer.alloc(0);
79
+ if (a.address) {
80
+ const dec = _address();
81
+ if (network.bech32Opnet !== dec.prefix)
82
+ throw new TypeError('Invalid prefix or network mismatch');
83
+ if (dec.version !== P2OP_WITNESS_VERSION)
84
+ throw new TypeError('Invalid witness version for p2op');
85
+ if (dec.data.length < MIN_SIZE || dec.data.length > MAX_SIZE)
86
+ throw new TypeError('Invalid witness program length');
87
+ prog = dec.data;
88
+ }
89
+ if (a.program) {
90
+ if (prog.length && !prog.equals(a.program))
91
+ throw new TypeError('Program mismatch');
92
+ prog = a.program;
93
+ }
94
+ if (a.output) {
95
+ const outProg = o.program;
96
+ if (prog.length && !prog.equals(outProg))
97
+ throw new TypeError('Program mismatch (output vs other source)');
98
+ prog = outProg;
99
+ }
100
+ if (prog.length < MIN_SIZE || prog.length > MAX_SIZE)
101
+ throw new TypeError('Witness program must be 2–40 bytes');
102
+ if (a.deploymentVersion !== undefined && a.deploymentVersion !== prog[0])
103
+ throw new TypeError('deploymentVersion mismatch');
104
+ if (a.hash160 && !a.hash160.equals(prog.slice(1)))
105
+ throw new TypeError('hash160 mismatch');
106
+ }
107
+ return Object.assign(o, a);
108
+ }
@@ -6,6 +6,7 @@ export declare const isP2WPKH: (script: Buffer) => boolean;
6
6
  export declare const isP2WSHScript: (script: Buffer) => boolean;
7
7
  export declare const isP2SHScript: (script: Buffer) => boolean;
8
8
  export declare const isP2TR: (script: Buffer) => boolean;
9
+ export declare const isP2OP: (script: Buffer) => boolean;
9
10
  export declare function witnessStackToScriptWitness(witness: Buffer[]): Buffer;
10
11
  export interface UncompressedPublicKey {
11
12
  hybrid: Buffer;
@@ -11,6 +11,7 @@ import { p2wsh } from '../payments/p2wsh.js';
11
11
  import * as bscript from '../script.js';
12
12
  import { Transaction } from '../transaction.js';
13
13
  import { toXOnly } from './bip371.js';
14
+ import { p2op } from '../payments/p2op.js';
14
15
  function isPaymentFactory(payment) {
15
16
  return (script) => {
16
17
  try {
@@ -29,6 +30,7 @@ export const isP2WPKH = isPaymentFactory(p2wpkh);
29
30
  export const isP2WSHScript = isPaymentFactory(p2wsh);
30
31
  export const isP2SHScript = isPaymentFactory(p2sh);
31
32
  export const isP2TR = isPaymentFactory(p2tr);
33
+ export const isP2OP = isPaymentFactory(p2op);
32
34
  export function witnessStackToScriptWitness(witness) {
33
35
  let buffer = Buffer.allocUnsafe(0);
34
36
  function writeSlice(slice) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/bitcoin",
3
3
  "type": "module",
4
- "version": "6.4.1",
4
+ "version": "6.4.2",
5
5
  "description": "Client-side Bitcoin JavaScript library",
6
6
  "engines": {
7
7
  "node": ">=16.0.0"
package/src/address.ts CHANGED
@@ -257,14 +257,11 @@ export function toOutputScript(address: string, network?: Network): Buffer {
257
257
  return payments.p2tr({ pubkey: decodeBech32.data }).output as Buffer;
258
258
  } else if (decodeBech32.version === FUTURE_OPNET_VERSION) {
259
259
  if (!network.bech32Opnet) throw new Error(address + ' has an invalid prefix');
260
- if (
261
- decodeBech32.data.length < FUTURE_SEGWIT_MIN_SIZE ||
262
- decodeBech32.data.length > FUTURE_SEGWIT_MAX_SIZE
263
- ) {
264
- throw new Error('Invalid program length for opnet address');
265
- }
266
-
267
- return bscript.compile([opcodes.OP_16, decodeBech32.data]);
260
+ return payments.p2op({
261
+ program: decodeBech32.data,
262
+ deploymentVersion: decodeBech32.version,
263
+ network,
264
+ }).output as Buffer;
268
265
  } else if (
269
266
  decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
270
267
  decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
@@ -1,62 +1,68 @@
1
- /**
2
- * Represents a payment object, which is used to create a payment.
3
- *
4
- * Supports P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
5
- *
6
- * @packageDocumentation
7
- */
8
-
9
- import { Network } from '../networks';
10
- import { Taptree } from '../types';
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
-
23
- export interface Payment {
24
- name?: string;
25
- network?: Network;
26
- output?: Buffer;
27
- data?: Buffer[];
28
- m?: number;
29
- n?: number;
30
- pubkeys?: Buffer[];
31
- input?: Buffer;
32
- signatures?: Buffer[];
33
- internalPubkey?: Buffer;
34
- pubkey?: Buffer;
35
- signature?: Buffer;
36
- address?: string;
37
- hash?: Buffer;
38
- redeem?: Payment;
39
- redeemVersion?: number;
40
- scriptTree?: Taptree;
41
- witness?: Buffer[];
42
-
43
- // NEW FIELDS
44
- useHybrid?: boolean;
45
- useUncompressed?: boolean;
46
- }
47
-
48
- export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
49
-
50
- export type PaymentFunction = () => Payment;
51
-
52
- export interface PaymentOpts {
53
- validate?: boolean;
54
- allowIncomplete?: boolean;
55
- }
56
-
57
- export type StackElement = Buffer | number;
58
- export type Stack = StackElement[];
59
- export type StackFunction = () => Stack;
60
-
61
- // TODO
62
- // witness commitment
1
+ /**
2
+ * Represents a payment object, which is used to create a payment.
3
+ *
4
+ * Supports P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+
9
+ import { Network } from '../networks';
10
+ import { Taptree } from '../types';
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
+ export * from './p2op.js';
23
+
24
+ export interface Payment {
25
+ name?: string;
26
+ network?: Network;
27
+ output?: Buffer;
28
+ data?: Buffer[];
29
+ m?: number;
30
+ n?: number;
31
+ pubkeys?: Buffer[];
32
+ input?: Buffer;
33
+ signatures?: Buffer[];
34
+ internalPubkey?: Buffer;
35
+ pubkey?: Buffer;
36
+ signature?: Buffer;
37
+ address?: string;
38
+ hash?: Buffer;
39
+ redeem?: Payment;
40
+ redeemVersion?: number;
41
+ scriptTree?: Taptree;
42
+ witness?: Buffer[];
43
+
44
+ // NEW FIELDS
45
+ useHybrid?: boolean;
46
+ useUncompressed?: boolean;
47
+
48
+ // p2op
49
+ program?: Buffer;
50
+ deploymentVersion?: number;
51
+ hash160?: Buffer;
52
+ }
53
+
54
+ export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
55
+
56
+ export type PaymentFunction = () => Payment;
57
+
58
+ export interface PaymentOpts {
59
+ validate?: boolean;
60
+ allowIncomplete?: boolean;
61
+ }
62
+
63
+ export type StackElement = Buffer | number;
64
+ export type Stack = StackElement[];
65
+ export type StackFunction = () => Stack;
66
+
67
+ // TODO
68
+ // witness commitment
@@ -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
+ }