@hashgraphonline/standards-sdk 0.1.137 → 0.1.138-feat-hcs-21-package-std.canary.4376298.35

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 (223) hide show
  1. package/dist/cjs/hcs-21/base-client.d.ts +33 -0
  2. package/dist/cjs/hcs-21/base-client.d.ts.map +1 -0
  3. package/dist/cjs/hcs-21/browser.d.ts +20 -0
  4. package/dist/cjs/hcs-21/browser.d.ts.map +1 -0
  5. package/dist/cjs/hcs-21/errors.d.ts +6 -0
  6. package/dist/cjs/hcs-21/errors.d.ts.map +1 -0
  7. package/dist/cjs/hcs-21/index.d.ts +7 -0
  8. package/dist/cjs/hcs-21/index.d.ts.map +1 -0
  9. package/dist/cjs/hcs-21/sdk.d.ts +48 -0
  10. package/dist/cjs/hcs-21/sdk.d.ts.map +1 -0
  11. package/dist/cjs/hcs-21/tx.d.ts +20 -0
  12. package/dist/cjs/hcs-21/tx.d.ts.map +1 -0
  13. package/dist/cjs/hcs-21/types.d.ts +89 -0
  14. package/dist/cjs/hcs-21/types.d.ts.map +1 -0
  15. package/dist/cjs/index.d.ts +1 -0
  16. package/dist/cjs/index.d.ts.map +1 -1
  17. package/dist/cjs/services/registry-broker/client.d.ts.map +1 -1
  18. package/dist/cjs/services/types.d.ts +1 -1
  19. package/dist/cjs/services/types.d.ts.map +1 -1
  20. package/dist/cjs/standards-sdk.cjs +2 -2
  21. package/dist/cjs/standards-sdk.cjs.map +1 -1
  22. package/dist/es/hcs-21/base-client.d.ts +33 -0
  23. package/dist/es/hcs-21/base-client.d.ts.map +1 -0
  24. package/dist/es/hcs-21/browser.d.ts +20 -0
  25. package/dist/es/hcs-21/browser.d.ts.map +1 -0
  26. package/dist/es/hcs-21/errors.d.ts +6 -0
  27. package/dist/es/hcs-21/errors.d.ts.map +1 -0
  28. package/dist/es/hcs-21/index.d.ts +7 -0
  29. package/dist/es/hcs-21/index.d.ts.map +1 -0
  30. package/dist/es/hcs-21/sdk.d.ts +48 -0
  31. package/dist/es/hcs-21/sdk.d.ts.map +1 -0
  32. package/dist/es/hcs-21/tx.d.ts +20 -0
  33. package/dist/es/hcs-21/tx.d.ts.map +1 -0
  34. package/dist/es/hcs-21/types.d.ts +89 -0
  35. package/dist/es/hcs-21/types.d.ts.map +1 -0
  36. package/dist/es/index.d.ts +1 -0
  37. package/dist/es/index.d.ts.map +1 -1
  38. package/dist/es/services/registry-broker/client.d.ts.map +1 -1
  39. package/dist/es/services/types.d.ts +1 -1
  40. package/dist/es/services/types.d.ts.map +1 -1
  41. package/dist/es/standards-sdk.es.js +67 -48
  42. package/dist/es/standards-sdk.es.js.map +1 -1
  43. package/dist/es/standards-sdk.es10.js +2 -2
  44. package/dist/es/standards-sdk.es100.js +231 -28
  45. package/dist/es/standards-sdk.es100.js.map +1 -1
  46. package/dist/es/standards-sdk.es101.js +109 -18
  47. package/dist/es/standards-sdk.es101.js.map +1 -1
  48. package/dist/es/standards-sdk.es102.js +32 -158
  49. package/dist/es/standards-sdk.es102.js.map +1 -1
  50. package/dist/es/standards-sdk.es103.js +80 -204
  51. package/dist/es/standards-sdk.es103.js.map +1 -1
  52. package/dist/es/standards-sdk.es104.js +21 -780
  53. package/dist/es/standards-sdk.es104.js.map +1 -1
  54. package/dist/es/standards-sdk.es105.js +140 -11
  55. package/dist/es/standards-sdk.es105.js.map +1 -1
  56. package/dist/es/standards-sdk.es106.js +29 -567
  57. package/dist/es/standards-sdk.es106.js.map +1 -1
  58. package/dist/es/standards-sdk.es107.js +17 -597
  59. package/dist/es/standards-sdk.es107.js.map +1 -1
  60. package/dist/es/standards-sdk.es108.js +159 -12
  61. package/dist/es/standards-sdk.es108.js.map +1 -1
  62. package/dist/es/standards-sdk.es109.js +208 -2
  63. package/dist/es/standards-sdk.es109.js.map +1 -1
  64. package/dist/es/standards-sdk.es110.js +774 -67
  65. package/dist/es/standards-sdk.es110.js.map +1 -1
  66. package/dist/es/standards-sdk.es111.js +11 -37
  67. package/dist/es/standards-sdk.es111.js.map +1 -1
  68. package/dist/es/standards-sdk.es112.js +567 -2
  69. package/dist/es/standards-sdk.es112.js.map +1 -1
  70. package/dist/es/standards-sdk.es113.js +576 -197
  71. package/dist/es/standards-sdk.es113.js.map +1 -1
  72. package/dist/es/standards-sdk.es114.js +12 -1139
  73. package/dist/es/standards-sdk.es114.js.map +1 -1
  74. package/dist/es/standards-sdk.es115.js +2 -306
  75. package/dist/es/standards-sdk.es115.js.map +1 -1
  76. package/dist/es/standards-sdk.es116.js +79 -418
  77. package/dist/es/standards-sdk.es116.js.map +1 -1
  78. package/dist/es/standards-sdk.es117.js +35 -351
  79. package/dist/es/standards-sdk.es117.js.map +1 -1
  80. package/dist/es/standards-sdk.es118.js +2 -761
  81. package/dist/es/standards-sdk.es118.js.map +1 -1
  82. package/dist/es/standards-sdk.es119.js +204 -185
  83. package/dist/es/standards-sdk.es119.js.map +1 -1
  84. package/dist/es/standards-sdk.es12.js +1 -1
  85. package/dist/es/standards-sdk.es120.js +1055 -1482
  86. package/dist/es/standards-sdk.es120.js.map +1 -1
  87. package/dist/es/standards-sdk.es121.js +265 -2024
  88. package/dist/es/standards-sdk.es121.js.map +1 -1
  89. package/dist/es/standards-sdk.es122.js +420 -50
  90. package/dist/es/standards-sdk.es122.js.map +1 -1
  91. package/dist/es/standards-sdk.es123.js +350 -82
  92. package/dist/es/standards-sdk.es123.js.map +1 -1
  93. package/dist/es/standards-sdk.es124.js +741 -139
  94. package/dist/es/standards-sdk.es124.js.map +1 -1
  95. package/dist/es/standards-sdk.es125.js +204 -7
  96. package/dist/es/standards-sdk.es125.js.map +1 -1
  97. package/dist/es/standards-sdk.es126.js +1533 -52
  98. package/dist/es/standards-sdk.es126.js.map +1 -1
  99. package/dist/es/standards-sdk.es127.js +2077 -59
  100. package/dist/es/standards-sdk.es127.js.map +1 -1
  101. package/dist/es/standards-sdk.es128.js +52 -28
  102. package/dist/es/standards-sdk.es128.js.map +1 -1
  103. package/dist/es/standards-sdk.es129.js +80 -131
  104. package/dist/es/standards-sdk.es129.js.map +1 -1
  105. package/dist/es/standards-sdk.es13.js +1 -1
  106. package/dist/es/standards-sdk.es130.js +152 -35
  107. package/dist/es/standards-sdk.es130.js.map +1 -1
  108. package/dist/es/standards-sdk.es131.js +7 -17
  109. package/dist/es/standards-sdk.es131.js.map +1 -1
  110. package/dist/es/standards-sdk.es132.js +53 -12241
  111. package/dist/es/standards-sdk.es132.js.map +1 -1
  112. package/dist/es/standards-sdk.es133.js +65 -694
  113. package/dist/es/standards-sdk.es133.js.map +1 -1
  114. package/dist/es/standards-sdk.es134.js +28 -172
  115. package/dist/es/standards-sdk.es134.js.map +1 -1
  116. package/dist/es/standards-sdk.es135.js +115 -299
  117. package/dist/es/standards-sdk.es135.js.map +1 -1
  118. package/dist/es/standards-sdk.es136.js +36 -340
  119. package/dist/es/standards-sdk.es136.js.map +1 -1
  120. package/dist/es/standards-sdk.es137.js +694 -453
  121. package/dist/es/standards-sdk.es137.js.map +1 -1
  122. package/dist/es/standards-sdk.es138.js +12210 -267
  123. package/dist/es/standards-sdk.es138.js.map +1 -1
  124. package/dist/es/standards-sdk.es139.js +15 -74
  125. package/dist/es/standards-sdk.es139.js.map +1 -1
  126. package/dist/es/standards-sdk.es14.js +1 -1
  127. package/dist/es/standards-sdk.es141.js +1 -1
  128. package/dist/es/standards-sdk.es142.js +175 -0
  129. package/dist/es/standards-sdk.es142.js.map +1 -0
  130. package/dist/es/standards-sdk.es143.js +325 -0
  131. package/dist/es/standards-sdk.es143.js.map +1 -0
  132. package/dist/es/standards-sdk.es144.js +349 -0
  133. package/dist/es/standards-sdk.es144.js.map +1 -0
  134. package/dist/es/standards-sdk.es145.js +456 -0
  135. package/dist/es/standards-sdk.es145.js.map +1 -0
  136. package/dist/es/standards-sdk.es146.js +334 -0
  137. package/dist/es/standards-sdk.es146.js.map +1 -0
  138. package/dist/es/standards-sdk.es147.js +79 -0
  139. package/dist/es/standards-sdk.es147.js.map +1 -0
  140. package/dist/es/standards-sdk.es15.js +1 -1
  141. package/dist/es/standards-sdk.es16.js +1 -1
  142. package/dist/es/standards-sdk.es17.js +5 -5
  143. package/dist/es/standards-sdk.es19.js +12 -12
  144. package/dist/es/standards-sdk.es20.js +9 -9
  145. package/dist/es/standards-sdk.es21.js +1 -1
  146. package/dist/es/standards-sdk.es22.js +1 -1
  147. package/dist/es/standards-sdk.es23.js +1 -1
  148. package/dist/es/standards-sdk.es24.js +1 -1
  149. package/dist/es/standards-sdk.es25.js +1 -1
  150. package/dist/es/standards-sdk.es26.js +1 -1
  151. package/dist/es/standards-sdk.es27.js +1 -1
  152. package/dist/es/standards-sdk.es28.js +12 -12
  153. package/dist/es/standards-sdk.es3.js +2 -2
  154. package/dist/es/standards-sdk.es31.js +2 -2
  155. package/dist/es/standards-sdk.es32.js +4 -4
  156. package/dist/es/standards-sdk.es33.js +1 -1
  157. package/dist/es/standards-sdk.es36.js +6 -6
  158. package/dist/es/standards-sdk.es37.js +4 -4
  159. package/dist/es/standards-sdk.es38.js +2 -2
  160. package/dist/es/standards-sdk.es39.js +2 -2
  161. package/dist/es/standards-sdk.es40.js +1 -1
  162. package/dist/es/standards-sdk.es41.js +1 -1
  163. package/dist/es/standards-sdk.es42.js +2 -2
  164. package/dist/es/standards-sdk.es47.js +1 -1
  165. package/dist/es/standards-sdk.es5.js +2 -2
  166. package/dist/es/standards-sdk.es52.js +1 -1
  167. package/dist/es/standards-sdk.es54.js +1 -1
  168. package/dist/es/standards-sdk.es57.js +1 -1
  169. package/dist/es/standards-sdk.es6.js +2 -2
  170. package/dist/es/standards-sdk.es61.js +7 -7
  171. package/dist/es/standards-sdk.es65.js +2 -2
  172. package/dist/es/standards-sdk.es66.js +3 -3
  173. package/dist/es/standards-sdk.es69.js +2 -2
  174. package/dist/es/standards-sdk.es7.js +2 -2
  175. package/dist/es/standards-sdk.es70.js +3 -3
  176. package/dist/es/standards-sdk.es71.js +2 -2
  177. package/dist/es/standards-sdk.es72.js +1 -1
  178. package/dist/es/standards-sdk.es75.js +2 -2
  179. package/dist/es/standards-sdk.es77.js +2 -2
  180. package/dist/es/standards-sdk.es78.js +4 -4
  181. package/dist/es/standards-sdk.es79.js +1 -1
  182. package/dist/es/standards-sdk.es8.js +1 -1
  183. package/dist/es/standards-sdk.es80.js +41 -27
  184. package/dist/es/standards-sdk.es80.js.map +1 -1
  185. package/dist/es/standards-sdk.es81.js +6 -78
  186. package/dist/es/standards-sdk.es81.js.map +1 -1
  187. package/dist/es/standards-sdk.es82.js +27 -64
  188. package/dist/es/standards-sdk.es82.js.map +1 -1
  189. package/dist/es/standards-sdk.es83.js +69 -23
  190. package/dist/es/standards-sdk.es83.js.map +1 -1
  191. package/dist/es/standards-sdk.es84.js +165 -23
  192. package/dist/es/standards-sdk.es84.js.map +1 -1
  193. package/dist/es/standards-sdk.es85.js +31 -166
  194. package/dist/es/standards-sdk.es85.js.map +1 -1
  195. package/dist/es/standards-sdk.es86.js +24 -127
  196. package/dist/es/standards-sdk.es86.js.map +1 -1
  197. package/dist/es/standards-sdk.es87.js +65 -142
  198. package/dist/es/standards-sdk.es87.js.map +1 -1
  199. package/dist/es/standards-sdk.es88.js +52 -127
  200. package/dist/es/standards-sdk.es88.js.map +1 -1
  201. package/dist/es/standards-sdk.es89.js +26 -41
  202. package/dist/es/standards-sdk.es89.js.map +1 -1
  203. package/dist/es/standards-sdk.es90.js +23 -261
  204. package/dist/es/standards-sdk.es90.js.map +1 -1
  205. package/dist/es/standards-sdk.es91.js +162 -93
  206. package/dist/es/standards-sdk.es91.js.map +1 -1
  207. package/dist/es/standards-sdk.es92.js +112 -83
  208. package/dist/es/standards-sdk.es92.js.map +1 -1
  209. package/dist/es/standards-sdk.es93.js +156 -28
  210. package/dist/es/standards-sdk.es93.js.map +1 -1
  211. package/dist/es/standards-sdk.es94.js +133 -225
  212. package/dist/es/standards-sdk.es94.js.map +1 -1
  213. package/dist/es/standards-sdk.es95.js +40 -108
  214. package/dist/es/standards-sdk.es95.js.map +1 -1
  215. package/dist/es/standards-sdk.es96.js +253 -24
  216. package/dist/es/standards-sdk.es96.js.map +1 -1
  217. package/dist/es/standards-sdk.es97.js +96 -80
  218. package/dist/es/standards-sdk.es97.js.map +1 -1
  219. package/dist/es/standards-sdk.es98.js +91 -21
  220. package/dist/es/standards-sdk.es98.js.map +1 -1
  221. package/dist/es/standards-sdk.es99.js +28 -141
  222. package/dist/es/standards-sdk.es99.js.map +1 -1
  223. package/package.json +62 -59
