@arkecosystem/typescript-crypto 0.0.9 → 0.0.11

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.
Files changed (62) hide show
  1. package/dist/enums/index.d.ts +0 -1
  2. package/dist/enums/index.d.ts.map +1 -1
  3. package/dist/enums/index.js +0 -1
  4. package/dist/identities/PrivateKey.d.ts +1 -0
  5. package/dist/identities/PrivateKey.d.ts.map +1 -1
  6. package/dist/identities/PrivateKey.js +11 -3
  7. package/dist/index.js +50 -43
  8. package/dist/transactions/Deserializer.d.ts.map +1 -1
  9. package/dist/transactions/Deserializer.js +18 -16
  10. package/dist/transactions/builders/AbstractTransactionBuilder.d.ts +2 -2
  11. package/dist/transactions/builders/AbstractTransactionBuilder.d.ts.map +1 -1
  12. package/dist/transactions/builders/AbstractTransactionBuilder.js +13 -12
  13. package/dist/transactions/types/AbstractTransaction.d.ts +1 -0
  14. package/dist/transactions/types/AbstractTransaction.d.ts.map +1 -1
  15. package/dist/transactions/types/AbstractTransaction.js +12 -6
  16. package/dist/types.d.ts +5 -4
  17. package/dist/types.d.ts.map +1 -1
  18. package/dist/utils/TransactionUtils.d.ts.map +1 -1
  19. package/dist/utils/TransactionUtils.js +17 -12
  20. package/package.json +1 -1
  21. package/src/enums/index.ts +0 -1
  22. package/src/identities/PrivateKey.ts +11 -4
  23. package/src/transactions/Deserializer.ts +22 -19
  24. package/src/transactions/builders/AbstractTransactionBuilder.ts +14 -14
  25. package/src/transactions/types/AbstractTransaction.ts +13 -6
  26. package/src/types.ts +5 -4
  27. package/src/utils/TransactionUtils.ts +15 -8
  28. package/tests/fixtures/identity.json +2 -1
  29. package/tests/fixtures/message-sign.json +1 -1
  30. package/tests/fixtures/sign-compact.json +1 -1
  31. package/tests/fixtures/transactions/evm-sign.json +7 -7
  32. package/tests/fixtures/transactions/multipayment-empty.json +7 -7
  33. package/tests/fixtures/transactions/multipayment-single.json +7 -7
  34. package/tests/fixtures/transactions/multipayment.json +7 -7
  35. package/tests/fixtures/transactions/transfer-0.json +7 -7
  36. package/tests/fixtures/transactions/transfer-large-amount.json +7 -7
  37. package/tests/fixtures/transactions/transfer-legacy-second-signature.json +19 -0
  38. package/tests/fixtures/transactions/transfer.json +7 -7
  39. package/tests/fixtures/transactions/unvote.json +7 -7
  40. package/tests/fixtures/transactions/username-registration.json +7 -7
  41. package/tests/fixtures/transactions/username-resignation.json +7 -7
  42. package/tests/fixtures/transactions/validator-registration.json +8 -8
  43. package/tests/fixtures/transactions/validator-resignation.json +7 -7
  44. package/tests/fixtures/transactions/vote.json +7 -7
  45. package/tests/unit/identities/PublicKey.test.ts +3 -4
  46. package/tests/unit/transactions/Deserializer.test.ts +12 -11
  47. package/tests/unit/transactions/Serializer.test.ts +5 -6
  48. package/tests/unit/transactions/builders/EvmCallBuilder.test.ts +7 -14
  49. package/tests/unit/transactions/builders/MultipaymentBuilder.test.ts +15 -29
  50. package/tests/unit/transactions/builders/TransferBuilder.test.ts +39 -23
  51. package/tests/unit/transactions/builders/UnvoteBuilder.test.ts +7 -14
  52. package/tests/unit/transactions/builders/UsernameRegistrationBuilder.test.ts +8 -15
  53. package/tests/unit/transactions/builders/UsernameResignationBuilder.test.ts +7 -14
  54. package/tests/unit/transactions/builders/ValidatorRegistrationBuilder.test.ts +9 -18
  55. package/tests/unit/transactions/builders/ValidatorResignationBuilder.test.ts +7 -14
  56. package/tests/unit/transactions/builders/VoteBuilder.test.ts +7 -14
  57. package/tests/unit/transactions/types/AbstractTransaction.test.ts +6 -8
  58. package/tests/unit/utils/TransactionUtils.test.ts +5 -7
  59. package/dist/enums/Constants.d.ts +0 -5
  60. package/dist/enums/Constants.d.ts.map +0 -1
  61. package/dist/enums/Constants.js +0 -5
  62. package/src/enums/Constants.ts +0 -4
@@ -1,5 +1,4 @@
1
1
  export * from "./AbiFunction";
2
- export * from "./Constants";
3
2
  export * from "./ContractAbiType";
4
3
  export * from "./ContractAddresses";
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/enums/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/enums/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC"}
@@ -1,4 +1,3 @@
1
1
  export * from "./AbiFunction";
2
- export * from "./Constants";
3
2
  export * from "./ContractAbiType";
4
3
  export * from "./ContractAddresses";
