@btc-vision/transaction 1.0.113 → 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 +1 -2
- package/browser/keypair/EcKeyPair.d.ts +9 -9
- package/browser/keypair/Wallet.d.ts +11 -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/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/MultiSignTransaction.d.ts +4 -5
- package/browser/transaction/builders/TransactionBuilder.d.ts +5 -6
- 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/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 +1 -2
- package/build/keypair/AddressVerificator.js +7 -2
- package/build/keypair/EcKeyPair.d.ts +9 -9
- package/build/keypair/EcKeyPair.js +9 -2
- package/build/keypair/Wallet.d.ts +11 -5
- package/build/keypair/Wallet.js +14 -1
- 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/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/DeploymentTransaction.d.ts +4 -2
- package/build/transaction/builders/DeploymentTransaction.js +6 -2
- package/build/transaction/builders/MultiSignTransaction.d.ts +4 -5
- package/build/transaction/builders/TransactionBuilder.d.ts +5 -6
- package/build/transaction/builders/UnwrapSegwitTransaction.js +2 -1
- package/build/transaction/builders/UnwrapTransaction.js +2 -1
- 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/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 +3 -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 +9 -5
- package/src/keypair/EcKeyPair.ts +39 -37
- package/src/keypair/Wallet.ts +61 -12
- 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/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 +4 -5
- package/src/transaction/builders/DeploymentTransaction.ts +17 -4
- package/src/transaction/builders/MultiSignTransaction.ts +4 -5
- package/src/transaction/builders/TransactionBuilder.ts +8 -9
- package/src/transaction/builders/UnwrapSegwitTransaction.ts +3 -1
- package/src/transaction/builders/UnwrapTransaction.ts +3 -17
- package/src/transaction/builders/WrapTransaction.ts +7 -8
- 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/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,235 @@
|
|
|
1
|
+
import { Network } from 'bitcoinjs-lib';
|
|
2
|
+
import { EcKeyPair } from './EcKeyPair.js';
|
|
3
|
+
import { ECPairInterface } from 'ecpair';
|
|
4
|
+
import { ADDRESS_BYTE_LENGTH } from '../utils/types.js';
|
|
5
|
+
import { AddressVerificator } from './AddressVerificator.js';
|
|
6
|
+
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
7
|
+
|
|
8
|
+
export class Address extends Uint8Array {
|
|
9
|
+
private isP2TROnly: boolean = false;
|
|
10
|
+
#p2tr: string | undefined;
|
|
11
|
+
#network: Network | undefined;
|
|
12
|
+
|
|
13
|
+
public constructor(bytes?: ArrayLike<number>) {
|
|
14
|
+
super(ADDRESS_BYTE_LENGTH);
|
|
15
|
+
|
|
16
|
+
if (!bytes) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this.set(bytes);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private _keyPair: ECPairInterface | undefined;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get the key pair for the address
|
|
27
|
+
*/
|
|
28
|
+
public get keyPair(): ECPairInterface {
|
|
29
|
+
if (!this._keyPair) {
|
|
30
|
+
throw new Error('Public key not set for address');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return this._keyPair;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static dead(): Address {
|
|
37
|
+
return Address.fromString(
|
|
38
|
+
'0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f',
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create an address from a hex string
|
|
44
|
+
* @param {string} pubKey The public key
|
|
45
|
+
* @returns {Address} The address
|
|
46
|
+
*/
|
|
47
|
+
public static fromString(pubKey: string): Address {
|
|
48
|
+
if (pubKey.startsWith('0x')) {
|
|
49
|
+
pubKey = pubKey.slice(2);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return new Address(Buffer.from(pubKey, 'hex'));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Create an address from a public key
|
|
57
|
+
* @returns {Address} The address
|
|
58
|
+
* @param {ArrayLike<number>} bytes The public key
|
|
59
|
+
*/
|
|
60
|
+
public static wrap(bytes: ArrayLike<number>): Address {
|
|
61
|
+
return new Address(bytes);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Converts the address to a hex string
|
|
66
|
+
* @returns {string} The hex string
|
|
67
|
+
*/
|
|
68
|
+
public toHex(): string {
|
|
69
|
+
return '0x' + Buffer.from(this).toString('hex');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public equals(a: Address): boolean {
|
|
73
|
+
if (a.length !== this.length) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < this.length; i++) {
|
|
78
|
+
if (this[i] !== a[i]) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Check if the address is bigger than another address
|
|
88
|
+
* @returns {boolean} If bigger
|
|
89
|
+
*/
|
|
90
|
+
public isBiggerThan(a: Address): boolean {
|
|
91
|
+
for (let i = 0; i < this.length; i++) {
|
|
92
|
+
if (this[i] < a[i]) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Check if the address is smaller than another address
|
|
102
|
+
* @returns {boolean} If smaller
|
|
103
|
+
*/
|
|
104
|
+
public isSmallerThan(a: Address): boolean {
|
|
105
|
+
for (let i = 0; i < this.length; i++) {
|
|
106
|
+
if (this[i] > a[i]) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Set the public key
|
|
116
|
+
* @param {ArrayLike<number>} publicKey The public key
|
|
117
|
+
* @returns {void}
|
|
118
|
+
*/
|
|
119
|
+
public override set(publicKey: ArrayLike<number>): void {
|
|
120
|
+
if (publicKey.length !== 33 && publicKey.length !== 32 && publicKey.length !== 65) {
|
|
121
|
+
throw new Error('Invalid public key length');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (publicKey.length === 32) {
|
|
125
|
+
this.isP2TROnly = true;
|
|
126
|
+
|
|
127
|
+
const buf = Buffer.alloc(32);
|
|
128
|
+
buf.set(publicKey);
|
|
129
|
+
|
|
130
|
+
/*let lowByte: number = 0;
|
|
131
|
+
try {
|
|
132
|
+
EcKeyPair.tweakPublicKey('02' + buf.toString('hex'));
|
|
133
|
+
lowByte = 2;
|
|
134
|
+
} catch (e) {
|
|
135
|
+
console.log(`Invalid pubkey (2) ${e}`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
EcKeyPair.tweakPublicKey('03' + buf.toString('hex'));
|
|
140
|
+
lowByte = 3;
|
|
141
|
+
} catch (e) {
|
|
142
|
+
console.log(`Invalid pubkey (3) ${e}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (lowByte === 0) {
|
|
146
|
+
console.log(AddressGenerator.generateTaprootAddress(buf, networks.regtest));
|
|
147
|
+
|
|
148
|
+
throw new Error('Invalid public key');
|
|
149
|
+
}*/
|
|
150
|
+
|
|
151
|
+
super.set(publicKey);
|
|
152
|
+
} else {
|
|
153
|
+
this._keyPair = EcKeyPair.fromPublicKey(Uint8Array.from(publicKey));
|
|
154
|
+
|
|
155
|
+
const tweaked = toXOnly(
|
|
156
|
+
Buffer.from(
|
|
157
|
+
EcKeyPair.tweakPublicKey(this._keyPair.publicKey.toString('hex')),
|
|
158
|
+
'hex',
|
|
159
|
+
),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
super.set(Uint8Array.from(tweaked));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Check if the public key is valid
|
|
168
|
+
* @param {Network} network The network
|
|
169
|
+
* @returns {boolean} If the public key is valid
|
|
170
|
+
*/
|
|
171
|
+
public isValid(network: Network): boolean {
|
|
172
|
+
return AddressVerificator.isValidPublicKey(Buffer.from(this).toString('hex'), network);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Get the address in p2wpkh format
|
|
177
|
+
* @param {Network} network The network
|
|
178
|
+
*/
|
|
179
|
+
public p2wpkh(network: Network): string {
|
|
180
|
+
return EcKeyPair.getP2WPKHAddress(this.keyPair, network);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get the address in p2pkh format
|
|
185
|
+
* @param {Network} network The network
|
|
186
|
+
*/
|
|
187
|
+
public p2pkh(network: Network): string {
|
|
188
|
+
return EcKeyPair.getLegacyAddress(this.keyPair, network);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get the address in p2sh-p2wpkh format
|
|
193
|
+
* @param {Network} network The network
|
|
194
|
+
*/
|
|
195
|
+
public p2shp2wpkh(network: Network): string {
|
|
196
|
+
return EcKeyPair.getLegacySegwitAddress(this.keyPair, network);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Convert the address to a string
|
|
201
|
+
*/
|
|
202
|
+
public toString(): string {
|
|
203
|
+
if (this.#p2tr) {
|
|
204
|
+
return this.#p2tr;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return this.toHex();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Get the address in p2tr format
|
|
212
|
+
* @param {Network} network The network
|
|
213
|
+
*/
|
|
214
|
+
public p2tr(network: Network): string {
|
|
215
|
+
if (this.#p2tr && this.#network === network) {
|
|
216
|
+
return this.#p2tr;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let p2trAddy: string | undefined;
|
|
220
|
+
if (this._keyPair) {
|
|
221
|
+
p2trAddy = EcKeyPair.getTaprootAddress(this.keyPair, network);
|
|
222
|
+
} else if (this.isP2TROnly) {
|
|
223
|
+
p2trAddy = EcKeyPair.tweakedPubKeyBufferToAddress(this, network);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (p2trAddy) {
|
|
227
|
+
this.#network = network;
|
|
228
|
+
this.#p2tr = p2trAddy;
|
|
229
|
+
|
|
230
|
+
return p2trAddy;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
throw new Error('Public key not set');
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { address, initEccLib, Network } from 'bitcoinjs-lib';
|
|
2
2
|
import * as ecc from '@bitcoinerlab/secp256k1';
|
|
3
|
-
import { Address } from '@btc-vision/bsi-binary';
|
|
4
3
|
import { EcKeyPair } from './EcKeyPair.js';
|
|
5
4
|
|
|
6
5
|
initEccLib(ecc);
|
|
@@ -23,7 +22,7 @@ export class AddressVerificator {
|
|
|
23
22
|
* @returns - True if the address is a valid P2PKH address, false otherwise.
|
|
24
23
|
* @remarks This method is useful for validating legacy addresses (P2PKH) without
|
|
25
24
|
*/
|
|
26
|
-
public static isValidP2TRAddress(inAddress:
|
|
25
|
+
public static isValidP2TRAddress(inAddress: string, network: Network): boolean {
|
|
27
26
|
if (!inAddress || inAddress.length < 50) return false;
|
|
28
27
|
|
|
29
28
|
let isValidTapRootAddress: boolean = false;
|
|
@@ -104,8 +103,15 @@ export class AddressVerificator {
|
|
|
104
103
|
// Compressed public keys are 66 characters long (0x02 or 0x03 prefix + 32 bytes)
|
|
105
104
|
// Uncompressed public keys are 130 characters long (0x04 prefix + 64 bytes)
|
|
106
105
|
const hexRegex = /^[0-9a-fA-F]+$/;
|
|
106
|
+
if (!hexRegex.test(input)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
107
109
|
|
|
108
|
-
if (
|
|
110
|
+
if (input.length === 64) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (input.length === 66 || input.length === 130) {
|
|
109
115
|
// Check if the input can be parsed as a valid public key
|
|
110
116
|
const pubKeyBuffer = Buffer.from(input, 'hex');
|
|
111
117
|
EcKeyPair.fromPublicKey(pubKeyBuffer, network);
|
|
@@ -113,8 +119,6 @@ export class AddressVerificator {
|
|
|
113
119
|
return true;
|
|
114
120
|
}
|
|
115
121
|
} catch (error) {
|
|
116
|
-
console.log(error);
|
|
117
|
-
|
|
118
122
|
// If any error occurs (invalid public key, etc.), return false
|
|
119
123
|
return false;
|
|
120
124
|
}
|
package/src/keypair/EcKeyPair.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as ecc from '@bitcoinerlab/secp256k1';
|
|
2
|
-
import { Address } from '@btc-vision/bsi-binary';
|
|
3
2
|
import bip32, { BIP32API, BIP32Factory, BIP32Interface } from 'bip32';
|
|
4
3
|
import { address, initEccLib, Network, networks, payments, Signer } from 'bitcoinjs-lib';
|
|
5
4
|
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
@@ -73,14 +72,14 @@ export class EcKeyPair {
|
|
|
73
72
|
* @param {Buffer[]} pubKeys - The public keys to use
|
|
74
73
|
* @param {number} minimumSignatureRequired - The minimum number of signatures required
|
|
75
74
|
* @param {Network} network - The network to use
|
|
76
|
-
* @returns {
|
|
75
|
+
* @returns {string} - The generated address
|
|
77
76
|
* @throws {Error} - If the address cannot be generated
|
|
78
77
|
*/
|
|
79
78
|
public static generateMultiSigAddress(
|
|
80
79
|
pubKeys: Buffer[],
|
|
81
80
|
minimumSignatureRequired: number,
|
|
82
81
|
network: Network = networks.bitcoin,
|
|
83
|
-
):
|
|
82
|
+
): string {
|
|
84
83
|
const publicKeys: Buffer[] = this.verifyPubKeys(pubKeys, network);
|
|
85
84
|
if (publicKeys.length !== pubKeys.length) throw new Error(`Contains invalid public keys`);
|
|
86
85
|
|
|
@@ -93,20 +92,6 @@ export class EcKeyPair {
|
|
|
93
92
|
const p2wsh = payments.p2wsh({ redeem: p2ms, network: network });
|
|
94
93
|
const address = p2wsh.address;
|
|
95
94
|
|
|
96
|
-
// fake params
|
|
97
|
-
/*const multiSignParams: MultiSignParameters = {
|
|
98
|
-
network: network,
|
|
99
|
-
utxos: [],
|
|
100
|
-
pubkeys: pubKeys,
|
|
101
|
-
minimumSignatures: minimumSignatureRequired,
|
|
102
|
-
feeRate: 100,
|
|
103
|
-
receiver: 'a',
|
|
104
|
-
requestedAmount: 1n,
|
|
105
|
-
refundVault: 'a',
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const address = new MultiSignTransaction(multiSignParams).getScriptAddress();
|
|
109
|
-
*/
|
|
110
95
|
if (!address) {
|
|
111
96
|
throw new Error('Failed to generate address');
|
|
112
97
|
}
|
|
@@ -137,12 +122,12 @@ export class EcKeyPair {
|
|
|
137
122
|
* Get a P2WPKH address from a keypair
|
|
138
123
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
139
124
|
* @param {Network} network - The network to use
|
|
140
|
-
* @returns {
|
|
125
|
+
* @returns {string} - The address
|
|
141
126
|
*/
|
|
142
127
|
public static getP2WPKHAddress(
|
|
143
128
|
keyPair: ECPairInterface,
|
|
144
129
|
network: Network = networks.bitcoin,
|
|
145
|
-
):
|
|
130
|
+
): string {
|
|
146
131
|
const res = payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey), network: network });
|
|
147
132
|
|
|
148
133
|
if (!res.address) {
|
|
@@ -156,7 +141,7 @@ export class EcKeyPair {
|
|
|
156
141
|
* Get the address of a tweaked public key
|
|
157
142
|
* @param {string} tweakedPubKeyHex - The tweaked public key hex string
|
|
158
143
|
* @param {Network} network - The network to use
|
|
159
|
-
* @returns {
|
|
144
|
+
* @returns {string} - The address
|
|
160
145
|
* @throws {Error} - If the address cannot be generated
|
|
161
146
|
*/
|
|
162
147
|
public static tweakedPubKeyToAddress(tweakedPubKeyHex: string, network: Network): string {
|
|
@@ -165,11 +150,28 @@ export class EcKeyPair {
|
|
|
165
150
|
}
|
|
166
151
|
|
|
167
152
|
// Convert the tweaked public key hex string to a Buffer
|
|
168
|
-
|
|
153
|
+
let tweakedPubKeyBuffer = Buffer.from(tweakedPubKeyHex, 'hex');
|
|
154
|
+
if (tweakedPubKeyBuffer.length !== 32) tweakedPubKeyBuffer = toXOnly(tweakedPubKeyBuffer);
|
|
155
|
+
|
|
156
|
+
return EcKeyPair.tweakedPubKeyBufferToAddress(tweakedPubKeyBuffer, network);
|
|
157
|
+
}
|
|
169
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Get the address of a tweaked public key
|
|
161
|
+
* @param {Buffer | Uint8Array} tweakedPubKeyBuffer - The tweaked public key buffer
|
|
162
|
+
* @param {Network} network - The network to use
|
|
163
|
+
* @returns {string} - The address
|
|
164
|
+
* @throws {Error} - If the address cannot be generated
|
|
165
|
+
*/
|
|
166
|
+
public static tweakedPubKeyBufferToAddress(
|
|
167
|
+
tweakedPubKeyBuffer: Buffer | Uint8Array,
|
|
168
|
+
network: Network,
|
|
169
|
+
): string {
|
|
170
170
|
// Generate the Taproot address using the p2tr payment method
|
|
171
171
|
const { address } = payments.p2tr({
|
|
172
|
-
pubkey:
|
|
172
|
+
pubkey: Buffer.isBuffer(tweakedPubKeyBuffer)
|
|
173
|
+
? tweakedPubKeyBuffer
|
|
174
|
+
: Buffer.from(tweakedPubKeyBuffer),
|
|
173
175
|
network: network,
|
|
174
176
|
});
|
|
175
177
|
|
|
@@ -184,7 +186,7 @@ export class EcKeyPair {
|
|
|
184
186
|
* Get the address of a xOnly tweaked public key
|
|
185
187
|
* @param {string} tweakedPubKeyHex - The xOnly tweaked public key hex string
|
|
186
188
|
* @param {Network} network - The network to use
|
|
187
|
-
* @returns {
|
|
189
|
+
* @returns {string} - The address
|
|
188
190
|
* @throws {Error} - If the address cannot be generated
|
|
189
191
|
*/
|
|
190
192
|
public static xOnlyTweakedPubKeyToAddress(tweakedPubKeyHex: string, network: Network): string {
|
|
@@ -267,12 +269,12 @@ export class EcKeyPair {
|
|
|
267
269
|
|
|
268
270
|
/**
|
|
269
271
|
* Verify that a contract address is a valid p2tr address
|
|
270
|
-
* @param {
|
|
272
|
+
* @param {string} contractAddress - The contract address to verify
|
|
271
273
|
* @param {Network} network - The network to use
|
|
272
274
|
* @returns {boolean} - Whether the address is valid
|
|
273
275
|
*/
|
|
274
276
|
public static verifyContractAddress(
|
|
275
|
-
contractAddress:
|
|
277
|
+
contractAddress: string,
|
|
276
278
|
network: Network = networks.bitcoin,
|
|
277
279
|
): boolean {
|
|
278
280
|
return !!address.toOutputScript(contractAddress, network);
|
|
@@ -282,12 +284,12 @@ export class EcKeyPair {
|
|
|
282
284
|
* Get the legacy segwit address from a keypair
|
|
283
285
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
284
286
|
* @param {Network} network - The network to use
|
|
285
|
-
* @returns {
|
|
287
|
+
* @returns {string} - The legacy address
|
|
286
288
|
*/
|
|
287
289
|
public static getLegacySegwitAddress(
|
|
288
290
|
keyPair: ECPairInterface,
|
|
289
291
|
network: Network = networks.bitcoin,
|
|
290
|
-
):
|
|
292
|
+
): string {
|
|
291
293
|
const wallet = payments.p2sh({
|
|
292
294
|
redeem: payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey), network: network }),
|
|
293
295
|
network: network,
|
|
@@ -304,12 +306,12 @@ export class EcKeyPair {
|
|
|
304
306
|
* Get the legacy address from a keypair
|
|
305
307
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
306
308
|
* @param {Network} network - The network to use
|
|
307
|
-
* @returns {
|
|
309
|
+
* @returns {string} - The legacy address
|
|
308
310
|
*/
|
|
309
311
|
public static getLegacyAddress(
|
|
310
312
|
keyPair: ECPairInterface,
|
|
311
313
|
network: Network = networks.bitcoin,
|
|
312
|
-
):
|
|
314
|
+
): string {
|
|
313
315
|
const wallet = payments.p2pkh({ pubkey: Buffer.from(keyPair.publicKey), network: network });
|
|
314
316
|
if (!wallet.address) {
|
|
315
317
|
throw new Error('Failed to generate wallet');
|
|
@@ -322,12 +324,12 @@ export class EcKeyPair {
|
|
|
322
324
|
* Get the legacy address from a keypair
|
|
323
325
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
324
326
|
* @param {Network} network - The network to use
|
|
325
|
-
* @returns {
|
|
327
|
+
* @returns {string} - The legacy address
|
|
326
328
|
*/
|
|
327
329
|
public static getP2PKAddress(
|
|
328
330
|
keyPair: ECPairInterface,
|
|
329
331
|
network: Network = networks.bitcoin,
|
|
330
|
-
):
|
|
332
|
+
): string {
|
|
331
333
|
const wallet = payments.p2pk({ pubkey: Buffer.from(keyPair.publicKey), network: network });
|
|
332
334
|
if (!wallet.output) {
|
|
333
335
|
throw new Error('Failed to generate wallet');
|
|
@@ -361,12 +363,12 @@ export class EcKeyPair {
|
|
|
361
363
|
* Get taproot address from keypair
|
|
362
364
|
* @param {ECPairInterface} keyPair - The keypair to get the taproot address for
|
|
363
365
|
* @param {Network} network - The network to use
|
|
364
|
-
* @returns {
|
|
366
|
+
* @returns {string} - The taproot address
|
|
365
367
|
*/
|
|
366
368
|
public static getTaprootAddress(
|
|
367
369
|
keyPair: ECPairInterface | Signer,
|
|
368
370
|
network: Network = networks.bitcoin,
|
|
369
|
-
):
|
|
371
|
+
): string {
|
|
370
372
|
const { address } = payments.p2tr({
|
|
371
373
|
internalPubkey: toXOnly(Buffer.from(keyPair.publicKey)),
|
|
372
374
|
network: network,
|
|
@@ -381,14 +383,14 @@ export class EcKeyPair {
|
|
|
381
383
|
|
|
382
384
|
/**
|
|
383
385
|
* Get taproot address from address
|
|
384
|
-
* @param {
|
|
386
|
+
* @param {string} inAddr - The address to convert to taproot
|
|
385
387
|
* @param {Network} network - The network to use
|
|
386
|
-
* @returns {
|
|
388
|
+
* @returns {string} - The taproot address
|
|
387
389
|
*/
|
|
388
390
|
public static getTaprootAddressFromAddress(
|
|
389
|
-
inAddr:
|
|
391
|
+
inAddr: string,
|
|
390
392
|
network: Network = networks.bitcoin,
|
|
391
|
-
):
|
|
393
|
+
): string {
|
|
392
394
|
const { address } = payments.p2tr({
|
|
393
395
|
address: inAddr,
|
|
394
396
|
network: network,
|
package/src/keypair/Wallet.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { IWallet } from './interfaces/IWallet.js';
|
|
|
2
2
|
import { ECPairInterface } from 'ecpair';
|
|
3
3
|
import { EcKeyPair } from './EcKeyPair.js';
|
|
4
4
|
import { Network, networks } from 'bitcoinjs-lib';
|
|
5
|
-
import { Address } from '@btc-vision/bsi-binary';
|
|
6
5
|
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
6
|
+
import { Address } from './Address.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Wallet class
|
|
@@ -19,19 +19,25 @@ export class Wallet {
|
|
|
19
19
|
* P2WPKH address for the wallet
|
|
20
20
|
* @private
|
|
21
21
|
*/
|
|
22
|
-
private readonly _p2wpkh:
|
|
22
|
+
private readonly _p2wpkh: string;
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* P2TR address for the wallet
|
|
26
26
|
* @private
|
|
27
27
|
*/
|
|
28
|
-
private readonly _p2tr:
|
|
28
|
+
private readonly _p2tr: string;
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Legacy address for the wallet
|
|
32
32
|
* @private
|
|
33
33
|
*/
|
|
34
|
-
private readonly _legacy:
|
|
34
|
+
private readonly _legacy: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Legacy address for the wallet
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
private readonly _segwitLegacy: string;
|
|
35
41
|
|
|
36
42
|
/**
|
|
37
43
|
* Buffer public key
|
|
@@ -39,6 +45,18 @@ export class Wallet {
|
|
|
39
45
|
*/
|
|
40
46
|
private readonly _bufferPubKey: Buffer;
|
|
41
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Tweaked key
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
private readonly _tweakedKey: Buffer;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Address corresponding to the wallet
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
private readonly _address: Address;
|
|
59
|
+
|
|
42
60
|
constructor(
|
|
43
61
|
wallet: IWallet,
|
|
44
62
|
public readonly network: Network = networks.bitcoin,
|
|
@@ -48,8 +66,31 @@ export class Wallet {
|
|
|
48
66
|
this._p2wpkh = EcKeyPair.getP2WPKHAddress(this._keypair, this.network);
|
|
49
67
|
this._p2tr = EcKeyPair.getTaprootAddress(this._keypair, this.network);
|
|
50
68
|
this._legacy = EcKeyPair.getLegacyAddress(this._keypair, this.network);
|
|
69
|
+
this._segwitLegacy = EcKeyPair.getLegacySegwitAddress(this._keypair, this.network);
|
|
70
|
+
|
|
71
|
+
this._tweakedKey = Buffer.from(
|
|
72
|
+
EcKeyPair.tweakPublicKey(this._keypair.publicKey.toString('hex')),
|
|
73
|
+
'hex',
|
|
74
|
+
);
|
|
51
75
|
|
|
52
76
|
this._bufferPubKey = Buffer.from(wallet.publicKey, 'hex');
|
|
77
|
+
this._address = new Address(this._keypair.publicKey);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the address for the wallet
|
|
82
|
+
* @returns {Address}
|
|
83
|
+
*/
|
|
84
|
+
public get address(): Address {
|
|
85
|
+
return this._address;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the tweaked key
|
|
90
|
+
* @returns {Buffer}
|
|
91
|
+
*/
|
|
92
|
+
public get tweakedPubKeyKey(): Buffer {
|
|
93
|
+
return this._tweakedKey;
|
|
53
94
|
}
|
|
54
95
|
|
|
55
96
|
/**
|
|
@@ -64,25 +105,25 @@ export class Wallet {
|
|
|
64
105
|
|
|
65
106
|
/**
|
|
66
107
|
* Get the P2WPKH address for the wallet
|
|
67
|
-
* @returns {
|
|
108
|
+
* @returns {string}
|
|
68
109
|
*/
|
|
69
|
-
public get p2wpkh():
|
|
110
|
+
public get p2wpkh(): string {
|
|
70
111
|
return this._p2wpkh;
|
|
71
112
|
}
|
|
72
113
|
|
|
73
114
|
/**
|
|
74
115
|
* Get the P2TR address for the wallet
|
|
75
|
-
* @returns {
|
|
116
|
+
* @returns {string}
|
|
76
117
|
*/
|
|
77
|
-
public get p2tr():
|
|
118
|
+
public get p2tr(): string {
|
|
78
119
|
return this._p2tr;
|
|
79
120
|
}
|
|
80
121
|
|
|
81
122
|
/**
|
|
82
123
|
* Get the legacy address for the wallet
|
|
83
|
-
* @returns {
|
|
124
|
+
* @returns {string}
|
|
84
125
|
*/
|
|
85
|
-
public get legacy():
|
|
126
|
+
public get legacy(): string {
|
|
86
127
|
return this._legacy;
|
|
87
128
|
}
|
|
88
129
|
|
|
@@ -90,8 +131,16 @@ export class Wallet {
|
|
|
90
131
|
* Get the addresses for the wallet
|
|
91
132
|
* @returns {Address[]}
|
|
92
133
|
*/
|
|
93
|
-
public get addresses():
|
|
94
|
-
return [this.p2wpkh, this.p2tr, this.legacy];
|
|
134
|
+
public get addresses(): string[] {
|
|
135
|
+
return [this.p2wpkh, this.p2tr, this.legacy, this.segwitLegacy];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Get the segwit legacy address for the wallet
|
|
140
|
+
* @returns {string}
|
|
141
|
+
*/
|
|
142
|
+
public get segwitLegacy(): string {
|
|
143
|
+
return this._segwitLegacy;
|
|
95
144
|
}
|
|
96
145
|
|
|
97
146
|
/**
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Address } from '@btc-vision/bsi-binary';
|
|
2
1
|
import { Network, networks } from 'bitcoinjs-lib';
|
|
3
2
|
|
|
4
3
|
export abstract class ContractBaseMetadata {
|
|
5
|
-
protected abstract readonly address:
|
|
4
|
+
protected abstract readonly address: string;
|
|
6
5
|
|
|
7
6
|
protected constructor(protected network: Network = networks.bitcoin) {}
|
|
8
7
|
|
|
@@ -10,14 +9,14 @@ export abstract class ContractBaseMetadata {
|
|
|
10
9
|
* @description Get the contract address
|
|
11
10
|
* @param {Network} network - The network to get the address for
|
|
12
11
|
*/
|
|
13
|
-
public static getAddress(network: Network = networks.bitcoin):
|
|
12
|
+
public static getAddress(network: Network = networks.bitcoin): string {
|
|
14
13
|
throw new Error('Method not implemented.');
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
17
|
* @description Get the contract address
|
|
19
18
|
*/
|
|
20
|
-
public getAddress():
|
|
19
|
+
public getAddress(): string {
|
|
21
20
|
return this.address;
|
|
22
21
|
}
|
|
23
22
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Address } from '@btc-vision/bsi-binary';
|
|
2
1
|
import { Network, networks } from 'bitcoinjs-lib';
|
|
3
2
|
import { ChainId } from '../../network/ChainId.js';
|
|
4
3
|
import { ContractBaseMetadata } from '../ContractBaseMetadata.js';
|
|
5
4
|
import { WBTC_ADDRESS_FRACTAL, WBTC_ADDRESS_REGTEST, WBTC_ADDRESS_TESTNET } from '../tokens.js';
|
|
5
|
+
import { Address } from '../../keypair/Address.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @description Wrapped Bitcoin (wBTC) contract metadata.
|
|
@@ -23,7 +23,7 @@ export class wBTC extends ContractBaseMetadata {
|
|
|
23
23
|
*/
|
|
24
24
|
public readonly decimals: number = 8;
|
|
25
25
|
|
|
26
|
-
protected readonly address:
|
|
26
|
+
protected readonly address: string;
|
|
27
27
|
|
|
28
28
|
constructor(
|
|
29
29
|
protected network: Network = networks.bitcoin,
|
|
@@ -34,14 +34,14 @@ export class wBTC extends ContractBaseMetadata {
|
|
|
34
34
|
this.address = wBTC.getAddress(network, chainId);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
public static getAddress(network: Network = networks.bitcoin, chainId?: ChainId):
|
|
37
|
+
public static getAddress(network: Network = networks.bitcoin, chainId?: ChainId): string {
|
|
38
38
|
switch (network.bech32) {
|
|
39
39
|
case networks.bitcoin.bech32:
|
|
40
|
-
return this.getWBTCAddressForChain(chainId ?? ChainId.Bitcoin);
|
|
40
|
+
return this.getWBTCAddressForChain(chainId ?? ChainId.Bitcoin).p2tr(network);
|
|
41
41
|
case networks.regtest.bech32:
|
|
42
|
-
return WBTC_ADDRESS_REGTEST;
|
|
42
|
+
return WBTC_ADDRESS_REGTEST.p2tr(network);
|
|
43
43
|
case networks.testnet.bech32:
|
|
44
|
-
return WBTC_ADDRESS_TESTNET;
|
|
44
|
+
return WBTC_ADDRESS_TESTNET.p2tr(network);
|
|
45
45
|
default:
|
|
46
46
|
throw new Error(`Invalid network: ${network.bech32}`);
|
|
47
47
|
}
|
|
@@ -49,12 +49,11 @@ export class wBTC extends ContractBaseMetadata {
|
|
|
49
49
|
|
|
50
50
|
private static getWBTCAddressForChain(chainId: ChainId): Address {
|
|
51
51
|
switch (chainId) {
|
|
52
|
-
case ChainId.Bitcoin:
|
|
53
|
-
return 'unknown';
|
|
52
|
+
//case ChainId.Bitcoin:
|
|
54
53
|
case ChainId.Fractal:
|
|
55
54
|
return WBTC_ADDRESS_FRACTAL;
|
|
56
55
|
default:
|
|
57
|
-
throw new Error(`
|
|
56
|
+
throw new Error(`Unsupported chainId: ${chainId}`);
|
|
58
57
|
}
|
|
59
58
|
}
|
|
60
59
|
}
|