@@ -1,226 +1,605 @@
1
- import { detectCryptoEnvironment } from "./standards-sdk.es111.js";
2
- import { NodeHashAdapter, WebHashAdapter, FallbackHashAdapter } from "./standards-sdk.es110.js";
3
- class NodeHmacAdapter {
4
- constructor(nodeHmac) {
5
- this.nodeHmac = nodeHmac;
6
- }
7
- update(data) {
8
- this.nodeHmac.update(data);
9
- return this;
10
- }
11
- digest(encoding) {
12
- return this.nodeHmac.digest(encoding);
13
- }
14
- }
15
- class WebHmacAdapter {
16
- constructor(key, algorithm = "sha256") {
17
- this.key = key;
18
- this.algorithm = algorithm;
19
- this.data = [];
20
- }
21
- update(data) {
22
- this.data.push(data);
23
- return this;
24
- }
25
- async digest(encoding) {
26
- const combined = Buffer.concat(this.data);
27
- const keyBytes = new Uint8Array(this.key);
28
- const keyBuffer = await crypto.subtle.importKey(
29
- "raw",
30
- keyBytes,
31
- { name: "HMAC", hash: this.mapAlgorithm(this.algorithm) },
32
- false,
33
- ["sign"]
34
- );
35
- const signature = await crypto.subtle.sign(
36
- "HMAC",
37
- keyBuffer,
38
- new Uint8Array(combined)
39
- );
40
- if (encoding === "hex") {
41
- return Array.from(new Uint8Array(signature)).map((b) => b.toString(16).padStart(2, "0")).join("");
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 {
9
+ /**
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-----`);
32
+ */
33
+ static detect(keyInput) {
34
+ try {
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;
63
+ }
64
+ return this.detectFromBytes(keyBytes, originalFormat);
65
+ } catch (error) {
66
+ return {
67
+ type: "unknown",
68
+ format: "raw",
69
+ isPrivateKey: false,
70
+ confidence: "certain"
71
+ };
42
72
  }
43
- return Buffer.from(signature);
44
73
  }
45
- mapAlgorithm(algorithm) {
46
- const algorithmMap = {
47
- sha256: "SHA-256",
48
- sha1: "SHA-1",
49
- sha512: "SHA-512"
74
+ /**
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
81
+ */
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
+ };
92
+ }
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
+ }
104
+ }
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
+ };
178
+ }
179
+ return {
180
+ type: "unknown",
181
+ format,
182
+ isPrivateKey: false,
183
+ rawBytes: bytes,
184
+ confidence: "certain"
50
185
  };
