@rsksmart/powpeg-sdk 1.2.0-beta.5 → 1.2.0-beta.7

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 CHANGED
@@ -249,9 +249,10 @@ declare class PowPegSDK {
249
249
  * @param {bigint} amount - Amount to peg in, in satoshis.
250
250
  * @param {string} recipientAddress - Rootstock address that will receive the pegged-in RBTC.
251
251
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
252
+ * @param {BitcoinSigner} [signer] - Signer to bind this PSBT to. Defaults to the signer configured in the constructor.
252
253
  * @returns {Promise<Psbt>} The unsigned, unfunded peg-in PSBT.
253
254
  */
254
- createPegin(amount: bigint, recipientAddress: string, selectedUtxos?: Utxo[]): Promise<Psbt>;
255
+ createPegin(amount: bigint, recipientAddress: string, selectedUtxos?: Utxo[], signer?: BitcoinSigner): Promise<Psbt>;
255
256
  private selectInputs;
256
257
  private validatePeginAmount;
257
258
  private validateFeeRate;
@@ -280,9 +281,10 @@ declare class PowPegSDK {
280
281
  * @param {BitcoinSigner} signer - Bitcoin signer used to derive the addresses funding this peg-in.
281
282
  * @param {FeeLevel} feeLevel - Fee priority level used to look up the current network fee rate. Defaults to `'fast'`.
282
283
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
284
+ * @param {number} [feeRate] - Fee rate, in sat/B, to fund with. When omitted, a fresh rate is fetched from the configured `BitcoinDataSource` and validated. Pass a rate obtained independently (e.g. from a prior `estimatePeginFee` call) to pin funding to that exact value instead of risking a second, possibly different, fetch.
283
285
  * @returns {Promise<UnsignedPegin>} The funded, unsigned peg-in PSBT along with its inputs, raw transactions, and total fee.
284
286
  */
285
- createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel, selectedUtxos?: Utxo[]): Promise<UnsignedPegin>;
287
+ createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel, selectedUtxos?: Utxo[], feeRate?: number): Promise<UnsignedPegin>;
286
288
  /**
287
289
  * Builds and funds a generic PSBT paying `amount` satoshis to `recipientAddress` using the given UTXOs,
288
290
  * without routing through the federation address (unlike {@link createPegin}).
package/dist/index.d.ts CHANGED
@@ -249,9 +249,10 @@ declare class PowPegSDK {
249
249
  * @param {bigint} amount - Amount to peg in, in satoshis.
250
250
  * @param {string} recipientAddress - Rootstock address that will receive the pegged-in RBTC.
251
251
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
252
+ * @param {BitcoinSigner} [signer] - Signer to bind this PSBT to. Defaults to the signer configured in the constructor.
252
253
  * @returns {Promise<Psbt>} The unsigned, unfunded peg-in PSBT.
253
254
  */
254
- createPegin(amount: bigint, recipientAddress: string, selectedUtxos?: Utxo[]): Promise<Psbt>;
255
+ createPegin(amount: bigint, recipientAddress: string, selectedUtxos?: Utxo[], signer?: BitcoinSigner): Promise<Psbt>;
255
256
  private selectInputs;
256
257
  private validatePeginAmount;
257
258
  private validateFeeRate;
