@btc-vision/bitcoin 6.4.0 → 6.4.1

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.
@@ -8,6 +8,7 @@ export interface Bech32Result {
8
8
  prefix: string;
9
9
  data: Buffer;
10
10
  }
11
+ export declare function toFutureOPNetAddress(output: Buffer, network: Network): string;
11
12
  export declare function fromBase58Check(address: string): Base58CheckResult;
12
13
  export declare function fromBech32(address: string): Bech32Result;
13
14
  export declare function toBase58Check(hash: Buffer, version: number): string;
package/build/address.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import { bech32, bech32m } from 'bech32';
2
2
  import * as bs58check from 'bs58check';
3
- import { payments } from './index.js';
3
+ import { opcodes, payments } from './index.js';
4
4
  import * as networks from './networks.js';
5
5
  import * as bscript from './script.js';
6
6
  import { Hash160bit, tuple, typeforce, UInt8 } from './types.js';
7
7
  const FUTURE_SEGWIT_MAX_SIZE = 40;
8
8
  const FUTURE_SEGWIT_MIN_SIZE = 2;
9
- const FUTURE_SEGWIT_MAX_VERSION = 16;
9
+ const FUTURE_SEGWIT_MAX_VERSION = 15;
10
+ const FUTURE_MAX_VERSION = 16;
10
11
  const FUTURE_OPNET_VERSION = 16;
11
12
  const FUTURE_SEGWIT_MIN_VERSION = 2;
12
13
  const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
@@ -14,15 +15,49 @@ const FUTURE_SEGWIT_VERSION_WARNING = 'WARNING: Sending to a future segwit versi
14
15
  'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
15
16
  'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
16
17
  'then decide when it is safe to use which version of segwit.';
18
+ export function toFutureOPNetAddress(output, network) {
19
+ if (!Buffer.isBuffer(output))
20
+ throw new TypeError('output must be a Buffer');
21
+ if (!network.bech32Opnet)
22
+ throw new Error('Network does not support opnet');
23
+ const opcode = output[0];
24
+ let pushPos = 1, progLen;
25
+ if (output[1] < 0x4c) {
26
+ progLen = output[1];
27
+ pushPos = 2;
28
+ }
29
+ else if (output[1] === 0x4c) {
30
+ progLen = output[2];
31
+ pushPos = 3;
32
+ }
33
+ else {
34
+ throw new TypeError('Unsupported push opcode in script');
35
+ }
36
+ const program = output.subarray(pushPos, pushPos + progLen);
37
+ if (program.length < FUTURE_SEGWIT_MIN_SIZE || program.length > FUTURE_SEGWIT_MAX_SIZE)
38
+ throw new TypeError('Invalid program length for segwit address');
39
+ const version = opcode === opcodes.OP_0
40
+ ? 0
41
+ : opcode >= opcodes.OP_1 && opcode <= opcodes.OP_16
42
+ ? opcode - (opcodes.OP_1 - 1)
43
+ : -1;
44
+ if (version < FUTURE_SEGWIT_MAX_VERSION || version > FUTURE_MAX_VERSION)
45
+ throw new TypeError(`Invalid segwit version ${version}`);
46
+ const words = [version, ...bech32m.toWords(program)];
47
+ return bech32m.encode(network.bech32Opnet, words);
48
+ }
17
49
  function _toFutureSegwitAddress(output, network) {
18
- const data = output.slice(2);
19
- if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE)
50
+ const data = output.subarray(2);
51
+ if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE) {
20
52
  throw new TypeError('Invalid program length for segwit address');
53
+ }
21
54
  const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
22
- if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION)
55
+ if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION) {
23
56
  throw new TypeError('Invalid version for segwit address');
24
- if (output[1] !== data.length)
25
- throw new TypeError('Invalid script for segwit address');
57
+ }
58
+ if (output[1] !== data.length) {
59
+ throw new TypeError(`Invalid script for segwit address ${output[1]} !== ${data.length}`);
60
+ }
26
61
  return toBech32(data, version, network.bech32, network.bech32Opnet);
27
62
  }
