koilib 5.5.6 → 5.6.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Julián González
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Julián González
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/koinos.js CHANGED
@@ -22702,6 +22702,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
22702
22702
  exports.Contract = void 0;
22703
22703
  const Serializer_1 = __webpack_require__(7187);
22704
22704
  const utils_1 = __webpack_require__(8593);
22705
+ const Transaction_1 = __webpack_require__(7592);
22705
22706
  /**
22706
22707
  * The contract class contains the contract ID and contract entries
22707
22708
  * definition needed to encode/decode operations during the
@@ -22838,6 +22839,7 @@ class Contract {
22838
22839
  return false;
22839
22840
  Object.keys(this.abi.methods).forEach((name) => {
22840
22841
  this.functions[name] = async (argu = {}, options) => {
22842
+ var _a;
22841
22843
  if (!this.provider)
22842
22844
  throw new Error("provider not found");
22843
22845
  if (!this.abi || !this.abi.methods)
@@ -22877,7 +22879,7 @@ class Contract {
22877
22879
  // write contract (sign and send)
22878
22880
  if (!this.signer)
22879
22881
  throw new Error("signer not found");
22880
- let tx = await this.signer.prepareTransaction({
22882
+ let tx = await Transaction_1.Transaction.prepareTransaction({
22881
22883
  header: {
22882
22884
  ...(opts.chainId && { chain_id: opts.chainId }),
22883
22885
  ...(opts.rcLimit && { rc_limit: opts.rcLimit }),
@@ -22890,7 +22892,7 @@ class Contract {
22890
22892
  operation,
22891
22893
  ...(opts.nextOperations ? opts.nextOperations : []),
22892
22894
  ],
22893
- });
22895
+ }, this.provider, (_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress());
22894
22896
  if (opts.sendAbis) {
22895
22897
  if (!opts.abis)
22896
22898
  opts.abis = {};
@@ -22958,6 +22960,7 @@ class Contract {
22958
22960
  * ```
22959
22961
  */
