@btc-vision/transaction 1.0.112 → 1.0.114
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/_version.d.ts +1 -1
- package/browser/abi/ABICoder.d.ts +31 -0
- package/browser/buffer/BinaryReader.d.ts +37 -0
- package/browser/buffer/BinaryWriter.d.ts +46 -0
- package/browser/deterministic/AddressMap.d.ts +10 -0
- package/browser/deterministic/AddressSet.d.ts +13 -0
- package/browser/deterministic/DeterministicMap.d.ts +17 -0
- package/browser/deterministic/DeterministicSet.d.ts +13 -0
- package/browser/deterministic/Map.d.ts +14 -0
- package/browser/event/NetEvent.d.ts +5 -0
- package/browser/generators/AddressGenerator.d.ts +3 -0
- package/browser/index.js +1 -1
- package/browser/keypair/Address.d.ts +23 -0
- package/browser/keypair/AddressVerificator.d.ts +15 -4
- package/browser/keypair/EcKeyPair.d.ts +15 -12
- package/browser/keypair/Wallet.d.ts +12 -5
- package/browser/metadata/ContractBaseMetadata.d.ts +3 -4
- package/browser/metadata/contracts/wBTC.d.ts +2 -3
- package/browser/metadata/tokens.d.ts +1 -1
- package/browser/opnet.d.ts +11 -0
- package/browser/signer/TweakedSigner.d.ts +2 -2
- package/browser/transaction/TransactionFactory.d.ts +6 -6
- package/browser/transaction/browser/BrowserSignerBase.d.ts +3 -4
- package/browser/transaction/browser/extensions/UnisatSigner.d.ts +3 -4
- package/browser/transaction/browser/types/Unisat.d.ts +2 -3
- package/browser/transaction/builders/CustomScriptTransaction.d.ts +4 -5
- package/browser/transaction/builders/DeploymentTransaction.d.ts +4 -2
- package/browser/transaction/builders/FundingTransaction.d.ts +2 -1
- package/browser/transaction/builders/MultiSignTransaction.d.ts +6 -6
- package/browser/transaction/builders/SharedInteractionTransaction.d.ts +1 -1
- package/browser/transaction/builders/TransactionBuilder.d.ts +6 -7
- package/browser/transaction/builders/WrapTransaction.d.ts +2 -2
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +6 -6
- package/browser/transaction/processor/PsbtTransaction.d.ts +3 -4
- package/browser/transaction/shared/P2TR_MS.d.ts +1 -2
- package/browser/transaction/shared/TweakedTransaction.d.ts +8 -7
- package/browser/utils/BufferHelper.d.ts +11 -0
- package/browser/utils/types.d.ts +12 -0
- package/browser/utxo/OPNetLimitedProvider.d.ts +1 -2
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/abi/ABICoder.d.ts +31 -0
- package/build/abi/ABICoder.js +137 -0
- package/build/buffer/BinaryReader.d.ts +37 -0
- package/build/buffer/BinaryReader.js +192 -0
- package/build/buffer/BinaryWriter.d.ts +46 -0
- package/build/buffer/BinaryWriter.js +258 -0
- package/build/deterministic/AddressMap.d.ts +10 -0
- package/build/deterministic/AddressMap.js +46 -0
- package/build/deterministic/AddressSet.d.ts +13 -0
- package/build/deterministic/AddressSet.js +44 -0
- package/build/deterministic/DeterministicMap.d.ts +17 -0
- package/build/deterministic/DeterministicMap.js +83 -0
- package/build/deterministic/DeterministicSet.d.ts +13 -0
- package/build/deterministic/DeterministicSet.js +46 -0
- package/build/deterministic/Map.d.ts +14 -0
- package/build/deterministic/Map.js +56 -0
- package/build/event/NetEvent.d.ts +5 -0
- package/build/event/NetEvent.js +6 -0
- package/build/generators/AddressGenerator.d.ts +3 -0
- package/build/generators/AddressGenerator.js +8 -1
- package/build/keypair/Address.d.ts +23 -0
- package/build/keypair/Address.js +129 -0
- package/build/keypair/AddressVerificator.d.ts +15 -4
- package/build/keypair/AddressVerificator.js +79 -3
- package/build/keypair/EcKeyPair.d.ts +15 -12
- package/build/keypair/EcKeyPair.js +61 -9
- package/build/keypair/Wallet.d.ts +12 -5
- package/build/keypair/Wallet.js +19 -5
- package/build/metadata/ContractBaseMetadata.d.ts +3 -4
- package/build/metadata/contracts/wBTC.d.ts +2 -3
- package/build/metadata/contracts/wBTC.js +4 -6
- package/build/metadata/tokens.d.ts +1 -1
- package/build/metadata/tokens.js +19 -15
- package/build/opnet.d.ts +11 -0
- package/build/opnet.js +11 -0
- package/build/signer/TweakedSigner.d.ts +2 -2
- package/build/signer/TweakedSigner.js +1 -1
- package/build/transaction/TransactionFactory.d.ts +6 -6
- package/build/transaction/TransactionFactory.js +3 -2
- package/build/transaction/browser/BrowserSignerBase.d.ts +3 -4
- package/build/transaction/browser/extensions/UnisatSigner.d.ts +3 -4
- package/build/transaction/browser/types/Unisat.d.ts +2 -3
- package/build/transaction/builders/CustomScriptTransaction.d.ts +4 -5
- package/build/transaction/builders/CustomScriptTransaction.js +3 -3
- package/build/transaction/builders/DeploymentTransaction.d.ts +4 -2
- package/build/transaction/builders/DeploymentTransaction.js +10 -6
- package/build/transaction/builders/FundingTransaction.d.ts +2 -1
- package/build/transaction/builders/MultiSignTransaction.d.ts +6 -6
- package/build/transaction/builders/SharedInteractionTransaction.d.ts +1 -1
- package/build/transaction/builders/SharedInteractionTransaction.js +4 -4
- package/build/transaction/builders/TransactionBuilder.d.ts +6 -7
- package/build/transaction/builders/UnwrapSegwitTransaction.js +2 -1
- package/build/transaction/builders/UnwrapTransaction.js +4 -3
- package/build/transaction/builders/WrapTransaction.d.ts +2 -2
- package/build/transaction/builders/WrapTransaction.js +5 -6
- package/build/transaction/interfaces/ITransactionParameters.d.ts +6 -6
- package/build/transaction/processor/PsbtTransaction.d.ts +3 -4
- package/build/transaction/shared/P2TR_MS.d.ts +1 -2
- package/build/transaction/shared/TweakedTransaction.d.ts +8 -7
- package/build/transaction/shared/TweakedTransaction.js +1 -1
- package/build/utils/BufferHelper.d.ts +11 -0
- package/build/utils/BufferHelper.js +50 -0
- package/build/utils/types.d.ts +12 -0
- package/build/utils/types.js +1 -0
- package/build/utxo/OPNetLimitedProvider.d.ts +1 -2
- package/build/utxo/OPNetLimitedProvider.js +1 -1
- package/build/verification/TapscriptVerificator.js +3 -2
- package/package.json +4 -2
- package/src/_version.ts +1 -1
- package/src/abi/ABICoder.ts +158 -0
- package/src/buffer/BinaryReader.ts +260 -0
- package/src/buffer/BinaryWriter.ts +331 -0
- package/src/deterministic/AddressMap.ts +57 -0
- package/src/deterministic/AddressSet.ts +62 -0
- package/src/deterministic/DeterministicMap.ts +88 -0
- package/src/deterministic/DeterministicSet.ts +56 -0
- package/src/deterministic/Map.ts +66 -0
- package/src/event/NetEvent.ts +6 -0
- package/src/generators/AddressGenerator.ts +15 -1
- package/src/keypair/Address.ts +235 -0
- package/src/keypair/AddressVerificator.ts +149 -6
- package/src/keypair/EcKeyPair.ts +146 -47
- package/src/keypair/Wallet.ts +72 -15
- package/src/metadata/ContractBaseMetadata.ts +3 -4
- package/src/metadata/contracts/wBTC.ts +8 -9
- package/src/metadata/tokens.ts +21 -16
- package/src/opnet.ts +13 -0
- package/src/signer/TweakedSigner.ts +3 -2
- package/src/transaction/TransactionFactory.ts +11 -10
- package/src/transaction/browser/BrowserSignerBase.ts +3 -4
- package/src/transaction/browser/extensions/UnisatSigner.ts +6 -7
- package/src/transaction/browser/types/Unisat.ts +2 -3
- package/src/transaction/builders/CustomScriptTransaction.ts +10 -10
- package/src/transaction/builders/DeploymentTransaction.ts +23 -9
- package/src/transaction/builders/FundingTransaction.ts +2 -1
- package/src/transaction/builders/MultiSignTransaction.ts +10 -7
- package/src/transaction/builders/SharedInteractionTransaction.ts +5 -5
- package/src/transaction/builders/TransactionBuilder.ts +26 -17
- package/src/transaction/builders/UnwrapSegwitTransaction.ts +10 -4
- package/src/transaction/builders/UnwrapTransaction.ts +5 -19
- package/src/transaction/builders/WrapTransaction.ts +7 -13
- package/src/transaction/interfaces/ITransactionParameters.ts +7 -6
- package/src/transaction/processor/PsbtTransaction.ts +3 -4
- package/src/transaction/shared/P2TR_MS.ts +2 -3
- package/src/transaction/shared/TweakedTransaction.ts +12 -12
- package/src/utils/BufferHelper.ts +71 -0
- package/src/utils/types.ts +19 -0
- package/src/utxo/OPNetLimitedProvider.ts +3 -8
- package/src/verification/TapscriptVerificator.ts +4 -2
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { BufferHelper } from '../utils/BufferHelper.js';
|
|
2
|
+
import { ADDRESS_BYTE_LENGTH, i32, Selector, u16, u32, u64, u8 } from '../utils/types.js';
|
|
3
|
+
import { Address } from '../keypair/Address.js';
|
|
4
|
+
import { BinaryReader } from './BinaryReader.js';
|
|
5
|
+
import { AddressMap } from '../deterministic/AddressMap.js';
|
|
6
|
+
|
|
7
|
+
export class BinaryWriter {
|
|
8
|
+
private currentOffset: u32 = 0;
|
|
9
|
+
private buffer: DataView;
|
|
10
|
+
|
|
11
|
+
constructor(length: number = 0) {
|
|
12
|
+
this.buffer = this.getDefaultBuffer(length);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public writeU8(value: u8): void {
|
|
16
|
+
if (value > 255) throw new Error('Value is too large.');
|
|
17
|
+
|
|
18
|
+
this.allocSafe(1);
|
|
19
|
+
this.buffer.setUint8(this.currentOffset++, value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public writeU16(value: u16): void {
|
|
23
|
+
if (value > 65535) throw new Error('Value is too large.');
|
|
24
|
+
|
|
25
|
+
this.allocSafe(2);
|
|
26
|
+
this.buffer.setUint16(this.currentOffset, value, true);
|
|
27
|
+
this.currentOffset += 2;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public writeU32(value: u32, le: boolean = true): void {
|
|
31
|
+
if (value > 4294967295) throw new Error('Value is too large.');
|
|
32
|
+
|
|
33
|
+
this.allocSafe(4);
|
|
34
|
+
this.buffer.setUint32(this.currentOffset, value, le);
|
|
35
|
+
this.currentOffset += 4;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public writeU64(value: u64): void {
|
|
39
|
+
if (value > 18446744073709551615n) throw new Error('Value is too large.');
|
|
40
|
+
|
|
41
|
+
this.allocSafe(8);
|
|
42
|
+
this.buffer.setBigUint64(this.currentOffset, value, true);
|
|
43
|
+
this.currentOffset += 8;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public writeSelector(value: Selector): void {
|
|
47
|
+
this.writeU32(value, false);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public writeBoolean(value: boolean): void {
|
|
51
|
+
this.writeU8(value ? 1 : 0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public writeU256(bigIntValue: bigint): void {
|
|
55
|
+
if (
|
|
56
|
+
bigIntValue >
|
|
57
|
+
115792089237316195423570985008687907853269984665640564039457584007913129639935n
|
|
58
|
+
) {
|
|
59
|
+
throw new Error('Value is too large.');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
this.allocSafe(32);
|
|
63
|
+
|
|
64
|
+
const bytesToHex = BufferHelper.valueToUint8Array(bigIntValue);
|
|
65
|
+
if (bytesToHex.byteLength !== 32) {
|
|
66
|
+
console.log('Invalid u256 value:', bytesToHex);
|
|
67
|
+
|
|
68
|
+
throw new Error(`Invalid u256 value: ${bigIntValue}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (let i = 0; i < bytesToHex.byteLength; i++) {
|
|
72
|
+
this.writeU8(bytesToHex[i]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public writeBytes(value: Uint8Array | Buffer): void {
|
|
77
|
+
this.allocSafe(value.byteLength);
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < value.byteLength; i++) {
|
|
80
|
+
this.writeU8(value[i]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public writeString(value: string): void {
|
|
85
|
+
this.allocSafe(value.length);
|
|
86
|
+
|
|
87
|
+
for (let i: i32 = 0; i < value.length; i++) {
|
|
88
|
+
this.writeU8(value.charCodeAt(i));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public writeAddress(value: Address): void {
|
|
93
|
+
const bytes = this.fromAddress(value);
|
|
94
|
+
this.writeBytes(bytes);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public writeStringWithLength(value: string): void {
|
|
98
|
+
this.allocSafe(value.length + 2);
|
|
99
|
+
|
|
100
|
+
this.writeU16(value.length);
|
|
101
|
+
this.writeString(value);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public getBuffer(clear: boolean = true): Uint8Array {
|
|
105
|
+
const buf = new Uint8Array(this.buffer.byteLength);
|
|
106
|
+
for (let i: u32 = 0; i < this.buffer.byteLength; i++) {
|
|
107
|
+
buf[i] = this.buffer.getUint8(i);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (clear) this.clear();
|
|
111
|
+
|
|
112
|
+
return buf;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public reset(): void {
|
|
116
|
+
this.currentOffset = 0;
|
|
117
|
+
this.buffer = this.getDefaultBuffer(4);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public writeTuple(values: bigint[]): void {
|
|
121
|
+
this.allocSafe(4 + values.length * 32);
|
|
122
|
+
this.writeU32(values.length);
|
|
123
|
+
|
|
124
|
+
for (let i = 0; i < values.length; i++) {
|
|
125
|
+
this.writeU256(values[i]);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public toBytesReader(): BinaryReader {
|
|
130
|
+
return new BinaryReader(this.getBuffer());
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public getOffset(): u32 {
|
|
134
|
+
return this.currentOffset;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public setOffset(offset: u32): void {
|
|
138
|
+
this.currentOffset = offset;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public clear(): void {
|
|
142
|
+
this.currentOffset = 0;
|
|
143
|
+
this.buffer = this.getDefaultBuffer();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public allocSafe(size: u32): void {
|
|
147
|
+
if (this.currentOffset + size > this.buffer.byteLength) {
|
|
148
|
+
this.resize(size);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public writeABISelector(name: string, selector: Selector): void {
|
|
153
|
+
this.writeStringWithLength(name);
|
|
154
|
+
this.writeSelector(selector);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public writeAddressValueTupleMap(map: AddressMap<bigint>): void {
|
|
158
|
+
if (map.size > 65535) throw new Error('Map size is too large');
|
|
159
|
+
|
|
160
|
+
this.writeU16(map.size);
|
|
161
|
+
|
|
162
|
+
const keys = Array.from(map.keys());
|
|
163
|
+
for (let i = 0; i < keys.length; i++) {
|
|
164
|
+
const key = keys[i];
|
|
165
|
+
const value = map.get(key);
|
|
166
|
+
|
|
167
|
+
if (value === null || value === undefined) throw new Error('Value not found');
|
|
168
|
+
|
|
169
|
+
this.writeAddress(key);
|
|
170
|
+
this.writeU256(value);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public writeLimitedAddressBytesMap(map: AddressMap<Uint8Array[]>): void {
|
|
175
|
+
if (map.size > 8) throw new Error('Too many contract calls');
|
|
176
|
+
|
|
177
|
+
this.writeU8(map.size);
|
|
178
|
+
|
|
179
|
+
const keys: Address[] = Array.from(map.keys());
|
|
180
|
+
for (let i: i32 = 0; i < keys.length; i++) {
|
|
181
|
+
const address: Address = keys[i];
|
|
182
|
+
const calls: Uint8Array[] | undefined = map.get(address);
|
|
183
|
+
|
|
184
|
+
if (!calls) throw new Error('Calls not found');
|
|
185
|
+
if (calls.length > 10) throw new Error('Too many calls.');
|
|
186
|
+
|
|
187
|
+
this.writeAddress(address);
|
|
188
|
+
this.writeU8(calls.length);
|
|
189
|
+
|
|
190
|
+
for (let j: i32 = 0; j < calls.length; j++) {
|
|
191
|
+
this.writeBytesWithLength(calls[j]);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public writeBytesWithLength(value: Uint8Array): void {
|
|
197
|
+
this.writeU32(value.length);
|
|
198
|
+
this.writeBytes(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public writeAddressArray(value: Address[]): void {
|
|
202
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
203
|
+
|
|
204
|
+
this.writeU16(value.length);
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < value.length; i++) {
|
|
207
|
+
this.writeAddress(value[i]);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
public writeU32Array(value: u32[]): void {
|
|
212
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
213
|
+
|
|
214
|
+
this.writeU16(value.length);
|
|
215
|
+
|
|
216
|
+
for (let i = 0; i < value.length; i++) {
|
|
217
|
+
this.writeU32(value[i]);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public writeU256Array(value: bigint[]): void {
|
|
222
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
223
|
+
|
|
224
|
+
this.writeU16(value.length);
|
|
225
|
+
|
|
226
|
+
for (let i = 0; i < value.length; i++) {
|
|
227
|
+
this.writeU256(value[i]);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public writeStringArray(value: string[]): void {
|
|
232
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
233
|
+
|
|
234
|
+
this.writeU16(value.length);
|
|
235
|
+
|
|
236
|
+
for (let i = 0; i < value.length; i++) {
|
|
237
|
+
this.writeStringWithLength(value[i]);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public writeU16Array(value: u16[]): void {
|
|
242
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
243
|
+
|
|
244
|
+
this.writeU16(value.length);
|
|
245
|
+
|
|
246
|
+
for (let i = 0; i < value.length; i++) {
|
|
247
|
+
this.writeU16(value[i]);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
public writeU8Array(value: u8[]): void {
|
|
252
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
253
|
+
|
|
254
|
+
this.writeU16(value.length);
|
|
255
|
+
|
|
256
|
+
for (let i = 0; i < value.length; i++) {
|
|
257
|
+
this.writeU8(value[i]);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
public writeU64Array(value: bigint[]): void {
|
|
262
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
263
|
+
|
|
264
|
+
this.writeU16(value.length);
|
|
265
|
+
|
|
266
|
+
for (let i = 0; i < value.length; i++) {
|
|
267
|
+
this.writeU64(value[i]);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
public writeBytesArray(value: Uint8Array[]): void {
|
|
272
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
273
|
+
|
|
274
|
+
this.writeU16(value.length);
|
|
275
|
+
|
|
276
|
+
for (let i = 0; i < value.length; i++) {
|
|
277
|
+
this.writeBytesWithLength(value[i]);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
public writeSelectorArray(value: Selector[]): void {
|
|
282
|
+
if (value.length > 65535) throw new Error('Array size is too large');
|
|
283
|
+
|
|
284
|
+
this.writeU16(value.length);
|
|
285
|
+
|
|
286
|
+
for (let i = 0; i < value.length; i++) {
|
|
287
|
+
this.writeSelector(value[i]);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private getChecksum(): u32 {
|
|
292
|
+
let checksum: u32 = 0;
|
|
293
|
+
for (let i = 0; i < this.buffer.byteLength; i++) {
|
|
294
|
+
checksum += this.buffer.getUint8(i);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return checksum % 2 ** 32;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private writeMethodSelectorMap(value: Set<Selector>): void {
|
|
301
|
+
this.writeU16(value.size);
|
|
302
|
+
|
|
303
|
+
value.forEach((selector: Selector, _value: Selector, _set: Set<Selector>): void => {
|
|
304
|
+
this.writeSelector(selector);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private fromAddress(pubKey: Address): Uint8Array {
|
|
309
|
+
if (pubKey.byteLength > ADDRESS_BYTE_LENGTH) {
|
|
310
|
+
throw new Error(
|
|
311
|
+
`Address is too long ${pubKey.byteLength} > ${ADDRESS_BYTE_LENGTH} bytes`,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return pubKey;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private resize(size: u32): void {
|
|
319
|
+
const buf: Uint8Array = new Uint8Array(this.buffer.byteLength + size);
|
|
320
|
+
|
|
321
|
+
for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
|
|
322
|
+
buf[i] = this.buffer.getUint8(i);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
this.buffer = new DataView(buf.buffer);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private getDefaultBuffer(length: number = 0): DataView {
|
|
329
|
+
return new DataView(new ArrayBuffer(length));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { i32 } from '../utils/types.js';
|
|
2
|
+
import { Address } from '../keypair/Address.js';
|
|
3
|
+
import { Map } from './Map.js';
|
|
4
|
+
|
|
5
|
+
export class AddressMap<V> extends Map<Address, V> {
|
|
6
|
+
public set(key: Address, value: V): void {
|
|
7
|
+
const index: i32 = this._keys.indexOf(key);
|
|
8
|
+
if (index == -1) {
|
|
9
|
+
this._keys.push(key);
|
|
10
|
+
this._values.push(value);
|
|
11
|
+
} else {
|
|
12
|
+
this._values[index] = value;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public indexOf(address: Address): i32 {
|
|
17
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
18
|
+
const key = this._keys[i];
|
|
19
|
+
|
|
20
|
+
if (address.equals(key)) {
|
|
21
|
+
return i;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return -1;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public has(key: Address): boolean {
|
|
29
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
30
|
+
if (key.equals(this._keys[i])) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public get(key: Address): V {
|
|
39
|
+
const index: i32 = this.indexOf(key);
|
|
40
|
+
if (index == -1) {
|
|
41
|
+
throw new Error('Key not found in map');
|
|
42
|
+
}
|
|
43
|
+
return this._values[index];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public delete(key: Address): boolean {
|
|
47
|
+
const index: i32 = this.indexOf(key);
|
|
48
|
+
if (index == -1) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this._keys.splice(index, 1);
|
|
53
|
+
this._values.splice(index, 1);
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { i32 } from '../utils/types.js';
|
|
2
|
+
import { Address } from '../keypair/Address.js';
|
|
3
|
+
|
|
4
|
+
export class AddressSet {
|
|
5
|
+
private keys: Address[];
|
|
6
|
+
|
|
7
|
+
public constructor(keys: Address[] = []) {
|
|
8
|
+
this.keys = keys;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public add(address: Address): void {
|
|
12
|
+
if (!this.contains(address)) {
|
|
13
|
+
this.keys.push(address);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public contains(address: Address): boolean {
|
|
18
|
+
for (let i = 0; i < this.keys.length; i++) {
|
|
19
|
+
if (this.keys[i].equals(address)) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public remove(address: Address): void {
|
|
28
|
+
const index = this.keys.findIndex((key) => key.equals(address));
|
|
29
|
+
|
|
30
|
+
if (index !== -1) {
|
|
31
|
+
this.keys.splice(index, 1);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public size(): i32 {
|
|
36
|
+
return this.keys.length;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public clone(): AddressSet {
|
|
40
|
+
const clone = new AddressSet();
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < this.keys.length; i++) {
|
|
43
|
+
clone.add(this.keys[i]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return clone;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public clear(): void {
|
|
50
|
+
this.keys = [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public combine(set: AddressSet): AddressSet {
|
|
54
|
+
const clone = this.clone();
|
|
55
|
+
|
|
56
|
+
for (let i = 0; i < set.keys.length; i++) {
|
|
57
|
+
clone.add(set.keys[i]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return clone;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export class DeterministicMap<K, V> {
|
|
2
|
+
private map: Map<K, V>;
|
|
3
|
+
#keys: K[];
|
|
4
|
+
|
|
5
|
+
constructor(private compareFn: (a: K, b: K) => number) {
|
|
6
|
+
this.map = new Map<K, V>();
|
|
7
|
+
this.#keys = [];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public get size(): number {
|
|
11
|
+
return this.map.size;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static fromMap<K, V>(
|
|
15
|
+
map: Map<K, V>,
|
|
16
|
+
compareFn: (a: K, b: K) => number,
|
|
17
|
+
): DeterministicMap<K, V> {
|
|
18
|
+
const deterministicMap = new DeterministicMap<K, V>(compareFn);
|
|
19
|
+
for (const [key, value] of map) {
|
|
20
|
+
deterministicMap.set(key, value);
|
|
21
|
+
}
|
|
22
|
+
return deterministicMap;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public set(key: K, value: V): void {
|
|
26
|
+
if (!this.map.has(key)) {
|
|
27
|
+
this.#keys.push(key);
|
|
28
|
+
this.#keys.sort(this.compareFn);
|
|
29
|
+
}
|
|
30
|
+
this.map.set(key, value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get(key: K): V | undefined {
|
|
34
|
+
return this.map.get(key);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public keys(): IterableIterator<K> {
|
|
38
|
+
return this.#keys.values();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public values(): IterableIterator<V> {
|
|
42
|
+
const values: V[] = [];
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < this.#keys.length; i++) {
|
|
45
|
+
const key = this.#keys[i];
|
|
46
|
+
const value = this.map.get(key);
|
|
47
|
+
|
|
48
|
+
if (value) {
|
|
49
|
+
values.push(value);
|
|
50
|
+
} else {
|
|
51
|
+
throw new Error('Value not found');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return values.values();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public has(key: K): boolean {
|
|
59
|
+
return this.map.has(key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public delete(key: K): boolean {
|
|
63
|
+
if (this.map.has(key)) {
|
|
64
|
+
this.map.delete(key);
|
|
65
|
+
this.#keys = this.#keys.filter((k) => k !== key);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public clear(): void {
|
|
72
|
+
this.map.clear();
|
|
73
|
+
this.#keys = [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public forEach(callback: (value: V, key: K, map: DeterministicMap<K, V>) => void): void {
|
|
77
|
+
for (const key of this.#keys) {
|
|
78
|
+
const value = this.map.get(key) as V;
|
|
79
|
+
callback(value, key, this);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
*[Symbol.iterator](): IterableIterator<[K, V]> {
|
|
84
|
+
for (const key of this.#keys) {
|
|
85
|
+
yield [key, this.map.get(key) as V];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export class DeterministicSet<T> {
|
|
2
|
+
private elements: T[];
|
|
3
|
+
|
|
4
|
+
constructor(private compareFn: (a: T, b: T) => number) {
|
|
5
|
+
this.elements = [];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
public add(value: T): void {
|
|
9
|
+
if (!this.elements.includes(value)) {
|
|
10
|
+
this.elements.push(value);
|
|
11
|
+
this.elements.sort(this.compareFn);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public delete(value: T): boolean {
|
|
16
|
+
const index = this.elements.indexOf(value);
|
|
17
|
+
if (index === -1) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.elements.splice(index, 1);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public has(value: T): boolean {
|
|
26
|
+
return this.elements.includes(value);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public clear(): void {
|
|
30
|
+
this.elements = [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public forEach(callback: (value: T, set: DeterministicSet<T>) => void): void {
|
|
34
|
+
for (const value of this.elements) {
|
|
35
|
+
callback(value, this);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T> {
|
|
40
|
+
const deterministicSet = new DeterministicSet<T>(compareFn);
|
|
41
|
+
for (const value of set) {
|
|
42
|
+
deterministicSet.add(value);
|
|
43
|
+
}
|
|
44
|
+
return deterministicSet;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public get size(): number {
|
|
48
|
+
return this.elements.length;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
*[Symbol.iterator](): IterableIterator<T> {
|
|
52
|
+
for (const value of this.elements) {
|
|
53
|
+
yield value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { i32 } from '../utils/types.js';
|
|
2
|
+
|
|
3
|
+
export class Map<K, V> {
|
|
4
|
+
protected _keys: K[] = [];
|
|
5
|
+
protected _values: V[] = [];
|
|
6
|
+
|
|
7
|
+
public get size(): i32 {
|
|
8
|
+
return this._keys.length;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public keys(): K[] {
|
|
12
|
+
return this._keys;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public values(): V[] {
|
|
16
|
+
return this._values;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public set(key: K, value: V): void {
|
|
20
|
+
const index: i32 = this.indexOf(key);
|
|
21
|
+
if (index == -1) {
|
|
22
|
+
this._keys.push(key);
|
|
23
|
+
this._values.push(value);
|
|
24
|
+
} else {
|
|
25
|
+
this._values[index] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public indexOf(key: K): i32 {
|
|
30
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
31
|
+
if (this._keys[i] == key) {
|
|
32
|
+
return i;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public get(key: K): V {
|
|
40
|
+
const index: i32 = this.indexOf(key);
|
|
41
|
+
if (index == -1) {
|
|
42
|
+
throw new Error('Key not found in map');
|
|
43
|
+
}
|
|
44
|
+
return this._values[index];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public has(key: K): boolean {
|
|
48
|
+
return this.indexOf(key) != -1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public delete(key: K): boolean {
|
|
52
|
+
const index: i32 = this.indexOf(key);
|
|
53
|
+
if (index == -1) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this._keys.splice(index, 1);
|
|
58
|
+
this._values.splice(index, 1);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public clear(): void {
|
|
63
|
+
this._keys = [];
|
|
64
|
+
this._values = [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { bech32 } from 'bech32';
|
|
1
|
+
import { bech32, bech32m } from 'bech32';
|
|
2
2
|
import { initEccLib, Network } from 'bitcoinjs-lib';
|
|
3
3
|
import * as ecc from '@bitcoinerlab/secp256k1';
|
|
4
4
|
import { ripemd160 } from 'bitcoinjs-lib/src/crypto.js';
|
|
@@ -14,6 +14,20 @@ export class AddressGenerator {
|
|
|
14
14
|
return this.toSegwitAddress(pkh, network);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
// Generate a valid Taproot address from a public key
|
|
18
|
+
public static generateTaprootAddress(pubKey: Buffer, network: { bech32: string }): string {
|
|
19
|
+
if (pubKey.length !== 32) throw new Error('Invalid public key length');
|
|
20
|
+
|
|
21
|
+
// Convert the public key to words
|
|
22
|
+
const words = bech32m.toWords(pubKey);
|
|
23
|
+
|
|
24
|
+
// Prepend the witness version (0x01 for Taproot)
|
|
25
|
+
words.unshift(0x01);
|
|
26
|
+
|
|
27
|
+
// Encode using Bech32m
|
|
28
|
+
return bech32m.encode(network.bech32, words);
|
|
29
|
+
}
|
|
30
|
+
|
|
17
31
|
// Convert a hash to a SegWit address
|
|
18
32
|
private static toSegwitAddress(pkh: Buffer, network: Network): string {
|
|
19
33
|
const words = bech32.toWords(pkh);
|