@rsksmart/powpeg-sdk 1.0.1-beta.1 → 1.2.0-beta.2
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/dist/index.d.mts +27 -7
- package/dist/index.d.ts +27 -7
- package/dist/index.js +79 -36
- package/dist/index.mjs +63 -24
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Psbt, networks as networks$1 } from 'bitcoinjs-lib';
|
|
2
|
-
import { ethers } from '@rsksmart/bridges-core-sdk';
|
|
2
|
+
import { isBtcTestnetAddress, isBtcMainnetAddress, ethers } from '@rsksmart/bridges-core-sdk';
|
|
3
3
|
import TrezorConnect from '@trezor/connect-web';
|
|
4
4
|
import { CreateTransactionArg } from '@ledgerhq/hw-app-btc/lib/createTransaction';
|
|
5
5
|
|
|
@@ -129,12 +129,14 @@ declare const networks: {
|
|
|
129
129
|
readonly coin: "1";
|
|
130
130
|
readonly lib: networks$1.Network;
|
|
131
131
|
readonly xpubVersion: 70617039;
|
|
132
|
+
readonly isBtcAddress: typeof isBtcTestnetAddress;
|
|
132
133
|
};
|
|
133
134
|
readonly MAIN: {
|
|
134
135
|
readonly currency: "bitcoin";
|
|
135
136
|
readonly coin: "0";
|
|
136
137
|
readonly lib: networks$1.Network;
|
|
137
138
|
readonly xpubVersion: 76067358;
|
|
139
|
+
readonly isBtcAddress: typeof isBtcMainnetAddress;
|
|
138
140
|
};
|
|
139
141
|
};
|
|
140
142
|
type AddressType = keyof typeof supportedAddressTypes;
|
|
@@ -157,7 +159,7 @@ declare class PowPegSDK {
|
|
|
157
159
|
private minPeginAmount;
|
|
158
160
|
private peginFeeEstimationInputs;
|
|
159
161
|
private minPegoutAmount;
|
|
160
|
-
private
|
|
162
|
+
private btcNetworkConfig;
|
|
161
163
|
private bridge;
|
|
162
164
|
private api;
|
|
163
165
|
private rskProvider;
|
|
@@ -179,21 +181,22 @@ declare class PowPegSDK {
|
|
|
179
181
|
private getAddressesWithDetails;
|
|
180
182
|
private groupAddressesByUsage;
|
|
181
183
|
private groupAddressesByBalance;
|
|
182
|
-
private
|
|
184
|
+
private getAddressesGroupedByUsage;
|
|
183
185
|
private getRskOutput;
|
|
184
186
|
estimatePeginFee(amount: bigint, feeLevel?: FeeLevel): Promise<number>;
|
|
185
|
-
createPegin(amount: bigint, recipientAddress: string): Promise<Psbt>;
|
|
187
|
+
createPegin(amount: bigint, recipientAddress: string, selectedUtxos?: Utxo[]): Promise<Psbt>;
|
|
186
188
|
private selectInputs;
|
|
187
189
|
private validatePeginAmount;
|
|
188
190
|
private calculatePeginFee;
|
|
189
191
|
private calculateFeeAndSelectedInputs;
|
|
190
|
-
fundPegin(psbt: Psbt, feeLevel?: FeeLevel): Promise<{
|
|
192
|
+
fundPegin(psbt: Psbt, feeLevel?: FeeLevel, value?: bigint): Promise<{
|
|
191
193
|
psbt: Psbt;
|
|
192
194
|
inputs: Utxo[];
|
|
193
195
|
transactions: string[];
|
|
194
196
|
fee: number;
|
|
195
197
|
}>;
|
|
196
|
-
createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel): Promise<UnsignedPegin>;
|
|
198
|
+
createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel, selectedUtxos?: Utxo[]): Promise<UnsignedPegin>;
|
|
199
|
+
createAndFundPsbt(amount: bigint, recipientAddress: string, utxos: Utxo[], feeLevel?: FeeLevel): Promise<UnsignedPegin>;
|
|
197
200
|
private signPegin;
|
|
198
201
|
signAndBroadcastPegin(psbt: Psbt, inputs?: Utxo[], transactions?: string[]): Promise<string>;
|
|
199
202
|
private validateMinimumPegoutAmount;
|
|
@@ -218,6 +221,7 @@ declare class PowPegSDK {
|
|
|
218
221
|
}> | Extract<PeginStatusData, {
|
|
219
222
|
type: T;
|
|
220
223
|
}>>;
|
|
224
|
+
getAvailableUtxos(addresses: string | string[]): Promise<Utxo[]>;
|
|
221
225
|
}
|
|
222
226
|
|
|
223
227
|
declare class TrezorSigner implements BitcoinSigner {
|
|
@@ -266,4 +270,20 @@ declare class LedgerSigner implements BitcoinSigner {
|
|
|
266
270
|
signTransaction(psbt: Psbt, inputs: Utxo[], transactions: string[]): Promise<string>;
|
|
267
271
|
}
|
|
268
272
|
|
|
269
|
-
|
|
273
|
+
declare class AmountBelowMinError extends Error {
|
|
274
|
+
constructor(message?: string);
|
|
275
|
+
}
|
|
276
|
+
declare class NotEnoughFundsError extends Error {
|
|
277
|
+
constructor(message?: string);
|
|
278
|
+
}
|
|
279
|
+
declare class APIError extends Error {
|
|
280
|
+
readonly statusCode?: number;
|
|
281
|
+
readonly data?: unknown;
|
|
282
|
+
constructor(message: string, statusCode?: number, data?: unknown);
|
|
283
|
+
}
|
|
284
|
+
declare class InvalidAddressError extends Error {
|
|
285
|
+
readonly invalidAddresses: string[];
|
|
286
|
+
constructor(invalidAddresses: string[], message?: string);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export { APIError, type AddressWithDetails, AmountBelowMinError, type BitcoinDataSource, type BitcoinSigner, type FeeLevel, InvalidAddressError, LedgerSigner, NotEnoughFundsError, type PeginStatusData, PeginStatuses, type PeginTxDetails, type PegoutFeeEstimation, type PegoutStatusData, PegoutStatuses, type PegoutTxDetails, PowPegSDK, type RejectedPegoutReason, RejectedPegoutReasons, type StatusData, TrezorSigner, TxType, type UnsignedPegin, type Utxo };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Psbt, networks as networks$1 } from 'bitcoinjs-lib';
|
|
2
|
-
import { ethers } from '@rsksmart/bridges-core-sdk';
|
|
2
|
+
import { isBtcTestnetAddress, isBtcMainnetAddress, ethers } from '@rsksmart/bridges-core-sdk';
|
|
3
3
|
import TrezorConnect from '@trezor/connect-web';
|
|
4
4
|
import { CreateTransactionArg } from '@ledgerhq/hw-app-btc/lib/createTransaction';
|
|
5
5
|
|
|
@@ -129,12 +129,14 @@ declare const networks: {
|
|
|
129
129
|
readonly coin: "1";
|
|
130
130
|
readonly lib: networks$1.Network;
|
|
131
131
|
readonly xpubVersion: 70617039;
|
|
132
|
+
readonly isBtcAddress: typeof isBtcTestnetAddress;
|
|
132
133
|
};
|
|
133
134
|
readonly MAIN: {
|
|
134
135
|
readonly currency: "bitcoin";
|
|
135
136
|
readonly coin: "0";
|
|
136
137
|
readonly lib: networks$1.Network;
|
|
137
138
|
readonly xpubVersion: 76067358;
|
|
139
|
+
readonly isBtcAddress: typeof isBtcMainnetAddress;
|
|
138
140
|
};
|
|
139
141
|
};
|
|
140
142
|
type AddressType = keyof typeof supportedAddressTypes;
|
|
@@ -157,7 +159,7 @@ declare class PowPegSDK {
|
|
|
157
159
|
private minPeginAmount;
|
|
158
160
|
private peginFeeEstimationInputs;
|
|
159
161
|
private minPegoutAmount;
|
|
160
|
-
private
|
|
162
|
+
private btcNetworkConfig;
|
|
161
163
|
private bridge;
|
|
162
164
|
private api;
|
|
163
165
|
private rskProvider;
|
|
@@ -179,21 +181,22 @@ declare class PowPegSDK {
|
|
|
179
181
|
private getAddressesWithDetails;
|
|
180
182
|
private groupAddressesByUsage;
|
|
181
183
|
private groupAddressesByBalance;
|
|
182
|
-
private
|
|
184
|
+
private getAddressesGroupedByUsage;
|
|
183
185
|
private getRskOutput;
|
|
184
186
|
estimatePeginFee(amount: bigint, feeLevel?: FeeLevel): Promise<number>;
|
|
185
|
-
createPegin(amount: bigint, recipientAddress: string): Promise<Psbt>;
|
|
187
|
+
createPegin(amount: bigint, recipientAddress: string, selectedUtxos?: Utxo[]): Promise<Psbt>;
|
|
186
188
|
private selectInputs;
|
|
187
189
|
private validatePeginAmount;
|
|
188
190
|
private calculatePeginFee;
|
|
189
191
|
private calculateFeeAndSelectedInputs;
|
|
190
|
-
fundPegin(psbt: Psbt, feeLevel?: FeeLevel): Promise<{
|
|
192
|
+
fundPegin(psbt: Psbt, feeLevel?: FeeLevel, value?: bigint): Promise<{
|
|
191
193
|
psbt: Psbt;
|
|
192
194
|
inputs: Utxo[];
|
|
193
195
|
transactions: string[];
|
|
194
196
|
fee: number;
|
|
195
197
|
}>;
|
|
196
|
-
createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel): Promise<UnsignedPegin>;
|
|
198
|
+
createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel, selectedUtxos?: Utxo[]): Promise<UnsignedPegin>;
|
|
199
|
+
createAndFundPsbt(amount: bigint, recipientAddress: string, utxos: Utxo[], feeLevel?: FeeLevel): Promise<UnsignedPegin>;
|
|
197
200
|
private signPegin;
|
|
198
201
|
signAndBroadcastPegin(psbt: Psbt, inputs?: Utxo[], transactions?: string[]): Promise<string>;
|
|
199
202
|
private validateMinimumPegoutAmount;
|
|
@@ -218,6 +221,7 @@ declare class PowPegSDK {
|
|
|
218
221
|
}> | Extract<PeginStatusData, {
|
|
219
222
|
type: T;
|
|
220
223
|
}>>;
|
|
224
|
+
getAvailableUtxos(addresses: string | string[]): Promise<Utxo[]>;
|
|
221
225
|
}
|
|
222
226
|
|
|
223
227
|
declare class TrezorSigner implements BitcoinSigner {
|
|
@@ -266,4 +270,20 @@ declare class LedgerSigner implements BitcoinSigner {
|
|
|
266
270
|
signTransaction(psbt: Psbt, inputs: Utxo[], transactions: string[]): Promise<string>;
|
|
267
271
|
}
|
|
268
272
|
|
|
269
|
-
|
|
273
|
+
declare class AmountBelowMinError extends Error {
|
|
274
|
+
constructor(message?: string);
|
|
275
|
+
}
|
|
276
|
+
declare class NotEnoughFundsError extends Error {
|
|
277
|
+
constructor(message?: string);
|
|
278
|
+
}
|
|
279
|
+
declare class APIError extends Error {
|
|
280
|
+
readonly statusCode?: number;
|
|
281
|
+
readonly data?: unknown;
|
|
282
|
+
constructor(message: string, statusCode?: number, data?: unknown);
|
|
283
|
+
}
|
|
284
|
+
declare class InvalidAddressError extends Error {
|
|
285
|
+
readonly invalidAddresses: string[];
|
|
286
|
+
constructor(invalidAddresses: string[], message?: string);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export { APIError, type AddressWithDetails, AmountBelowMinError, type BitcoinDataSource, type BitcoinSigner, type FeeLevel, InvalidAddressError, LedgerSigner, NotEnoughFundsError, type PeginStatusData, PeginStatuses, type PeginTxDetails, type PegoutFeeEstimation, type PegoutStatusData, PegoutStatuses, type PegoutTxDetails, PowPegSDK, type RejectedPegoutReason, RejectedPegoutReasons, type StatusData, TrezorSigner, TxType, type UnsignedPegin, type Utxo };
|
package/dist/index.js
CHANGED
|
@@ -30,7 +30,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
APIError: () => APIError,
|
|
34
|
+
AmountBelowMinError: () => AmountBelowMinError,
|
|
35
|
+
InvalidAddressError: () => InvalidAddressError,
|
|
33
36
|
LedgerSigner: () => LedgerSigner,
|
|
37
|
+
NotEnoughFundsError: () => NotEnoughFundsError,
|
|
34
38
|
PeginStatuses: () => PeginStatuses,
|
|
35
39
|
PegoutStatuses: () => PegoutStatuses,
|
|
36
40
|
PowPegSDK: () => PowPegSDK,
|
|
@@ -44,6 +48,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
44
48
|
var import_bitcoinjs_lib4 = require("bitcoinjs-lib");
|
|
45
49
|
|
|
46
50
|
// src/constants.ts
|
|
51
|
+
var import_bridges_core_sdk = require("@rsksmart/bridges-core-sdk");
|
|
47
52
|
var import_bitcoinjs_lib = require("bitcoinjs-lib");
|
|
48
53
|
var supportedAddressTypes = {
|
|
49
54
|
"NATIVE SEGWIT": {
|
|
@@ -64,13 +69,15 @@ var networks = {
|
|
|
64
69
|
currency: "bitcoin_testnet",
|
|
65
70
|
coin: "1",
|
|
66
71
|
lib: import_bitcoinjs_lib.networks.testnet,
|
|
67
|
-
xpubVersion: 70617039
|
|
72
|
+
xpubVersion: 70617039,
|
|
73
|
+
isBtcAddress: import_bridges_core_sdk.isBtcTestnetAddress
|
|
68
74
|
},
|
|
69
75
|
MAIN: {
|
|
70
76
|
currency: "bitcoin",
|
|
71
77
|
coin: "0",
|
|
72
78
|
lib: import_bitcoinjs_lib.networks.bitcoin,
|
|
73
|
-
xpubVersion: 76067358
|
|
79
|
+
xpubVersion: 76067358,
|
|
80
|
+
isBtcAddress: import_bridges_core_sdk.isBtcMainnetAddress
|
|
74
81
|
}
|
|
75
82
|
};
|
|
76
83
|
|
|
@@ -133,12 +140,12 @@ function deriveAddress(xpub, index, addressType, network) {
|
|
|
133
140
|
|
|
134
141
|
// src/bridge.ts
|
|
135
142
|
var import_rsk_precompiled_abis = require("@rsksmart/rsk-precompiled-abis");
|
|
136
|
-
var
|
|
143
|
+
var import_bridges_core_sdk2 = require("@rsksmart/bridges-core-sdk");
|
|
137
144
|
var Bridge = class {
|
|
138
145
|
address = "0x0000000000000000000000000000000001000006";
|
|
139
146
|
bridgeContract;
|
|
140
147
|
constructor(provider) {
|
|
141
|
-
this.bridgeContract = new
|
|
148
|
+
this.bridgeContract = new import_bridges_core_sdk2.ethers.Contract(import_rsk_precompiled_abis.bridge.address, import_rsk_precompiled_abis.bridge.abi, provider);
|
|
142
149
|
}
|
|
143
150
|
async getFederationAddress() {
|
|
144
151
|
return this.bridgeContract.getFederationAddress?.();
|
|
@@ -215,9 +222,18 @@ var APIError = class extends Error {
|
|
|
215
222
|
this.data = data;
|
|
216
223
|
}
|
|
217
224
|
};
|
|
225
|
+
var InvalidAddressError = class extends Error {
|
|
226
|
+
invalidAddresses;
|
|
227
|
+
constructor(invalidAddresses, message) {
|
|
228
|
+
const defaultMessage = `Invalid address${invalidAddresses.length > 1 ? "es" : ""}: ${invalidAddresses.join(", ")}.`;
|
|
229
|
+
super(message || defaultMessage);
|
|
230
|
+
this.name = "InvalidAddressError";
|
|
231
|
+
this.invalidAddresses = invalidAddresses;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
218
234
|
|
|
219
235
|
// src/api/api.ts
|
|
220
|
-
var
|
|
236
|
+
var import_bridges_core_sdk3 = require("@rsksmart/bridges-core-sdk");
|
|
221
237
|
var ApiService = class {
|
|
222
238
|
apiUrls = {
|
|
223
239
|
MAIN: "https://api.2wp.rootstock.io",
|
|
@@ -251,7 +267,7 @@ var ApiService = class {
|
|
|
251
267
|
async getFeeRate(level) {
|
|
252
268
|
const blocks = this.feeLevelBlocks[level];
|
|
253
269
|
const response = await this.api.get(`/estimate-fee/${blocks}`).catch(this.handleError);
|
|
254
|
-
const rate =
|
|
270
|
+
const rate = import_bridges_core_sdk3.ethers.utils.parseUnits(response.data.amount, 8).div(1e3).toNumber();
|
|
255
271
|
return rate;
|
|
256
272
|
}
|
|
257
273
|
async getTxHex(txId) {
|
|
@@ -288,7 +304,7 @@ var ApiService = class {
|
|
|
288
304
|
};
|
|
289
305
|
|
|
290
306
|
// src/sdk/powpeg.ts
|
|
291
|
-
var
|
|
307
|
+
var import_bridges_core_sdk4 = require("@rsksmart/bridges-core-sdk");
|
|
292
308
|
var PowPegSDK = class {
|
|
293
309
|
/**
|
|
294
310
|
* @param {BitcoinSigner | null} _bitcoinSigner - An instance of a class that implements the BitcoinSigner interface.
|
|
@@ -305,8 +321,8 @@ var PowPegSDK = class {
|
|
|
305
321
|
this.network = network;
|
|
306
322
|
this.maxBundleSize = maxBundleSize;
|
|
307
323
|
this.burnDustValue = burnDustValue;
|
|
308
|
-
this.
|
|
309
|
-
this.rskProvider = new
|
|
324
|
+
this.btcNetworkConfig = networks[network];
|
|
325
|
+
this.rskProvider = new import_bridges_core_sdk4.ethers.providers.JsonRpcProvider(rpcProviderUrl ?? this.publicNodes[network]);
|
|
310
326
|
this.bridge = new Bridge(this.rskProvider);
|
|
311
327
|
this.api = new ApiService(network, apiUrl);
|
|
312
328
|
}
|
|
@@ -321,7 +337,7 @@ var PowPegSDK = class {
|
|
|
321
337
|
minPeginAmount = 500000n;
|
|
322
338
|
peginFeeEstimationInputs = 2;
|
|
323
339
|
minPegoutAmount = "0.004";
|
|
324
|
-
|
|
340
|
+
btcNetworkConfig;
|
|
325
341
|
bridge;
|
|
326
342
|
api;
|
|
327
343
|
rskProvider;
|
|
@@ -330,7 +346,7 @@ var PowPegSDK = class {
|
|
|
330
346
|
TEST: "https://public-node.testnet.rsk.co"
|
|
331
347
|
};
|
|
332
348
|
get bitcoinSigner() {
|
|
333
|
-
(0,
|
|
349
|
+
(0, import_bridges_core_sdk4.assertTruthy)(this._bitcoinSigner, "Bitcoin signer is required");
|
|
334
350
|
return this._bitcoinSigner;
|
|
335
351
|
}
|
|
336
352
|
set bitcoinSigner(signer) {
|
|
@@ -340,7 +356,9 @@ var PowPegSDK = class {
|
|
|
340
356
|
return this._bitcoinDataSource ?? this.api;
|
|
341
357
|
}
|
|
342
358
|
async getUtxos(addresses) {
|
|
343
|
-
|
|
359
|
+
const rawAddresses = addresses.map((address2) => typeof address2 === "string" ? address2 : address2.address);
|
|
360
|
+
const utxoLists = await Promise.all(rawAddresses.map((address2) => this.bitcoinDataSource.getOutputs(address2)));
|
|
361
|
+
return utxoLists.flat();
|
|
344
362
|
}
|
|
345
363
|
async getAddressesWithDetails(addresses) {
|
|
346
364
|
return Promise.all(addresses.map((address2) => this.bitcoinDataSource.getAddressDetails(address2)));
|
|
@@ -369,20 +387,17 @@ var PowPegSDK = class {
|
|
|
369
387
|
});
|
|
370
388
|
return { withBalance, withoutBalance };
|
|
371
389
|
}
|
|
372
|
-
async
|
|
390
|
+
async getAddressesGroupedByUsage() {
|
|
373
391
|
const nonChangeAddresses = await this.bitcoinSigner.getNonChangeAddresses(this.maxBundleSize);
|
|
374
392
|
const changeAddresses = await this.bitcoinSigner.getChangeAddresses(this.maxBundleSize);
|
|
375
|
-
const [
|
|
393
|
+
const [nonChangeDetails, changeDetails] = await Promise.all([
|
|
376
394
|
this.getAddressesWithDetails(nonChangeAddresses),
|
|
377
395
|
this.getAddressesWithDetails(changeAddresses)
|
|
378
396
|
]);
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
const refundAddress = unusedNonChangeAddress[0];
|
|
384
|
-
const changeAddress = unusedChangeAddresses[0];
|
|
385
|
-
return { addressesWithBalance, refundAddress, changeAddress };
|
|
397
|
+
return {
|
|
398
|
+
nonChange: this.groupAddressesByUsage(nonChangeDetails),
|
|
399
|
+
change: this.groupAddressesByUsage(changeDetails)
|
|
400
|
+
};
|
|
386
401
|
}
|
|
387
402
|
getRskOutput(recipientAddress, refundAddress) {
|
|
388
403
|
let output = `${this.powpegRsktHeader}${remove0x(recipientAddress)}`;
|
|
@@ -405,10 +420,11 @@ var PowPegSDK = class {
|
|
|
405
420
|
const totalFee = baseFee + feePerInput * this.peginFeeEstimationInputs;
|
|
406
421
|
return totalFee;
|
|
407
422
|
}
|
|
408
|
-
async createPegin(amount, recipientAddress) {
|
|
409
|
-
const
|
|
410
|
-
const psbt = new import_bitcoinjs_lib4.Psbt({ network: this.
|
|
411
|
-
const
|
|
423
|
+
async createPegin(amount, recipientAddress, selectedUtxos) {
|
|
424
|
+
const addresses = await this.getAddressesGroupedByUsage();
|
|
425
|
+
const psbt = new import_bitcoinjs_lib4.Psbt({ network: this.btcNetworkConfig.lib });
|
|
426
|
+
const refundAddress = addresses.nonChange.unused[0]?.address;
|
|
427
|
+
const { output: script } = import_bitcoinjs_lib4.payments.embed({ data: [this.getRskOutput(recipientAddress, refundAddress)] });
|
|
412
428
|
if (script) {
|
|
413
429
|
psbt.addOutput({
|
|
414
430
|
script,
|
|
@@ -420,8 +436,14 @@ var PowPegSDK = class {
|
|
|
420
436
|
address: bridgeAddress,
|
|
421
437
|
value: Number(amount)
|
|
422
438
|
});
|
|
423
|
-
|
|
424
|
-
|
|
439
|
+
if (selectedUtxos) {
|
|
440
|
+
this.utxos = selectedUtxos;
|
|
441
|
+
} else {
|
|
442
|
+
const usedAddresses = addresses.nonChange.used.concat(addresses.change.used);
|
|
443
|
+
const { withBalance } = this.groupAddressesByBalance(usedAddresses);
|
|
444
|
+
this.utxos = await this.getUtxos(withBalance);
|
|
445
|
+
}
|
|
446
|
+
this.changeAddress = addresses.change.unused[0]?.address;
|
|
425
447
|
return psbt;
|
|
426
448
|
}
|
|
427
449
|
selectInputs(amount, utxos, baseFee, feePerInput) {
|
|
@@ -457,8 +479,8 @@ var PowPegSDK = class {
|
|
|
457
479
|
const totalFee = baseFee + feePerInput * inputs.length;
|
|
458
480
|
return { inputs, change: Math.abs(rest), totalFee };
|
|
459
481
|
}
|
|
460
|
-
async fundPegin(psbt, feeLevel = "fast") {
|
|
461
|
-
const amount = BigInt(psbt.txOutputs[1].value);
|
|
482
|
+
async fundPegin(psbt, feeLevel = "fast", value) {
|
|
483
|
+
const amount = value ?? BigInt(psbt.txOutputs[1].value);
|
|
462
484
|
const feeRate = await this.bitcoinDataSource.getFeeRate(feeLevel);
|
|
463
485
|
const { inputs, change, totalFee } = await this.calculateFeeAndSelectedInputs(amount, this.utxos, feeRate);
|
|
464
486
|
if (change > Math.min(this.burnDustValue, this.burnDustMaxValue)) {
|
|
@@ -481,12 +503,21 @@ var PowPegSDK = class {
|
|
|
481
503
|
});
|
|
482
504
|
return { psbt, inputs, transactions: hexTransactions, fee: totalFee };
|
|
483
505
|
}
|
|
484
|
-
async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast") {
|
|
506
|
+
async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast", selectedUtxos) {
|
|
485
507
|
this.bitcoinSigner = signer;
|
|
486
508
|
this.validatePeginAmount(amount);
|
|
487
|
-
const psbt = await this.createPegin(amount, recipientAddress);
|
|
509
|
+
const psbt = await this.createPegin(amount, recipientAddress, selectedUtxos);
|
|
488
510
|
return this.fundPegin(psbt, feeLevel);
|
|
489
511
|
}
|
|
512
|
+
async createAndFundPsbt(amount, recipientAddress, utxos, feeLevel = "fast") {
|
|
513
|
+
const psbt = new import_bitcoinjs_lib4.Psbt({ network: this.btcNetworkConfig.lib });
|
|
514
|
+
psbt.addOutput({
|
|
515
|
+
address: recipientAddress,
|
|
516
|
+
value: Number(amount)
|
|
517
|
+
});
|
|
518
|
+
this.utxos = utxos;
|
|
519
|
+
return this.fundPegin(psbt, feeLevel, amount);
|
|
520
|
+
}
|
|
490
521
|
async signPegin(psbt, inputs, transactions) {
|
|
491
522
|
return this.bitcoinSigner.signTransaction(psbt, inputs, transactions);
|
|
492
523
|
}
|
|
@@ -495,21 +526,21 @@ var PowPegSDK = class {
|
|
|
495
526
|
return this.bitcoinDataSource.broadcast(signedTx);
|
|
496
527
|
}
|
|
497
528
|
validateMinimumPegoutAmount(amount) {
|
|
498
|
-
const amountBN =
|
|
499
|
-
const minAmountBN =
|
|
529
|
+
const amountBN = import_bridges_core_sdk4.ethers.utils.parseUnits(amount, 18).toBigInt();
|
|
530
|
+
const minAmountBN = import_bridges_core_sdk4.ethers.utils.parseUnits(this.minPegoutAmount, 18).toBigInt();
|
|
500
531
|
if (amountBN < minAmountBN) {
|
|
501
532
|
throw new AmountBelowMinError(`Minimum allowed amount is ${this.minPegoutAmount}.`);
|
|
502
533
|
}
|
|
503
534
|
}
|
|
504
535
|
createPegoutTransaction(amount, fromAddress) {
|
|
505
|
-
const amountBN =
|
|
536
|
+
const amountBN = import_bridges_core_sdk4.ethers.utils.parseUnits(amount, 18).toBigInt();
|
|
506
537
|
return {
|
|
507
538
|
from: fromAddress,
|
|
508
539
|
to: this.bridge.address,
|
|
509
540
|
value: amountBN.toString()
|
|
510
541
|
};
|
|
511
542
|
}
|
|
512
|
-
async estimatePegoutFees(amount, fromAddress =
|
|
543
|
+
async estimatePegoutFees(amount, fromAddress = import_bridges_core_sdk4.ethers.constants.AddressZero) {
|
|
513
544
|
this.validateMinimumPegoutAmount(amount);
|
|
514
545
|
const tx = this.createPegoutTransaction(amount, fromAddress);
|
|
515
546
|
const [gas, gasPrice, bitcoinFee] = await Promise.all([
|
|
@@ -525,7 +556,7 @@ var PowPegSDK = class {
|
|
|
525
556
|
}
|
|
526
557
|
async createPegout(amount, senderAccount) {
|
|
527
558
|
const fees = await this.estimatePegoutFees(amount, senderAccount);
|
|
528
|
-
const amountBN =
|
|
559
|
+
const amountBN = import_bridges_core_sdk4.ethers.utils.parseUnits(amount, 18).toBigInt();
|
|
529
560
|
const balance = await this.rskProvider.getBalance(senderAccount);
|
|
530
561
|
if (balance.lt(amountBN)) {
|
|
531
562
|
throw new NotEnoughFundsError(`Requested amount ${amountBN} is greater than current balance ${balance}.`);
|
|
@@ -544,6 +575,14 @@ var PowPegSDK = class {
|
|
|
544
575
|
async getTransactionStatus(txHash, txType) {
|
|
545
576
|
return this.api.getTransactionStatus(txHash, txType);
|
|
546
577
|
}
|
|
578
|
+
async getAvailableUtxos(addresses) {
|
|
579
|
+
const addressList = Array.isArray(addresses) ? addresses : [addresses];
|
|
580
|
+
const invalidAddresses = addressList.filter((address2) => !this.btcNetworkConfig.isBtcAddress(address2));
|
|
581
|
+
if (invalidAddresses.length > 0) {
|
|
582
|
+
throw new InvalidAddressError(invalidAddresses);
|
|
583
|
+
}
|
|
584
|
+
return this.getUtxos(addressList);
|
|
585
|
+
}
|
|
547
586
|
};
|
|
548
587
|
|
|
549
588
|
// src/bitcoin-signers/trezor/trezor.ts
|
|
@@ -802,7 +841,11 @@ var LedgerSigner = class _LedgerSigner {
|
|
|
802
841
|
};
|
|
803
842
|
// Annotate the CommonJS export names for ESM import in node:
|
|
804
843
|
0 && (module.exports = {
|
|
844
|
+
APIError,
|
|
845
|
+
AmountBelowMinError,
|
|
846
|
+
InvalidAddressError,
|
|
805
847
|
LedgerSigner,
|
|
848
|
+
NotEnoughFundsError,
|
|
806
849
|
PeginStatuses,
|
|
807
850
|
PegoutStatuses,
|
|
808
851
|
PowPegSDK,
|
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { address, payments as payments2, Psbt as Psbt2, Transaction } from "bitcoinjs-lib";
|
|
3
3
|
|
|
4
4
|
// src/constants.ts
|
|
5
|
+
import { isBtcMainnetAddress, isBtcTestnetAddress } from "@rsksmart/bridges-core-sdk";
|
|
5
6
|
import { networks as bitcoinJsNetworks } from "bitcoinjs-lib";
|
|
6
7
|
var supportedAddressTypes = {
|
|
7
8
|
"NATIVE SEGWIT": {
|
|
@@ -22,13 +23,15 @@ var networks = {
|
|
|
22
23
|
currency: "bitcoin_testnet",
|
|
23
24
|
coin: "1",
|
|
24
25
|
lib: bitcoinJsNetworks.testnet,
|
|
25
|
-
xpubVersion: 70617039
|
|
26
|
+
xpubVersion: 70617039,
|
|
27
|
+
isBtcAddress: isBtcTestnetAddress
|
|
26
28
|
},
|
|
27
29
|
MAIN: {
|
|
28
30
|
currency: "bitcoin",
|
|
29
31
|
coin: "0",
|
|
30
32
|
lib: bitcoinJsNetworks.bitcoin,
|
|
31
|
-
xpubVersion: 76067358
|
|
33
|
+
xpubVersion: 76067358,
|
|
34
|
+
isBtcAddress: isBtcMainnetAddress
|
|
32
35
|
}
|
|
33
36
|
};
|
|
34
37
|
|
|
@@ -173,6 +176,15 @@ var APIError = class extends Error {
|
|
|
173
176
|
this.data = data;
|
|
174
177
|
}
|
|
175
178
|
};
|
|
179
|
+
var InvalidAddressError = class extends Error {
|
|
180
|
+
invalidAddresses;
|
|
181
|
+
constructor(invalidAddresses, message) {
|
|
182
|
+
const defaultMessage = `Invalid address${invalidAddresses.length > 1 ? "es" : ""}: ${invalidAddresses.join(", ")}.`;
|
|
183
|
+
super(message || defaultMessage);
|
|
184
|
+
this.name = "InvalidAddressError";
|
|
185
|
+
this.invalidAddresses = invalidAddresses;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
176
188
|
|
|
177
189
|
// src/api/api.ts
|
|
178
190
|
import { ethers as ethers2 } from "@rsksmart/bridges-core-sdk";
|
|
@@ -263,7 +275,7 @@ var PowPegSDK = class {
|
|
|
263
275
|
this.network = network;
|
|
264
276
|
this.maxBundleSize = maxBundleSize;
|
|
265
277
|
this.burnDustValue = burnDustValue;
|
|
266
|
-
this.
|
|
278
|
+
this.btcNetworkConfig = networks[network];
|
|
267
279
|
this.rskProvider = new ethers3.providers.JsonRpcProvider(rpcProviderUrl ?? this.publicNodes[network]);
|
|
268
280
|
this.bridge = new Bridge(this.rskProvider);
|
|
269
281
|
this.api = new ApiService(network, apiUrl);
|
|
@@ -279,7 +291,7 @@ var PowPegSDK = class {
|
|
|
279
291
|
minPeginAmount = 500000n;
|
|
280
292
|
peginFeeEstimationInputs = 2;
|
|
281
293
|
minPegoutAmount = "0.004";
|
|
282
|
-
|
|
294
|
+
btcNetworkConfig;
|
|
283
295
|
bridge;
|
|
284
296
|
api;
|
|
285
297
|
rskProvider;
|
|
@@ -298,7 +310,9 @@ var PowPegSDK = class {
|
|
|
298
310
|
return this._bitcoinDataSource ?? this.api;
|
|
299
311
|
}
|
|
300
312
|
async getUtxos(addresses) {
|
|
301
|
-
|
|
313
|
+
const rawAddresses = addresses.map((address2) => typeof address2 === "string" ? address2 : address2.address);
|
|
314
|
+
const utxoLists = await Promise.all(rawAddresses.map((address2) => this.bitcoinDataSource.getOutputs(address2)));
|
|
315
|
+
return utxoLists.flat();
|
|
302
316
|
}
|
|
303
317
|
async getAddressesWithDetails(addresses) {
|
|
304
318
|
return Promise.all(addresses.map((address2) => this.bitcoinDataSource.getAddressDetails(address2)));
|
|
@@ -327,20 +341,17 @@ var PowPegSDK = class {
|
|
|
327
341
|
});
|
|
328
342
|
return { withBalance, withoutBalance };
|
|
329
343
|
}
|
|
330
|
-
async
|
|
344
|
+
async getAddressesGroupedByUsage() {
|
|
331
345
|
const nonChangeAddresses = await this.bitcoinSigner.getNonChangeAddresses(this.maxBundleSize);
|
|
332
346
|
const changeAddresses = await this.bitcoinSigner.getChangeAddresses(this.maxBundleSize);
|
|
333
|
-
const [
|
|
347
|
+
const [nonChangeDetails, changeDetails] = await Promise.all([
|
|
334
348
|
this.getAddressesWithDetails(nonChangeAddresses),
|
|
335
349
|
this.getAddressesWithDetails(changeAddresses)
|
|
336
350
|
]);
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const refundAddress = unusedNonChangeAddress[0];
|
|
342
|
-
const changeAddress = unusedChangeAddresses[0];
|
|
343
|
-
return { addressesWithBalance, refundAddress, changeAddress };
|
|
351
|
+
return {
|
|
352
|
+
nonChange: this.groupAddressesByUsage(nonChangeDetails),
|
|
353
|
+
change: this.groupAddressesByUsage(changeDetails)
|
|
354
|
+
};
|
|
344
355
|
}
|
|
345
356
|
getRskOutput(recipientAddress, refundAddress) {
|
|
346
357
|
let output = `${this.powpegRsktHeader}${remove0x(recipientAddress)}`;
|
|
@@ -363,10 +374,11 @@ var PowPegSDK = class {
|
|
|
363
374
|
const totalFee = baseFee + feePerInput * this.peginFeeEstimationInputs;
|
|
364
375
|
return totalFee;
|
|
365
376
|
}
|
|
366
|
-
async createPegin(amount, recipientAddress) {
|
|
367
|
-
const
|
|
368
|
-
const psbt = new Psbt2({ network: this.
|
|
369
|
-
const
|
|
377
|
+
async createPegin(amount, recipientAddress, selectedUtxos) {
|
|
378
|
+
const addresses = await this.getAddressesGroupedByUsage();
|
|
379
|
+
const psbt = new Psbt2({ network: this.btcNetworkConfig.lib });
|
|
380
|
+
const refundAddress = addresses.nonChange.unused[0]?.address;
|
|
381
|
+
const { output: script } = payments2.embed({ data: [this.getRskOutput(recipientAddress, refundAddress)] });
|
|
370
382
|
if (script) {
|
|
371
383
|
psbt.addOutput({
|
|
372
384
|
script,
|
|
@@ -378,8 +390,14 @@ var PowPegSDK = class {
|
|
|
378
390
|
address: bridgeAddress,
|
|
379
391
|
value: Number(amount)
|
|
380
392
|
});
|
|
381
|
-
|
|
382
|
-
|
|
393
|
+
if (selectedUtxos) {
|
|
394
|
+
this.utxos = selectedUtxos;
|
|
395
|
+
} else {
|
|
396
|
+
const usedAddresses = addresses.nonChange.used.concat(addresses.change.used);
|
|
397
|
+
const { withBalance } = this.groupAddressesByBalance(usedAddresses);
|
|
398
|
+
this.utxos = await this.getUtxos(withBalance);
|
|
399
|
+
}
|
|
400
|
+
this.changeAddress = addresses.change.unused[0]?.address;
|
|
383
401
|
return psbt;
|
|
384
402
|
}
|
|
385
403
|
selectInputs(amount, utxos, baseFee, feePerInput) {
|
|
@@ -415,8 +433,8 @@ var PowPegSDK = class {
|
|
|
415
433
|
const totalFee = baseFee + feePerInput * inputs.length;
|
|
416
434
|
return { inputs, change: Math.abs(rest), totalFee };
|
|
417
435
|
}
|
|
418
|
-
async fundPegin(psbt, feeLevel = "fast") {
|
|
419
|
-
const amount = BigInt(psbt.txOutputs[1].value);
|
|
436
|
+
async fundPegin(psbt, feeLevel = "fast", value) {
|
|
437
|
+
const amount = value ?? BigInt(psbt.txOutputs[1].value);
|
|
420
438
|
const feeRate = await this.bitcoinDataSource.getFeeRate(feeLevel);
|
|
421
439
|
const { inputs, change, totalFee } = await this.calculateFeeAndSelectedInputs(amount, this.utxos, feeRate);
|
|
422
440
|
if (change > Math.min(this.burnDustValue, this.burnDustMaxValue)) {
|
|
@@ -439,12 +457,21 @@ var PowPegSDK = class {
|
|
|
439
457
|
});
|
|
440
458
|
return { psbt, inputs, transactions: hexTransactions, fee: totalFee };
|
|
441
459
|
}
|
|
442
|
-
async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast") {
|
|
460
|
+
async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast", selectedUtxos) {
|
|
443
461
|
this.bitcoinSigner = signer;
|
|
444
462
|
this.validatePeginAmount(amount);
|
|
445
|
-
const psbt = await this.createPegin(amount, recipientAddress);
|
|
463
|
+
const psbt = await this.createPegin(amount, recipientAddress, selectedUtxos);
|
|
446
464
|
return this.fundPegin(psbt, feeLevel);
|
|
447
465
|
}
|
|
466
|
+
async createAndFundPsbt(amount, recipientAddress, utxos, feeLevel = "fast") {
|
|
467
|
+
const psbt = new Psbt2({ network: this.btcNetworkConfig.lib });
|
|
468
|
+
psbt.addOutput({
|
|
469
|
+
address: recipientAddress,
|
|
470
|
+
value: Number(amount)
|
|
471
|
+
});
|
|
472
|
+
this.utxos = utxos;
|
|
473
|
+
return this.fundPegin(psbt, feeLevel, amount);
|
|
474
|
+
}
|
|
448
475
|
async signPegin(psbt, inputs, transactions) {
|
|
449
476
|
return this.bitcoinSigner.signTransaction(psbt, inputs, transactions);
|
|
450
477
|
}
|
|
@@ -502,6 +529,14 @@ var PowPegSDK = class {
|
|
|
502
529
|
async getTransactionStatus(txHash, txType) {
|
|
503
530
|
return this.api.getTransactionStatus(txHash, txType);
|
|
504
531
|
}
|
|
532
|
+
async getAvailableUtxos(addresses) {
|
|
533
|
+
const addressList = Array.isArray(addresses) ? addresses : [addresses];
|
|
534
|
+
const invalidAddresses = addressList.filter((address2) => !this.btcNetworkConfig.isBtcAddress(address2));
|
|
535
|
+
if (invalidAddresses.length > 0) {
|
|
536
|
+
throw new InvalidAddressError(invalidAddresses);
|
|
537
|
+
}
|
|
538
|
+
return this.getUtxos(addressList);
|
|
539
|
+
}
|
|
505
540
|
};
|
|
506
541
|
|
|
507
542
|
// src/bitcoin-signers/trezor/trezor.ts
|
|
@@ -759,7 +794,11 @@ var LedgerSigner = class _LedgerSigner {
|
|
|
759
794
|
}
|
|
760
795
|
};
|
|
761
796
|
export {
|
|
797
|
+
APIError,
|
|
798
|
+
AmountBelowMinError,
|
|
799
|
+
InvalidAddressError,
|
|
762
800
|
LedgerSigner,
|
|
801
|
+
NotEnoughFundsError,
|
|
763
802
|
PeginStatuses,
|
|
764
803
|
PegoutStatuses,
|
|
765
804
|
PowPegSDK,
|
package/package.json
CHANGED