@hashgraphonline/standards-sdk 0.0.157-canary.2 → 0.0.157-canary.4

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 (178) hide show
  1. package/dist/cjs/hcs-10/base-client.d.ts +1 -1
  2. package/dist/cjs/hcs-10/base-client.d.ts.map +1 -1
  3. package/dist/cjs/hcs-10/sdk.d.ts +1 -1
  4. package/dist/cjs/hcs-10/sdk.d.ts.map +1 -1
  5. package/dist/cjs/hcs-11/agent-builder.d.ts +2 -0
  6. package/dist/cjs/hcs-11/agent-builder.d.ts.map +1 -1
  7. package/dist/cjs/hcs-11/client.d.ts +2 -0
  8. package/dist/cjs/hcs-11/client.d.ts.map +1 -1
  9. package/dist/cjs/hcs-11/types.d.ts +4 -2
  10. package/dist/cjs/hcs-11/types.d.ts.map +1 -1
  11. package/dist/cjs/hcs-15/index.d.ts +3 -0
  12. package/dist/cjs/hcs-15/index.d.ts.map +1 -0
  13. package/dist/cjs/hcs-15/petal-manager.d.ts +27 -0
  14. package/dist/cjs/hcs-15/petal-manager.d.ts.map +1 -0
  15. package/dist/cjs/hcs-15/types.d.ts +41 -0
  16. package/dist/cjs/hcs-15/types.d.ts.map +1 -0
  17. package/dist/cjs/hcs-16/flora-account-manager.d.ts.map +1 -1
  18. package/dist/cjs/hcs-16/types.d.ts +3 -5
  19. package/dist/cjs/hcs-16/types.d.ts.map +1 -1
  20. package/dist/cjs/hcs-18/discovery.d.ts +1 -0
  21. package/dist/cjs/hcs-18/discovery.d.ts.map +1 -1
  22. package/dist/cjs/hcs-18/types.d.ts +1 -0
  23. package/dist/cjs/hcs-18/types.d.ts.map +1 -1
  24. package/dist/cjs/hcs-20/sdk.d.ts.map +1 -1
  25. package/dist/cjs/hcs-7/evm-bridge.d.ts.map +1 -1
  26. package/dist/cjs/hcs-7/redis-cache.d.ts.map +1 -1
  27. package/dist/cjs/hcs-7/wasm-bridge.d.ts.map +1 -1
  28. package/dist/cjs/{index-xZmk-HBD.cjs → index-CJsmyij2.cjs} +12 -12
  29. package/dist/cjs/index-CJsmyij2.cjs.map +1 -0
  30. package/dist/cjs/{index-CyAbgH2f-Cv8mxAhS.cjs → index-CyAbgH2f-Dn5WQtPx.cjs} +2 -2
  31. package/dist/cjs/{index-CyAbgH2f-Cv8mxAhS.cjs.map → index-CyAbgH2f-Dn5WQtPx.cjs.map} +1 -1
  32. package/dist/cjs/index.d.ts +1 -1
  33. package/dist/cjs/services/types.d.ts +1 -1
  34. package/dist/cjs/services/types.d.ts.map +1 -1
  35. package/dist/cjs/standards-sdk.cjs +1 -1
  36. package/dist/cjs/{standards-sdk.es45-BxAtDqsy-ELd4REyl.cjs → standards-sdk.es45-BxAtDqsy-CiMtsDbD.cjs} +2 -2
  37. package/dist/cjs/{standards-sdk.es45-BxAtDqsy-ELd4REyl.cjs.map → standards-sdk.es45-BxAtDqsy-CiMtsDbD.cjs.map} +1 -1
  38. package/dist/cjs/{standards-sdk.es46-BI8ACeLM-DRmcgoN0.cjs → standards-sdk.es46-BI8ACeLM-DFYgO-fP.cjs} +2 -2
  39. package/dist/cjs/{standards-sdk.es46-BI8ACeLM-DRmcgoN0.cjs.map → standards-sdk.es46-BI8ACeLM-DFYgO-fP.cjs.map} +1 -1
  40. package/dist/cjs/{standards-sdk.es47-DbkY3FlD-DfMpZygD.cjs → standards-sdk.es47-DbkY3FlD-2qTWs2BD.cjs} +2 -2
  41. package/dist/cjs/{standards-sdk.es47-DbkY3FlD-DfMpZygD.cjs.map → standards-sdk.es47-DbkY3FlD-2qTWs2BD.cjs.map} +1 -1
  42. package/dist/cjs/{standards-sdk.es48-DzT9Qc3F-CmCkJl3x.cjs → standards-sdk.es48-DzT9Qc3F-Byz0fQnW.cjs} +2 -2
  43. package/dist/cjs/{standards-sdk.es48-DzT9Qc3F-CmCkJl3x.cjs.map → standards-sdk.es48-DzT9Qc3F-Byz0fQnW.cjs.map} +1 -1
  44. package/dist/cjs/utils/logger.d.ts.map +1 -1
  45. package/dist/es/hcs-10/base-client.d.ts +1 -1
  46. package/dist/es/hcs-10/base-client.d.ts.map +1 -1
  47. package/dist/es/hcs-10/sdk.d.ts +1 -1
  48. package/dist/es/hcs-10/sdk.d.ts.map +1 -1
  49. package/dist/es/hcs-11/agent-builder.d.ts +2 -0
  50. package/dist/es/hcs-11/agent-builder.d.ts.map +1 -1
  51. package/dist/es/hcs-11/client.d.ts +2 -0
  52. package/dist/es/hcs-11/client.d.ts.map +1 -1
  53. package/dist/es/hcs-11/types.d.ts +4 -2
  54. package/dist/es/hcs-11/types.d.ts.map +1 -1
  55. package/dist/es/hcs-15/index.d.ts +3 -0
  56. package/dist/es/hcs-15/index.d.ts.map +1 -0
  57. package/dist/es/hcs-15/petal-manager.d.ts +27 -0
  58. package/dist/es/hcs-15/petal-manager.d.ts.map +1 -0
  59. package/dist/es/hcs-15/types.d.ts +41 -0
  60. package/dist/es/hcs-15/types.d.ts.map +1 -0
  61. package/dist/es/hcs-16/flora-account-manager.d.ts.map +1 -1
  62. package/dist/es/hcs-16/types.d.ts +3 -5
  63. package/dist/es/hcs-16/types.d.ts.map +1 -1
  64. package/dist/es/hcs-18/discovery.d.ts +1 -0
  65. package/dist/es/hcs-18/discovery.d.ts.map +1 -1
  66. package/dist/es/hcs-18/types.d.ts +1 -0
  67. package/dist/es/hcs-18/types.d.ts.map +1 -1
  68. package/dist/es/hcs-20/sdk.d.ts.map +1 -1
  69. package/dist/es/hcs-7/evm-bridge.d.ts.map +1 -1
  70. package/dist/es/hcs-7/redis-cache.d.ts.map +1 -1
  71. package/dist/es/hcs-7/wasm-bridge.d.ts.map +1 -1
  72. package/dist/es/index.d.ts +1 -1
  73. package/dist/es/services/types.d.ts +1 -1
  74. package/dist/es/services/types.d.ts.map +1 -1
  75. package/dist/es/standards-sdk.es.js +19 -22
  76. package/dist/es/standards-sdk.es.js.map +1 -1
  77. package/dist/es/standards-sdk.es10.js +1 -1
  78. package/dist/es/standards-sdk.es11.js +10 -2
  79. package/dist/es/standards-sdk.es11.js.map +1 -1
  80. package/dist/es/standards-sdk.es12.js +1 -1
  81. package/dist/es/standards-sdk.es13.js +1 -1
  82. package/dist/es/standards-sdk.es13.js.map +1 -1
  83. package/dist/es/standards-sdk.es14.js +1 -1
  84. package/dist/es/standards-sdk.es15.js +39 -9
  85. package/dist/es/standards-sdk.es15.js.map +1 -1
  86. package/dist/es/standards-sdk.es16.js +1 -0
  87. package/dist/es/standards-sdk.es16.js.map +1 -1
  88. package/dist/es/standards-sdk.es18.js +1 -1
  89. package/dist/es/standards-sdk.es19.js +1 -1
  90. package/dist/es/standards-sdk.es2.js +1 -1
  91. package/dist/es/standards-sdk.es20.js +2 -2
  92. package/dist/es/standards-sdk.es23.js +2 -2
  93. package/dist/es/standards-sdk.es25.js +4 -2
  94. package/dist/es/standards-sdk.es25.js.map +1 -1
  95. package/dist/es/standards-sdk.es26.js +2 -2
  96. package/dist/es/standards-sdk.es27.js +152 -6
  97. package/dist/es/standards-sdk.es27.js.map +1 -1
  98. package/dist/es/standards-sdk.es28.js +26 -189
  99. package/dist/es/standards-sdk.es28.js.map +1 -1
  100. package/dist/es/standards-sdk.es29.js +446 -26
  101. package/dist/es/standards-sdk.es29.js.map +1 -1
  102. package/dist/es/standards-sdk.es3.js +1 -1
  103. package/dist/es/standards-sdk.es3.js.map +1 -1
  104. package/dist/es/standards-sdk.es30.js +6 -342
  105. package/dist/es/standards-sdk.es30.js.map +1 -1
  106. package/dist/es/standards-sdk.es31.js +202 -6
  107. package/dist/es/standards-sdk.es31.js.map +1 -1
  108. package/dist/es/standards-sdk.es32.js +34 -202
  109. package/dist/es/standards-sdk.es32.js.map +1 -1
  110. package/dist/es/standards-sdk.es33.js +529 -34
  111. package/dist/es/standards-sdk.es33.js.map +1 -1
  112. package/dist/es/standards-sdk.es34.js +87 -501
  113. package/dist/es/standards-sdk.es34.js.map +1 -1
  114. package/dist/es/standards-sdk.es35.js +19 -98
  115. package/dist/es/standards-sdk.es35.js.map +1 -1
  116. package/dist/es/standards-sdk.es36.js +156 -18
  117. package/dist/es/standards-sdk.es36.js.map +1 -1
  118. package/dist/es/standards-sdk.es37.js +146 -150
  119. package/dist/es/standards-sdk.es37.js.map +1 -1
  120. package/dist/es/standards-sdk.es38.js +561 -134
  121. package/dist/es/standards-sdk.es38.js.map +1 -1
  122. package/dist/es/standards-sdk.es39.js +562 -543
  123. package/dist/es/standards-sdk.es39.js.map +1 -1
  124. package/dist/es/standards-sdk.es4.js +6 -4
  125. package/dist/es/standards-sdk.es4.js.map +1 -1
  126. package/dist/es/standards-sdk.es40.js +4 -601
  127. package/dist/es/standards-sdk.es40.js.map +1 -1
  128. package/dist/es/standards-sdk.es41.js +415 -4
  129. package/dist/es/standards-sdk.es41.js.map +1 -1
  130. package/dist/es/standards-sdk.es42.js +1536 -389
  131. package/dist/es/standards-sdk.es42.js.map +1 -1
  132. package/dist/es/standards-sdk.es43.js +133 -1541
  133. package/dist/es/standards-sdk.es43.js.map +1 -1
  134. package/dist/es/standards-sdk.es44.js +7 -155
  135. package/dist/es/standards-sdk.es44.js.map +1 -1
  136. package/dist/es/standards-sdk.es45.js +2 -7
  137. package/dist/es/standards-sdk.es45.js.map +1 -1
  138. package/dist/es/standards-sdk.es46.js +5 -5
  139. package/dist/es/standards-sdk.es48.js +1 -1
  140. package/dist/es/standards-sdk.es49.js +1 -1
  141. package/dist/es/standards-sdk.es5.js +4 -6
  142. package/dist/es/standards-sdk.es5.js.map +1 -1
  143. package/dist/es/standards-sdk.es50.js +1 -1
  144. package/dist/es/standards-sdk.es51.js +1 -1
  145. package/dist/es/standards-sdk.es52.js +1 -1
  146. package/dist/es/standards-sdk.es54.js +7135 -2
  147. package/dist/es/standards-sdk.es54.js.map +1 -1
  148. package/dist/es/standards-sdk.es55.js +7134 -41
  149. package/dist/es/standards-sdk.es55.js.map +1 -1
  150. package/dist/es/standards-sdk.es56.js +37 -37
  151. package/dist/es/standards-sdk.es56.js.map +1 -1
  152. package/dist/es/standards-sdk.es57.js +37 -37
  153. package/dist/es/standards-sdk.es57.js.map +1 -1
  154. package/dist/es/standards-sdk.es58.js +78 -72
  155. package/dist/es/standards-sdk.es58.js.map +1 -1
  156. package/dist/es/standards-sdk.es59.js +41 -7134
  157. package/dist/es/standards-sdk.es59.js.map +1 -1
  158. package/dist/es/standards-sdk.es6.js.map +1 -1
  159. package/dist/es/standards-sdk.es7.js +28 -22
  160. package/dist/es/standards-sdk.es7.js.map +1 -1
  161. package/dist/es/standards-sdk.es8.js +5 -5
  162. package/dist/es/utils/logger.d.ts.map +1 -1
  163. package/package.json +37 -35
  164. package/dist/cjs/hcs-21/index.d.ts +0 -4
  165. package/dist/cjs/hcs-21/index.d.ts.map +0 -1
  166. package/dist/cjs/hcs-21/petal-account-manager.d.ts +0 -52
  167. package/dist/cjs/hcs-21/petal-account-manager.d.ts.map +0 -1
  168. package/dist/cjs/hcs-21/types.d.ts +0 -44
  169. package/dist/cjs/hcs-21/types.d.ts.map +0 -1
  170. package/dist/cjs/index-xZmk-HBD.cjs.map +0 -1
  171. package/dist/es/hcs-21/index.d.ts +0 -4
  172. package/dist/es/hcs-21/index.d.ts.map +0 -1
  173. package/dist/es/hcs-21/petal-account-manager.d.ts +0 -52
  174. package/dist/es/hcs-21/petal-account-manager.d.ts.map +0 -1
  175. package/dist/es/hcs-21/types.d.ts +0 -44
  176. package/dist/es/hcs-21/types.d.ts.map +0 -1
  177. package/dist/es/standards-sdk.es60.js +0 -7144
  178. package/dist/es/standards-sdk.es60.js.map +0 -1