@@ -11,6 +11,7 @@ export declare class PrivateKey {
11
11
  static fromHex(hex: string): PrivateKey;
12
12
  static fromWif(wif: string): PrivateKey;
13
13
  sign(message: string): Promise<RecoverableSignature>;
14
+ signToEcdsa(message: string): Promise<string>;
14
15
  }
15
16
  export {};
16
17
  //# sourceMappingURL=PrivateKey.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PrivateKey.d.ts","sourceRoot":"","sources":["../../src/identities/PrivateKey.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAkC,MAAM,QAAQ,CAAC;AAMhE,UAAU,oBAAoB;IAC7B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACV;AAED,qBAAa,UAAU;IACf,UAAU,EAAE,MAAM,CAAC;gBAEd,OAAO,EAAE,MAAM,CAAC,MAAM;IAIlC,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU;IAIrD,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMvC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMjC,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAW1D"}
1
+ {"version":3,"file":"PrivateKey.d.ts","sourceRoot":"","sources":["../../src/identities/PrivateKey.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAkC,MAAM,QAAQ,CAAC;AAKhE,UAAU,oBAAoB;IAC7B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACV;AAED,qBAAa,UAAU;IACf,UAAU,EAAE,MAAM,CAAC;gBAEd,OAAO,EAAE,MAAM,CAAC,MAAM;IAIlC,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU;IAIrD,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMvC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMjC,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAWpD,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAQnD"}
@@ -9,9 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import * as WIF from "wif";
11
11
  import { ethers, keccak256, sha256, toUtf8Bytes } from "ethers";
12
- import { signAsync } from "@noble/secp256k1";
13
- import { Constants } from "@/enums/Constants";
14
12
  import { Network } from "@/configuration/Network";
13
+ import { signAsync } from "@noble/secp256k1";
15
14
  export class PrivateKey {
16
15
  constructor(keyPair) {
17
16
  this.privateKey = keyPair.privateKey.substring(2);
@@ -34,8 +33,17 @@ export class PrivateKey {
34
33
  return {
35
34
  r: signature.r.toString(16).padStart(64, "0"),
36
35
  s: signature.s.toString(16).padStart(64, "0"),
37
- v: signature.recovery + Constants.ETHEREUM_RECOVERY_ID_OFFSET,
36
+ v: signature.recovery,
38
37
  };
39
38
  });
40
39
  }
40
+ signToEcdsa(message) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const hash = keccak256("0x" + message).slice(2);
43
+ const signature = yield signAsync(hash, this.privateKey);
44
+ const v = signature.recovery;
45
+ // @see https://github.com/ArkEcosystem/mainsail/blob/16e50d45dcfbf15b20245f11b123634f0fd0b713/packages/utils/source/format-ecdsa-signature.ts#L2
46
+ return signature.toCompactHex() + v.toString(16).padStart(2, "0");
47
+ });
48
+ }
41
49
  }
package/dist/index.js CHANGED
@@ -46,13 +46,6 @@ var AbiFunction = /* @__PURE__ */ ((AbiFunction2) => {
46
46
  return AbiFunction2;
47
47
  })(AbiFunction || {});
48
48
 
49
- // src/enums/Constants.ts
50
- var Constants = /* @__PURE__ */ ((Constants2) => {
51
- Constants2["EIP_1559_PREFIX"] = "02";
52
- Constants2[Constants2["ETHEREUM_RECOVERY_ID_OFFSET"] = 27] = "ETHEREUM_RECOVERY_ID_OFFSET";
53
- return Constants2;
54
- })(Constants || {});
55
-
56
49
  // src/enums/ContractAbiType.ts
57
50
  var ContractAbiType = /* @__PURE__ */ ((ContractAbiType2) => {
58
51
  ContractAbiType2["CUSTOM"] = "custom";
@@ -23525,9 +23518,15 @@ var PrivateKey = class _PrivateKey {
23525
23518
  return {
23526
23519
  r: signature.r.toString(16).padStart(64, "0"),
23527
23520
  s: signature.s.toString(16).padStart(64, "0"),
23528
- v: signature.recovery + 27 /* ETHEREUM_RECOVERY_ID_OFFSET */
23521
+ v: signature.recovery
23529
23522
  };
23530
23523
  }
23524
+ async signToEcdsa(message) {
23525
+ const hash2 = keccak256("0x" + message).slice(2);
23526
+ const signature = await signAsync(hash2, this.privateKey);
23527
+ const v = signature.recovery;
23528
+ return signature.toCompactHex() + v.toString(16).padStart(2, "0");
23529
+ }
23531
23530
  };
23532
23531
 
23533
23532
  // src/identities/PublicKey.ts
@@ -24926,18 +24925,17 @@ var AbstractTransactionBuilder = class {
24926
24925
  value: "0",
24927
24926
  senderPublicKey: "",
24928
24927
  gasPrice: "5",
24928
+ gasLimit: 1e6,
24929
24929
  nonce: "1",
24930
- network: Network.get().chainId(),
24931
- gas: 1e6,
24932
24930
  data: ""
24933
24931
  }
24934
24932
  );
24935
24933
  }