51
- return algorithmMap[algorithm.toLowerCase()] || "SHA-256";
52
- }
53
- }
54
- class FallbackHmacAdapter {
55
- constructor(key, algorithm = "sha256") {
56
- this.key = key;
57
- this.algorithm = algorithm;
58
- this.data = [];
59
186
  }
60
- update(data) {
61
- this.data.push(data);
62
- return this;
63
- }
64
- digest(encoding) {
65
- const combined = Buffer.concat(this.data);
66
- const hash2 = this.simpleHmac(combined, this.key);
67
- if (encoding === "hex") {
68
- return hash2.toString(16).padStart(8, "0");
187
+ /**
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
193
+ */
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-----/
202
+ };
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
+ }
69
209
  }
70
- return hash2.toString();
71
- }
72
- simpleHmac(data, key) {
73
- let hash2 = 0;
74
- for (let i = 0; i < data.length; i++) {
75
- const keyByte = key[i % key.length];
76
- hash2 = (hash2 << 5) - hash2 + data[i] + keyByte & 4294967295;
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
+ };
77
220
  }
78
- return Math.abs(hash2);
79
- }
80
- }
81
- class NodeCryptoAdapter {
82
- constructor() {
221
+ const base64Content = base64Match[1].replace(/\s/g, "");
83
222
  try {
84
- const moduleName = "crypto";
85
- this.crypto = require(moduleName);
86
- } catch (error) {
87
- throw new Error("Node.js crypto module not available");
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
+ };
88
245
  }
89
246
  }