28
63
  export function fromBase58Check(address) {
@@ -32,7 +67,7 @@ export function fromBase58Check(address) {
32
67
  if (payload.length > 21)
33
68
  throw new TypeError(address + ' is too long');
34
69
  const version = payload.readUInt8(0);
35
- const hash = payload.slice(1);
70
+ const hash = payload.subarray(1);
36
71
  return { version, hash };
37
72
  }
38
73
  export function fromBech32(address) {
@@ -97,6 +132,10 @@ export function fromOutputScript(output, network) {
97
132
  return payments.p2tr({ output, network }).address;
98
133
  }
99
134
  catch (e) { }
135
+ try {
136
+ return toFutureOPNetAddress(output, network);
137
+ }
138
+ catch (e) { }
100
139
  try {
101
140
  return _toFutureSegwitAddress(output, network);
102
141
  }
@@ -137,6 +176,15 @@ export function toOutputScript(address, network) {
137
176
  if (decodeBech32.data.length === 32)
138
177
  return payments.p2tr({ pubkey: decodeBech32.data }).output;
139
178
  }
179
+ else if (decodeBech32.version === FUTURE_OPNET_VERSION) {
180
+ if (!network.bech32Opnet)
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]);
187
+ }
140
188
  else if (decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
141
189
  decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
142
190
  decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
package/build/networks.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export const bitcoin = {
2
2
  messagePrefix: '\x18Bitcoin Signed Message:\n',
3
3
  bech32: 'bc',
4
- bech32Opnet: 'opnet',
4
+ bech32Opnet: 'op',
5
5
  bip32: {
6
6
  public: 0x0488b21e,
7
7
  private: 0x0488ade4,
@@ -13,7 +13,7 @@ export const bitcoin = {
13
13
  export const regtest = {
14
14
  messagePrefix: '\x18Bitcoin Signed Message:\n',
15
15
  bech32: 'bcrt',
16
- bech32Opnet: 'opreg',
16
+ bech32Opnet: 'opr',
17
17
  bip32: {
18
18
  public: 0x043587cf,
19
19
  private: 0x04358394,
@@ -25,7 +25,7 @@ export const regtest = {
25
25
  export const testnet = {
26
26
  messagePrefix: '\x18Bitcoin Signed Message:\n',
27
27
  bech32: 'tb',
28
- bech32Opnet: 'optest',
28
+ bech32Opnet: 'opt',
29
29
  bip32: {
30
30
  public: 0x043587cf,
31
31
  private: 0x04358394,
@@ -61,7 +61,7 @@ export const dogecoinTestnet = {
61
61
  export const litecoin = {
62
62
  messagePrefix: '\x19Litecoin Signed Message:\n',
63
63
  bech32: 'ltc',
64
- bech32Opnet: 'opltc',
64
+ bech32Opnet: 'opl',
65
65
  bip32: {
66
66
  public: 0x019da462,
67
67
  private: 0x019d9cfe,
@@ -73,7 +73,7 @@ export const litecoin = {
73
73
  export const litecoinTestnet = {
74
74
  messagePrefix: '\x19Litecoin Signed Message:\n',
75
75
  bech32: 'tltc',
76
- bech32Opnet: 'opltct',
76
+ bech32Opnet: 'oplt',
77
77
  bip32: {
78
78
  public: 0x0436ef7d,
79
79
  private: 0x0436f6e1,
@@ -97,7 +97,7 @@ export const bitcoinCash = {
97
97
  export const bitcoinCashTestnet = {
98
98
  messagePrefix: '\x18Bitcoin Signed Message:\n',
99
99
  bech32: 'bchtest',
100
- bech32Opnet: 'opbchtest',
100
+ bech32Opnet: 'opbcht',
101
101
  bip32: {
102
102
  public: 0x043587cf,
103
103
  private: 0x04358394,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/bitcoin",
3
3
  "type": "module",
4
- "version": "6.4.0",
4
+ "version": "6.4.1",
5
5
  "description": "Client-side Bitcoin JavaScript library",
6
6
  "engines": {
7
7
  "node": ">=16.0.0"
package/src/address.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
  import { bech32, bech32m } from 'bech32';
11
11
  import * as bs58check from 'bs58check';
12
- import { payments } from './index.js';
12
+ import { opcodes, payments } from './index.js';
13
13
  import * as networks from './networks.js';
14
14
  import { Network } from './networks.js';
15
15
  import * as bscript from './script.js';
@@ -35,7 +35,8 @@ export interface Bech32Result {
35
35
 
36
36
  const FUTURE_SEGWIT_MAX_SIZE: number = 40;
37
37
  const FUTURE_SEGWIT_MIN_SIZE: number = 2;
38
- const FUTURE_SEGWIT_MAX_VERSION: number = 16;
38
+ const FUTURE_SEGWIT_MAX_VERSION: number = 15;
39
+ const FUTURE_MAX_VERSION: number = 16;
39
40
  const FUTURE_OPNET_VERSION: number = 16;
40
41
  const FUTURE_SEGWIT_MIN_VERSION: number = 2;
41
42
  const FUTURE_SEGWIT_VERSION_DIFF: number = 0x50;
@@ -45,18 +46,66 @@ const FUTURE_SEGWIT_VERSION_WARNING: string =
45
46
  'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
46
47
  'then decide when it is safe to use which version of segwit.';
47
48
 
48
- function _toFutureSegwitAddress(output: Buffer, network: Network): string {
49
- const data = output.slice(2);
49
+ /**
50
+ * Encode a future Taproot-style segwit address (SegWit v2 - v16) using bech32m.
51
+ * Only for versions not yet assigned specific meanings (future use).
52
+ *
53
+ * @param output - Output script buffer containing the version and witness program
54
+ * @param network - Network object containing bech32 and optional bech32Opnet prefix
55
+ * @returns Bech32m-encoded future Taproot-style address
56
+ */
57
+ export function toFutureOPNetAddress(output: Buffer, network: Network): string {
58
+ if (!Buffer.isBuffer(output)) throw new TypeError('output must be a Buffer');
59
+ if (!network.bech32Opnet) throw new Error('Network does not support opnet');
60
+
61
+ const opcode = output[0];
62
+
63
+ // work out where the push-data really starts
64
+ let pushPos = 1,
65
+ progLen: number;
66
+ if (output[1] < 0x4c) {
67
+ progLen = output[1];
68
+ pushPos = 2;
69
+ } else if (output[1] === 0x4c) {
70
+ progLen = output[2];
71
+ pushPos = 3;
72
+ } else {
73
+ throw new TypeError('Unsupported push opcode in script');
74
+ }
50
75
 
51
- if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE)
76
+ const program = output.subarray(pushPos, pushPos + progLen);
77
+
78
+ if (program.length < FUTURE_SEGWIT_MIN_SIZE || program.length > FUTURE_SEGWIT_MAX_SIZE)
52
79
  throw new TypeError('Invalid program length for segwit address');
53
80
 
54
- const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
81
+ const version =
82
+ opcode === opcodes.OP_0
83
+ ? 0
84
+ : opcode >= opcodes.OP_1 && opcode <= opcodes.OP_16
85
+ ? opcode - (opcodes.OP_1 - 1)
86
+ : -1;
87
+
88
+ if (version < FUTURE_SEGWIT_MAX_VERSION || version > FUTURE_MAX_VERSION)
89
+ throw new TypeError(`Invalid segwit version ${version}`);
90
+
91
+ const words = [version, ...bech32m.toWords(program)];
92
+ return bech32m.encode(network.bech32Opnet, words);
93
+ }
55
94
 
56
- if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION)
95
+ function _toFutureSegwitAddress(output: Buffer, network: Network): string {
96
+ const data = output.subarray(2);
97
+ if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE) {
98
+ throw new TypeError('Invalid program length for segwit address');
99
+ }
100
+
101
+ const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
102
+ if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION) {
57
103
  throw new TypeError('Invalid version for segwit address');
104
+ }
58
105
 
59
- if (output[1] !== data.length) throw new TypeError('Invalid script for segwit address');
106
+ if (output[1] !== data.length) {
107
+ throw new TypeError(`Invalid script for segwit address ${output[1]} !== ${data.length}`);
108
+ }
60
109
 
61
110
  return toBech32(data, version, network.bech32, network.bech32Opnet);
62
111
  }
@@ -72,7 +121,7 @@ export function fromBase58Check(address: string): Base58CheckResult {
72
121
  if (payload.length > 21) throw new TypeError(address + ' is too long');
73
122
 
74
123
  const version = payload.readUInt8(0);
75
- const hash = payload.slice(1);
124
+ const hash = payload.subarray(1);
76
125
 
77
126
  return { version, hash };
78
127
  }
@@ -159,6 +208,9 @@ export function fromOutputScript(output: Buffer, network?: Network): string {
159
208
  try {
160
209
  return payments.p2tr({ output, network }).address as string;
161
210
  } catch (e) {}
211
+ try {
212
+ return toFutureOPNetAddress(output, network);
213
+ } catch (e) {}
162
214
  try {
163
215
  return _toFutureSegwitAddress(output, network);
164
216
  } catch (e) {}
@@ -203,6 +255,16 @@ export function toOutputScript(address: string, network?: Network): Buffer {
203
255
  } else if (decodeBech32.version === 1) {
204
256
  if (decodeBech32.data.length === 32)
205
257
  return payments.p2tr({ pubkey: decodeBech32.data }).output as Buffer;
258
+ } else if (decodeBech32.version === FUTURE_OPNET_VERSION) {
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]);
206
268
  } else if (
207
269
  decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
208
270
  decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&