@@ -280,9 +281,10 @@ declare class PowPegSDK {
280
281
  * @param {BitcoinSigner} signer - Bitcoin signer used to derive the addresses funding this peg-in.
281
282
  * @param {FeeLevel} feeLevel - Fee priority level used to look up the current network fee rate. Defaults to `'fast'`.
282
283
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
284
+ * @param {number} [feeRate] - Fee rate, in sat/B, to fund with. When omitted, a fresh rate is fetched from the configured `BitcoinDataSource` and validated. Pass a rate obtained independently (e.g. from a prior `estimatePeginFee` call) to pin funding to that exact value instead of risking a second, possibly different, fetch.
283
285
  * @returns {Promise<UnsignedPegin>} The funded, unsigned peg-in PSBT along with its inputs, raw transactions, and total fee.
284
286
  */
285
- createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel, selectedUtxos?: Utxo[]): Promise<UnsignedPegin>;
287
+ createAndFundPegin(amount: bigint, recipientAddress: string, signer: BitcoinSigner, feeLevel?: FeeLevel, selectedUtxos?: Utxo[], feeRate?: number): Promise<UnsignedPegin>;
286
288
  /**
287
289
  * Builds and funds a generic PSBT paying `amount` satoshis to `recipientAddress` using the given UTXOs,
288
290
  * without routing through the federation address (unlike {@link createPegin}).
package/dist/index.js CHANGED
@@ -249,20 +249,20 @@ var InvalidFeeRateError = class extends Error {
249
249
  // src/api/api.ts
250
250
  var import_bridges_core_sdk3 = require("@rsksmart/bridges-core-sdk");
251
251
  var ApiService = class {
252
+ constructor(network, apiUrl, maxFeeRateSatPerByte = 1e3) {
253
+ this.maxFeeRateSatPerByte = maxFeeRateSatPerByte;
254
+ this.api = import_axios.default.create({ baseURL: apiUrl ?? this.apiUrls[network] });
255
+ }
252
256
  apiUrls = {
253
257
  MAIN: "https://api.2wp.rootstock.io",
254
258
  TEST: "https://api.2wp.testnet.rootstock.io"
255
259
  };
256
- maxFeeRateSatPerByte = 1e3;
257
260
  feeLevelBlocks = {
258
261
  slow: 5,
259
262
  average: 3,
260
263
  fast: 1
261
264
  };
262
265
  api;
263
- constructor(network, apiUrl) {
264
- this.api = import_axios.default.create({ baseURL: apiUrl ?? this.apiUrls[network] });
265
- }
266
266
  handleError(error) {
267
267
  if (import_axios.default.isAxiosError(error)) {
268
268
  if (error.response) {
@@ -362,7 +362,7 @@ var PowPegSDK = class {
362
362
  this.btcNetworkConfig = networks[network];
363
363
  this.rskProvider = new import_bridges_core_sdk4.ethers.providers.JsonRpcProvider(rpcProviderUrl ?? this.publicNodes[network]);
364
364
  this.bridge = new Bridge(this.rskProvider);
365
- this.api = new ApiService(network, apiUrl);
365
+ this.api = new ApiService(network, apiUrl, maxFeeRateSatPerByte);
366
366
  }
367
367
  txHeaderSizeInBytes = 13;
368
368
  txOutputSizeInBytes = 32;
@@ -514,10 +514,10 @@ var PowPegSDK = class {
514
514
  * @param {bigint} amount - Amount to peg in, in satoshis.
515
515
  * @param {string} recipientAddress - Rootstock address that will receive the pegged-in RBTC.
516
516
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
517
+ * @param {BitcoinSigner} [signer] - Signer to bind this PSBT to. Defaults to the signer configured in the constructor.
517
518
  * @returns {Promise<Psbt>} The unsigned, unfunded peg-in PSBT.
518
519
  */