@@ -1,586 +1,605 @@
1
- import { proto } from "@hashgraph/proto";
2
- import { Buffer } from "buffer";
3
- import { Hbar, Long, HbarUnit } from "@hashgraph/sdk";
4
- import { ethers } from "ethers";
5
- import { HTSParser } from "./standards-sdk.es48.js";
6
- import { HCSParser } from "./standards-sdk.es49.js";
7
- import { FileParser } from "./standards-sdk.es50.js";
8
- import { CryptoParser } from "./standards-sdk.es51.js";
9
- import { SCSParser } from "./standards-sdk.es52.js";
10
- import { UtilParser } from "./standards-sdk.es53.js";
11
- class TransactionParser {
1
+ import { PrivateKey } from "@hashgraph/sdk";
2
+ var KeyType = /* @__PURE__ */ ((KeyType2) => {
3
+ KeyType2["ED25519"] = "ed25519";
4
+ KeyType2["ECDSA"] = "ecdsa";
5
+ KeyType2["UNKNOWN"] = "unknown";
6
+ return KeyType2;
7
+ })(KeyType || {});
8
+ const _KeyTypeDetector = class _KeyTypeDetector {
12
9
  /**
13
- * Parse a base64 encoded transaction body and return structured data
14
- * @param transactionBodyBase64 - The base64 encoded transaction body
15
- * @returns The parsed transaction
10
+ * Detects private key type from various input formats without throwing errors
11
+ *
12
+ * This detector is designed for private keys only. It uses various heuristics to determine
13
+ * the key type for ambiguous 32-byte keys, including byte patterns and statistical analysis.
14
+ *
15
+ * For ambiguous cases (where confidence is 'uncertain'), if you have the associated account ID,
16
+ * you can use the Hedera mirror node to confirm the key type.
17
+ *
18
+ * @param keyInput - The key to detect, can be a string (hex, base64, PEM), Buffer, or Uint8Array
19
+ * @returns KeyInfo object containing the detected key type and metadata
20
+ *
21
+ * @example
22
+ * // Detect from hex string
23
+ * const info1 = KeyTypeDetector.detect('0x7f96cea0c0d9d5bfb3ab8a42cf0cea44d57f62f2e068f8e5a3251914a9252b04');
24
+ *
25
+ * // Detect from DER-encoded string
26
+ * const info2 = KeyTypeDetector.detect(privateKey.toStringDer());
27
+ *
28
+ * // Detect from PEM format
29
+ * const info3 = KeyTypeDetector.detect(`-----BEGIN PRIVATE KEY-----
30
+ * MC4CAQAwBQYDK2VwBCIEIGRchBsQGQduAAGBQ7GAkKCKkmQ3EGCARwHsHiRqTNR9
31
+ * -----END PRIVATE KEY-----`);
16
32
  */
17
- static parseTransactionBody(transactionBodyBase64) {
33
+ static detect(keyInput) {
18
34
  try {
19
- const buffer = ethers.decodeBase64(transactionBodyBase64);
20
- const txBody = proto.SchedulableTransactionBody.decode(buffer);
21
- const transactionType = this.getTransactionType(txBody);
22
- const result = {
23
- type: transactionType,
24
- humanReadableType: this.getHumanReadableType(transactionType),
25
- transfers: [],
26
- tokenTransfers: [],
27
- raw: txBody
28
- };
29
- if (txBody.memo) {
30
- result.memo = txBody.memo;
31
- }
32
- if (txBody.transactionFee) {
33
- const hbarAmount = Hbar.fromTinybars(
34
- Long.fromValue(txBody.transactionFee)
35
- );
36
- result.transactionFee = hbarAmount.toString(HbarUnit.Hbar);
37
- }
38
- if (txBody.cryptoTransfer) {
39
- CryptoParser.parseCryptoTransfers(txBody.cryptoTransfer, result);
40
- }
41
- if (txBody.cryptoDelete) {
42
- result.cryptoDelete = CryptoParser.parseCryptoDelete(
43
- txBody.cryptoDelete
44
- );
45
- }
46
- if (txBody.cryptoCreateAccount) {
47
- result.cryptoCreateAccount = CryptoParser.parseCryptoCreateAccount(
48
- txBody.cryptoCreateAccount
49
- );
50
- }
51
- if (txBody.cryptoUpdateAccount) {
52
- result.cryptoUpdateAccount = CryptoParser.parseCryptoUpdateAccount(
53
- txBody.cryptoUpdateAccount
54
- );
55
- }
56
- if (txBody.cryptoApproveAllowance) {
57
- result.cryptoApproveAllowance = CryptoParser.parseCryptoApproveAllowance(
58
- txBody.cryptoApproveAllowance
59
- );
60
- }
61
- if (txBody.cryptoDeleteAllowance) {
62
- result.cryptoDeleteAllowance = CryptoParser.parseCryptoDeleteAllowance(
63
- txBody.cryptoDeleteAllowance
64
- );
65
- }
66
- if (txBody.contractCall) {
67
- result.contractCall = SCSParser.parseContractCall(txBody.contractCall);
68
- }
69
- if (txBody.contractCreateInstance) {
70
- result.contractCreate = SCSParser.parseContractCreate(
71
- txBody.contractCreateInstance
72
- );
73
- }
74
- if (txBody.contractUpdateInstance) {
75
- result.contractUpdate = SCSParser.parseContractUpdate(
76
- txBody.contractUpdateInstance
77
- );
78
- }
79
- if (txBody.contractDeleteInstance) {
80
- result.contractDelete = SCSParser.parseContractDelete(
81
- txBody.contractDeleteInstance
82
- );
83
- }
84
- if (txBody.tokenCreation) {
85
- result.tokenCreation = HTSParser.parseTokenCreate(txBody.tokenCreation);
86
- }
87
- if (txBody.tokenMint) {
88
- result.tokenMint = HTSParser.parseTokenMint(txBody.tokenMint);
89
- }
90
- if (txBody.tokenBurn) {
91
- result.tokenBurn = HTSParser.parseTokenBurn(txBody.tokenBurn);
92
- }
93
- if (txBody.tokenUpdate) {
94
- result.tokenUpdate = HTSParser.parseTokenUpdate(txBody.tokenUpdate);
95
- }
96
- if (txBody.tokenFeeScheduleUpdate) {
97
- result.tokenFeeScheduleUpdate = HTSParser.parseTokenFeeScheduleUpdate(
98
- txBody.tokenFeeScheduleUpdate
99
- );
100
- }
101
- if (txBody.tokenFreeze) {
102
- result.tokenFreeze = HTSParser.parseTokenFreeze(txBody.tokenFreeze);
103
- }
104
- if (txBody.tokenUnfreeze) {
105
- result.tokenUnfreeze = HTSParser.parseTokenUnfreeze(
106
- txBody.tokenUnfreeze
107
- );
108
- }
109
- if (txBody.tokenGrantKyc) {
110
- result.tokenGrantKyc = HTSParser.parseTokenGrantKyc(
111
- txBody.tokenGrantKyc
112
- );
113
- }
114
- if (txBody.tokenRevokeKyc) {
115
- result.tokenRevokeKyc = HTSParser.parseTokenRevokeKyc(
116
- txBody.tokenRevokeKyc
117
- );
118
- }
119
- if (txBody.tokenPause) {
120
- result.tokenPause = HTSParser.parseTokenPause(txBody.tokenPause);
121
- }
122
- if (txBody.tokenUnpause) {
123
- result.tokenUnpause = HTSParser.parseTokenUnpause(txBody.tokenUnpause);
124
- }
125
- if (txBody.tokenWipe) {
126
- result.tokenWipeAccount = HTSParser.parseTokenWipeAccount(
127
- txBody.tokenWipe
128
- );
129
- }
130
- if (txBody.tokenDeletion) {
131
- result.tokenDelete = HTSParser.parseTokenDelete(txBody.tokenDeletion);
132
- }
133
- if (txBody.tokenAssociate) {
134
- result.tokenAssociate = HTSParser.parseTokenAssociate(
135
- txBody.tokenAssociate
136
- );
137
- }
138
- if (txBody.tokenDissociate) {
139
- result.tokenDissociate = HTSParser.parseTokenDissociate(
140
- txBody.tokenDissociate
141
- );
142
- }
143
- if (txBody.consensusCreateTopic) {
144
- result.consensusCreateTopic = HCSParser.parseConsensusCreateTopic(
145
- txBody.consensusCreateTopic
146
- );
147
- }
148
- if (txBody.consensusSubmitMessage) {
149
- result.consensusSubmitMessage = HCSParser.parseConsensusSubmitMessage(
150
- txBody.consensusSubmitMessage
151
- );
152
- }
153
- if (txBody.consensusUpdateTopic) {
154
- result.consensusUpdateTopic = HCSParser.parseConsensusUpdateTopic(
155
- txBody.consensusUpdateTopic
156
- );
157
- }
158
- if (txBody.consensusDeleteTopic) {
159
- result.consensusDeleteTopic = HCSParser.parseConsensusDeleteTopic(
160
- txBody.consensusDeleteTopic
161
- );
162
- }
163
- if (txBody.fileCreate) {
164
- result.fileCreate = FileParser.parseFileCreate(txBody.fileCreate);
165
- }
166
- if (txBody.fileAppend) {
167
- result.fileAppend = FileParser.parseFileAppend(txBody.fileAppend);
168
- }
169
- if (txBody.fileUpdate) {
170
- result.fileUpdate = FileParser.parseFileUpdate(txBody.fileUpdate);
171
- }
172
- if (txBody.fileDelete) {
173
- result.fileDelete = FileParser.parseFileDelete(txBody.fileDelete);
174
- }
175
- if (txBody.utilPrng) {
176
- result.utilPrng = UtilParser.parseUtilPrng(txBody.utilPrng);
35
+ let keyBytes;
36
+ let originalFormat = "raw";
37
+ if (typeof keyInput === "string") {
38
+ let trimmed = keyInput.trim();
39
+ if (trimmed.includes("-----BEGIN")) {
40
+ return this.detectFromPem(trimmed);
41
+ }
42
+ if (trimmed.toLowerCase().startsWith("0x")) {
43
+ trimmed = trimmed.substring(2);
44
+ }
45
+ if (this.isValidHex(trimmed)) {
46
+ keyBytes = this.hexToBytes(trimmed);
47
+ originalFormat = "hex";
48
+ } else if (this.isBase64(trimmed)) {
49
+ keyBytes = Buffer.from(trimmed, "base64");
50
+ originalFormat = "der";
51
+ } else {
52
+ return {
53
+ type: "unknown",
54
+ format: "raw",
55
+ isPrivateKey: false,
56
+ confidence: "certain"
57
+ };
58
+ }
59
+ } else if (keyInput instanceof Buffer) {
60
+ keyBytes = new Uint8Array(keyInput);
61
+ } else {
62
+ keyBytes = keyInput;
177
63
  }
178
- return result;
64
+ return this.detectFromBytes(keyBytes, originalFormat);
179
65
  } catch (error) {
180
- throw new Error(
181
- `Failed to parse transaction body: ${error instanceof Error ? error.message : String(error)}`
182
- );
66
+ return {
67
+ type: "unknown",
68
+ format: "raw",
69
+ isPrivateKey: false,
70
+ confidence: "certain"
71
+ };
183
72
  }
184
73
  }
185
74
  /**
186
- * Parse details from a complete schedule response
187
- * @param scheduleResponse - The schedule response to parse
188
- * @returns The parsed transaction
75
+ * Detect key type from raw bytes
76
+ *
77
+ * @param bytes - The raw bytes of the key
78
+ * @param format - The original format of the key
79
+ * @returns KeyInfo object with detection results
80
+ * @private
189
81
  */
190
- static parseScheduleResponse(scheduleResponse) {
191
- if (!scheduleResponse.transaction_body) {
192
- throw new Error("Schedule response missing transaction_body");
82
+ static detectFromBytes(bytes, format) {
83
+ if (this.hasPrefix(bytes, this.ED25519_PUBLIC_KEY_PREFIX)) {
84
+ const keyStart = this.ED25519_PUBLIC_KEY_PREFIX.length;
85
+ return {
86
+ type: "ed25519",
87
+ format: "der",
88
+ isPrivateKey: false,
89
+ rawBytes: bytes.slice(keyStart),
90
+ confidence: "certain"
91
+ };
193
92
  }
194
- const parsed = this.parseTransactionBody(scheduleResponse.transaction_body);
195
- if (scheduleResponse.memo) {
196
- parsed.memo = scheduleResponse.memo;
93
+ if (this.hasPrefix(bytes, this.ED25519_PRIVATE_KEY_PREFIX)) {
94
+ const keyStart = this.ED25519_PRIVATE_KEY_PREFIX.length;
95
+ if (bytes.length >= keyStart + 32) {
96
+ return {
97
+ type: "ed25519",
98
+ format: "der",
99
+ isPrivateKey: true,
100
+ rawBytes: bytes.slice(keyStart, keyStart + 32),
101
+ confidence: "certain"
102
+ };
103
+ }
197
104
  }
198
- return parsed;
199
- }
200
- /**
201
- * Determine the transaction type
202
- * @param txBody - The transaction body to determine the type of
203
- * @returns The type of the transaction
204
- */
205
- static getTransactionType(txBody) {
206
- let transactionType = "unknown";
207
- if (txBody.cryptoTransfer) {
208
- transactionType = "cryptoTransfer";
209
- } else if (txBody.cryptoCreateAccount) {
210
- transactionType = "cryptoCreateAccount";
211
- } else if (txBody.cryptoUpdateAccount) {
212
- transactionType = "cryptoUpdateAccount";
213
- } else if (txBody.cryptoApproveAllowance) {
214
- transactionType = "cryptoApproveAllowance";
215
- } else if (txBody.cryptoDeleteAllowance) {
216
- transactionType = "cryptoDeleteAllowance";
217
- } else if (txBody.cryptoDelete) {
218
- transactionType = "cryptoDelete";
219
- } else if (txBody.consensusCreateTopic) {
220
- transactionType = "consensusCreateTopic";
221
- } else if (txBody.consensusUpdateTopic) {
222
- transactionType = "consensusUpdateTopic";
223
- } else if (txBody.consensusSubmitMessage) {
224
- transactionType = "consensusSubmitMessage";
225
- } else if (txBody.consensusDeleteTopic) {
226
- transactionType = "consensusDeleteTopic";
227
- } else if (txBody.fileCreate) {
228
- transactionType = "fileCreate";
229
- } else if (txBody.fileAppend) {
230
- transactionType = "fileAppend";
231
- } else if (txBody.fileUpdate) {
232
- transactionType = "fileUpdate";
233
- } else if (txBody.fileDelete) {
234
- transactionType = "fileDelete";
235
- } else if (txBody.contractCall) {
236
- transactionType = "contractCall";
237
- } else if (txBody.contractCreateInstance) {
238
- transactionType = "contractCreate";
239
- } else if (txBody.contractUpdateInstance) {
240
- transactionType = "contractUpdate";
241
- } else if (txBody.contractDeleteInstance) {
242
- transactionType = "contractDelete";
243
- } else if (txBody.tokenCreation) {
244
- transactionType = "tokenCreate";
245
- } else if (txBody.tokenUpdate) {
246
- transactionType = "tokenUpdate";
247
- } else if (txBody.tokenDeletion) {
248
- transactionType = "tokenDelete";
249
- } else if (txBody.tokenAssociate) {
250
- transactionType = "tokenAssociate";
251
- } else if (txBody.tokenDissociate) {
252
- transactionType = "tokenDissociate";
253
- } else if (txBody.tokenMint) {
254
- transactionType = "tokenMint";
255
- } else if (txBody.tokenBurn) {
256
- transactionType = "tokenBurn";
257
- } else if (txBody.tokenFeeScheduleUpdate) {
258
- transactionType = "tokenFeeScheduleUpdate";
259
- } else if (txBody.tokenFreeze) {
260
- transactionType = "tokenFreeze";
261
- } else if (txBody.tokenUnfreeze) {
262
- transactionType = "tokenUnfreeze";
263
- } else if (txBody.tokenGrantKyc) {
264
- transactionType = "tokenGrantKyc";
265
- } else if (txBody.tokenRevokeKyc) {
266
- transactionType = "tokenRevokeKyc";
267
- } else if (txBody.tokenPause) {
268
- transactionType = "tokenPause";
269
- } else if (txBody.tokenUnpause) {
270
- transactionType = "tokenUnpause";
271
- } else if (txBody.tokenWipe) {
272
- transactionType = "tokenWipe";
273
- } else if (txBody.utilPrng) {
274
- transactionType = "utilPrng";
105
+ if (this.hasPrefix(bytes, this.ECDSA_SECP256K1_PUBLIC_KEY_PREFIX)) {
106
+ const keyStart = this.ECDSA_SECP256K1_PUBLIC_KEY_PREFIX.length;
107
+ return {
108
+ type: "ecdsa",
109
+ format: "der",
110
+ isPrivateKey: false,
111
+ rawBytes: bytes.slice(keyStart),
112
+ confidence: "certain"
113
+ };
114
+ }
115
+ if (this.hasPrefix(bytes, this.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX_SHORT)) {
116
+ const keyStart = this.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX_SHORT.length;
117
+ return {
118
+ type: "ecdsa",
119
+ format: "der",
120
+ isPrivateKey: true,
121
+ rawBytes: bytes.slice(keyStart),
122
+ confidence: "certain"
123
+ };
124
+ }
125
+ if (this.hasPrefix(bytes, this.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX)) {
126
+ const keyStart = this.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX.length;
127
+ return {
128
+ type: "ecdsa",
129
+ format: "der",
130
+ isPrivateKey: true,
131
+ rawBytes: bytes.slice(keyStart, keyStart + 32),
132
+ confidence: "certain"
133
+ };
134
+ }
135
+ if (this.hasPrefix(bytes, this.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX_LONG)) {
136
+ const keyStart = this.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX_LONG.length;
137
+ return {
138
+ type: "ecdsa",
139
+ format: "der",
140
+ isPrivateKey: true,
141
+ rawBytes: bytes.slice(keyStart, keyStart + 32),
142
+ confidence: "certain"
143
+ };
144
+ }
145
+ if (bytes.length > 36 && this.containsECDSAPrivateKeyPattern(bytes)) {
146
+ return {
147
+ type: "ecdsa",
148
+ format: "der",
149
+ isPrivateKey: true,
150
+ rawBytes: this.extractECDSAPrivateKey(bytes),
151
+ confidence: "certain"
152
+ };
153
+ }
154
+ switch (bytes.length) {
155
+ case this.ED25519_PUBLIC_KEY_LENGTH:
156
+ if (format === "hex") {
157
+ const hexStr = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
158
+ const privateResult = this.tryCreateKey(hexStr);
159
+ if (privateResult.type !== "unknown") {
160
+ return privateResult;
161
+ }
162
+ }
163
+ return {
164
+ type: "unknown",
165
+ format,
166
+ isPrivateKey: false,
167
+ rawBytes: bytes,
168
+ confidence: "certain"
169
+ };
170
+ case this.ED25519_EXPANDED_PRIVATE_KEY_LENGTH:
171
+ return {
172
+ type: "ed25519",
173
+ format,
174
+ isPrivateKey: true,
175
+ rawBytes: bytes,
176
+ confidence: "certain"
177
+ };
275
178
  }
276
- return transactionType;
179
+ return {
180
+ type: "unknown",
181
+ format,
182
+ isPrivateKey: false,
183
+ rawBytes: bytes,
184
+ confidence: "certain"
185
+ };
277
186
  }
278
187
  /**
279
- * Convert technical transaction type to human-readable format
280
- * @param type - The technical transaction type
281
- * @returns The human-readable transaction type
188
+ * Detect key type from PEM format
189
+ *
190
+ * @param pem - The PEM-encoded key string
191
+ * @returns KeyInfo object with detection results
192
+ * @private
282
193
  */
283
- static getHumanReadableType(type) {
284
- const typeMap = {
285
- cryptoTransfer: "HBAR Transfer",
286
- cryptoCreateAccount: "Create Account",
287
- cryptoUpdateAccount: "Update Account",
288
- cryptoDeleteAccount: "Delete Account",
289
- cryptoApproveAllowance: "Approve Allowance",
290
- cryptoDeleteAllowance: "Delete Allowance",
291
- cryptoDelete: "Delete Account",
292
- consensusCreateTopic: "Create Topic",
293
- consensusUpdateTopic: "Update Topic",
294
- consensusSubmitMessage: "Submit Message",
295
- consensusDeleteTopic: "Delete Topic",
296
- fileCreate: "Create File",
297
- fileAppend: "Append File",
298
- fileUpdate: "Update File",
299
- fileDelete: "Delete File",
300
- contractCall: "Contract Call",
301
- contractCreate: "Create Contract",
302
- contractUpdate: "Update Contract",
303
- contractDelete: "Delete Contract",
304
- ethereumTransaction: "Ethereum Transaction",
305
- tokenCreate: "Create Token",
306
- tokenUpdate: "Update Token",
307
- tokenDelete: "Delete Token",
308
- tokenAssociate: "Associate Token",
309
- tokenDissociate: "Dissociate Token",
310
- tokenMint: "Mint Token",
311
- tokenBurn: "Burn Token",
312
- tokenFeeScheduleUpdate: "Update Token Fee Schedule",
313
- tokenFreeze: "Freeze Token",
314
- tokenUnfreeze: "Unfreeze Token",
315
- tokenGrantKyc: "Grant KYC",
316
- tokenRevokeKyc: "Revoke KYC",
317
- tokenPause: "Pause Token",
318
- tokenUnpause: "Unpause Token",
319
- tokenWipe: "Wipe Token",
320
- scheduleCreate: "Create Schedule",
321
- scheduleSign: "Sign Schedule",
322
- utilPrng: "Generate Random Number",
323
- unknown: "Unknown Transaction"
194
+ static detectFromPem(pem) {
195
+ const trimmedPem = pem.trim();
196
+ const isPrivateKey = trimmedPem.includes("PRIVATE KEY");
197
+ const pemTypes = {
198
+ EC_PRIVATE: /-----BEGIN EC PRIVATE KEY-----/,
199
+ EC_PUBLIC: /-----BEGIN EC PUBLIC KEY-----/,
200
+ PRIVATE: /-----BEGIN PRIVATE KEY-----/,
201
+ PUBLIC: /-----BEGIN PUBLIC KEY-----/
324
202
  };
325
- let result;
326
- if (typeMap[type]) {
327
- result = typeMap[type];
328
- } else {
329
- result = "Unknown Transaction";
203
+ let isECKey = false;
204
+ for (const [type, regex] of Object.entries(pemTypes)) {
205
+ if (regex.test(trimmedPem) && type.includes("EC")) {
206
+ isECKey = true;
207
+ break;
208
+ }
209
+ }
210
+ const base64Match = trimmedPem.match(
211
+ /-----BEGIN[\s\S]+?-----[\r\n]+([\s\S]+?)[\r\n]+-----END/
212
+ );
213
+ if (!base64Match) {
214
+ return {
215
+ type: "unknown",
216
+ format: "pem",
217
+ isPrivateKey,
218
+ confidence: "certain"
219
+ };
220
+ }
221
+ const base64Content = base64Match[1].replace(/\s/g, "");
222
+ try {
223
+ const derBytes = Buffer.from(base64Content, "base64");
224
+ const result = this.detectFromBytes(new Uint8Array(derBytes), "der");
225
+ if (result.type === "unknown" && isECKey) {
226
+ return {
227
+ type: "ecdsa",
228
+ format: "pem",
229
+ isPrivateKey,
230
+ rawBytes: derBytes,
231
+ confidence: "certain"
232
+ };
233
+ }
234
+ return {
235
+ ...result,
236
+ format: "pem"
237
+ };
238
+ } catch {
239
+ return {
240
+ type: "unknown",
241
+ format: "pem",
242
+ isPrivateKey,
243
+ confidence: "certain"
244
+ };
330
245
  }
331
- return result;
332
246
  }
333
247
  /**
334
- * Get a human-readable summary of the transaction
335
- * @param parsedTx - The parsed transaction
336
- * @returns The human-readable summary of the transaction
248
+ * Try to create a key using Hedera SDK to validate the type
249
+ *
250
+ * @param keyInput - The key input string or bytes
251
+ * @returns KeyInfo object with detection results
252
+ * @private
337
253
  */
338
- static getTransactionSummary(parsedTx) {
339
- if (parsedTx.type === "cryptoTransfer") {
340
- const senders = [];
341
- const receivers = [];
342
- for (const transfer of parsedTx.transfers) {
343
- const originalAmountFloat = parseFloat(transfer.amount);
344
- let displayStr = transfer.amount;
345
- if (displayStr.startsWith("-")) {
346
- displayStr = displayStr.substring(1);
347
- }
348
- displayStr = displayStr.replace(/\s*ℏ$/, "");
349
- if (originalAmountFloat < 0) {
350
- senders.push(`${transfer.accountId} (${displayStr} ℏ)`);
351
- } else if (originalAmountFloat > 0) {
352
- receivers.push(`${transfer.accountId} (${displayStr} ℏ)`);
353
- }
354
- }
355
- if (senders.length > 0 && receivers.length > 0) {
356
- return `Transfer of HBAR from ${senders.join(", ")} to ${receivers.join(
357
- ", "
358
- )}`;
359
- } else {
360
- return parsedTx.humanReadableType;
361
- }
362
- } else if (parsedTx.contractCall) {
363
- let contractCallSummary = `Contract call to ${parsedTx.contractCall.contractId} with ${parsedTx.contractCall.gas} gas`;
364
- if (parsedTx.contractCall.amount > 0) {
365
- contractCallSummary += ` and ${parsedTx.contractCall.amount} HBAR`;
366
- }
367
- if (parsedTx.contractCall.functionName) {
368
- contractCallSummary += ` calling function ${parsedTx.contractCall.functionName}`;
369
- }
370
- return contractCallSummary;
371
- } else if (parsedTx.tokenMint) {
372
- return `Mint ${parsedTx.tokenMint.amount} tokens for token ${parsedTx.tokenMint.tokenId}`;
373
- } else if (parsedTx.tokenBurn) {
374
- return `Burn ${parsedTx.tokenBurn.amount} tokens for token ${parsedTx.tokenBurn.tokenId}`;
375
- } else if (parsedTx.tokenCreation) {
376
- let summary = `Create token ${parsedTx.tokenCreation.tokenName || "(No Name)"} (${parsedTx.tokenCreation.tokenSymbol || "(No Symbol)"})`;
377
- if (parsedTx.tokenCreation.initialSupply) {
378
- summary += ` with initial supply ${parsedTx.tokenCreation.initialSupply}`;
379
- }
380
- if (parsedTx.tokenCreation.customFees?.length) {
381
- summary += ` including ${parsedTx.tokenCreation.customFees.length} custom fee(s)`;
382
- }
383
- return summary;
384
- } else if (parsedTx.tokenTransfers.length > 0) {
385
- const tokenGroups = {};
386
- for (const transfer of parsedTx.tokenTransfers) {
387
- if (!tokenGroups[transfer.tokenId]) {
388
- tokenGroups[transfer.tokenId] = [];
254
+ static tryCreateKey(keyInput) {
255
+ try {
256
+ if (typeof keyInput === "string") {
257
+ let keyStr = keyInput.trim();
258
+ if (keyStr.toLowerCase().startsWith("0x")) {
259
+ keyStr = keyStr.substring(2);
389
260
  }
390
- tokenGroups[transfer.tokenId].push(transfer);
391
- }
392
- const tokenSummaries = [];
393
- for (const [tokenId, transfers] of Object.entries(tokenGroups)) {
394
- const tokenSenders = [];
395
- const tokenReceivers = [];
396
- for (const transfer of transfers) {
397
- const transferAmountValue = parseFloat(transfer.amount.toString());
398
- if (transferAmountValue < 0) {
399
- tokenSenders.push(
400
- `${transfer.accountId} (${Math.abs(transferAmountValue)})`
401
- );
402
- } else if (transferAmountValue > 0) {
403
- tokenReceivers.push(
404
- `${transfer.accountId} (${transferAmountValue})`
405
- );
261
+ if (this.isValidHex(keyStr) && keyStr.length === 64) {
262
+ const keyBytes = this.hexToBytes(keyStr);
263
+ const signatureResult = this.detectBySignature(keyStr);
264
+ if (signatureResult.type !== "unknown") {
265
+ return {
266
+ type: signatureResult.type,
267
+ format: "hex",
268
+ isPrivateKey: true,
269
+ rawBytes: keyBytes,
270
+ confidence: signatureResult.confidence,
271
+ warning: signatureResult.warning
272
+ };
406
273
  }
274
+ return {
275
+ type: "unknown",
276
+ format: "hex",
277
+ isPrivateKey: false,
278
+ rawBytes: keyBytes,
279
+ confidence: "certain"
280
+ };
407
281
  }
408
- if (tokenSenders.length > 0 && tokenReceivers.length > 0) {
409
- tokenSummaries.push(
410
- `Transfer of token ${tokenId} from ${tokenSenders.join(
411
- ", "
412
- )} to ${tokenReceivers.join(", ")}`
413
- );
414
- }
415
- }
416
- if (tokenSummaries.length > 0) {
417
- return tokenSummaries.join("; ");
418
- } else {
419
- return parsedTx.humanReadableType;
420
- }
421
- } else if (parsedTx.consensusCreateTopic) {
422
- let summary = `Create new topic`;
423
- if (parsedTx.consensusCreateTopic.memo) {
424
- summary += ` with memo "${parsedTx.consensusCreateTopic.memo}"`;
425
- }
426
- if (parsedTx.consensusCreateTopic.autoRenewAccountId) {
427
- summary += `, auto-renew by ${parsedTx.consensusCreateTopic.autoRenewAccountId}`;
428
- }
429
- return summary;
430
- } else if (parsedTx.consensusSubmitMessage) {
431
- let summary = `Submit message`;
432
- if (parsedTx.consensusSubmitMessage.topicId) {
433
- summary += ` to topic ${parsedTx.consensusSubmitMessage.topicId}`;
434
- }
435
- if (parsedTx.consensusSubmitMessage.message) {
436
- if (parsedTx.consensusSubmitMessage.messageEncoding === "utf8") {
437
- const messagePreview = parsedTx.consensusSubmitMessage.message.substring(0, 70);
438
- summary += `: "${messagePreview}${parsedTx.consensusSubmitMessage.message.length > 70 ? "..." : ""}"`;
439
- } else {
440
- summary += ` (binary message data, length: ${Buffer.from(parsedTx.consensusSubmitMessage.message, "base64").length} bytes)`;
441
- }
442
- }
443
- if (parsedTx.consensusSubmitMessage.chunkInfoNumber && parsedTx.consensusSubmitMessage.chunkInfoTotal) {
444
- summary += ` (chunk ${parsedTx.consensusSubmitMessage.chunkInfoNumber}/${parsedTx.consensusSubmitMessage.chunkInfoTotal})`;
445
- }
446
- return summary;
447
- } else if (parsedTx.fileCreate) {
448
- let summary = "Create File";
449
- if (parsedTx.fileCreate.memo) {
450
- summary += ` with memo "${parsedTx.fileCreate.memo}"`;
451
- }
452
- if (parsedTx.fileCreate.contents) {
453
- summary += ` (includes content)`;
454
- }
455
- return summary;
456
- } else if (parsedTx.fileAppend) {
457
- return `Append to File ${parsedTx.fileAppend.fileId || "(Unknown ID)"}`;
458
- } else if (parsedTx.fileUpdate) {
459
- return `Update File ${parsedTx.fileUpdate.fileId || "(Unknown ID)"}`;
460
- } else if (parsedTx.fileDelete) {
461
- return `Delete File ${parsedTx.fileDelete.fileId || "(Unknown ID)"}`;
462
- } else if (parsedTx.consensusUpdateTopic) {
463
- return `Update Topic ${parsedTx.consensusUpdateTopic.topicId || "(Unknown ID)"}`;
464
- } else if (parsedTx.consensusDeleteTopic) {
465
- return `Delete Topic ${parsedTx.consensusDeleteTopic.topicId || "(Unknown ID)"}`;
466
- } else if (parsedTx.tokenUpdate) {
467
- return `Update Token ${parsedTx.tokenUpdate.tokenId || "(Unknown ID)"}`;
468
- } else if (parsedTx.tokenFeeScheduleUpdate) {
469
- return `Update Fee Schedule for Token ${parsedTx.tokenFeeScheduleUpdate.tokenId || "(Unknown ID)"}`;
470
- } else if (parsedTx.utilPrng) {
471
- let summary = "Generate Random Number";
472
- if (parsedTx.utilPrng.range && parsedTx.utilPrng.range > 0) {
473
- summary += ` (range up to ${parsedTx.utilPrng.range - 1})`;
474
282
  }
475
- return summary;
476
- } else if (parsedTx.tokenFreeze) {
477
- return `Freeze Token ${parsedTx.tokenFreeze.tokenId} for Account ${parsedTx.tokenFreeze.accountId}`;
478
- } else if (parsedTx.tokenUnfreeze) {
479
- return `Unfreeze Token ${parsedTx.tokenUnfreeze.tokenId} for Account ${parsedTx.tokenUnfreeze.accountId}`;
480
- } else if (parsedTx.tokenGrantKyc) {
481
- return `Grant KYC for Token ${parsedTx.tokenGrantKyc.tokenId} to Account ${parsedTx.tokenGrantKyc.accountId}`;
482
- } else if (parsedTx.tokenRevokeKyc) {
483
- return `Revoke KYC for Token ${parsedTx.tokenRevokeKyc.tokenId} from Account ${parsedTx.tokenRevokeKyc.accountId}`;
484
- } else if (parsedTx.tokenPause) {
485
- return `Pause Token ${parsedTx.tokenPause.tokenId}`;
486
- } else if (parsedTx.tokenUnpause) {
487
- return `Unpause Token ${parsedTx.tokenUnpause.tokenId}`;
488
- } else if (parsedTx.tokenWipeAccount) {
489
- let summary = `Wipe Token ${parsedTx.tokenWipeAccount.tokenId} from Account ${parsedTx.tokenWipeAccount.accountId}`;
490
- if (parsedTx.tokenWipeAccount.serialNumbers?.length) {
491
- summary += ` (Serials: ${parsedTx.tokenWipeAccount.serialNumbers.join(
492
- ", "
493
- )})`;
494
- }
495
- if (parsedTx.tokenWipeAccount.amount) {
496
- summary += ` (Amount: ${parsedTx.tokenWipeAccount.amount})`;
497
- }
498
- return summary;
499
- } else if (parsedTx.tokenDelete) {
500
- return `Delete Token ${parsedTx.tokenDelete.tokenId}`;
501
- } else if (parsedTx.tokenAssociate) {
502
- return `Associate Account ${parsedTx.tokenAssociate.accountId} with Tokens: ${parsedTx.tokenAssociate.tokenIds?.join(", ")}`;
503
- } else if (parsedTx.tokenDissociate) {
504
- return `Dissociate Account ${parsedTx.tokenDissociate.accountId} from Tokens: ${parsedTx.tokenDissociate.tokenIds?.join(", ")}`;
505
- } else if (parsedTx.cryptoDelete) {
506
- return `Delete Account ${parsedTx.cryptoDelete.deleteAccountId}`;
283
+ } catch {
284
+ }
285
+ return {
286
+ type: "unknown",
287
+ format: "hex",
288
+ isPrivateKey: false,
289
+ rawBytes: new Uint8Array(),
290
+ confidence: "certain"
291
+ };
292
+ }
293
+ /**
294
+ * Detect key type by attempting to sign a message with both algorithms
295
+ *
296
+ * For ambiguous cases where both algorithms can use the key, this method
297
+ * uses heuristics to make a best-effort determination. For production use
298
+ * with ambiguous keys, consider using the Hedera mirror node to confirm
299
+ * the key type if you have the associated account ID.
300
+ *
301
+ * @param hexKey - The hex string representation of the key
302
+ * @returns Object containing type, confidence level, and optional warning
303
+ * @private
304
+ */
305
+ static detectBySignature(hexKey) {
306
+ const keyBigInt = BigInt("0x" + hexKey);
307
+ const secp256k1Order = BigInt(
308
+ "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"
309
+ );
310
+ if (keyBigInt === 0n) {
311
+ return {
312
+ type: "ed25519",
313
+ confidence: "certain"
314
+ };
315
+ }
316
+ if (keyBigInt >= secp256k1Order) {
317
+ return {
318
+ type: "ed25519",
319
+ confidence: "certain"
320
+ };
507
321
  }
508
- if (parsedTx.cryptoCreateAccount) {
509
- let summary = "Create Account";
510
- if (parsedTx.cryptoCreateAccount.initialBalance && parsedTx.cryptoCreateAccount.initialBalance !== "0") {
511
- summary += ` with balance ${parsedTx.cryptoCreateAccount.initialBalance}`;
322
+ let ed25519CanSign = false;
323
+ let ecdsaCanSign = false;
324
+ try {
325
+ const ed25519Key = PrivateKey.fromStringED25519(hexKey);
326
+ const testMessage = new Uint8Array([1, 2, 3, 4, 5]);
327
+ const signature = ed25519Key.sign(testMessage);
328
+ if (ed25519Key.publicKey.verify(testMessage, signature)) {
329
+ ed25519CanSign = true;
512
330
  }
513
- if (parsedTx.cryptoCreateAccount.alias) {
514
- summary += ` (Alias: ${parsedTx.cryptoCreateAccount.alias})`;
331
+ } catch {
332
+ }
333
+ try {
334
+ const ecdsaKey = PrivateKey.fromStringECDSA(hexKey);
335
+ const testMessage = new Uint8Array([1, 2, 3, 4, 5]);
336
+ const signature = ecdsaKey.sign(testMessage);
337
+ if (ecdsaKey.publicKey.verify(testMessage, signature)) {
338
+ ecdsaCanSign = true;
515
339
  }
516
- return summary;
340
+ } catch {
517
341
  }
518
- if (parsedTx.cryptoUpdateAccount) {
519
- return `Update Account ${parsedTx.cryptoUpdateAccount.accountIdToUpdate || "(Unknown ID)"}`;
342
+ if (ed25519CanSign && ecdsaCanSign) {
343
+ const keyBytes = this.hexToBytes(hexKey);
344
+ if (keyBytes[0] === 168 && keyBytes[1] === 1) {
345
+ return {
346
+ type: "ed25519",
347
+ confidence: "uncertain",
348
+ warning: "Detection based on byte pattern heuristic. Both ED25519 and ECDSA accept this key."
349
+ };
350
+ }
351
+ let highBytes = 0;
352
+ for (const byte of keyBytes) {
353
+ if (byte >= 128) highBytes++;
354
+ }
355
+ const highByteRatio = highBytes / keyBytes.length;
356
+ if (highByteRatio >= 0.4 && highByteRatio <= 0.6) {
357
+ return {
358
+ type: "ecdsa",
359
+ confidence: "uncertain",
360
+ warning: "Detection based on entropy heuristic. Both ED25519 and ECDSA accept this key."
361
+ };
362
+ }
363
+ return {
364
+ type: "ecdsa",
365
+ confidence: "uncertain",
366
+ warning: "Detection based on entropy heuristic. Both ED25519 and ECDSA accept this key. Defaulting to ECDSA."
367
+ };
520
368
  }
521
- if (parsedTx.cryptoApproveAllowance) {
522
- let count = (parsedTx.cryptoApproveAllowance.hbarAllowances?.length || 0) + (parsedTx.cryptoApproveAllowance.tokenAllowances?.length || 0) + (parsedTx.cryptoApproveAllowance.nftAllowances?.length || 0);
523
- return `Approve ${count} Crypto Allowance(s)`;
369
+ if (ed25519CanSign && !ecdsaCanSign) {
370
+ return {
371
+ type: "ed25519",
372
+ confidence: "certain"
373
+ };
524
374
  }
525
- if (parsedTx.cryptoDeleteAllowance) {
526
- return `Delete ${parsedTx.cryptoDeleteAllowance.nftAllowancesToRemove?.length || 0} NFT Crypto Allowance(s)`;
375
+ if (ecdsaCanSign && !ed25519CanSign) {
376
+ return {
377
+ type: "ecdsa",
378
+ confidence: "certain"
379
+ };
527
380
  }
528
- if (parsedTx.contractCreate) {
529
- let summary = "Create Contract";
530
- if (parsedTx.contractCreate.memo) {
531
- summary += ` (Memo: ${parsedTx.contractCreate.memo})`;
532
- }
533
- return summary;
381
+ return {
382
+ type: "unknown",
383
+ confidence: "certain"
384
+ };
385
+ }
386
+ /**
387
+ * Check if a string is valid hexadecimal with even length
388
+ *
389
+ * @param str - The string to check
390
+ * @returns True if the string is valid hex with even length
391
+ * @private
392
+ */
393
+ static isValidHex(str) {
394
+ return /^[0-9a-fA-F]+$/.test(str) && str.length % 2 === 0;
395
+ }
396
+ /**
397
+ * Check if a string is valid base64
398
+ *
399
+ * @param str - The string to check
400
+ * @returns True if the string is valid base64
401
+ * @private
402
+ */
403
+ static isBase64(str) {
404
+ try {
405
+ return Buffer.from(str, "base64").toString("base64") === str;
406
+ } catch {
407
+ return false;
534
408
  }
535
- if (parsedTx.contractUpdate) {
536
- return `Update Contract ${parsedTx.contractUpdate.contractIdToUpdate || "(Unknown ID)"}`;
409
+ }
410
+ /**
411
+ * Convert a hex string to bytes
412
+ *
413
+ * @param hex - The hex string to convert
414
+ * @returns Uint8Array of bytes
415
+ * @private
416
+ */
417
+ static hexToBytes(hex) {
418
+ const bytes = new Uint8Array(hex.length / 2);
419
+ for (let i = 0; i < hex.length; i += 2) {
420
+ bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
537
421
  }
538
- if (parsedTx.contractDelete) {
539
- let summary = `Delete Contract ${parsedTx.contractDelete.contractIdToDelete || "(Unknown ID)"}`;
540
- if (parsedTx.contractDelete.transferAccountId) {
541
- summary += ` (Transfer to Account: ${parsedTx.contractDelete.transferAccountId})`;
542
- } else if (parsedTx.contractDelete.transferContractId) {
543
- summary += ` (Transfer to Contract: ${parsedTx.contractDelete.transferContractId})`;
544
- }
545
- return summary;
422
+ return bytes;
423
+ }
424
+ /**
425
+ * Check if bytes start with a specific prefix
426
+ *
427
+ * @param bytes - The bytes to check
428
+ * @param prefix - The prefix to look for
429
+ * @returns True if bytes start with the prefix
430
+ * @private
431
+ */
432
+ static hasPrefix(bytes, prefix) {
433
+ if (bytes.length < prefix.length) return false;
434
+ for (let i = 0; i < prefix.length; i++) {
435
+ if (bytes[i] !== prefix[i]) return false;
546
436
  }
547
- if (parsedTx.humanReadableType && parsedTx.humanReadableType !== "Unknown Transaction") {
548
- return parsedTx.humanReadableType;
437
+ return true;
438
+ }
439
+ /**
440
+ * Check if bytes contain an ECDSA private key pattern
441
+ *
442
+ * @param bytes - The bytes to check
443
+ * @returns True if ECDSA private key pattern is found
444
+ * @private
445
+ */
446
+ static containsECDSAPrivateKeyPattern(bytes) {
447
+ for (let i = 0; i < bytes.length - 7; i++) {
448
+ if (bytes[i] === 48 && bytes[i + 1] === 116 && bytes[i + 2] === 2 && bytes[i + 3] === 1 && bytes[i + 4] === 1 && bytes[i + 5] === 4 && bytes[i + 6] === 32) {
449
+ return true;
450
+ }
549
451
  }
550
- if (parsedTx.tokenTransfers.length > 0) {
551
- const tokenGroups = {};
552
- for (const transfer of parsedTx.tokenTransfers) {
553
- if (!tokenGroups[transfer.tokenId]) {
554
- tokenGroups[transfer.tokenId] = [];
555
- }
556
- tokenGroups[transfer.tokenId].push(transfer);
452
+ return false;
453
+ }
454
+ /**
455
+ * Extract ECDSA private key from DER structure
456
+ *
457
+ * @param bytes - The DER encoded bytes
458
+ * @returns The extracted private key bytes
459
+ * @private
460
+ */
461
+ static extractECDSAPrivateKey(bytes) {
462
+ for (let i = 0; i < bytes.length - 32; i++) {
463
+ if (bytes[i] === 4 && bytes[i + 1] === 32) {
464
+ return bytes.slice(i + 2, i + 34);
557
465
  }
558
- const tokenSummaries = [];
559
- for (const [tokenId, transfers] of Object.entries(tokenGroups)) {
560
- const tokenSenders = transfers.filter((t) => t.amount < 0).map((t) => `${t.accountId} (${Math.abs(t.amount)})`);
561
- const tokenReceivers = transfers.filter((t) => t.amount > 0).map((t) => `${t.accountId} (${t.amount})`);
562
- if (tokenSenders.length > 0 && tokenReceivers.length > 0) {
563
- tokenSummaries.push(
564
- `Transfer of token ${tokenId} from ${tokenSenders.join(
565
- ", "
566
- )} to ${tokenReceivers.join(", ")}`
567
- );
568
- } else if (tokenReceivers.length > 0) {
569
- tokenSummaries.push(
570
- `Token ${tokenId} received by ${tokenReceivers.join(", ")}`
571
- );
572
- } else if (tokenSenders.length > 0) {
573
- tokenSummaries.push(
574
- `Token ${tokenId} sent from ${tokenSenders.join(", ")}`
575
- );
576
- }
466
+ }
467
+ return bytes;
468
+ }
469
+ };
470
+ _KeyTypeDetector.ED25519_PUBLIC_KEY_LENGTH = 32;
471
+ _KeyTypeDetector.ED25519_EXPANDED_PRIVATE_KEY_LENGTH = 64;
472
+ _KeyTypeDetector.ED25519_PUBLIC_KEY_PREFIX = Buffer.from([
473
+ 48,
474
+ 42,
475
+ 48,
476
+ 5,
477
+ 6,
478
+ 3,
479
+ 43,
480
+ 101,
481
+ 112,
482
+ 3,
483
+ 33,
484
+ 0
485
+ ]);
486
+ _KeyTypeDetector.ED25519_PRIVATE_KEY_PREFIX = Buffer.from([
487
+ 48,
488
+ 46,
489
+ 2,
490
+ 1,
491
+ 0,
492
+ 48,
493
+ 5,
494
+ 6,
495
+ 3,
496
+ 43,
497
+ 101,
498
+ 112,
499
+ 4,
500
+ 34,
501
+ 4,
502
+ 32
503
+ ]);
504
+ _KeyTypeDetector.ECDSA_SECP256K1_PUBLIC_KEY_PREFIX = Buffer.from([
505
+ 48,
506
+ 86,
507
+ 48,
508
+ 16,
509
+ 6,
510
+ 7,
511
+ 42,
512
+ 134,
513
+ 72,
514
+ 206,
515
+ 61,
516
+ 2,
517
+ 1,
518
+ 6,
519
+ 5,
520
+ 43,
521
+ 129,
522
+ 4,
523
+ 0,
524
+ 10,
525
+ 3,
526
+ 66,
527
+ 0
528
+ ]);
529
+ _KeyTypeDetector.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX = Buffer.from([
530
+ 48,
531
+ 116,
532
+ 2,
533
+ 1,
534
+ 1,
535
+ 4,
536
+ 32
537
+ ]);
538
+ _KeyTypeDetector.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX_SHORT = Buffer.from([
539
+ 48,
540
+ 48,
541
+ 2,
542
+ 1,
543
+ 0,
544
+ 48,
545
+ 7,
546
+ 6,
547
+ 5,
548
+ 43,
549
+ 129,
550
+ 4,
551
+ 0,
552
+ 10,
553
+ 4,
554
+ 34,
555
+ 4,
556
+ 32
557
+ ]);
558
+ _KeyTypeDetector.ECDSA_SECP256K1_PRIVATE_KEY_PREFIX_LONG = Buffer.from(
559
+ [48, 119, 2, 1, 1, 4, 32]
560
+ );
561
+ let KeyTypeDetector = _KeyTypeDetector;
562
+ function detectKeyTypeFromString(privateKeyString) {
563
+ const keyInfo = KeyTypeDetector.detect(privateKeyString);
564
+ if (keyInfo.type !== "unknown") {
565
+ try {
566
+ const privateKey = keyInfo.type === "ecdsa" ? PrivateKey.fromStringECDSA(privateKeyString) : PrivateKey.fromStringED25519(privateKeyString);
567
+ const result = {
568
+ detectedType: keyInfo.type,
569
+ privateKey
570
+ };
571
+ if (keyInfo.confidence === "uncertain") {
572
+ result.warning = `Key type detection is uncertain. If you have the associated account ID, consider using the Hedera mirror node to confirm the key type.`;
577
573
  }
578
- if (tokenSummaries.length > 0) return tokenSummaries.join("; ");
574
+ return result;
575
+ } catch (error) {
576
+ }
577
+ }
578
+ try {
579
+ const privateKey = PrivateKey.fromStringECDSA(privateKeyString);
580
+ return {
581
+ detectedType: "ecdsa",
582
+ privateKey,
583
+ warning: `Using ECDSA as default. If you have the associated account ID, consider using the Hedera mirror node to confirm the key type.`
584
+ };
585
+ } catch (ecdsaError) {
586
+ try {
587
+ const privateKey = PrivateKey.fromStringED25519(privateKeyString);
588
+ return {
589
+ detectedType: "ed25519",
590
+ privateKey,
591
+ warning: `Using ED25519 as fallback. If you have the associated account ID, consider using the Hedera mirror node to confirm the key type.`
592
+ };
593
+ } catch (ed25519Error) {
594
+ throw new Error(
595
+ `Failed to parse private key as either ECDSA or ED25519: ${ecdsaError}`
596
+ );
579
597
  }
580
- return "Unknown Transaction";
581
598
  }
582
599
  }
583
600
  export {
584
- TransactionParser
601
+ KeyType,
602
+ KeyTypeDetector,
603
+ detectKeyTypeFromString
585
604
  };
586
605
  //# sourceMappingURL=standards-sdk.es39.js.map