koilib 7.1.1 → 8.0.0

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/README.md CHANGED
@@ -342,6 +342,37 @@ const receipt = await tx.send();
342
342
  await tx.wait();
343
343
  ```
344
344
 
345
+ ### Estimate mana
346
+
347
+ Here is an example to estimate the mana. First create a transaction and sign it:
348
+
349
+ ```ts
350
+ const tx = new Transaction({ signer, provider });
351
+ await tx.pushOperation(koin.transfer, {
352
+ from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
353
+ to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
354
+ value: "1000",
355
+ });
356
+ await tx.sign();
357
+ ```
358
+
359
+ Now send it with broadcast:false. With this option the rpc node will compute the
360
+ transaction but it will not broadcast it, then it will not be included in a block:
361
+
362
+ ```ts
363
+ let receipt = await tx.send({ broadcast: false });
364
+ ```
365
+
366
+ Finally, adjust the mana by taking a look to the rc used. In this example we will
367
+ adjust it by increasing it 10%. Then sign and send with the default option
368
+ (broadcast:true):
369
+
370
+ ```ts
371
+ tx.adjustRcLimit(Math.round(1.1 * Number(receipt.rc_used)));
372
+ await tx.sign();
373
+ receipt = await tx.send();
374
+ ```
375
+
345
376
  ### Create ABIs
346
377
 
347
378
  ABIs are composed of 2 elements: methods and types.
package/dist/koinos.js CHANGED
@@ -23766,7 +23766,7 @@ class Provider {
23766
23766
  }
23767
23767
  /**
23768
23768
  * Function to get the contract metadata of a specific contract.
23769
- * @param contractId contract ID
23769
+ * @param contractId - contract ID
23770
23770
  *
23771
23771
  * @example
23772
23772
  * ```ts
@@ -24190,7 +24190,6 @@ exports.Signer = void 0;
24190
24190
  /* eslint-disable no-param-reassign, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
24191
24191
  const sha256_1 = __webpack_require__(3061);
24192
24192
  const secp = __importStar(__webpack_require__(9656));
24193
- const Transaction_1 = __webpack_require__(7592);
24194
24193
  const utils_1 = __webpack_require__(8593);
24195
24194
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24196
24195
  // @ts-ignore
@@ -24275,7 +24274,6 @@ class Signer {
24275
24274
  * @param compressed - compressed format is true by default
24276
24275
  * @param provider - provider to connect with the blockchain
24277
24276
  * @param sendOptions - Send options
24278
- * @param rcOptions - options for mana estimation
24279
24277
  * @example
24280
24278
  * ```ts
24281
24279
  * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
@@ -24283,42 +24281,8 @@ class Signer {
24283
24281
  * console.log(signer.getAddress());
24284
24282
  * // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
24285
24283
  * ```
24286
- *
24287
- * By default the mana is estimated as 110% the mana used. This
24288
- * estimation is computed using the "broadcast:false" option.
24289
- * For instance, if the transaction consumes 1 mana, the rc_limit
24290
- * will be set to 1.1 mana.
24291
- *
24292
- * You can also adjust the rc limit.
24293
- * @example
24294
- * ```ts
24295
- * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
24296
- * cons signer = new Signer({
24297
- * privateKey,
24298
- * provider,
24299
- * rcOptions: {
24300
- * estimateRc: true,
24301
- * // use 2 times rc_used as rc_limit
24302
- * adjustRcLimit: (r) => 2 * Number(r.rc_used),
24303
- * },
24304
- * });
24305
- * ```
24306
- *
24307
- * The rpc node must be highly trusted because the transaction
24308
- * is signed during the estimation of the mana. You can also
24309
- * disable the mana estimation:
24310
- * @example
24311
- * ```ts
24312
- * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
24313
- * cons signer = new Signer({
24314
- * privateKey,
24315
- * provider,
24316
- * rcOptions: { estimateRc: false },
24317
- * });
24318
- * ```
24319
24284
  */
24320
24285
  constructor(c) {
24321
- var _a;
24322
24286
  this.compressed = typeof c.compressed === "undefined" ? true : c.compressed;
24323
24287
  this.privateKey = c.privateKey;
24324
24288
  this.provider = c.provider;
@@ -24334,10 +24298,6 @@ class Signer {
24334
24298
  broadcast: true,
24335
24299
  ...c.sendOptions,
24336
24300
  };
24337
- this.rcOptions = (_a = c.rcOptions) !== null && _a !== void 0 ? _a : {
24338
- estimateRc: true,
24339
- adjustRcLimit: (receipt) => Promise.resolve(Math.min(Number(receipt.max_payer_rc), Math.floor(1.1 * Number(receipt.rc_used)))),
24340
- };
24341
24301
  }
