koilib 2.7.0 → 2.8.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/dist/koinos.js CHANGED
@@ -10386,10 +10386,13 @@ class Serializer {
10386
10386
  * It also prepares the bytes for special cases (base58, hex string)
10387
10387
  * when bytesConversion param is true.
10388
10388
  */
10389
- async serialize(valueDecoded, typeName) {
10389
+ async serialize(valueDecoded, typeName, opts) {
10390
10390
  const protobufType = this.defaultType || this.root.lookupType(typeName);
10391
10391
  let object = {};
10392
- if (this.bytesConversion) {
10392
+ const bytesConversion = (opts === null || opts === void 0 ? void 0 : opts.bytesConversion) === undefined
10393
+ ? this.bytesConversion
10394
+ : opts.bytesConversion;
10395
+ if (bytesConversion) {
10393
10396
  // TODO: format from Buffer to base58/base64 for nested fields
10394
10397
  Object.keys(protobufType.fields).forEach((fieldName) => {
10395
10398
  const { options, name, type } = protobufType.fields[fieldName];
@@ -10435,14 +10438,17 @@ class Serializer {
10435
10438
  * It also encodes the bytes for special cases (base58, hex string)
10436
10439
  * when bytesConversion param is true.
10437
10440
  */
10438
- async deserialize(valueEncoded, typeName) {
10441
+ async deserialize(valueEncoded, typeName, opts) {
10439
10442
  const valueBuffer = typeof valueEncoded === "string"
10440
10443
  ? (0, utils_1.decodeBase64)(valueEncoded)
10441
10444
  : valueEncoded;
10442
10445
  const protobufType = this.defaultType || this.root.lookupType(typeName);
10443
10446
  const message = protobufType.decode(valueBuffer);
10444
10447
  const object = protobufType.toObject(message, { longs: String });
10445
- if (!this.bytesConversion)
10448
+ const bytesConversion = (opts === null || opts === void 0 ? void 0 : opts.bytesConversion) === undefined
10449
+ ? this.bytesConversion
10450
+ : opts.bytesConversion;
10451
+ if (!bytesConversion)
10446
10452
  return object;
10447
10453
  // TODO: format from Buffer to base58/base64 for nested fields
10448
10454
  Object.keys(protobufType.fields).forEach((fieldName) => {
@@ -10598,7 +10604,6 @@ class Signer {
10598
10604
  }
10599
10605
  else {
10600
10606
  this.serializer = new Serializer_1.Serializer(protocol_proto_json_1.default, {
10601
- defaultTypeName: "active_transaction_data",
10602
10607
  bytesConversion: false,
10603
10608
  });
10604
10609
  }
@@ -10700,15 +10705,12 @@ class Signer {
10700
10705
  }
10701
10706
  }
10702
10707
  /**
10703
- * Function to sign a transaction. It's important to remark that
10704
- * the transaction parameter is modified inside this function.
10705
- * @param tx - Unsigned transaction
10706
- * @returns
10708
+ * Function to sign a hash value. It returns the signature encoded
10709
+ * in base64. The signature is in compact format with the
10710
+ * recovery byte
10711
+ * @param hash Hash value. Also known as digest
10707
10712
  */
10708
- async signTransaction(tx) {
10709
- if (!tx.active)
10710
- throw new Error("Active data is not defined");
10711
- const hash = (0, sha256_1.sha256)((0, utils_1.decodeBase64)(tx.active));
10713
+ async signHash(hash) {
10712
10714
  const [compSignature, recovery] = await secp.sign(hash, this.privateKey, {
10713
10715
  recovered: true,
10714
10716
  canonical: true,
@@ -10717,11 +10719,41 @@ class Signer {
10717
10719
  const compactSignature = new Uint8Array(65);
10718
10720
  compactSignature.set([recovery + 31], 0);
10719
10721
  compactSignature.set(compSignature, 1);
10720
- tx.signature_data = (0, utils_1.encodeBase64)(compactSignature);
10721
- const multihash = `0x1220${(0, utils_1.toHexString)(hash)}`; // 12: code sha2-256. 20: length (32 bytes)
10722
- tx.id = multihash;
10722
+ return (0, utils_1.encodeBase64)(compactSignature);
10723
+ }
10724
+ /**
10725
+ * Function to sign a transaction. It's important to remark that
10726
+ * the transaction parameter is modified inside this function.
10727
+ * @param tx - Unsigned transaction
10728
+ */
10729
+ async signTransaction(tx) {
10730
+ if (!tx.active)
10731
+ throw new Error("Active data is not defined");
10732
+ const hash = (0, sha256_1.sha256)((0, utils_1.decodeBase64)(tx.active));
10733
+ tx.signature_data = await this.signHash(hash);
10734
+ // multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
10735
+ tx.id = `0x1220${(0, utils_1.toHexString)(hash)}`;
10723
10736
  return tx;
10724
10737
  }
10738
+ /**
10739
+ * Function to sign a block for federated consensus. That is,
10740
+ * just the ecdsa signature. For other algorithms, like PoW,
10741
+ * you have to sign the block and then process the signature
10742
+ * to add the extra data (nonce in the case of PoW).
10743
+ * @param block - Unsigned block
10744
+ */
10745
+ async signBlock(block) {
10746
+ const activeBytes = (0, utils_1.decodeBase64)(block.active);
10747
+ const headerBytes = await this.serializer.serialize(block.header, "block_header", { bytesConversion: true });
10748
+ const headerActiveBytes = new Uint8Array(headerBytes.length + activeBytes.length);
10749
+ headerActiveBytes.set(headerBytes, 0);
10750
+ headerActiveBytes.set(activeBytes, headerBytes.length);
10751
+ const hash = (0, sha256_1.sha256)(headerActiveBytes);
10752
+ block.signature_data = await this.signHash(hash);
10753
+ // multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
10754
+ block.id = `0x1220${(0, utils_1.toHexString)(hash)}`;
10755
+ return block;
10756
+ }
10725
10757
  /**
10726
10758
  * Function to sign and send a transaction. It internally uses
10727
10759
  * [[Provider.sendTransaction]]
@@ -10787,7 +10819,7 @@ class Signer {
10787
10819
  * defaultTypeName: "pow_signature_data",
10788
10820
  * });
10789
10821
  *
10790
- * const signer = await Signer.recoverPublicKey(block, {
10822
+ * const signer = await signer.recoverPublicKey(block, {
10791
10823
  * transformSignature: async (signatureData) => {
10792
10824
  * const powSignatureData = await serializer.deserialize(signatureData);
10793
10825
  * return powSignatureData.recoverable_signature;
@@ -10795,7 +10827,7 @@ class Signer {
10795
10827
  * });
10796
10828
  * ```
10797
10829
  */
10798
- static async recoverPublicKey(txOrBlock, opts) {
10830
+ async recoverPublicKey(txOrBlock, opts) {
10799
10831
  if (!txOrBlock.active)
10800
10832
  throw new Error("active is not defined");
10801
10833
  if (!txOrBlock.signature_data)
@@ -10808,7 +10840,20 @@ class Signer {
10808
10840
  if (opts && typeof opts.compressed !== "undefined") {
10809
10841
  compressed = opts.compressed;
10810
10842
  }
10811
- const hash = (0, sha256_1.sha256)((0, utils_1.decodeBase64)(txOrBlock.active));
10843
+ const block = txOrBlock;
10844
+ let hash;
10845
+ const activeBytes = (0, utils_1.decodeBase64)(block.active);
10846
+ if (block.header) {
10847
+ const headerBytes = await this.serializer.serialize(block.header, "block_header", { bytesConversion: true });
10848
+ const headerActiveBytes = new Uint8Array(headerBytes.length + activeBytes.length);
10849
+ headerActiveBytes.set(headerBytes, 0);
10850
+ headerActiveBytes.set(activeBytes, headerBytes.length);
10851
+ hash = (0, sha256_1.sha256)(headerActiveBytes);
10852
+ }
10853
+ else {
10854
+ // transaction
10855
+ hash = (0, sha256_1.sha256)(activeBytes);
10856
+ }
10812
10857
  const compactSignatureHex = (0, utils_1.toHexString)((0, utils_1.decodeBase64)(signatureData));
10813
10858
  const recovery = Number(`0x${compactSignatureHex.slice(0, 2)}`) - 31;
10814
10859
  const rHex = compactSignatureHex.slice(2, 66);
@@ -10829,7 +10874,7 @@ class Signer {
10829
10874
  * The output format can be compressed (default) or uncompressed.
10830
10875
  * @example
10831
10876
  * ```ts
10832
- * const publicKey = await Signer.recoverAddress(tx);
10877
+ * const publicKey = await signer.recoverAddress(tx);
10833
10878
  * ```
10834
10879
  *
10835
10880
  * If the signature data contains more data, like in the
@@ -10863,7 +10908,7 @@ class Signer {
10863
10908
  * defaultTypeName: "pow_signature_data",
10864
10909
  * });
10865
10910
  *
10866
- * const signer = await Signer.recoverAddress(block, {
10911
+ * const signer = await signer.recoverAddress(block, {
10867
10912
  * transformSignature: async (signatureData) => {
10868
10913
  * const powSignatureData = await serializer.deserialize(signatureData);
10869
10914
  * return powSignatureData.recoverable_signature;
@@ -10871,8 +10916,8 @@ class Signer {
10871
10916
  * });
10872
10917
  * ```
10873
10918
  */
10874
- static async recoverAddress(txOrBlock, opts) {
10875
- const publicKey = await Signer.recoverPublicKey(txOrBlock, opts);
10919
+ async recoverAddress(txOrBlock, opts) {
10920
+ const publicKey = await this.recoverPublicKey(txOrBlock, opts);
10876
10921
  return (0, utils_1.bitcoinAddress)((0, utils_1.toUint8Array)(publicKey));
10877
10922
  }
10878
10923
  /**
@@ -10899,7 +10944,7 @@ class Signer {
10899
10944
  nonce,
10900
10945
  operations,
10901
10946
  };
10902
- const buffer = await this.serializer.serialize(activeData2);
10947
+ const buffer = await this.serializer.serialize(activeData2, "active_transaction_data");
10903
10948
  return {
10904
10949
  active: (0, utils_1.encodeBase64)(buffer),
10905
10950
  };
@@ -10910,7 +10955,7 @@ class Signer {
10910
10955
  async decodeTransaction(tx) {
10911
10956
  if (!tx.active)
10912
10957
  throw new Error("Active data is not defined");
10913
- return this.serializer.deserialize(tx.active);
10958
+ return this.serializer.deserialize(tx.active, "active_transaction_data");
10914
10959
  }
10915
10960
  }
10916
10961
  exports.Signer = Signer;