90
- createHash(algorithm) {
91
- return new NodeHashAdapter(this.crypto.createHash(algorithm));
92
- }
93
- createHmac(algorithm, key) {
94
- return new NodeHmacAdapter(this.crypto.createHmac(algorithm, key));
95
- }
96
- async pbkdf2(password, salt, iterations, keylen, digest) {
97
- return new Promise((resolve, reject) => {
98
- this.crypto.pbkdf2(
99
- password,
100
- salt,
101
- iterations,
102
- keylen,
103
- digest,
104
- (err, derivedKey) => {
105
- if (err) reject(err);
106
- else resolve(derivedKey);
247
+ /**
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
253
+ */
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);
107
260
  }
108
- );
109
- });
110
- }
111
- timingSafeEqual(a, b) {
112
- return this.crypto.timingSafeEqual(a, b);
113
- }
114
- }
115
- class WebCryptoAdapter {
116
- createHash(algorithm) {
117
- return new WebHashAdapter(algorithm);
118
- }
119
- createHmac(algorithm, key) {
120
- return new WebHmacAdapter(key, algorithm);
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
+ };
273
+ }
274
+ return {
275
+ type: "unknown",
276
+ format: "hex",
277
+ isPrivateKey: false,
278
+ rawBytes: keyBytes,
279
+ confidence: "certain"
280
+ };
281
+ }
282
+ }
283
+ } catch {
284
+ }
285
+ return {
286
+ type: "unknown",
287
+ format: "hex",
288
+ isPrivateKey: false,
289
+ rawBytes: new Uint8Array(),
290
+ confidence: "certain"
291
+ };
121
292
  }