24342
24302
  /**
24343
24303
  * Function to import a private key from the WIF
@@ -24457,15 +24417,6 @@ class Signer {
24457
24417
  async signTransaction(tx, _abis) {
24458
24418
  if (!tx.id)
24459
24419
  throw new Error("Missing transaction id");
24460
- // estimation of rcLimit
24461
- if (this.rcOptions.estimateRc &&
24462
- (!tx.signatures || tx.signatures.length === 0)) {
24463
- const receipt = await this.estimateReceipt(tx);
24464
- tx.header.rc_limit = this.rcOptions.adjustRcLimit
24465
- ? await this.rcOptions.adjustRcLimit(receipt)
24466
- : receipt.rc_used;
24467
- tx.id = Transaction_1.Transaction.computeTransactionId(tx.header);
24468
- }
24469
24420
  // multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
24470
24421
  // tx id is a stringified multihash, need to extract the hash digest only
24471
24422
  const hash = (0, utils_1.toUint8Array)(tx.id.slice(6));
@@ -24514,43 +24465,6 @@ class Signer {
24514
24465
  }
24515
24466
  return this.provider.sendTransaction(transaction, opts.broadcast);
24516
24467
  }
24517
- /**
24518
- * Estimate the receipt associated to the transaction if
24519
- * it sent to the blockchain. It is useful to estimate the
24520
- * consumption of mana.
24521
- * The transaction is signed during this process and sent
24522
- * to the rpc node with the "broadcast:false" option to
24523
- * just compute the transaction without broadcasting it to
24524
- * the network.
24525
- * After that, the initial signatures are restored (if any)
24526
- * and the ones used for the estimation will be removed.
24527
- */
24528
- async estimateReceipt(tx) {
24529
- if (!tx.id)
24530
- throw new Error("Missing transaction id");
24531
- if (!tx.signatures)
24532
- tx.signatures = [];
24533
- const signaturesCopy = [...tx.signatures];
24534
- // sign if there are no signatures
24535
- if (tx.signatures.length === 0) {
24536
- const hash = (0, utils_1.toUint8Array)(tx.id.slice(6));
24537
- const signature = await this.signHash(hash);
24538
- tx.signatures.push((0, utils_1.encodeBase64url)(signature));
24539
- }
24540
- try {
24541
- const { receipt } = await this.sendTransaction(tx, {
24542
- broadcast: false,
24543
- });
24544
- // restore signatures
24545
- tx.signatures = signaturesCopy;
24546
- return receipt;
24547
- }
24548
- catch (error) {
24549
- // restore signatures
24550
- tx.signatures = signaturesCopy;
24551
- throw error;
24552
- }
24553
- }
24554
24468
  /**
24555
24469
  * Function to recover the public key from hash and signature
24556
24470
  * @param hash - hash sha256
@@ -24627,7 +24541,7 @@ class Signer {
24627
24541
  * });
24628
24542
  * ```
24629
24543
  */
24630
- async recoverPublicKeys(txOrBlock, opts) {
24544
+ static async recoverPublicKeys(txOrBlock, opts) {
24631
24545
  let compressed = true;
24632
24546
  if (opts && opts.compressed !== undefined) {
24633
24547
  compressed = opts.compressed;
@@ -24712,7 +24626,7 @@ class Signer {
24712
24626
  * });
24713
24627
  * ```
24714
24628
  */
24715
- async recoverAddresses(txOrBlock, opts) {
24629
+ static async recoverAddresses(txOrBlock, opts) {
24716
24630
  const publicKeys = await this.recoverPublicKeys(txOrBlock, opts);
24717
24631
  return publicKeys.map((publicKey) => (0, utils_1.bitcoinAddress)((0, utils_1.toUint8Array)(publicKey)));
24718
24632
  }
@@ -24876,6 +24790,7 @@ class Transaction {
24876
24790
  ...(((_f = c === null || c === void 0 ? void 0 : c.options) === null || _f === void 0 ? void 0 : _f.payee) && { payee: c.options.payee }),
24877
24791
  },
24878
24792
  operations: [],
24793
+ ...c === null || c === void 0 ? void 0 : c.transaction,
24879
24794
  };
24880
24795
  }
24881
24796
  /**
@@ -25062,6 +24977,21 @@ class Transaction {
25062
24977
  this.transaction = await Transaction.prepareTransaction(this.transaction, this.provider, (_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress());
25063
24978
  return this.transaction;
25064
24979
  }
24980
+ /**
24981
+ * Update the rc limit with a new value and update the
24982
+ * transaction ID accordingly. The signatures will be removed
24983
+ * if the transaction ID changed
24984
+ */
24985
+ adjustRcLimit(newRcLimit) {
24986
+ if (!this.transaction.header)
24987
+ throw new Error("transaction header not defined");
24988
+ this.transaction.header.rc_limit = newRcLimit;
24989
+ const newTxId = Transaction.computeTransactionId(this.transaction.header);
24990
+ if (this.transaction.id !== newTxId) {
24991
+ this.transaction.signatures = [];
24992
+ }
24993
+ this.transaction.id = newTxId;
24994
+ }
25065
24995
  /**
25066
24996
  * Function to sign the transaction
25067
24997
  */