22960
22962
  async deploy(options) {
22963
+ var _a;
22961
22964
  if (!this.signer)
22962
22965
  throw new Error("signer not found");
22963
22966
  if (!this.bytecode)
@@ -22986,7 +22989,7 @@ class Contract {
22986
22989
  if (opts.onlyOperation) {
22987
22990
  return { operation };
22988
22991
  }
22989
- let tx = await this.signer.prepareTransaction({
22992
+ let tx = await Transaction_1.Transaction.prepareTransaction({
22990
22993
  header: {
22991
22994
  ...(opts.chainId && { chain_id: opts.chainId }),
22992
22995
  ...(opts.rcLimit && { rc_limit: opts.rcLimit }),
@@ -22999,7 +23002,7 @@ class Contract {
22999
23002
  operation,
23000
23003
  ...(opts.nextOperations ? opts.nextOperations : []),
23001
23004
  ],
23002
- });
23005
+ }, this.provider, (_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress());
23003
23006
  // return result if the transaction will not be broadcasted
23004
23007
  if (!opts.sendTransaction) {
23005
23008
  const noWait = () => {
@@ -24318,9 +24321,10 @@ class Signer {
24318
24321
  }
24319
24322
  /**
24320
24323
  * Function to prepare a transaction
24324
+ * @deprecated - Use [[Transaction.prepareTransaction]] instead.
24321
24325
  * @param tx - Do not set the nonce to get it from the blockchain
24322
24326
  * using the provider. The rc_limit is 1e8 by default.
24323
- * @returns A prepared transaction. ()
24327
+ * @returns A prepared transaction.
24324
24328
  */
24325
24329
  async prepareTransaction(tx) {
24326
24330
  var _a, _b;
@@ -24470,12 +24474,72 @@ exports["default"] = Signer;
24470
24474
 
24471
24475
  Object.defineProperty(exports, "__esModule", ({ value: true }));
24472
24476
  exports.Transaction = void 0;
24473
- const Signer_1 = __webpack_require__(6991);
24477
+ /* eslint-disable no-param-reassign, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
24478
+ const sha256_1 = __webpack_require__(3061);
24479
+ const utils_1 = __webpack_require__(8593);
24480
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24481
+ // @ts-ignore
24482
+ const protocol_proto_js_1 = __webpack_require__(9104);
24483
+ const btypeTransactionHeader = {
24484
+ chain_id: { type: "bytes" },
24485
+ rc_limit: { type: "uint64" },
24486
+ nonce: { type: "bytes" },
24487
+ operation_merkle_root: { type: "bytes" },
24488
+ payer: { type: "bytes", btype: "ADDRESS" },
24489
+ payee: { type: "bytes", btype: "ADDRESS" },
24490
+ };
24491
+ const btypesOperation = {
24492
+ upload_contract: {
24493
+ type: "object",
24494
+ subtypes: {
24495
+ contract_id: { type: "bytes", btype: "CONTRACT_ID" },
24496
+ bytecode: { type: "bytes" },
24497
+ abi: { type: "string" },
24498
+ authorizes_call_contract: { type: "bool" },
24499
+ authorizes_transaction_application: { type: "bool" },
24500
+ authorizes_upload_contract: { type: "bool" },
24501
+ },
24502
+ },
24503
+ call_contract: {
24504
+ type: "object",
24505
+ subtypes: {
24506
+ contract_id: { type: "bytes", btype: "CONTRACT_ID" },
24507
+ entry_point: { type: "uint32" },
24508
+ args: { type: "bytes" },
24509
+ },
24510
+ },
24511
+ set_system_call: {
24512
+ type: "object",
24513
+ subtypes: {
24514
+ call_id: { type: "uint32" },
24515
+ target: {
24516
+ type: "object",
24517
+ subtypes: {
24518
+ thunk_id: { type: "uint32" },
24519
+ system_call_bundle: {
24520
+ type: "object",
24521
+ subtypes: {
24522
+ contract_id: { type: "bytes", btype: "CONTRACT_ID" },
24523
+ entry_point: { type: "uint32" },
24524
+ },
24525
+ },
24526
+ },
24527
+ },
24528
+ },
24529
+ },
24530
+ set_system_contract: {
24531
+ type: "object",
24532
+ subtypes: {
24533
+ contract_id: { type: "bytes", btype: "CONTRACT_ID" },
24534
+ system_contract: { type: "bool" },
24535
+ },
24536
+ },
24537
+ };
24474
24538
  class Transaction {
24475
24539
  constructor(c) {
24476
- var _a, _b, _c, _d, _e;
24540
+ var _a, _b, _c, _d, _e, _f;
24477
24541
  this.signer = c === null || c === void 0 ? void 0 : c.signer;
24478
- this.provider = c === null || c === void 0 ? void 0 : c.provider;
24542
+ this.provider = (c === null || c === void 0 ? void 0 : c.provider) || ((_a = c === null || c === void 0 ? void 0 : c.signer) === null || _a === void 0 ? void 0 : _a.provider);
24479
24543
  this.options = {
24480
24544
  broadcast: true,
24481
24545
  sendAbis: true,
@@ -24483,11 +24547,11 @@ class Transaction {
24483
24547
  };
24484
24548
  this.transaction = {
24485
24549
  header: {
24486
- ...(((_a = c === null || c === void 0 ? void 0 : c.options) === null || _a === void 0 ? void 0 : _a.chainId) && { chain_id: c.options.chainId }),
24487
- ...(((_b = c === null || c === void 0 ? void 0 : c.options) === null || _b === void 0 ? void 0 : _b.rcLimit) && { rc_limit: c.options.rcLimit }),
24488
- ...(((_c = c === null || c === void 0 ? void 0 : c.options) === null || _c === void 0 ? void 0 : _c.nonce) && { nonce: c.options.nonce }),
24489
- ...(((_d = c === null || c === void 0 ? void 0 : c.options) === null || _d === void 0 ? void 0 : _d.payer) && { payer: c.options.payer }),
24490
- ...(((_e = c === null || c === void 0 ? void 0 : c.options) === null || _e === void 0 ? void 0 : _e.payee) && { payee: c.options.payee }),
24550
+ ...(((_b = c === null || c === void 0 ? void 0 : c.options) === null || _b === void 0 ? void 0 : _b.chainId) && { chain_id: c.options.chainId }),
24551
+ ...(((_c = c === null || c === void 0 ? void 0 : c.options) === null || _c === void 0 ? void 0 : _c.rcLimit) && { rc_limit: c.options.rcLimit }),
24552
+ ...(((_d = c === null || c === void 0 ? void 0 : c.options) === null || _d === void 0 ? void 0 : _d.nonce) && { nonce: c.options.nonce }),
24553
+ ...(((_e = c === null || c === void 0 ? void 0 : c.options) === null || _e === void 0 ? void 0 : _e.payer) && { payer: c.options.payer }),
24554
+ ...(((_f = c === null || c === void 0 ? void 0 : c.options) === null || _f === void 0 ? void 0 : _f.payee) && { payee: c.options.payee }),
24491
24555
  },
24492
24556
  operations: [],
24493
24557
  };
@@ -24573,11 +24637,90 @@ class Transaction {
24573
24637
  this.transaction.operations = [];
24574
24638
  this.transaction.operations.push(operation);
24575
24639
  }
24640
+ /**
24641
+ * Function to prepare a transaction
24642
+ * @param tx - Do not set the nonce to get it from the blockchain
24643
+ * using the provider. The rc_limit is 1e8 by default.
24644
+ * @param provider - Provider
24645
+ * @param payer - payer to be used in case it is not defined in the transaction
24646
+ * @returns A prepared transaction.
24647
+ */
24648
+ static async prepareTransaction(tx, provider, payer) {
24649
+ var _a;
24650
+ if (!tx.header) {
24651
+ tx.header = {};
24652
+ }
24653
+ const { payer: payerHeader, payee } = tx.header;
24654
+ if (!payerHeader)
24655
+ tx.header.payer = payer;
24656
+ payer = tx.header.payer;
24657
+ if (!payer)
24658
+ throw new Error("payer is undefined");
24659
+ let nonce;
24660
+ if (tx.header.nonce === undefined) {
24661
+ if (!provider)
24662
+ throw new Error("Cannot get the nonce because provider is undefined. To skip this call set a nonce in the transaction header");
24663
+ nonce = await provider.getNextNonce(payee || payer);
24664
+ }
24665
+ else {
24666
+ nonce = tx.header.nonce;
24667
+ }
24668
+ let rcLimit;
24669
+ if (tx.header.rc_limit === undefined) {
24670
+ if (!provider)
24671
+ throw new Error("Cannot get the rc_limit because provider is undefined. To skip this call set a rc_limit in the transaction header");
24672
+ rcLimit = await provider.getAccountRc(payer);
24673
+ }
24674
+ else {
24675
+ rcLimit = tx.header.rc_limit;
24676
+ }
24677
+ if (!tx.header.chain_id) {
24678
+ if (!provider)
24679
+ throw new Error("Cannot get the chain_id because provider is undefined. To skip this call set a chain_id");
24680
+ tx.header.chain_id = await provider.getChainId();
24681
+ }
24682
+ const operationsHashes = [];
24683
+ if (tx.operations) {
24684
+ for (let index = 0; index < ((_a = tx.operations) === null || _a === void 0 ? void 0 : _a.length); index += 1) {
24685
+ const operationDecoded = (0, utils_1.btypeDecode)(tx.operations[index], btypesOperation, false);
24686
+ const message = protocol_proto_js_1.koinos.protocol.operation.create(operationDecoded);
24687
+ const operationEncoded = protocol_proto_js_1.koinos.protocol.operation
24688
+ .encode(message)
24689
+ .finish();
24690
+ operationsHashes.push((0, sha256_1.sha256)(operationEncoded));
24691
+ }
24692
+ }
24693
+ const operationMerkleRoot = (0, utils_1.encodeBase64url)(new Uint8Array([
24694
+ // multihash sha256: 18, 32
24695
+ 18,
24696
+ 32,
24697
+ ...(0, utils_1.calculateMerkleRoot)(operationsHashes),
24698
+ ]));
24699
+ tx.header = {
24700
+ chain_id: tx.header.chain_id,
24701
+ rc_limit: rcLimit,
24702
+ nonce,
24703
+ operation_merkle_root: operationMerkleRoot,
24704
+ payer,
24705
+ ...(payee && { payee }),
24706
+ // TODO: Option to resolve names (payer, payee)
24707
+ };
24708
+ const headerDecoded = (0, utils_1.btypeDecode)(tx.header, btypeTransactionHeader, false);
24709
+ const message = protocol_proto_js_1.koinos.protocol.transaction_header.create(headerDecoded);
24710
+ const headerBytes = protocol_proto_js_1.koinos.protocol.transaction_header
24711
+ .encode(message)
24712
+ .finish();
24713
+ const hash = (0, sha256_1.sha256)(headerBytes);
24714
+ // multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
24715
+ tx.id = `0x1220${(0, utils_1.toHexString)(hash)}`;
24716
+ return tx;
24717
+ }
24576
24718
  /**
24577
24719
  * Functon to prepare the transaction (set headers, merkle
24578
24720
  * root, etc)
24579
24721
  */
24580
24722
  async prepare(options) {
24723
+ var _a;
24581
24724
  if (options) {
24582
24725
  const header = {
24583
24726
  ...((options === null || options === void 0 ? void 0 : options.chainId) && { chain_id: options.chainId }),
@@ -24591,17 +24734,7 @@ class Transaction {
24591
24734
  ...header,
24592
24735
  };
24593
24736
  }
24594
- if (this.signer) {
24595
- this.transaction = await this.signer.prepareTransaction(this.transaction);
24596
- }
24597
- else {
24598
- if (!this.transaction.header || !this.transaction.header.payer) {
24599
- throw new Error("no payer defined");
24600
- }
24601
- const signer = Signer_1.Signer.fromSeed("0");
24602
- signer.provider = this.provider;
24603
- this.transaction = await signer.prepareTransaction(this.transaction);
24604
- }
24737
+ this.transaction = await Transaction.prepareTransaction(this.transaction, this.provider, (_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress());
24605
24738
  return this.transaction;
24606
24739
  }
24607
24740
  /**