24936
- gas(gas) {
24937
- if (gas instanceof bignumber_default) {
24938
- gas = gas.toFixed();
24934
+ gasLimit(gasLimit) {
24935
+ if (gasLimit instanceof bignumber_default) {
24936
+ gasLimit = gasLimit.toFixed();
24939
24937
  }
24940
- this.transaction.data.gas = gas;
24938
+ this.transaction.data.gasLimit = gasLimit;
24941
24939
  return this;
24942
24940
  }
24943
24941
  to(to) {
@@ -24955,16 +24953,17 @@ var AbstractTransactionBuilder = class {
24955
24953
  this.transaction.data.nonce = nonce;
24956
24954
  return this;
24957
24955
  }
24958
- network(network) {
24959
- this.transaction.data.network = network;
24960
- return this;
24961
- }
24962
24956
  async sign(passphrase) {
24963
24957
  this.transaction.data.senderPublicKey = PublicKey.fromPassphrase(passphrase).publicKey;
24964
24958
  this.transaction = await this.transaction.sign(PrivateKey.fromPassphrase(passphrase));
24965
24959
  this.transaction.data.hash = this.transaction.hash();
24966
24960
  return this;
24967
24961
  }
24962
+ async legacySecondSign(passphrase, secondPassprase) {
24963
+ await this.sign(passphrase);
24964
+ await this.transaction.legacySecondSign(PrivateKey.fromPassphrase(secondPassprase));
24965
+ return this;
24966
+ }
24968
24967
  verify() {
24969
24968
  return this.transaction.verify();
24970
24969
  }
@@ -24983,23 +24982,25 @@ var AbstractTransactionBuilder = class {
24983
24982
  var TransactionUtils = class {
24984
24983
  static toBuffer(transaction, skipSignature = false) {
24985
24984
  const fields = [
24986
- toBeArray(transaction["network"] ?? 0),
24987
24985
  toBeArray(transaction["nonce"] ? transaction["nonce"] : 0),
24988
- toBeArray(0),
24989
24986
  toBeArray(transaction["gasPrice"] ?? 0),
24990
- toBeArray(transaction["gas"] ?? 0),
24987
+ toBeArray(transaction["gasLimit"] ?? 0),
24991
24988
  transaction["to"] ?? "0x",
24992
24989
  toBeArray(transaction["value"] ? transaction["value"] : 0),
24993
- transaction["data"] && transaction["data"].startsWith("0x") ? transaction["data"] : "0x" + (transaction["data"] ?? ""),
24994
- []
24990
+ transaction["data"] && transaction["data"].startsWith("0x") ? transaction["data"] : "0x" + (transaction["data"] ?? "")
24995
24991
  ];
24996
- if (!skipSignature && transaction["v"] && transaction["r"] && transaction["s"]) {
24997
- fields.push(toBeArray(transaction["v"] - 27 /* ETHEREUM_RECOVERY_ID_OFFSET */));
24992
+ if (!skipSignature && transaction["v"] !== void 0 && transaction["r"] && transaction["s"]) {
24993
+ fields.push(toBeArray(transaction["v"] + Network.get().chainId() * 2 + 35));
24998
24994
  fields.push("0x" + transaction["r"]);
24999
24995
  fields.push("0x" + transaction["s"]);
24996
+ if (transaction.legacySecondSignature) {
24997
+ fields.push(`0x${transaction.legacySecondSignature}`);
24998
+ }
24999
+ } else {
25000
+ fields.push(toBeArray(Network.get().chainId()), toBeArray(0), toBeArray(0));
25000
25001
  }
25001
25002
  const encoded = encodeRlp(fields);
25002
- const payload = "02" /* EIP_1559_PREFIX */ + encoded.substring(2);
25003
+ const payload = encoded.substring(2);
25003
25004
  return Buffer.from(payload, "hex");
25004
25005
  }
25005
25006
  static toHash(transaction, skipSignature = false) {
@@ -25040,6 +25041,11 @@ var AbstractTransaction = class {
25040
25041
  this.data["s"] = signature.s;
25041
25042
  return this;
25042
25043
  }
25044
+ async legacySecondSign(privateKey) {
25045
+ const hash2 = TransactionUtils.toBuffer(this.data, true).toString("hex");
25046
+ this.data.legacySecondSignature = await privateKey.signToEcdsa(hash2);
25047
+ return this;
25048
+ }
25043
25049
  recoverSender() {
25044
25050
  const publicKey = this.recoverPublicKey();
25045
25051
  this.data["senderPublicKey"] = publicKey.publicKey;
@@ -25056,11 +25062,11 @@ var AbstractTransaction = class {
25056
25062
  toObject() {
25057
25063
  return [
25058
25064
  "gasPrice",
25059
- "network",
25060
25065
  "hash",
25061
- "gas",
25066
+ "gasLimit",
25062
25067
  "nonce",
25063
25068
  "senderPublicKey",
25069
+ "legacySecondSignature",
25064
25070
  "to",
25065
25071
  "value",
25066
25072
  "data",
@@ -25088,14 +25094,14 @@ var AbstractTransaction = class {
25088
25094
  return Serializer.new(this).serialize(skipSignature);
25089
25095
  }
25090
25096
  recoverPublicKey() {
25091
- if (!this.data.v || !this.data.r || !this.data.s) {
25097
+ if (this.data.v === void 0 || !this.data.r || !this.data.s) {
25092
25098
  throw new Error("Transaction signature is missing");
25093
25099
  }
25094
25100
  return PublicKey.recover(
25095
25101
  Buffer.from(this.hash(true), "hex"),
25096
25102
  BigInt(`0x${this.data.r}`),
25097
25103
  BigInt(`0x${this.data.s}`),
25098
- this.data.v - 27 /* ETHEREUM_RECOVERY_ID_OFFSET */
25104
+ this.data.v
25099
25105
  );
25100
25106
  }
25101
25107
  };
@@ -28226,7 +28232,7 @@ var Deserializer = class _Deserializer {
28226
28232
  this.SIGNATURE_SIZE = 64;
28227
28233
  this.RECOVERY_SIZE = 1;
28228
28234
  this.serialized = serialized;
28229
- this.encoded_rlp = "0x" + serialized.substring(2);
28235
+ this.encoded_rlp = "0x" + serialized;
28230
28236
  }
28231
28237
  static new(serialized) {
28232
28238
  return new _Deserializer(serialized);
@@ -28234,18 +28240,20 @@ var Deserializer = class _Deserializer {
28234
28240
  deserialize() {
28235
28241
  const decodedRlp = decodeRlp(this.encoded_rlp);
28236
28242
  const data = {
28237
- network: Number(decodedRlp[0]),
28238
- nonce: _Deserializer.parseNumber(decodedRlp[1]),
28239
- gasPrice: _Deserializer.parseNumber(decodedRlp[3]).toString(),
28240
- gas: _Deserializer.parseNumber(decodedRlp[4]).toString(),
28241
- to: _Deserializer.parseAddress(decodedRlp[5]),
28242
- value: _Deserializer.parseBigNumber(decodedRlp[6]),
28243
- data: _Deserializer.parseHex(decodedRlp[7])
28243
+ nonce: _Deserializer.parseNumber(decodedRlp[0]),
28244
+ gasPrice: _Deserializer.parseNumber(decodedRlp[1]).toString(),
28245
+ gasLimit: _Deserializer.parseNumber(decodedRlp[2]).toString(),
28246
+ to: _Deserializer.parseAddress(decodedRlp[3]),
28247
+ value: _Deserializer.parseBigNumber(decodedRlp[4]),
28248
+ data: _Deserializer.parseHex(decodedRlp[5])
28244
28249
  };
28245
- if (decodedRlp.length === 12) {
28246
- data["v"] = _Deserializer.parseNumber(decodedRlp[9]) + 27 /* ETHEREUM_RECOVERY_ID_OFFSET */;
28247
- data["r"] = _Deserializer.parseHex(decodedRlp[10]);
28248
- data["s"] = _Deserializer.parseHex(decodedRlp[11]);
28250
+ if (decodedRlp.length >= 9) {
28251
+ data["v"] = _Deserializer.parseNumber(decodedRlp[6]) - (Network.get().chainId() * 2 + 35);
28252
+ data["r"] = _Deserializer.parseHex(decodedRlp[7]);
28253
+ data["s"] = _Deserializer.parseHex(decodedRlp[8]);
28254
+ if (decodedRlp.length === 10) {
28255
+ data.legacySecondSignature = decodedRlp[9].toString().slice(2);
28256
+ }
28249
28257
  }
28250
28258
  const transaction = this.guessTransactionFromData(data);
28251
28259
  transaction.data = data;
@@ -28327,7 +28335,6 @@ export {
28327
28335
  AbstractTransactionBuilder,
28328
28336
  Address,
28329
28337
  Abi_Consensus_default as ConsensusContract,
28330
- Constants,
28331
28338
  ContractAbiType,
28332
28339
  ContractAddresses,
28333
28340
  Deserializer,
@@ -1 +1 @@
1
- {"version":3,"file":"Deserializer.d.ts","sourceRoot":"","sources":["../../src/transactions/Deserializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAInE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAgB1D,qBAAa,YAAY;IACxB,cAAc,SAAM;IACpB,aAAa,SAAK;IAElB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;gBAEP,UAAU,EAAE,MAAM;IAK9B,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,YAAY;IAI5C,WAAW,IAAI,YAAY;IA6B3B,MAAM,CAAC,aAAa,CACnB,IAAI,EAAE,eAAe,EACrB,OAAO,GAAE,eAA2C,GAClD,SAAS,GAAG,IAAI;IAiBnB,OAAO,CAAC,wBAAwB;IAgDhC,OAAO,CAAC,MAAM,CAAC,WAAW;IAI1B,OAAO,CAAC,MAAM,CAAC,cAAc;IAI7B,OAAO,CAAC,MAAM,CAAC,YAAY;IAI3B,OAAO,CAAC,MAAM,CAAC,QAAQ;CAGvB"}
1
+ {"version":3,"file":"Deserializer.d.ts","sourceRoot":"","sources":["../../src/transactions/Deserializer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAenE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAK1D,qBAAa,YAAY;IACxB,cAAc,SAAM;IACpB,aAAa,SAAK;IAElB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;gBAEP,UAAU,EAAE,MAAM;IAK9B,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,YAAY;IAI5C,WAAW,IAAI,YAAY;IAgC3B,MAAM,CAAC,aAAa,CACnB,IAAI,EAAE,eAAe,EACrB,OAAO,GAAE,eAA2C,GAClD,SAAS,GAAG,IAAI;IAiBnB,OAAO,CAAC,wBAAwB;IAgDhC,OAAO,CAAC,MAAM,CAAC,WAAW;IAI1B,OAAO,CAAC,MAAM,CAAC,cAAc;IAI7B,OAAO,CAAC,MAAM,CAAC,YAAY;IAI3B,OAAO,CAAC,MAAM,CAAC,QAAQ;CAGvB"}
@@ -1,17 +1,17 @@
1
+ import { EvmCall, Multipayment, Unvote, UsernameRegistration, UsernameResignation, ValidatorRegistration, ValidatorResignation, Vote, } from "./types";
2
+ import { AbiDecoder } from "@/utils";
3
+ import { AbiFunction } from "@/enums";
1
4
  import BigNumber from "bignumber.js";
2
- import { Constants } from "@/enums/Constants";
3
5
  import { ContractAbiType } from "@/enums/ContractAbiType";
6
+ import { Network } from "@/configuration/Network";
4
7
  import { Transfer } from "./types/Transfer";
5
8
  import { decodeRlp } from "ethers";
6
- import { EvmCall, Multipayment, Unvote, UsernameRegistration, UsernameResignation, ValidatorRegistration, ValidatorResignation, Vote, } from "./types";
7
- import { AbiFunction } from "@/enums";
8
- import { AbiDecoder } from "@/utils";
9
9
  export class Deserializer {
10
10
  constructor(serialized) {
11
11
  this.SIGNATURE_SIZE = 64;
12
12
  this.RECOVERY_SIZE = 1;
13
13
  this.serialized = serialized;
14
- this.encoded_rlp = "0x" + serialized.substring(2);
14
+ this.encoded_rlp = "0x" + serialized;
15
15
  }
16
16
  static new(serialized) {
17
17
  return new Deserializer(serialized);
@@ -19,18 +19,20 @@ export class Deserializer {
19
19
  deserialize() {
20
20
  const decodedRlp = decodeRlp(this.encoded_rlp);
21
21
  const data = {
22
- network: Number(decodedRlp[0]),
23
- nonce: Deserializer.parseNumber(decodedRlp[1]),
24
- gasPrice: Deserializer.parseNumber(decodedRlp[3]).toString(),
25
- gas: Deserializer.parseNumber(decodedRlp[4]).toString(),
26
- to: Deserializer.parseAddress(decodedRlp[5]),
27
- value: Deserializer.parseBigNumber(decodedRlp[6]),
28
- data: Deserializer.parseHex(decodedRlp[7]),
22
+ nonce: Deserializer.parseNumber(decodedRlp[0]),
23
+ gasPrice: Deserializer.parseNumber(decodedRlp[1]).toString(),
24
+ gasLimit: Deserializer.parseNumber(decodedRlp[2]).toString(),
25
+ to: Deserializer.parseAddress(decodedRlp[3]),
26
+ value: Deserializer.parseBigNumber(decodedRlp[4]),
27
+ data: Deserializer.parseHex(decodedRlp[5]),
29
28
  };
30
- if (decodedRlp.length === 12) {
31
- data["v"] = Deserializer.parseNumber(decodedRlp[9]) + Constants.ETHEREUM_RECOVERY_ID_OFFSET;
32
- data["r"] = Deserializer.parseHex(decodedRlp[10]);
33
- data["s"] = Deserializer.parseHex(decodedRlp[11]);
29
+ if (decodedRlp.length >= 9) {
30
+ data["v"] = Deserializer.parseNumber(decodedRlp[6]) - (Network.get().chainId() * 2 + 35);
31
+ data["r"] = Deserializer.parseHex(decodedRlp[7]);
32
+ data["s"] = Deserializer.parseHex(decodedRlp[8]);
33
+ if (decodedRlp.length === 10) {
34
+ data.legacySecondSignature = decodedRlp[9].toString().slice(2);
35
+ }
34
36
  }
35
37
  const transaction = this.guessTransactionFromData(data);
36
38
  transaction.data = data;
@@ -3,12 +3,12 @@ import BigNumber from "bignumber.js";
3
3
  export declare abstract class AbstractTransactionBuilder<T extends ITransactionBuilder<T>> implements ITransactionBuilder<T> {
4
4
  transaction: ITransaction;
5
5
  constructor(data?: Record<string, unknown>);
6
- gas(gas: string | BigNumber): T;
6
+ gasLimit(gasLimit: string | BigNumber): T;
7
7
  to(to: string): T;
8
8
  gasPrice(gasPrice: string | BigNumber): T;
9
9
  nonce(nonce: string): T;
10
- network(network: number): T;
11
10
  sign(passphrase: string): Promise<T>;
11
+ legacySecondSign(passphrase: string, secondPassprase: string): Promise<T>;
12
12
  verify(): boolean;
13
13
  toObject(): TransactionData;
14
14
  toJson(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"AbstractTransactionBuilder.d.ts","sourceRoot":"","sources":["../../../src/transactions/builders/AbstractTransactionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAK7E,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC,8BAAsB,0BAA0B,CAAC,CAAC,SAAS,mBAAmB,CAAC,CAAC,CAAC,CAAE,YAAW,mBAAmB,CAAC,CAAC,CAAC;IAC5G,WAAW,EAAE,YAAY,CAAC;gBAErB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAcnC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,CAAC;IAU/B,EAAE,CAAC,EAAE,EAAE,MAAM,GAAG,CAAC;IAMjB,QAAQ,CAAC,QAAQ,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/E,MAAM,IAAI,OAAO;IAIjB,QAAQ,IAAI,eAAe;IAI3B,MAAM,IAAI,MAAM;IAIhB,QAAQ,IAAI,MAAM;IAIzB,SAAS,CAAC,QAAQ,CAAC,sBAAsB,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,YAAY;CACvF"}
@@ -7,27 +7,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { Network } from "@/configuration/Network";
10
+ import BigNumber from "bignumber.js";
11
11
  import { PrivateKey } from "@/identities/PrivateKey";
12
12
  import { PublicKey } from "@/identities/PublicKey";
13
- import BigNumber from "bignumber.js";
14
13
  export class AbstractTransactionBuilder {
15
14
  constructor(data) {
16
15
  this.transaction = this.getTransactionInstance(data !== null && data !== void 0 ? data : {
17
16
  value: "0",
18
17
  senderPublicKey: "",
19
18
  gasPrice: "5",
19
+ gasLimit: 1000000,
20
20
  nonce: "1",
21
- network: Network.get().chainId(),
22
- gas: 1000000,
23
21
  data: "",
24
22
  });
25
23
  }
26
- gas(gas) {
27
- if (gas instanceof BigNumber) {
28
- gas = gas.toFixed();
24
+ gasLimit(gasLimit) {
25
+ if (gasLimit instanceof BigNumber) {
26
+ gasLimit = gasLimit.toFixed();
29
27
  }
30
- this.transaction.data.gas = gas;
28
+ this.transaction.data.gasLimit = gasLimit;
31
29
  return this;
32
30
  }
33
31
  to(to) {
@@ -45,10 +43,6 @@ export class AbstractTransactionBuilder {
45
43
  this.transaction.data.nonce = nonce;
46
44
  return this;
47
45
  }
48
- network(network) {
49
- this.transaction.data.network = network;
50
- return this;
51
- }
52
46
  sign(passphrase) {
53
47
  return __awaiter(this, void 0, void 0, function* () {
54
48
  this.transaction.data.senderPublicKey = PublicKey.fromPassphrase(passphrase).publicKey;
@@ -57,6 +51,13 @@ export class AbstractTransactionBuilder {
57
51
  return this;
58
52
  });
59
53
  }
54
+ legacySecondSign(passphrase, secondPassprase) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ yield this.sign(passphrase);
57
+ yield this.transaction.legacySecondSign(PrivateKey.fromPassphrase(secondPassprase));
58
+ return this;
59
+ });
60
+ }
60
61
  verify() {
61
62
  return this.transaction.verify();
62
63
  }
@@ -8,6 +8,7 @@ export declare abstract class AbstractTransaction implements ITransaction {
8
8
  abstract getPayload(): string;
9
9
  refreshPayloadData(): ITransaction;
10
10
  sign(privateKey: PrivateKey): Promise<ITransaction>;
11
+ legacySecondSign(privateKey: PrivateKey): Promise<ITransaction>;
11
12
  recoverSender(): void;
12
13
  verify(): boolean;
13
14
  toObject(): TransactionData;
@@ -1 +1 @@
1
- {"version":3,"file":"AbstractTransaction.d.ts","sourceRoot":"","sources":["../../../src/transactions/types/AbstractTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAIxD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAKnD,8BAAsB,mBAAoB,YAAW,YAAY;IACzD,IAAI,EAAE,eAAe,CAAC;IACtB,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEnB,IAAI,EAAE,eAAe;aAMxB,UAAU,IAAI,MAAM;IAE7B,kBAAkB,IAAI,YAAY;IAM5B,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAYzD,aAAa,IAAI,IAAI;IAOrB,MAAM,IAAI,OAAO;IAWjB,QAAQ,IAAI,eAAe;IA4B3B,MAAM,IAAI,MAAM;IAIhB,IAAI,CAAC,aAAa,UAAQ,GAAG,MAAM;IAInC,SAAS,CAAC,aAAa,UAAQ,GAAG,MAAM;IAI/C,SAAS,CAAC,gBAAgB,IAAI,SAAS;CAYvC"}
1
+ {"version":3,"file":"AbstractTransaction.d.ts","sourceRoot":"","sources":["../../../src/transactions/types/AbstractTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAIxD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAInD,8BAAsB,mBAAoB,YAAW,YAAY;IACzD,IAAI,EAAE,eAAe,CAAC;IACtB,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEnB,IAAI,EAAE,eAAe;aAMxB,UAAU,IAAI,MAAM;IAE7B,kBAAkB,IAAI,YAAY;IAM5B,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAYnD,gBAAgB,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAQrE,aAAa,IAAI,IAAI;IAOrB,MAAM,IAAI,OAAO;IAWjB,QAAQ,IAAI,eAAe;IA4B3B,MAAM,IAAI,MAAM;IAIhB,IAAI,CAAC,aAAa,UAAQ,GAAG,MAAM;IAInC,SAAS,CAAC,aAAa,UAAQ,GAAG,MAAM;IAI/C,SAAS,CAAC,gBAAgB,IAAI,SAAS;CAYvC"}
@@ -8,11 +8,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { Address } from "@/identities/Address";
11
- import { Constants } from "@/enums/Constants";
11
+ import BigNumber from "bignumber.js";
12
12
  import { PublicKey } from "@/identities/PublicKey";
13
13
  import { Serializer } from "@/transactions/Serializer";
14
14
  import { TransactionUtils } from "@/utils/TransactionUtils";
15
- import BigNumber from "bignumber.js";
16
15
  export class AbstractTransaction {
17
16
  constructor(data) {
18
17
  this.data = Object.assign({}, data);
@@ -32,6 +31,13 @@ export class AbstractTransaction {
32
31
  return this;
33
32
  });
34
33
  }
34
+ legacySecondSign(privateKey) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const hash = TransactionUtils.toBuffer(this.data, true).toString("hex");
37
+ this.data.legacySecondSignature = yield privateKey.signToEcdsa(hash);
38
+ return this;
39
+ });
40
+ }
35
41
  recoverSender() {
36
42
  const publicKey = this.recoverPublicKey();
37
43
  this.data["senderPublicKey"] = publicKey.publicKey;
@@ -48,11 +54,11 @@ export class AbstractTransaction {
48
54
  toObject() {
49
55
  return [
50
56
  "gasPrice",
51
- "network",
52
57
  "hash",
53
- "gas",
58
+ "gasLimit",
54
59
  "nonce",
55
60
  "senderPublicKey",
61
+ "legacySecondSignature",
56
62
  "to",
57
63
  "value",
58
64
  "data",
@@ -80,9 +86,9 @@ export class AbstractTransaction {
80
86
  return Serializer.new(this).serialize(skipSignature);
81
87
  }
82
88
  recoverPublicKey() {
83
- if (!this.data.v || !this.data.r || !this.data.s) {
89
+ if (this.data.v === undefined || !this.data.r || !this.data.s) {
84
90
  throw new Error("Transaction signature is missing");
85
91
  }
86
- return PublicKey.recover(Buffer.from(this.hash(true), "hex"), BigInt(`0x${this.data.r}`), BigInt(`0x${this.data.s}`), this.data.v - Constants.ETHEREUM_RECOVERY_ID_OFFSET);
92
+ return PublicKey.recover(Buffer.from(this.hash(true), "hex"), BigInt(`0x${this.data.r}`), BigInt(`0x${this.data.s}`), this.data.v);
87
93
  }
88
94
  }
package/dist/types.d.ts CHANGED
@@ -2,9 +2,9 @@ import BigNumber from "bignumber.js";
2
2
  import { Result } from "ethers";
3
3
  export interface TransactionData {
4
4
  gasPrice?: string | null;
5
- network?: number | string | null;
5
+ gasLimit?: string | null;
6
+ legacySecondSignature?: number | string | null;
6
7
  hash?: string | null;
7
- gas?: string | null;
8
8
  nonce?: number | string | null;
9
9
  senderPublicKey?: string | null;
10
10
  to?: string | null;
@@ -30,6 +30,7 @@ export interface ITransaction {
30
30
  getPayload(): string;
31
31
  refreshPayloadData(): ITransaction;
32
32
  sign(privateKey: unknown): Promise<ITransaction>;
33
+ legacySecondSign(privateKey: unknown): Promise<ITransaction>;
33
34
  recoverSender(): void;
34
35
  verify(): boolean;
35
36
  toObject(): TransactionData;
@@ -39,12 +40,12 @@ export interface ITransaction {
39
40
  }
40
41
  export interface ITransactionBuilder<T extends ITransactionBuilder<T>> {
41
42
  transaction: ITransaction;
42
- gas(gas: string | BigNumber): T;
43
+ gasLimit(gasLimit: string | BigNumber): T;
43
44
  gasPrice(gasPrice: string | BigNumber): T;
44
45
  to(to: string): T;
45
46
  nonce(nonce: string): T;
46
- network(network: number): T;
47
47
  sign(passphrase: string): Promise<T>;
48
+ legacySecondSign(passphrase: string, secondPassphrase: string): Promise<T>;
48
49
  verify(): boolean;
49
50
  toObject(): TransactionData;
50
51
  toJson(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC/B,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;IAEX,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,G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EAAE,MAAM,GAAG,SAAS,GAAG,6BAA6B,CAAC;CAChE;AAED,MAAM,WAAW,4BAA6B,SAAQ,mBAAmB,CAAC,4BAA4B,CAAC;IACtG,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,4BAA4B,CAAC;CACzD;AAED,MAAM,WAAW,eAAgB,SAAQ,mBAAmB,CAAC,eAAe,CAAC;IAC5E,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,CAAC;CAC1C;AAED,MAAM,WAAW,oBAAqB,SAAQ,mBAAmB,CAAC,oBAAoB,CAAC;IACtF,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,oBAAoB,CAAC;CACvE;AAED,MAAM,WAAW,wBAAyB,SAAQ,mBAAmB,CAAC,wBAAwB,CAAC;CAAG"}
@@ -1 +1 @@
1
- {"version":3,"file":"TransactionUtils.d.ts","sourceRoot":"","sources":["../../src/utils/TransactionUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,qBAAa,gBAAgB;WACd,QAAQ,CAAC,WAAW,EAAE,eAAe,EAAE,aAAa,UAAQ,GAAG,MAAM;WA4BrE,MAAM,CAAC,WAAW,EAAE,eAAe,EAAE,aAAa,UAAQ,GAAG,MAAM;CAKjF"}
1
+ {"version":3,"file":"TransactionUtils.d.ts","sourceRoot":"","sources":["../../src/utils/TransactionUtils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,qBAAa,gBAAgB;WACd,QAAQ,CAAC,WAAW,EAAE,eAAe,EAAE,aAAa,UAAQ,GAAG,MAAM;WAkCrE,MAAM,CAAC,WAAW,EAAE,eAAe,EAAE,aAAa,UAAQ,GAAG,MAAM;CAKjF"}
@@ -1,28 +1,33 @@
1
1
  import { encodeRlp, keccak256, toBeArray } from "ethers";
2
- import { Constants } from "@/enums/Constants";
2
+ import { Network } from "@/configuration/Network";
3
3
  export class TransactionUtils {
4
4
  static toBuffer(transaction, skipSignature = false) {
5
- var _a, _b, _c, _d, _e;
5
+ var _a, _b, _c, _d;
6
6
  const fields = [
7
- toBeArray((_a = transaction["network"]) !== null && _a !== void 0 ? _a : 0),
8
7
  toBeArray(transaction["nonce"] ? transaction["nonce"] : 0),
9
- toBeArray(0),
10
- toBeArray((_b = transaction["gasPrice"]) !== null && _b !== void 0 ? _b : 0),
11
- toBeArray((_c = transaction["gas"]) !== null && _c !== void 0 ? _c : 0),
12
- (_d = transaction["to"]) !== null && _d !== void 0 ? _d : "0x",
8
+ toBeArray((_a = transaction["gasPrice"]) !== null && _a !== void 0 ? _a : 0),
9
+ toBeArray((_b = transaction["gasLimit"]) !== null && _b !== void 0 ? _b : 0),
10
+ (_c = transaction["to"]) !== null && _c !== void 0 ? _c : "0x",
13
11
  toBeArray(transaction["value"] ? transaction["value"] : 0),
14
12
  transaction["data"] && transaction["data"].startsWith("0x")
15
13
  ? transaction["data"]
16
- : "0x" + ((_e = transaction["data"]) !== null && _e !== void 0 ? _e : ""),
17
- [],
14
+ : "0x" + ((_d = transaction["data"]) !== null && _d !== void 0 ? _d : ""),
18
15
  ];
19
- if (!skipSignature && transaction["v"] && transaction["r"] && transaction["s"]) {
20
- fields.push(toBeArray(transaction["v"] - Constants.ETHEREUM_RECOVERY_ID_OFFSET));
16
+ if (!skipSignature && transaction["v"] !== undefined && transaction["r"] && transaction["s"]) {
17
+ fields.push(toBeArray(transaction["v"] + Network.get().chainId() * 2 + 35));
21
18
  fields.push("0x" + transaction["r"]);
22
19
  fields.push("0x" + transaction["s"]);
20
+ if (transaction.legacySecondSignature) {
21
+ fields.push(`0x${transaction.legacySecondSignature}`);
22
+ }
23
23
  }
24
+ else {
25
+ // Push chainId + 0s for r and s
26
+ fields.push(toBeArray(Network.get().chainId()), toBeArray(0), toBeArray(0));
27
+ }
28
+ // TODO: second signature handling
24
29
  const encoded = encodeRlp(fields);
25
- const payload = Constants.EIP_1559_PREFIX + encoded.substring(2);
30
+ const payload = encoded.substring(2);
26
31
  return Buffer.from(payload, "hex");
27
32
  }
28
33
  static toHash(transaction, skipSignature = false) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkecosystem/typescript-crypto",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "engines": {
5
5
  "node": ">=20.12.2"
6
6
  },
@@ -1,4 +1,3 @@
1
1
  export * from "./AbiFunction";
2
- export * from "./Constants";
3
2
  export * from "./ContractAbiType";
4
3
  export * from "./ContractAddresses";
@@ -1,10 +1,9 @@
1
1
  import * as WIF from "wif";
2
2
 
3
3
  import { ethers, keccak256, sha256, toUtf8Bytes } from "ethers";
4
- import { signAsync } from "@noble/secp256k1";
5
4
 
6
- import { Constants } from "@/enums/Constants";
7
5
  import { Network } from "@/configuration/Network";
6
+ import { signAsync } from "@noble/secp256k1";
8
7
 
9
8
  interface RecoverableSignature {
10
9
  r: string;
@@ -37,13 +36,21 @@ export class PrivateKey {
37
36
 
38
37
  async sign(message: string): Promise<RecoverableSignature> {
39
38
  const hash = keccak256("0x" + message).slice(2);
40
-
41
39
  const signature = await signAsync(hash, this.privateKey);
42
40
 
43
41
  return {
44
42
  r: signature.r.toString(16).padStart(64, "0"),
45
43
  s: signature.s.toString(16).padStart(64, "0"),
46
- v: signature.recovery + Constants.ETHEREUM_RECOVERY_ID_OFFSET,
44
+ v: signature.recovery,
47
45
  };
48
46
  }
47
+
48
+ async signToEcdsa(message: string): Promise<string> {
49
+ const hash = keccak256("0x" + message).slice(2);
50
+ const signature = await signAsync(hash, this.privateKey);
51
+
52
+ const v = signature.recovery;
53
+ // @see https://github.com/ArkEcosystem/mainsail/blob/16e50d45dcfbf15b20245f11b123634f0fd0b713/packages/utils/source/format-ecdsa-signature.ts#L2
54
+ return signature.toCompactHex() + v.toString(16).padStart(2, "0");
55
+ }
49
56
  }