@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
package/src/keypair/EcKeyPair.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
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
|
-
import { address, initEccLib, Network, networks, payments } from 'bitcoinjs-lib';
|
|
3
|
+
import { address, initEccLib, Network, networks, payments, Signer } from 'bitcoinjs-lib';
|
|
5
4
|
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
6
5
|
import { ECPairAPI, ECPairFactory, ECPairInterface } from 'ecpair';
|
|
7
6
|
import { IWallet } from './interfaces/IWallet.js';
|
|
7
|
+
import { CURVE, Point, utils } from '@noble/secp256k1';
|
|
8
|
+
import { taggedHash } from 'bitcoinjs-lib/src/crypto.js';
|
|
8
9
|
|
|
9
10
|
initEccLib(ecc);
|
|
10
11
|
|
|
@@ -42,23 +43,28 @@ export class EcKeyPair {
|
|
|
42
43
|
* @returns {ECPairInterface} - The generated keypair
|
|
43
44
|
*/
|
|
44
45
|
public static fromPrivateKey(
|
|
45
|
-
privateKey: Buffer,
|
|
46
|
+
privateKey: Buffer | Uint8Array,
|
|
46
47
|
network: Network = networks.bitcoin,
|
|
47
48
|
): ECPairInterface {
|
|
48
|
-
return this.ECPair.fromPrivateKey(
|
|
49
|
+
return this.ECPair.fromPrivateKey(
|
|
50
|
+
!Buffer.isBuffer(privateKey) ? Buffer.from(privateKey) : privateKey,
|
|
51
|
+
{ network },
|
|
52
|
+
);
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
/**
|
|
52
56
|
* Generate a keypair from a public key
|
|
53
|
-
* @param {Buffer} publicKey - The public key to use
|
|
57
|
+
* @param {Buffer | Uint8Array} publicKey - The public key to use
|
|
54
58
|
* @param {Network} network - The network to use
|
|
55
59
|
* @returns {ECPairInterface} - The generated keypair
|
|
56
60
|
*/
|
|
57
61
|
public static fromPublicKey(
|
|
58
|
-
publicKey: Buffer,
|
|
62
|
+
publicKey: Buffer | Uint8Array,
|
|
59
63
|
network: Network = networks.bitcoin,
|
|
60
64
|
): ECPairInterface {
|
|
61
|
-
|
|
65
|
+
const buf = !Buffer.isBuffer(publicKey) ? Buffer.from(publicKey) : publicKey;
|
|
66
|
+
|
|
67
|
+
return this.ECPair.fromPublicKey(buf, { network });
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
/**
|
|
@@ -66,14 +72,14 @@ export class EcKeyPair {
|
|
|
66
72
|
* @param {Buffer[]} pubKeys - The public keys to use
|
|
67
73
|
* @param {number} minimumSignatureRequired - The minimum number of signatures required
|
|
68
74
|
* @param {Network} network - The network to use
|
|
69
|
-
* @returns {
|
|
75
|
+
* @returns {string} - The generated address
|
|
70
76
|
* @throws {Error} - If the address cannot be generated
|
|
71
77
|
*/
|
|
72
78
|
public static generateMultiSigAddress(
|
|
73
79
|
pubKeys: Buffer[],
|
|
74
80
|
minimumSignatureRequired: number,
|
|
75
81
|
network: Network = networks.bitcoin,
|
|
76
|
-
):
|
|
82
|
+
): string {
|
|
77
83
|
const publicKeys: Buffer[] = this.verifyPubKeys(pubKeys, network);
|
|
78
84
|
if (publicKeys.length !== pubKeys.length) throw new Error(`Contains invalid public keys`);
|
|
79
85
|
|
|
@@ -86,20 +92,6 @@ export class EcKeyPair {
|
|
|
86
92
|
const p2wsh = payments.p2wsh({ redeem: p2ms, network: network });
|
|
87
93
|
const address = p2wsh.address;
|
|
88
94
|
|
|
89
|
-
// fake params
|
|
90
|
-
/*const multiSignParams: MultiSignParameters = {
|
|
91
|
-
network: network,
|
|
92
|
-
utxos: [],
|
|
93
|
-
pubkeys: pubKeys,
|
|
94
|
-
minimumSignatures: minimumSignatureRequired,
|
|
95
|
-
feeRate: 100,
|
|
96
|
-
receiver: 'a',
|
|
97
|
-
requestedAmount: 1n,
|
|
98
|
-
refundVault: 'a',
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const address = new MultiSignTransaction(multiSignParams).getScriptAddress();
|
|
102
|
-
*/
|
|
103
95
|
if (!address) {
|
|
104
96
|
throw new Error('Failed to generate address');
|
|
105
97
|
}
|
|
@@ -122,7 +114,7 @@ export class EcKeyPair {
|
|
|
122
114
|
throw new Error('Failed to regenerate key');
|
|
123
115
|
}
|
|
124
116
|
|
|
125
|
-
return key.publicKey;
|
|
117
|
+
return Buffer.from(key.publicKey);
|
|
126
118
|
});
|
|
127
119
|
}
|
|
128
120
|
|
|
@@ -130,13 +122,13 @@ export class EcKeyPair {
|
|
|
130
122
|
* Get a P2WPKH address from a keypair
|
|
131
123
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
132
124
|
* @param {Network} network - The network to use
|
|
133
|
-
* @returns {
|
|
125
|
+
* @returns {string} - The address
|
|
134
126
|
*/
|
|
135
127
|
public static getP2WPKHAddress(
|
|
136
128
|
keyPair: ECPairInterface,
|
|
137
129
|
network: Network = networks.bitcoin,
|
|
138
|
-
):
|
|
139
|
-
const res = payments.p2wpkh({ pubkey: keyPair.publicKey, network: network });
|
|
130
|
+
): string {
|
|
131
|
+
const res = payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey), network: network });
|
|
140
132
|
|
|
141
133
|
if (!res.address) {
|
|
142
134
|
throw new Error('Failed to generate wallet');
|
|
@@ -145,6 +137,113 @@ export class EcKeyPair {
|
|
|
145
137
|
return res.address;
|
|
146
138
|
}
|
|
147
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Get the address of a tweaked public key
|
|
142
|
+
* @param {string} tweakedPubKeyHex - The tweaked public key hex string
|
|
143
|
+
* @param {Network} network - The network to use
|
|
144
|
+
* @returns {string} - The address
|
|
145
|
+
* @throws {Error} - If the address cannot be generated
|
|
146
|
+
*/
|
|
147
|
+
public static tweakedPubKeyToAddress(tweakedPubKeyHex: string, network: Network): string {
|
|
148
|
+
if (tweakedPubKeyHex.startsWith('0x')) {
|
|
149
|
+
tweakedPubKeyHex = tweakedPubKeyHex.slice(2);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Convert the tweaked public key hex string to a Buffer
|
|
153
|
+
let tweakedPubKeyBuffer = Buffer.from(tweakedPubKeyHex, 'hex');
|
|
154
|
+
if (tweakedPubKeyBuffer.length !== 32) tweakedPubKeyBuffer = toXOnly(tweakedPubKeyBuffer);
|
|
155
|
+
|
|
156
|
+
return EcKeyPair.tweakedPubKeyBufferToAddress(tweakedPubKeyBuffer, network);
|
|
157
|
+
}
|
|
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
|
+
// Generate the Taproot address using the p2tr payment method
|
|
171
|
+
const { address } = payments.p2tr({
|
|
172
|
+
pubkey: Buffer.isBuffer(tweakedPubKeyBuffer)
|
|
173
|
+
? tweakedPubKeyBuffer
|
|
174
|
+
: Buffer.from(tweakedPubKeyBuffer),
|
|
175
|
+
network: network,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if (!address) {
|
|
179
|
+
throw new Error('Failed to generate Taproot address');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return address;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get the address of a xOnly tweaked public key
|
|
187
|
+
* @param {string} tweakedPubKeyHex - The xOnly tweaked public key hex string
|
|
188
|
+
* @param {Network} network - The network to use
|
|
189
|
+
* @returns {string} - The address
|
|
190
|
+
* @throws {Error} - If the address cannot be generated
|
|
191
|
+
*/
|
|
192
|
+
public static xOnlyTweakedPubKeyToAddress(tweakedPubKeyHex: string, network: Network): string {
|
|
193
|
+
if (tweakedPubKeyHex.startsWith('0x')) {
|
|
194
|
+
tweakedPubKeyHex = tweakedPubKeyHex.slice(2);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Convert the tweaked public key hex string to a Buffer
|
|
198
|
+
const tweakedPubKeyBuffer = Buffer.from(tweakedPubKeyHex, 'hex');
|
|
199
|
+
|
|
200
|
+
// Generate the Taproot address using the p2tr payment method
|
|
201
|
+
const { address } = payments.p2tr({
|
|
202
|
+
pubkey: tweakedPubKeyBuffer,
|
|
203
|
+
network: network,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
if (!address) {
|
|
207
|
+
throw new Error('Failed to generate Taproot address');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return address;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Tweak a public key
|
|
215
|
+
* @param {string} compressedPubKeyHex - The compressed public key hex string
|
|
216
|
+
* @returns {string} - The tweaked public key hex string
|
|
217
|
+
* @throws {Error} - If the public key cannot be tweaked
|
|
218
|
+
*/
|
|
219
|
+
public static tweakPublicKey(compressedPubKeyHex: string): string {
|
|
220
|
+
if (compressedPubKeyHex.startsWith('0x')) {
|
|
221
|
+
compressedPubKeyHex = compressedPubKeyHex.slice(2);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Convert the compressed public key hex string to a Point on the curve
|
|
225
|
+
let P = Point.fromHex(compressedPubKeyHex);
|
|
226
|
+
|
|
227
|
+
// Ensure the point has an even y-coordinate
|
|
228
|
+
if (!P.hasEvenY()) {
|
|
229
|
+
// Negate the point to get an even y-coordinate
|
|
230
|
+
P = P.negate();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Get the x-coordinate (32 bytes) of the point
|
|
234
|
+
const x = P.toRawBytes(true).slice(1); // Remove the prefix byte
|
|
235
|
+
|
|
236
|
+
// Compute the tweak t = H_tapTweak(x)
|
|
237
|
+
const tHash = taggedHash('TapTweak', Buffer.from(x));
|
|
238
|
+
const t = utils.mod(BigInt('0x' + Buffer.from(tHash).toString('hex')), CURVE.n);
|
|
239
|
+
|
|
240
|
+
// Compute Q = P + t*G (where G is the generator point)
|
|
241
|
+
const Q = P.add(Point.BASE.multiply(t));
|
|
242
|
+
|
|
243
|
+
// Return the tweaked public key in compressed form (hex string)
|
|
244
|
+
return Q.toHex(true);
|
|
245
|
+
}
|
|
246
|
+
|
|
148
247
|
/**
|
|
149
248
|
* Generate a random wallet
|
|
150
249
|
* @param {Network} network - The network to use
|
|
@@ -164,18 +263,18 @@ export class EcKeyPair {
|
|
|
164
263
|
return {
|
|
165
264
|
address: wallet,
|
|
166
265
|
privateKey: keyPair.toWIF(),
|
|
167
|
-
publicKey: keyPair.publicKey.toString('hex'),
|
|
266
|
+
publicKey: Buffer.from(keyPair.publicKey).toString('hex'),
|
|
168
267
|
};
|
|
169
268
|
}
|
|
170
269
|
|
|
171
270
|
/**
|
|
172
271
|
* Verify that a contract address is a valid p2tr address
|
|
173
|
-
* @param {
|
|
272
|
+
* @param {string} contractAddress - The contract address to verify
|
|
174
273
|
* @param {Network} network - The network to use
|
|
175
274
|
* @returns {boolean} - Whether the address is valid
|
|
176
275
|
*/
|
|
177
276
|
public static verifyContractAddress(
|
|
178
|
-
contractAddress:
|
|
277
|
+
contractAddress: string,
|
|
179
278
|
network: Network = networks.bitcoin,
|
|
180
279
|
): boolean {
|
|
181
280
|
return !!address.toOutputScript(contractAddress, network);
|
|
@@ -185,14 +284,14 @@ export class EcKeyPair {
|
|
|
185
284
|
* Get the legacy segwit address from a keypair
|
|
186
285
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
187
286
|
* @param {Network} network - The network to use
|
|
188
|
-
* @returns {
|
|
287
|
+
* @returns {string} - The legacy address
|
|
189
288
|
*/
|
|
190
289
|
public static getLegacySegwitAddress(
|
|
191
290
|
keyPair: ECPairInterface,
|
|
192
291
|
network: Network = networks.bitcoin,
|
|
193
|
-
):
|
|
292
|
+
): string {
|
|
194
293
|
const wallet = payments.p2sh({
|
|
195
|
-
redeem: payments.p2wpkh({ pubkey: keyPair.publicKey, network: network }),
|
|
294
|
+
redeem: payments.p2wpkh({ pubkey: Buffer.from(keyPair.publicKey), network: network }),
|
|
196
295
|
network: network,
|
|
197
296
|
});
|
|
198
297
|
|
|
@@ -207,13 +306,13 @@ export class EcKeyPair {
|
|
|
207
306
|
* Get the legacy address from a keypair
|
|
208
307
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
209
308
|
* @param {Network} network - The network to use
|
|
210
|
-
* @returns {
|
|
309
|
+
* @returns {string} - The legacy address
|
|
211
310
|
*/
|
|
212
311
|
public static getLegacyAddress(
|
|
213
312
|
keyPair: ECPairInterface,
|
|
214
313
|
network: Network = networks.bitcoin,
|
|
215
|
-
):
|
|
216
|
-
const wallet = payments.p2pkh({ pubkey: keyPair.publicKey, network: network });
|
|
314
|
+
): string {
|
|
315
|
+
const wallet = payments.p2pkh({ pubkey: Buffer.from(keyPair.publicKey), network: network });
|
|
217
316
|
if (!wallet.address) {
|
|
218
317
|
throw new Error('Failed to generate wallet');
|
|
219
318
|
}
|
|
@@ -225,13 +324,13 @@ export class EcKeyPair {
|
|
|
225
324
|
* Get the legacy address from a keypair
|
|
226
325
|
* @param {ECPairInterface} keyPair - The keypair to get the address for
|
|
227
326
|
* @param {Network} network - The network to use
|
|
228
|
-
* @returns {
|
|
327
|
+
* @returns {string} - The legacy address
|
|
229
328
|
*/
|
|
230
329
|
public static getP2PKAddress(
|
|
231
330
|
keyPair: ECPairInterface,
|
|
232
331
|
network: Network = networks.bitcoin,
|
|
233
|
-
):
|
|
234
|
-
const wallet = payments.p2pk({ pubkey: keyPair.publicKey, network: network });
|
|
332
|
+
): string {
|
|
333
|
+
const wallet = payments.p2pk({ pubkey: Buffer.from(keyPair.publicKey), network: network });
|
|
235
334
|
if (!wallet.output) {
|
|
236
335
|
throw new Error('Failed to generate wallet');
|
|
237
336
|
}
|
|
@@ -264,14 +363,14 @@ export class EcKeyPair {
|
|
|
264
363
|
* Get taproot address from keypair
|
|
265
364
|
* @param {ECPairInterface} keyPair - The keypair to get the taproot address for
|
|
266
365
|
* @param {Network} network - The network to use
|
|
267
|
-
* @returns {
|
|
366
|
+
* @returns {string} - The taproot address
|
|
268
367
|
*/
|
|
269
368
|
public static getTaprootAddress(
|
|
270
|
-
keyPair: ECPairInterface,
|
|
369
|
+
keyPair: ECPairInterface | Signer,
|
|
271
370
|
network: Network = networks.bitcoin,
|
|
272
|
-
):
|
|
371
|
+
): string {
|
|
273
372
|
const { address } = payments.p2tr({
|
|
274
|
-
internalPubkey: toXOnly(keyPair.publicKey),
|
|
373
|
+
internalPubkey: toXOnly(Buffer.from(keyPair.publicKey)),
|
|
275
374
|
network: network,
|
|
276
375
|
});
|
|
277
376
|
|
|
@@ -284,14 +383,14 @@ export class EcKeyPair {
|
|
|
284
383
|
|
|
285
384
|
/**
|
|
286
385
|
* Get taproot address from address
|
|
287
|
-
* @param {
|
|
386
|
+
* @param {string} inAddr - The address to convert to taproot
|
|
288
387
|
* @param {Network} network - The network to use
|
|
289
|
-
* @returns {
|
|
388
|
+
* @returns {string} - The taproot address
|
|
290
389
|
*/
|
|
291
390
|
public static getTaprootAddressFromAddress(
|
|
292
|
-
inAddr:
|
|
391
|
+
inAddr: string,
|
|
293
392
|
network: Network = networks.bitcoin,
|
|
294
|
-
):
|
|
393
|
+
): string {
|
|
295
394
|
const { address } = payments.p2tr({
|
|
296
395
|
address: inAddr,
|
|
297
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,43 @@ 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;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Buffer public key
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
private readonly _bufferPubKey: Buffer;
|
|
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;
|
|
35
59
|
|
|
36
60
|
constructor(
|
|
37
61
|
wallet: IWallet,
|
|
@@ -42,6 +66,31 @@ export class Wallet {
|
|
|
42
66
|
this._p2wpkh = EcKeyPair.getP2WPKHAddress(this._keypair, this.network);
|
|
43
67
|
this._p2tr = EcKeyPair.getTaprootAddress(this._keypair, this.network);
|
|
44
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
|
+
);
|
|
75
|
+
|
|
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;
|
|
45
94
|
}
|
|
46
95
|
|
|
47
96
|
/**
|
|
@@ -56,25 +105,25 @@ export class Wallet {
|
|
|
56
105
|
|
|
57
106
|
/**
|
|
58
107
|
* Get the P2WPKH address for the wallet
|
|
59
|
-
* @returns {
|
|
108
|
+
* @returns {string}
|
|
60
109
|
*/
|
|
61
|
-
public get p2wpkh():
|
|
110
|
+
public get p2wpkh(): string {
|
|
62
111
|
return this._p2wpkh;
|
|
63
112
|
}
|
|
64
113
|
|
|
65
114
|
/**
|
|
66
115
|
* Get the P2TR address for the wallet
|
|
67
|
-
* @returns {
|
|
116
|
+
* @returns {string}
|
|
68
117
|
*/
|
|
69
|
-
public get p2tr():
|
|
118
|
+
public get p2tr(): string {
|
|
70
119
|
return this._p2tr;
|
|
71
120
|
}
|
|
72
121
|
|
|
73
122
|
/**
|
|
74
123
|
* Get the legacy address for the wallet
|
|
75
|
-
* @returns {
|
|
124
|
+
* @returns {string}
|
|
76
125
|
*/
|
|
77
|
-
public get legacy():
|
|
126
|
+
public get legacy(): string {
|
|
78
127
|
return this._legacy;
|
|
79
128
|
}
|
|
80
129
|
|
|
@@ -82,8 +131,16 @@ export class Wallet {
|
|
|
82
131
|
* Get the addresses for the wallet
|
|
83
132
|
* @returns {Address[]}
|
|
84
133
|
*/
|
|
85
|
-
public get addresses():
|
|
86
|
-
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;
|
|
87
144
|
}
|
|
88
145
|
|
|
89
146
|
/**
|
|
@@ -92,9 +149,9 @@ export class Wallet {
|
|
|
92
149
|
* @returns {Buffer}
|
|
93
150
|
*/
|
|
94
151
|
public get publicKey(): Buffer {
|
|
95
|
-
if (!this.
|
|
152
|
+
if (!this._bufferPubKey) throw new Error('Public key not set');
|
|
96
153
|
|
|
97
|
-
return this.
|
|
154
|
+
return this._bufferPubKey;
|
|
98
155
|
}
|
|
99
156
|
|
|
100
157
|
/**
|
|
@@ -105,7 +162,7 @@ export class Wallet {
|
|
|
105
162
|
public get xOnly(): Buffer {
|
|
106
163
|
if (!this.keypair) throw new Error('Keypair not set');
|
|
107
164
|
|
|
108
|
-
return toXOnly(this.
|
|
165
|
+
return toXOnly(this._bufferPubKey);
|
|
109
166
|
}
|
|
110
167
|
|
|
111
168
|
/**
|
|
@@ -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
|
}
|
package/src/metadata/tokens.ts
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
|
-
import { Address } from '@btc-vision/bsi-binary';
|
|
2
1
|
import { ChainId } from '../network/ChainId.js';
|
|
2
|
+
import { Address } from '../keypair/Address.js';
|
|
3
|
+
|
|
4
|
+
function deadAddress(): Address {
|
|
5
|
+
return Address.dead();
|
|
6
|
+
}
|
|
3
7
|
|
|
4
8
|
// Addresses Regtest
|
|
5
|
-
export const FACTORY_ADDRESS_REGTEST: Address =
|
|
6
|
-
|
|
7
|
-
export const
|
|
8
|
-
export const
|
|
9
|
-
export const
|
|
9
|
+
export const FACTORY_ADDRESS_REGTEST: Address = deadAddress();
|
|
10
|
+
|
|
11
|
+
export const POOL_ADDRESS_REGTEST: Address = deadAddress();
|
|
12
|
+
export const WBTC_ADDRESS_REGTEST: Address = deadAddress();
|
|
13
|
+
export const MOTO_ADDRESS_REGTEST: Address = deadAddress();
|
|
14
|
+
export const ROUTER_ADDRESS_REGTEST: Address = deadAddress();
|
|
10
15
|
|
|
11
16
|
// Addresses Testnet
|
|
12
|
-
export const FACTORY_ADDRESS_TESTNET: Address =
|
|
13
|
-
export const POOL_ADDRESS_TESTNET: Address =
|
|
14
|
-
export const WBTC_ADDRESS_TESTNET: Address =
|
|
15
|
-
export const MOTO_ADDRESS_TESTNET: Address =
|
|
16
|
-
export const ROUTER_ADDRESS_TESTNET: Address =
|
|
17
|
+
export const FACTORY_ADDRESS_TESTNET: Address = deadAddress();
|
|
18
|
+
export const POOL_ADDRESS_TESTNET: Address = deadAddress();
|
|
19
|
+
export const WBTC_ADDRESS_TESTNET: Address = deadAddress();
|
|
20
|
+
export const MOTO_ADDRESS_TESTNET: Address = deadAddress();
|
|
21
|
+
export const ROUTER_ADDRESS_TESTNET: Address = deadAddress();
|
|
17
22
|
|
|
18
23
|
// Addresses Fractal
|
|
19
|
-
export const FACTORY_ADDRESS_FRACTAL: Address =
|
|
20
|
-
export const POOL_ADDRESS_FRACTAL: Address =
|
|
21
|
-
export const WBTC_ADDRESS_FRACTAL: Address =
|
|
22
|
-
export const MOTO_ADDRESS_FRACTAL: Address =
|
|
23
|
-
export const ROUTER_ADDRESS_FRACTAL: Address =
|
|
24
|
+
export const FACTORY_ADDRESS_FRACTAL: Address = deadAddress();
|
|
25
|
+
export const POOL_ADDRESS_FRACTAL: Address = deadAddress();
|
|
26
|
+
export const WBTC_ADDRESS_FRACTAL: Address = deadAddress();
|
|
27
|
+
export const MOTO_ADDRESS_FRACTAL: Address = deadAddress();
|
|
28
|
+
export const ROUTER_ADDRESS_FRACTAL: Address = deadAddress();
|
|
24
29
|
|
|
25
30
|
export enum OPNetNetwork {
|
|
26
31
|
Mainnet = 'mainnet',
|
package/src/opnet.ts
CHANGED
|
@@ -73,6 +73,19 @@ export * from './consensus/ConsensusConfig.js';
|
|
|
73
73
|
export * from './consensus/Consensus.js';
|
|
74
74
|
export * from './consensus/metadata/RoswellConsensus.js';
|
|
75
75
|
|
|
76
|
+
/** Binary */
|
|
77
|
+
export * from './utils/BufferHelper.js';
|
|
78
|
+
export * from './utils/types.js';
|
|
79
|
+
export * from './keypair/Address.js';
|
|
80
|
+
export * from './event/NetEvent.js';
|
|
81
|
+
export * from './deterministic/DeterministicMap.js';
|
|
82
|
+
export * from './deterministic/DeterministicSet.js';
|
|
83
|
+
export * from './deterministic/AddressMap.js';
|
|
84
|
+
export * from './deterministic/AddressSet.js';
|
|
85
|
+
export * from './abi/ABICoder.js';
|
|
86
|
+
export * from './buffer/BinaryWriter.js';
|
|
87
|
+
export * from './buffer/BinaryReader.js';
|
|
88
|
+
|
|
76
89
|
/** Custom signers */
|
|
77
90
|
export * from './transaction/browser/BrowserSignerBase.js';
|
|
78
91
|
export * from './transaction/browser/extensions/UnisatSigner.js';
|
|
@@ -31,8 +31,9 @@ export class TweakedSigner {
|
|
|
31
31
|
* Tweak a signer
|
|
32
32
|
* @param {Signer} signer - The signer to tweak
|
|
33
33
|
* @param {TweakSettings} opts - The tweak settings
|
|
34
|
+
* @returns {ECPairInterface} - The tweaked signer
|
|
34
35
|
*/
|
|
35
|
-
public static tweakSigner(signer: ECPairInterface, opts: TweakSettings = {}):
|
|
36
|
+
public static tweakSigner(signer: ECPairInterface, opts: TweakSettings = {}): ECPairInterface {
|
|
36
37
|
let privateKey: Uint8Array | undefined = signer.privateKey;
|
|
37
38
|
if (!privateKey) {
|
|
38
39
|
throw new Error('Private key is required for tweaking signer!');
|
|
@@ -44,7 +45,7 @@ export class TweakedSigner {
|
|
|
44
45
|
|
|
45
46
|
const tweakedPrivateKey = ecc.privateAdd(
|
|
46
47
|
privateKey,
|
|
47
|
-
tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash),
|
|
48
|
+
tapTweakHash(toXOnly(Buffer.from(signer.publicKey)), opts.tweakHash),
|
|
48
49
|
);
|
|
49
50
|
|
|
50
51
|
if (!tweakedPrivateKey) {
|