122
- async pbkdf2(password, salt, iterations, keylen, digest) {
123
- const encoder = new TextEncoder();
124
- const keyMaterial = await crypto.subtle.importKey(
125
- "raw",
126
- encoder.encode(password),
127
- { name: "PBKDF2" },
128
- false,
129
- ["deriveBits"]
130
- );
131
- const derivedBits = await crypto.subtle.deriveBits(
132
- {
133
- name: "PBKDF2",
134
- salt: new Uint8Array(salt),
135
- iterations,
136
- hash: this.mapDigest(digest)
137
- },
138
- keyMaterial,
139
- keylen * 8
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"
140
309
  );
141
- return Buffer.from(derivedBits);
142
- }
143
- timingSafeEqual(a, b) {
144
- if (a.length !== b.length) return false;
145
- let result = 0;
146
- for (let i = 0; i < a.length; i++) {
147
- result |= a[i] ^ b[i];
310
+ if (keyBigInt === 0n) {
311
+ return {
312
+ type: "ed25519",
313
+ confidence: "certain"
314
+ };
148
315
  }
149
- return result === 0;
150
- }
151
- mapDigest(digest) {
152
- const digestMap = {
153
- sha256: "SHA-256",
154
- sha1: "SHA-1",
155
- sha512: "SHA-512"
316
+ if (keyBigInt >= secp256k1Order) {
317
+ return {
318
+ type: "ed25519",
319
+ confidence: "certain"
320
+ };
321
+ }
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;
330
+ }
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;
339
+ }
340
+ } catch {
341
+ }
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
+ };
368
+ }
369
+ if (ed25519CanSign && !ecdsaCanSign) {
370
+ return {
371
+ type: "ed25519",
372
+ confidence: "certain"
373
+ };
374
+ }
375
+ if (ecdsaCanSign && !ed25519CanSign) {
376
+ return {
377
+ type: "ecdsa",
378
+ confidence: "certain"
379
+ };
380
+ }
381
+ return {
382
+ type: "unknown",
383
+ confidence: "certain"
156
384
  };
