@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.
@@ -1,188 +1,188 @@
1
- import * as varuint from 'varuint-bitcoin';
2
- import * as types from './types.js';
3
-
4
- const { typeforce } = types;
5
-
6
- export { varuint };
7
-
8
- // https://github.com/feross/buffer/blob/master/index.js#L1127
9
- function verifuint(value: number, max: number): void {
10
- if (typeof value !== 'number') throw new Error('cannot write a non-number as a number');
11
- if (value < 0) throw new Error('specified a negative value for writing an unsigned value');
12
- if (value > max) throw new Error('RangeError: value out of range');
13
- if (Math.floor(value) !== value) throw new Error('value has a fractional component');
14
- }
15
-
16
- export function readUInt64LE(buffer: Buffer, offset: number): number {
17
- const a = buffer.readUInt32LE(offset);
18
- let b = buffer.readUInt32LE(offset + 4);
19
- b *= 0x100000000;
20
-
21
- verifuint(b + a, 0x001fffffffffffff);
22
- return b + a;
23
- }
24
-
25
- /**
26
- * Writes a 64-bit unsigned integer in little-endian format to the specified buffer at the given offset.
27
- *
28
- * @param buffer - The buffer to write the value to.
29
- * @param value - The 64-bit unsigned integer value to write.
30
- * @param offset - The offset in the buffer where the value should be written.
31
- * @returns The new offset after writing the value.
32
- */
33
- export function writeUInt64LE(buffer: Buffer, value: number, offset: number): number {
34
- verifuint(value, 0x001fffffffffffff);
35
-
36
- buffer.writeInt32LE(value & -1, offset);
37
- buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4);
38
- return offset + 8;
39
- }
40
-
41
- /**
42
- * Reverses the order of bytes in a buffer.
43
- * @param buffer - The buffer to reverse.
44
- * @returns A new buffer with the bytes reversed.
45
- */
46
- export function reverseBuffer(buffer: Buffer): Buffer {
47
- if (buffer.length < 1) return buffer;
48
- let j = buffer.length - 1;
49
- let tmp = 0;
50
- for (let i = 0; i < buffer.length / 2; i++) {
51
- tmp = buffer[i];
52
- buffer[i] = buffer[j];
53
- buffer[j] = tmp;
54
- j--;
55
- }
56
- return buffer;
57
- }
58
-
59
- export function cloneBuffer(buffer: Buffer): Buffer {
60
- const clone = Buffer.allocUnsafe(buffer.length);
61
- buffer.copy(clone);
62
- return clone;
63
- }
64
-
65
- /**
66
- * Helper class for serialization of bitcoin data types into a pre-allocated buffer.
67
- */
68
- export class BufferWriter {
69
- constructor(
70
- public buffer: Buffer,
71
- public offset: number = 0,
72
- ) {
73
- typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
74
- }
75
-
76
- static withCapacity(size: number): BufferWriter {
77
- return new BufferWriter(Buffer.alloc(size));
78
- }
79
-
80
- writeUInt8(i: number): void {
81
- this.offset = this.buffer.writeUInt8(i, this.offset);
82
- }
83
-
84
- writeInt32(i: number): void {
85
- this.offset = this.buffer.writeInt32LE(i, this.offset);
86
- }
87
-
88
- writeUInt32(i: number): void {
89
- this.offset = this.buffer.writeUInt32LE(i, this.offset);
90
- }
91
-
92
- writeUInt64(i: number): void {
93
- this.offset = writeUInt64LE(this.buffer, i, this.offset);
94
- }
95
-
96
- writeVarInt(i: number): void {
97
- const encode = varuint.encode(i, this.buffer, this.offset);
98
- this.offset += encode.bytes;
99
- }
100
-
101
- writeSlice(slice: Buffer): void {
102
- if (this.buffer.length < this.offset + slice.length) {
103
- throw new Error('Cannot write slice out of bounds');
104
- }
105
- this.offset += slice.copy(this.buffer, this.offset);
106
- }
107
-
108
- writeVarSlice(slice: Buffer): void {
109
- this.writeVarInt(slice.length);
110
- this.writeSlice(slice);
111
- }
112
-
113
- writeVector(vector: Buffer[]): void {
114
- this.writeVarInt(vector.length);
115
- vector.forEach((buf: Buffer) => this.writeVarSlice(buf));
116
- }
117
-
118
- end(): Buffer {
119
- if (this.buffer.length === this.offset) {
120
- return this.buffer;
121
- }
122
- throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
123
- }
124
- }
125
-
126
- /**
127
- * Helper class for reading of bitcoin data types from a buffer.
128
- */
129
- export class BufferReader {
130
- constructor(
131
- public buffer: Buffer,
132
- public offset: number = 0,
133
- ) {
134
- typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
135
- }
136
-
137
- readUInt8(): number {
138
- const result = this.buffer.readUInt8(this.offset);
139
- this.offset++;
140
- return result;
141
- }
142
-
143
- readInt32(): number {
144
- const result = this.buffer.readInt32LE(this.offset);
145
- this.offset += 4;
146
- return result;
147
- }
148
-
149
- readUInt32(): number {
150
- const result = this.buffer.readUInt32LE(this.offset);
151
- this.offset += 4;
152
- return result;
153
- }
154
-
155
- readUInt64(): number {
156
- const result = readUInt64LE(this.buffer, this.offset);
157
- this.offset += 8;
158
- return result;
159
- }
160
-
161
- readVarInt(): number {
162
- const vi = varuint.decode(this.buffer, this.offset);
163
- this.offset += vi.bytes;
164
-
165
- return vi.numberValue || 0;
166
- }
167
-
168
- readSlice(n: number): Buffer {
169
- if (this.buffer.length < this.offset + n) {
170
- throw new Error('Cannot read slice out of bounds');
171
- }
172
-
173
- const result = this.buffer.subarray(this.offset, this.offset + n);
174
- this.offset += n;
175
- return result;
176
- }
177
-
178
- readVarSlice(): Buffer {
179
- return this.readSlice(this.readVarInt());
180
- }
181
-
182
- readVector(): Buffer[] {
183
- const count = this.readVarInt();
184
- const vector: Buffer[] = [];
185
- for (let i = 0; i < count; i++) vector.push(this.readVarSlice());
186
- return vector;
187
- }
188
- }
1
+ import * as varuint from 'varuint-bitcoin';
2
+ import * as types from './types.js';
3
+
4
+ const { typeforce } = types;
5
+
6
+ export { varuint };
7
+
8
+ // https://github.com/feross/buffer/blob/master/index.js#L1127
9
+ function verifuint(value: number, max: number): void {
10
+ if (typeof value !== 'number') throw new Error('cannot write a non-number as a number');
11
+ if (value < 0) throw new Error('specified a negative value for writing an unsigned value');
12
+ if (value > max) throw new Error('RangeError: value out of range');
13
+ if (Math.floor(value) !== value) throw new Error('value has a fractional component');
14
+ }
15
+
16
+ export function readUInt64LE(buffer: Buffer, offset: number): number {
17
+ const a = buffer.readUInt32LE(offset);
18
+ let b = buffer.readUInt32LE(offset + 4);
19
+ b *= 0x100000000;
20
+
21
+ verifuint(b + a, 0x001fffffffffffff);
22
+ return b + a;
23
+ }
24
+
25
+ /**
26
+ * Writes a 64-bit unsigned integer in little-endian format to the specified buffer at the given offset.
27
+ *
28
+ * @param buffer - The buffer to write the value to.
29
+ * @param value - The 64-bit unsigned integer value to write.
30
+ * @param offset - The offset in the buffer where the value should be written.
31
+ * @returns The new offset after writing the value.
32
+ */
33
+ export function writeUInt64LE(buffer: Buffer, value: number, offset: number): number {
34
+ verifuint(value, 0x001fffffffffffff);
35
+
36
+ buffer.writeInt32LE(value & -1, offset);
37
+ buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4);
38
+ return offset + 8;
39
+ }
40
+
41
+ /**
42
+ * Reverses the order of bytes in a buffer.
43
+ * @param buffer - The buffer to reverse.
44
+ * @returns A new buffer with the bytes reversed.
45
+ */
46
+ export function reverseBuffer(buffer: Buffer): Buffer {
47
+ if (buffer.length < 1) return buffer;
48
+ let j = buffer.length - 1;
49
+ let tmp = 0;
50
+ for (let i = 0; i < buffer.length / 2; i++) {
51
+ tmp = buffer[i];
52
+ buffer[i] = buffer[j];
53
+ buffer[j] = tmp;
54
+ j--;
55
+ }
56
+ return buffer;
57
+ }
58
+
59
+ export function cloneBuffer(buffer: Buffer): Buffer {
60
+ const clone = Buffer.allocUnsafe(buffer.length);
61
+ buffer.copy(clone);
62
+ return clone;
63
+ }
64
+
65
+ /**
66
+ * Helper class for serialization of bitcoin data types into a pre-allocated buffer.
67
+ */
68
+ export class BufferWriter {
69
+ constructor(
70
+ public buffer: Buffer,
71
+ public offset: number = 0,
72
+ ) {
73
+ typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
74
+ }
75
+
76
+ static withCapacity(size: number): BufferWriter {
77
+ return new BufferWriter(Buffer.alloc(size));
78
+ }
79
+
80
+ writeUInt8(i: number): void {
81
+ this.offset = this.buffer.writeUInt8(i, this.offset);
82
+ }
83
+
84
+ writeInt32(i: number): void {
85
+ this.offset = this.buffer.writeInt32LE(i, this.offset);
86
+ }
87
+
88
+ writeUInt32(i: number): void {
89
+ this.offset = this.buffer.writeUInt32LE(i, this.offset);
90
+ }
91
+
92
+ writeUInt64(i: number): void {
93
+ this.offset = writeUInt64LE(this.buffer, i, this.offset);
94
+ }
95
+
96
+ writeVarInt(i: number): void {
97
+ const encode = varuint.encode(i, this.buffer, this.offset);
98
+ this.offset += encode.bytes;
99
+ }
100
+
101
+ writeSlice(slice: Buffer): void {
102
+ if (this.buffer.length < this.offset + slice.length) {
103
+ throw new Error('Cannot write slice out of bounds');
104
+ }
105
+ this.offset += slice.copy(this.buffer, this.offset);
106
+ }
107
+
108
+ writeVarSlice(slice: Buffer): void {
109
+ this.writeVarInt(slice.length);
110
+ this.writeSlice(slice);
111
+ }
112
+
113
+ writeVector(vector: Buffer[]): void {
114
+ this.writeVarInt(vector.length);
115
+ vector.forEach((buf: Buffer) => this.writeVarSlice(buf));
116
+ }
117
+
118
+ end(): Buffer {
119
+ if (this.buffer.length === this.offset) {
120
+ return this.buffer;
121
+ }
122
+ throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Helper class for reading of bitcoin data types from a buffer.
128
+ */
129
+ export class BufferReader {
130
+ constructor(
131
+ public buffer: Buffer,
132
+ public offset: number = 0,
133
+ ) {
134
+ typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
135
+ }
136
+
137
+ readUInt8(): number {
138
+ const result = this.buffer.readUInt8(this.offset);
139
+ this.offset++;
140
+ return result;
141
+ }
142
+
143
+ readInt32(): number {
144
+ const result = this.buffer.readInt32LE(this.offset);
145
+ this.offset += 4;
146
+ return result;
147
+ }
148
+
149
+ readUInt32(): number {
150
+ const result = this.buffer.readUInt32LE(this.offset);
151
+ this.offset += 4;
152
+ return result;
153
+ }
154
+
155
+ readUInt64(): number {
156
+ const result = readUInt64LE(this.buffer, this.offset);
157
+ this.offset += 8;
158
+ return result;
159
+ }
160
+
161
+ readVarInt(): number {
162
+ const vi = varuint.decode(this.buffer, this.offset);
163
+ this.offset += vi.bytes;
164
+
165
+ return vi.numberValue || 0;
166
+ }
167
+
168
+ readSlice(n: number): Buffer {
169
+ if (this.buffer.length < this.offset + n) {
170
+ throw new Error('Cannot read slice out of bounds');
171
+ }
172
+
173
+ const result = this.buffer.subarray(this.offset, this.offset + n);
174
+ this.offset += n;
175
+ return result;
176
+ }
177
+
178
+ readVarSlice(): Buffer {
179
+ return this.readSlice(this.readVarInt());
180
+ }
181
+
182
+ readVector(): Buffer[] {
183
+ const count = this.readVarInt();
184
+ const vector: Buffer[] = [];
185
+ for (let i = 0; i < count; i++) vector.push(this.readVarSlice());
186
+ return vector;
187
+ }
188
+ }
package/src/networks.ts CHANGED
@@ -34,7 +34,7 @@ export const bitcoin: Network = {
34
34
  * The Bech32 prefix used for Bitcoin addresses.
35
35
  */
36
36
  bech32: 'bc',
37
- bech32Opnet: 'opnet',
37
+ bech32Opnet: 'op',
38
38
  /**
39
39
  * The BIP32 key prefixes for Bitcoin.
40
40
  */
@@ -67,7 +67,7 @@ export const bitcoin: Network = {
67
67
  export const regtest: Network = {
68
68
  messagePrefix: '\x18Bitcoin Signed Message:\n',
69
69
  bech32: 'bcrt',
70
- bech32Opnet: 'opreg',
70
+ bech32Opnet: 'opr',
71
71
  bip32: {
72
72
  public: 0x043587cf,
73
73
  private: 0x04358394,
@@ -82,7 +82,7 @@ export const regtest: Network = {
82
82
  export const testnet: Network = {
83
83
  messagePrefix: '\x18Bitcoin Signed Message:\n',
84
84
  bech32: 'tb',
85
- bech32Opnet: 'optest',
85
+ bech32Opnet: 'opt',
86
86
  bip32: {
87
87
  public: 0x043587cf,
88
88
  private: 0x04358394,
@@ -150,7 +150,7 @@ export const dogecoinTestnet: Network = {
150
150
  export const litecoin: Network = {
151
151
  messagePrefix: '\x19Litecoin Signed Message:\n',
152
152
  bech32: 'ltc',
153
- bech32Opnet: 'opltc',
153
+ bech32Opnet: 'opl',
154
154
  bip32: {
155
155
  public: 0x019da462,
156
156
  private: 0x019d9cfe,
@@ -166,7 +166,7 @@ export const litecoin: Network = {
166
166
  export const litecoinTestnet: Network = {
167
167
  messagePrefix: '\x19Litecoin Signed Message:\n',
168
168
  bech32: 'tltc',
169
- bech32Opnet: 'opltct',
169
+ bech32Opnet: 'oplt',
170
170
  bip32: {
171
171
  public: 0x0436ef7d,
172
172
  private: 0x0436f6e1,
@@ -202,7 +202,7 @@ export const bitcoinCash: Network = {
202
202
  export const bitcoinCashTestnet: Network = {
203
203
  messagePrefix: '\x18Bitcoin Signed Message:\n',
204
204
  bech32: 'bchtest',
205
- bech32Opnet: 'opbchtest',
205
+ bech32Opnet: 'opbcht',
206
206
  bip32: {
207
207
  public: 0x043587cf,
208
208
  private: 0x04358394,