519
- async createPegin(amount, recipientAddress, selectedUtxos) {
520
- const signer = this.bitcoinSigner;
520
+ async createPegin(amount, recipientAddress, selectedUtxos, signer = this.bitcoinSigner) {
521
521
  const addresses = await this.getAddressesGroupedByUsage(signer);
522
522
  const psbt = new import_bitcoinjs_lib4.Psbt({ network: this.btcNetworkConfig.lib });
523
523
  const refundAddress = addresses.nonChange.unused[0]?.address;
@@ -612,13 +612,23 @@ var PowPegSDK = class {
612
612
  const { inputs, change, totalFee } = await this.calculateFeeAndSelectedInputs(amount, funding.utxos, resolvedFeeRate);
613
613
  const hexTransactions = await Promise.all(inputs.map((input) => this.bitcoinDataSource.getTxHex(input.txid)));
614
614
  const parsedOutputs = hexTransactions.map((hex, index) => {
615
- const output = import_bitcoinjs_lib4.Transaction.fromHex(hex).outs[inputs[index].vout];
615
+ const tx = import_bitcoinjs_lib4.Transaction.fromHex(hex);
616
+ (0, import_bridges_core_sdk4.assertTruthy)(
617
+ tx.getId() === inputs[index].txid,
618
+ `Fetched transaction for UTXO ${inputs[index].txid}:${inputs[index].vout} does not match the requested txid (got ${tx.getId()}).`
619
+ );
620
+ const output = tx.outs[inputs[index].vout];
616
621
  (0, import_bridges_core_sdk4.assertTruthy)(output, `UTXO ${inputs[index].txid}:${inputs[index].vout} was not found in the fetched transaction.`);
622
+ (0, import_bridges_core_sdk4.assertTruthy)(
623
+ BigInt(output.value) === inputs[index].amount,
624
+ `UTXO ${inputs[index].txid}:${inputs[index].vout} value mismatch: fetched transaction reports ${output.value}, expected ${inputs[index].amount}.`
625
+ );
617
626
  return output;
618
627
  });
619
628
  const initialInputCount = psbt.txInputs.length;
620
629
  const initialOutputCount = psbt.txOutputs.length;
621
630
  const addChange = change > Math.min(this.burnDustValue, this.burnDustMaxValue);
631
+ this.funding.delete(psbt);
622
632
  if (addChange) {
623
633
  psbt.addOutput({
624
634
  // Fall back to the first funding input's address when every derived
@@ -644,7 +654,6 @@ var PowPegSDK = class {
644
654
  psbt.txInputs.length === expectedInputCount && psbt.txOutputs.length === expectedOutputCount,
645
655
  `Funded PSBT structure mismatch: expected ${expectedInputCount} inputs and ${expectedOutputCount} outputs, got ${psbt.txInputs.length} and ${psbt.txOutputs.length}.`
646
656
  );
647
- this.funding.delete(psbt);
648
657
  return { psbt, inputs, transactions: hexTransactions, fee: totalFee };
649
658
  }
650
659
  /**
@@ -654,13 +663,13 @@ var PowPegSDK = class {
654
663
  * @param {BitcoinSigner} signer - Bitcoin signer used to derive the addresses funding this peg-in.
655
664
  * @param {FeeLevel} feeLevel - Fee priority level used to look up the current network fee rate. Defaults to `'fast'`.
656
665
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
666
+ * @param {number} [feeRate] - Fee rate, in sat/B, to fund with. When omitted, a fresh rate is fetched from the configured `BitcoinDataSource` and validated. Pass a rate obtained independently (e.g. from a prior `estimatePeginFee` call) to pin funding to that exact value instead of risking a second, possibly different, fetch.
657
667
  * @returns {Promise<UnsignedPegin>} The funded, unsigned peg-in PSBT along with its inputs, raw transactions, and total fee.
658
668
  */
659
- async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast", selectedUtxos) {
660
- this.bitcoinSigner = signer;
669
+ async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast", selectedUtxos, feeRate) {
661
670
  this.validatePeginAmount(amount);
662
- const psbt = await this.createPegin(amount, recipientAddress, selectedUtxos);
663
- return this.fundPegin(psbt, feeLevel);
671
+ const psbt = await this.createPegin(amount, recipientAddress, selectedUtxos, signer);
672
+ return this.fundPegin(psbt, feeLevel, void 0, feeRate);
664
673
  }
665
674
  /**
666
675
  * Builds and funds a generic PSBT paying `amount` satoshis to `recipientAddress` using the given UTXOs,
package/dist/index.mjs CHANGED
@@ -201,20 +201,20 @@ var InvalidFeeRateError = class extends Error {
201
201
  // src/api/api.ts
202
202
  import { ethers as ethers2 } from "@rsksmart/bridges-core-sdk";
203
203
  var ApiService = class {
204
+ constructor(network, apiUrl, maxFeeRateSatPerByte = 1e3) {
205
+ this.maxFeeRateSatPerByte = maxFeeRateSatPerByte;
206
+ this.api = axios.create({ baseURL: apiUrl ?? this.apiUrls[network] });
207
+ }
204
208
  apiUrls = {
205
209
  MAIN: "https://api.2wp.rootstock.io",
206
210
  TEST: "https://api.2wp.testnet.rootstock.io"
207
211
  };
208
- maxFeeRateSatPerByte = 1e3;
209
212
  feeLevelBlocks = {
210
213
  slow: 5,
211
214
  average: 3,
212
215
  fast: 1
213
216
  };
214
217
  api;
215
- constructor(network, apiUrl) {
216
- this.api = axios.create({ baseURL: apiUrl ?? this.apiUrls[network] });
217
- }
218
218
  handleError(error) {
219
219
  if (axios.isAxiosError(error)) {
220
220
  if (error.response) {
@@ -314,7 +314,7 @@ var PowPegSDK = class {
314
314
  this.btcNetworkConfig = networks[network];
315
315
  this.rskProvider = new ethers3.providers.JsonRpcProvider(rpcProviderUrl ?? this.publicNodes[network]);
316
316
  this.bridge = new Bridge(this.rskProvider);
317
- this.api = new ApiService(network, apiUrl);
317
+ this.api = new ApiService(network, apiUrl, maxFeeRateSatPerByte);
318
318
  }
319
319
  txHeaderSizeInBytes = 13;
320
320
  txOutputSizeInBytes = 32;
@@ -466,10 +466,10 @@ var PowPegSDK = class {
466
466
  * @param {bigint} amount - Amount to peg in, in satoshis.
467
467
  * @param {string} recipientAddress - Rootstock address that will receive the pegged-in RBTC.
468
468
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
469
+ * @param {BitcoinSigner} [signer] - Signer to bind this PSBT to. Defaults to the signer configured in the constructor.
469
470
  * @returns {Promise<Psbt>} The unsigned, unfunded peg-in PSBT.
470
471
  */
471
- async createPegin(amount, recipientAddress, selectedUtxos) {
472
- const signer = this.bitcoinSigner;
472
+ async createPegin(amount, recipientAddress, selectedUtxos, signer = this.bitcoinSigner) {
473
473
  const addresses = await this.getAddressesGroupedByUsage(signer);
474
474
  const psbt = new Psbt2({ network: this.btcNetworkConfig.lib });
475
475
  const refundAddress = addresses.nonChange.unused[0]?.address;
@@ -564,13 +564,23 @@ var PowPegSDK = class {
564
564
  const { inputs, change, totalFee } = await this.calculateFeeAndSelectedInputs(amount, funding.utxos, resolvedFeeRate);
565
565
  const hexTransactions = await Promise.all(inputs.map((input) => this.bitcoinDataSource.getTxHex(input.txid)));
566
566
  const parsedOutputs = hexTransactions.map((hex, index) => {
567
- const output = Transaction.fromHex(hex).outs[inputs[index].vout];
567
+ const tx = Transaction.fromHex(hex);
568
+ assertTruthy(
569
+ tx.getId() === inputs[index].txid,
570
+ `Fetched transaction for UTXO ${inputs[index].txid}:${inputs[index].vout} does not match the requested txid (got ${tx.getId()}).`
571
+ );
572
+ const output = tx.outs[inputs[index].vout];
568
573
  assertTruthy(output, `UTXO ${inputs[index].txid}:${inputs[index].vout} was not found in the fetched transaction.`);
574
+ assertTruthy(
575
+ BigInt(output.value) === inputs[index].amount,
576
+ `UTXO ${inputs[index].txid}:${inputs[index].vout} value mismatch: fetched transaction reports ${output.value}, expected ${inputs[index].amount}.`
577
+ );
569
578
  return output;
570
579
  });
571
580
  const initialInputCount = psbt.txInputs.length;
572
581
  const initialOutputCount = psbt.txOutputs.length;
573
582
  const addChange = change > Math.min(this.burnDustValue, this.burnDustMaxValue);
583
+ this.funding.delete(psbt);
574
584
  if (addChange) {
575
585
  psbt.addOutput({
576
586
  // Fall back to the first funding input's address when every derived
@@ -596,7 +606,6 @@ var PowPegSDK = class {
596
606
  psbt.txInputs.length === expectedInputCount && psbt.txOutputs.length === expectedOutputCount,
597
607
  `Funded PSBT structure mismatch: expected ${expectedInputCount} inputs and ${expectedOutputCount} outputs, got ${psbt.txInputs.length} and ${psbt.txOutputs.length}.`
598
608
  );
599
- this.funding.delete(psbt);
600
609
  return { psbt, inputs, transactions: hexTransactions, fee: totalFee };
601
610
  }
602
611
  /**
@@ -606,13 +615,13 @@ var PowPegSDK = class {
606
615
  * @param {BitcoinSigner} signer - Bitcoin signer used to derive the addresses funding this peg-in.
607
616
  * @param {FeeLevel} feeLevel - Fee priority level used to look up the current network fee rate. Defaults to `'fast'`.
608
617
  * @param {Utxo[]} [selectedUtxos] - UTXOs to fund the transaction with. If omitted, they're derived from the signer's used addresses.
618
+ * @param {number} [feeRate] - Fee rate, in sat/B, to fund with. When omitted, a fresh rate is fetched from the configured `BitcoinDataSource` and validated. Pass a rate obtained independently (e.g. from a prior `estimatePeginFee` call) to pin funding to that exact value instead of risking a second, possibly different, fetch.
609
619
  * @returns {Promise<UnsignedPegin>} The funded, unsigned peg-in PSBT along with its inputs, raw transactions, and total fee.
610
620
  */
611
- async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast", selectedUtxos) {
612
- this.bitcoinSigner = signer;
621
+ async createAndFundPegin(amount, recipientAddress, signer, feeLevel = "fast", selectedUtxos, feeRate) {
613
622
  this.validatePeginAmount(amount);
614
- const psbt = await this.createPegin(amount, recipientAddress, selectedUtxos);
615
- return this.fundPegin(psbt, feeLevel);
623
+ const psbt = await this.createPegin(amount, recipientAddress, selectedUtxos, signer);
624
+ return this.fundPegin(psbt, feeLevel, void 0, feeRate);
616
625
  }
617
626
  /**
618
627
  * Builds and funds a generic PSBT paying `amount` satoshis to `recipientAddress` using the given UTXOs,
package/docs/setup.md CHANGED
@@ -50,7 +50,7 @@ pnpm add @rsksmart/powpeg-sdk
50
50
  | `bitcoinDataSource` | `BitcoinDataSource` used for fee rates, UTXOs, tx broadcast and tx status | `null` — falls back to the built-in `apiUrl`-backed source |
51
51
  | `network` | `'MAIN'` or `'TEST'` — selects Bitcoin network params and address validation rules | required |
52
52
  | `rpcProviderUrl` | Rootstock JSON-RPC endpoint used to read the bridge precompile and send peg-outs | RSK public node for the given network (`https://public-node.rsk.co` / `https://public-node.testnet.rsk.co`) |
53
- | `apiUrl` | 2WP API used as the default `BitcoinDataSource` (fee rates, UTXOs, tx broadcast, tx status) when no custom `BitcoinDataSource` is supplied | production 2WP API for the given network (`https://api.2wp.rootstock.io` / `https://api.2wp.testnet.rootstock.io`) |
53
+ | `apiUrl` | 2WP API endpoint. Used as the default `BitcoinDataSource` when no custom one is supplied, and always used for federation-address verification during peg-in creation regardless of `bitcoinDataSource` — see External dependencies below | production 2WP API for the given network (`https://api.2wp.rootstock.io` / `https://api.2wp.testnet.rootstock.io`) |
54
54
  | `maxBundleSize` | Number of addresses to derive per `BitcoinSigner` call while creating a peg-in | `10` |
55
55
  | `burnDustValue` | Change amount, in satoshis, below which it's dropped into the fee instead of added as an output | `2000` |
56
56
  | `maxFeeRateSatPerByte` | Upper bound, in sat/B, for a fee rate coming from the configured `BitcoinDataSource`; a higher value throws `InvalidFeeRateError` | `1000` |
@@ -59,7 +59,7 @@ pnpm add @rsksmart/powpeg-sdk
59
59
  ## External dependencies
60
60
 
61
61
  - **Rootstock RPC node** — read via `ethers.providers.JsonRpcProvider`, used for the bridge precompile (`Bridge` in `src/bridge.ts`) and to send peg-out transactions.
62
- - **2WP API** — the SDK's built-in `BitcoinDataSource` implementation (`src/api/api.ts`); can be replaced with your own `BitcoinDataSource` for fee rates, UTXOs, tx broadcasting and address details.
62
+ - **2WP API** — the SDK's built-in `BitcoinDataSource` implementation (`src/api/api.ts`); the fee-rate/UTXO/broadcast/address-details methods can be replaced with your own `BitcoinDataSource`, but `createPegin` always calls the 2WP API's `/pegin-configuration` endpoint directly (via the internal `ApiService`, independent of `bitcoinDataSource`) to verify the federation address against the Bridge contract — this call cannot be substituted, so the 2WP API stays a required dependency even when every other data source is custom.
63
63
  - **Hardware wallets** (only needed if you use the bundled signers — see [`bitcoin-signers.md`](./bitcoin-signers.md)):
64
64
  - **Ledger** — via `@ledgerhq/hw-transport-webusb`, requires a browser environment with WebUSB support.
65
65
  - **Trezor** — via `@trezor/connect-web`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsksmart/powpeg-sdk",
3
- "version": "1.2.0-beta.5",
3
+ "version": "1.2.0-beta.7",
4
4
  "description": "SDK for creating native peg-in and peg-out transactions following the PowPeg protocol.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",