157
- return digestMap[digest.toLowerCase()] || "SHA-256";
158
385
  }
159
- }
160
- class FallbackCryptoAdapter {
161
- createHash(algorithm) {
162
- return new FallbackHashAdapter(algorithm);
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;
408
+ }
163
409
  }
164
- createHmac(algorithm, key) {
165
- return new FallbackHmacAdapter(key, algorithm);
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);
421
+ }
422
+ return bytes;
166
423
  }
167
- async pbkdf2(password, salt, iterations, keylen, digest) {
168
- const encoder = new TextEncoder();
169
- const passwordBuffer = Buffer.from(encoder.encode(password));
170
- let result = Buffer.alloc(keylen);
171
- for (let i = 0; i < iterations; i++) {
172
- const combined = Buffer.concat([passwordBuffer, salt, Buffer.from([i])]);
173
- let hash2 = 0;
174
- for (let j = 0; j < combined.length; j++) {
175
- hash2 = (hash2 << 5) - hash2 + combined[j] & 4294967295;
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;
436
+ }
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;
176
450
  }
177
- result[i % keylen] ^= hash2 & 255;
178
451
  }
179
- return result;
452
+ return false;
180
453
  }
181
- timingSafeEqual(a, b) {
182
- if (a.length !== b.length) return false;
183
- let result = 0;
184
- for (let i = 0; i < a.length; i++) {
185
- result |= a[i] ^ b[i];
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);
465
+ }
186
466
  }
187
- return result === 0;
467
+ return bytes;
188
468
  }
189
- }
190
- function getCryptoAdapter() {
191
- const env = detectCryptoEnvironment();
192
- switch (env.preferredAPI) {
193
- case "node":
194
- try {
195
- return new NodeCryptoAdapter();
196
- } catch {
197
- return new FallbackCryptoAdapter();
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.`;
198
573
  }
199
- case "web":
200
- return new WebCryptoAdapter();
201
- case "none":
202
- default:
203
- return new FallbackCryptoAdapter();
574
+ return result;
575
+ } catch (error) {
576
+ }
204
577
  }
205
- }
206
- async function hash(content, algorithm = "sha256") {
207
- const adapter = getCryptoAdapter();
208
- const hasher = adapter.createHash(algorithm);
209
- const buffer = typeof content === "string" ? Buffer.from(content) : content;
210
- const result = hasher.update(buffer).digest("hex");
211
- if (result instanceof Promise) {
212
- return await result;
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
+ );
597
+ }
213
598
  }
214
- return result;
215
599
  }
216
600
  export {
217
- FallbackCryptoAdapter,
218
- FallbackHmacAdapter,
219
- NodeCryptoAdapter,
220
- NodeHmacAdapter,
221
- WebCryptoAdapter,
222
- WebHmacAdapter,
223
- getCryptoAdapter,
224
- hash
601
+ KeyType,
602
+ KeyTypeDetector,
603
+ detectKeyTypeFromString
225
604
  };
226
605
  //# sourceMappingURL=standards-sdk.es113.js.map