@icp-sdk/core 5.3.1 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/lib/cjs/agent/actor.d.ts +7 -3
  2. package/lib/cjs/agent/actor.d.ts.map +1 -1
  3. package/lib/cjs/agent/actor.js +18 -5
  4. package/lib/cjs/agent/actor.js.map +1 -1
  5. package/lib/cjs/agent/agent/api.d.ts +35 -6
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/api.js.map +1 -1
  8. package/lib/cjs/agent/agent/http/index.d.ts +11 -25
  9. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  10. package/lib/cjs/agent/agent/http/index.js +198 -173
  11. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  12. package/lib/cjs/agent/auth.d.ts +6 -5
  13. package/lib/cjs/agent/auth.d.ts.map +1 -1
  14. package/lib/cjs/agent/auth.js +3 -3
  15. package/lib/cjs/agent/auth.js.map +1 -1
  16. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  17. package/lib/cjs/agent/canisterStatus/index.js +10 -10
  18. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  19. package/lib/cjs/agent/cbor.d.ts +2 -1
  20. package/lib/cjs/agent/cbor.d.ts.map +1 -1
  21. package/lib/cjs/agent/cbor.js +4 -1
  22. package/lib/cjs/agent/cbor.js.map +1 -1
  23. package/lib/cjs/agent/certificate.d.ts +5 -4
  24. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  25. package/lib/cjs/agent/certificate.js +14 -18
  26. package/lib/cjs/agent/certificate.js.map +1 -1
  27. package/lib/cjs/agent/der.d.ts +3 -2
  28. package/lib/cjs/agent/der.d.ts.map +1 -1
  29. package/lib/cjs/agent/der.js.map +1 -1
  30. package/lib/cjs/agent/errors.d.ts +7 -1
  31. package/lib/cjs/agent/errors.d.ts.map +1 -1
  32. package/lib/cjs/agent/errors.js +34 -16
  33. package/lib/cjs/agent/errors.js.map +1 -1
  34. package/lib/cjs/agent/polling/index.d.ts +3 -4
  35. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  36. package/lib/cjs/agent/polling/index.js +17 -10
  37. package/lib/cjs/agent/polling/index.js.map +1 -1
  38. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  39. package/lib/cjs/agent/polling/strategy.js +6 -6
  40. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  41. package/lib/cjs/agent/polling/types.d.ts +3 -4
  42. package/lib/cjs/agent/polling/types.d.ts.map +1 -1
  43. package/lib/cjs/agent/public_key.d.ts +3 -2
  44. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  45. package/lib/cjs/agent/public_key.js +2 -2
  46. package/lib/cjs/agent/public_key.js.map +1 -1
  47. package/lib/cjs/agent/request_id.js +10 -10
  48. package/lib/cjs/agent/request_id.js.map +1 -1
  49. package/lib/cjs/agent/subnetStatus/index.js +7 -7
  50. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  51. package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
  52. package/lib/cjs/agent/utils/bls.js +7 -6
  53. package/lib/cjs/agent/utils/bls.js.map +1 -1
  54. package/lib/cjs/agent/utils/readState.d.ts +2 -2
  55. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  56. package/lib/cjs/agent/utils/readState.js +4 -4
  57. package/lib/cjs/agent/utils/readState.js.map +1 -1
  58. package/lib/cjs/agent/utils/target.d.ts +15 -0
  59. package/lib/cjs/agent/utils/target.d.ts.map +1 -0
  60. package/lib/cjs/agent/utils/target.js +27 -0
  61. package/lib/cjs/agent/utils/target.js.map +1 -0
  62. package/lib/cjs/candid/utils/buffer.d.ts +2 -1
  63. package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
  64. package/lib/cjs/candid/utils/buffer.js +5 -2
  65. package/lib/cjs/candid/utils/buffer.js.map +1 -1
  66. package/lib/cjs/identity/identity/attributes.d.ts +6 -2
  67. package/lib/cjs/identity/identity/attributes.d.ts.map +1 -1
  68. package/lib/cjs/identity/identity/attributes.js +9 -1
  69. package/lib/cjs/identity/identity/attributes.js.map +1 -1
  70. package/lib/cjs/identity/identity/delegation.d.ts +10 -3
  71. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  72. package/lib/cjs/identity/identity/delegation.js +25 -12
  73. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  74. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  75. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  76. package/lib/cjs/identity/identity/ecdsa.js +3 -2
  77. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  78. package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
  79. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  80. package/lib/cjs/identity/identity/ed25519.js +18 -15
  81. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  82. package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
  83. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
  84. package/lib/cjs/identity/identity/webauthn.js +12 -11
  85. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  86. package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
  87. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  88. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  89. package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
  90. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
  91. package/lib/cjs/identity/secp256k1/pem.js +2 -2
  92. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  93. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
  94. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  95. package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
  96. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  97. package/lib/cjs/principal/principal.js +5 -5
  98. package/lib/cjs/principal/principal.js.map +1 -1
  99. package/lib/esm/agent/actor.d.ts +7 -3
  100. package/lib/esm/agent/actor.d.ts.map +1 -1
  101. package/lib/esm/agent/actor.js +18 -5
  102. package/lib/esm/agent/actor.js.map +1 -1
  103. package/lib/esm/agent/agent/api.d.ts +35 -6
  104. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  105. package/lib/esm/agent/agent/api.js.map +1 -1
  106. package/lib/esm/agent/agent/http/index.d.ts +11 -25
  107. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  108. package/lib/esm/agent/agent/http/index.js +191 -166
  109. package/lib/esm/agent/agent/http/index.js.map +1 -1
  110. package/lib/esm/agent/auth.d.ts +6 -5
  111. package/lib/esm/agent/auth.d.ts.map +1 -1
  112. package/lib/esm/agent/auth.js +1 -1
  113. package/lib/esm/agent/auth.js.map +1 -1
  114. package/lib/esm/agent/canister-env/index.js +1 -1
  115. package/lib/esm/agent/canister-env/index.js.map +1 -1
  116. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  117. package/lib/esm/agent/canisterStatus/index.js +2 -2
  118. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  119. package/lib/esm/agent/cbor.d.ts +2 -1
  120. package/lib/esm/agent/cbor.d.ts.map +1 -1
  121. package/lib/esm/agent/cbor.js +4 -1
  122. package/lib/esm/agent/cbor.js.map +1 -1
  123. package/lib/esm/agent/certificate.d.ts +5 -4
  124. package/lib/esm/agent/certificate.d.ts.map +1 -1
  125. package/lib/esm/agent/certificate.js +5 -9
  126. package/lib/esm/agent/certificate.js.map +1 -1
  127. package/lib/esm/agent/der.d.ts +3 -2
  128. package/lib/esm/agent/der.d.ts.map +1 -1
  129. package/lib/esm/agent/der.js.map +1 -1
  130. package/lib/esm/agent/errors.d.ts +7 -1
  131. package/lib/esm/agent/errors.d.ts.map +1 -1
  132. package/lib/esm/agent/errors.js +18 -1
  133. package/lib/esm/agent/errors.js.map +1 -1
  134. package/lib/esm/agent/polling/index.d.ts +3 -4
  135. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  136. package/lib/esm/agent/polling/index.js +15 -8
  137. package/lib/esm/agent/polling/index.js.map +1 -1
  138. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  139. package/lib/esm/agent/polling/strategy.js +6 -6
  140. package/lib/esm/agent/polling/strategy.js.map +1 -1
  141. package/lib/esm/agent/polling/types.d.ts +3 -4
  142. package/lib/esm/agent/polling/types.d.ts.map +1 -1
  143. package/lib/esm/agent/public_key.d.ts +3 -2
  144. package/lib/esm/agent/public_key.d.ts.map +1 -1
  145. package/lib/esm/agent/public_key.js +2 -2
  146. package/lib/esm/agent/public_key.js.map +1 -1
  147. package/lib/esm/agent/request_id.js +2 -2
  148. package/lib/esm/agent/request_id.js.map +1 -1
  149. package/lib/esm/agent/subnetStatus/index.js +1 -1
  150. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  151. package/lib/esm/agent/utils/bls.d.ts.map +1 -1
  152. package/lib/esm/agent/utils/bls.js +7 -6
  153. package/lib/esm/agent/utils/bls.js.map +1 -1
  154. package/lib/esm/agent/utils/readState.d.ts +2 -2
  155. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  156. package/lib/esm/agent/utils/readState.js +1 -1
  157. package/lib/esm/agent/utils/readState.js.map +1 -1
  158. package/lib/esm/agent/utils/target.d.ts +15 -0
  159. package/lib/esm/agent/utils/target.d.ts.map +1 -0
  160. package/lib/esm/agent/utils/target.js +23 -0
  161. package/lib/esm/agent/utils/target.js.map +1 -0
  162. package/lib/esm/candid/utils/buffer.d.ts +2 -1
  163. package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
  164. package/lib/esm/candid/utils/buffer.js +5 -2
  165. package/lib/esm/candid/utils/buffer.js.map +1 -1
  166. package/lib/esm/identity/identity/attributes.d.ts +6 -2
  167. package/lib/esm/identity/identity/attributes.d.ts.map +1 -1
  168. package/lib/esm/identity/identity/attributes.js +9 -1
  169. package/lib/esm/identity/identity/attributes.js.map +1 -1
  170. package/lib/esm/identity/identity/delegation.d.ts +10 -3
  171. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  172. package/lib/esm/identity/identity/delegation.js +22 -9
  173. package/lib/esm/identity/identity/delegation.js.map +1 -1
  174. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  175. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  176. package/lib/esm/identity/identity/ecdsa.js +3 -2
  177. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  178. package/lib/esm/identity/identity/ed25519.d.ts +3 -2
  179. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  180. package/lib/esm/identity/identity/ed25519.js +10 -7
  181. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  182. package/lib/esm/identity/identity/webauthn.d.ts +4 -3
  183. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
  184. package/lib/esm/identity/identity/webauthn.js +8 -7
  185. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  186. package/lib/esm/identity/secp256k1/der.d.ts +2 -1
  187. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  188. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  189. package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
  190. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
  191. package/lib/esm/identity/secp256k1/pem.js +2 -2
  192. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  193. package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
  194. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  195. package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
  196. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  197. package/lib/esm/principal/principal.js +2 -2
  198. package/lib/esm/principal/principal.js.map +1 -1
  199. package/package.json +20 -10
  200. package/src/agent/actor.ts +0 -524
  201. package/src/agent/agent/api.ts +0 -292
  202. package/src/agent/agent/http/expiry.ts +0 -107
  203. package/src/agent/agent/http/index.ts +0 -1727
  204. package/src/agent/agent/http/transforms.ts +0 -54
  205. package/src/agent/agent/http/types.ts +0 -145
  206. package/src/agent/agent/index.ts +0 -4
  207. package/src/agent/auth.ts +0 -147
  208. package/src/agent/canister-env/index.ts +0 -192
  209. package/src/agent/canisterStatus/index.ts +0 -260
  210. package/src/agent/cbor.ts +0 -63
  211. package/src/agent/certificate.ts +0 -1058
  212. package/src/agent/constants.ts +0 -24
  213. package/src/agent/der.ts +0 -191
  214. package/src/agent/errors.ts +0 -962
  215. package/src/agent/fetch_candid.ts +0 -37
  216. package/src/agent/index.ts +0 -130
  217. package/src/agent/observable.ts +0 -49
  218. package/src/agent/polling/backoff.ts +0 -129
  219. package/src/agent/polling/index.ts +0 -238
  220. package/src/agent/polling/strategy.ts +0 -140
  221. package/src/agent/polling/types.ts +0 -28
  222. package/src/agent/public_key.ts +0 -65
  223. package/src/agent/request_id.ts +0 -99
  224. package/src/agent/subnetStatus/index.ts +0 -263
  225. package/src/agent/utils/bls.ts +0 -18
  226. package/src/agent/utils/buffer.ts +0 -64
  227. package/src/agent/utils/certificateReject.ts +0 -25
  228. package/src/agent/utils/createExpirableStore.ts +0 -31
  229. package/src/agent/utils/expirableStore.ts +0 -47
  230. package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
  231. package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
  232. package/src/agent/utils/leb.ts +0 -14
  233. package/src/agent/utils/random.ts +0 -30
  234. package/src/agent/utils/readState.ts +0 -180
  235. package/src/candid/candid-core.ts +0 -274
  236. package/src/candid/candid-ui.ts +0 -281
  237. package/src/candid/idl.ts +0 -2654
  238. package/src/candid/index.ts +0 -35
  239. package/src/candid/types.ts +0 -7
  240. package/src/candid/utils/bigint-math.ts +0 -26
  241. package/src/candid/utils/buffer.ts +0 -221
  242. package/src/candid/utils/hash.ts +0 -31
  243. package/src/candid/utils/leb128.ts +0 -233
  244. package/src/identity/identity/attributes.ts +0 -84
  245. package/src/identity/identity/delegation.ts +0 -411
  246. package/src/identity/identity/ecdsa.ts +0 -162
  247. package/src/identity/identity/ed25519.ts +0 -246
  248. package/src/identity/identity/partial.ts +0 -60
  249. package/src/identity/identity/webauthn.ts +0 -265
  250. package/src/identity/index.ts +0 -44
  251. package/src/identity/secp256k1/der.ts +0 -177
  252. package/src/identity/secp256k1/index.ts +0 -52
  253. package/src/identity/secp256k1/pem.ts +0 -39
  254. package/src/identity/secp256k1/secp256k1.ts +0 -276
  255. package/src/index.ts +0 -3
  256. package/src/principal/index.ts +0 -19
  257. package/src/principal/principal.ts +0 -181
  258. package/src/principal/utils/base32.ts +0 -101
  259. package/src/principal/utils/getCrc.ts +0 -52
@@ -1,140 +0,0 @@
1
- import type { Principal } from '#principal';
2
- import type { RequestStatusResponseStatus } from '../agent/http/types.ts';
3
- import type { RequestId } from '../request_id.ts';
4
- import { ProtocolError, TimeoutWaitingForResponseErrorCode } from '../errors.ts';
5
-
6
- import type { PollStrategy, Predicate } from './types.ts';
7
- export type { PollStrategy, Predicate } from './types.ts';
8
-
9
- const FIVE_MINUTES_IN_MSEC = 5 * 60 * 1000;
10
-
11
- /**
12
- * A best practices polling strategy: wait 2 seconds before the first poll, then 1 second
13
- * with an exponential backoff factor of 1.2. Timeout after 5 minutes.
14
- *
15
- * Note that calling this function will create the strategy chain described above and already start the 5 minutes timeout.
16
- * You should only call this function when you want to start the polling, and not before, to avoid exhausting the 5 minutes timeout in advance.
17
- */
18
- export function defaultStrategy(): PollStrategy {
19
- return chain(conditionalDelay(once(), 1000), backoff(1000, 1.2), timeout(FIVE_MINUTES_IN_MSEC));
20
- }
21
-
22
- /**
23
- * Predicate that returns true once.
24
- */
25
- export function once(): Predicate<boolean> {
26
- let first = true;
27
- return async () => {
28
- if (first) {
29
- first = false;
30
- return true;
31
- }
32
- return false;
33
- };
34
- }
35
-
36
- /**
37
- * Delay the polling once.
38
- * @param condition A predicate that indicates when to delay.
39
- * @param timeInMsec The amount of time to delay.
40
- */
41
- export function conditionalDelay(condition: Predicate<boolean>, timeInMsec: number): PollStrategy {
42
- return async (
43
- canisterId: Principal,
44
- requestId: RequestId,
45
- status: RequestStatusResponseStatus,
46
- ) => {
47
- if (await condition(canisterId, requestId, status)) {
48
- return new Promise(resolve => setTimeout(resolve, timeInMsec));
49
- }
50
- };
51
- }
52
-
53
- /**
54
- * Error out after a maximum number of polling has been done.
55
- * @param count The maximum attempts to poll.
56
- */
57
- export function maxAttempts(count: number): PollStrategy {
58
- let attempts = count;
59
- return async (
60
- _canisterId: Principal,
61
- requestId: RequestId,
62
- status: RequestStatusResponseStatus,
63
- ) => {
64
- if (--attempts <= 0) {
65
- throw ProtocolError.fromCode(
66
- new TimeoutWaitingForResponseErrorCode(
67
- `Failed to retrieve a reply for request after ${count} attempts`,
68
- requestId,
69
- status,
70
- ),
71
- );
72
- }
73
- };
74
- }
75
-
76
- /**
77
- * Throttle polling.
78
- * @param throttleInMsec Amount in millisecond to wait between each polling.
79
- */
80
- export function throttle(throttleInMsec: number): PollStrategy {
81
- return () => new Promise(resolve => setTimeout(resolve, throttleInMsec));
82
- }
83
-
84
- /**
85
- * Reject a call after a certain amount of time.
86
- * @param timeInMsec Time in milliseconds before the polling should be rejected.
87
- */
88
- export function timeout(timeInMsec: number): PollStrategy {
89
- const end = Date.now() + timeInMsec;
90
- return async (
91
- _canisterId: Principal,
92
- requestId: RequestId,
93
- status: RequestStatusResponseStatus,
94
- ) => {
95
- if (Date.now() > end) {
96
- throw ProtocolError.fromCode(
97
- new TimeoutWaitingForResponseErrorCode(
98
- `Request timed out after ${timeInMsec} msec`,
99
- requestId,
100
- status,
101
- ),
102
- );
103
- }
104
- };
105
- }
106
-
107
- /**
108
- * A strategy that throttle, but using an exponential backoff strategy.
109
- * @param startingThrottleInMsec The throttle in milliseconds to start with.
110
- * @param backoffFactor The factor to multiple the throttle time between every poll. For
111
- * example if using 2, the throttle will double between every run.
112
- */
113
- export function backoff(startingThrottleInMsec: number, backoffFactor: number): PollStrategy {
114
- let currentThrottling = startingThrottleInMsec;
115
-
116
- return () =>
117
- new Promise(resolve =>
118
- setTimeout(() => {
119
- currentThrottling *= backoffFactor;
120
- resolve();
121
- }, currentThrottling),
122
- );
123
- }
124
-
125
- /**
126
- * Chain multiple polling strategy. This _chains_ the strategies, so if you pass in,
127
- * say, two throttling strategy of 1 second, it will result in a throttle of 2 seconds.
128
- * @param strategies A strategy list to chain.
129
- */
130
- export function chain(...strategies: PollStrategy[]): PollStrategy {
131
- return async (
132
- canisterId: Principal,
133
- requestId: RequestId,
134
- status: RequestStatusResponseStatus,
135
- ) => {
136
- for (const a of strategies) {
137
- await a(canisterId, requestId, status);
138
- }
139
- };
140
- }
@@ -1,28 +0,0 @@
1
- import type { Certificate } from '../certificate.ts';
2
- import type { Principal } from '#principal';
3
- import type { RequestStatusResponseStatus } from '../agent/http/types.ts';
4
- import type { RequestId } from '../request_id.ts';
5
-
6
- export type PollStrategy = (
7
- canisterId: Principal,
8
- requestId: RequestId,
9
- status: RequestStatusResponseStatus,
10
- ) => Promise<void>;
11
-
12
- export type Predicate<T> = (
13
- canisterId: Principal,
14
- requestId: RequestId,
15
- status: RequestStatusResponseStatus,
16
- ) => Promise<T>;
17
-
18
- /**
19
- * The result of polling for a response, including the certificate, reply bytes, and raw certificate bytes.
20
- */
21
- export interface PollForResponseResult {
22
- /** The certificate for the request, which can be used to verify the reply. */
23
- certificate: Certificate;
24
- /** The reply bytes for the request. */
25
- reply: Uint8Array;
26
- /** The raw certificate bytes for the request. */
27
- rawCertificate: Uint8Array;
28
- }
@@ -1,65 +0,0 @@
1
- import type { DerEncodedPublicKey, PublicKey } from './auth.ts';
2
- import { ED25519_OID, unwrapDER, wrapDER } from './der.ts';
3
- import { DerDecodeErrorCode, InputError } from './errors.ts';
4
-
5
- export class Ed25519PublicKey implements PublicKey {
6
- public static from(key: PublicKey): Ed25519PublicKey {
7
- return this.fromDer(key.toDer());
8
- }
9
-
10
- public static fromRaw(rawKey: Uint8Array): Ed25519PublicKey {
11
- return new Ed25519PublicKey(rawKey);
12
- }
13
-
14
- public static fromDer(derKey: DerEncodedPublicKey): Ed25519PublicKey {
15
- return new Ed25519PublicKey(this.derDecode(derKey));
16
- }
17
-
18
- // The length of Ed25519 public keys is always 32 bytes.
19
- private static RAW_KEY_LENGTH = 32;
20
-
21
- private static derEncode(publicKey: Uint8Array): DerEncodedPublicKey {
22
- return wrapDER(publicKey, ED25519_OID) as DerEncodedPublicKey;
23
- }
24
-
25
- private static derDecode(key: DerEncodedPublicKey): Uint8Array {
26
- const unwrapped = unwrapDER(key, ED25519_OID);
27
- if (unwrapped.length !== this.RAW_KEY_LENGTH) {
28
- throw InputError.fromCode(
29
- new DerDecodeErrorCode('An Ed25519 public key must be exactly 32 bytes long'),
30
- );
31
- }
32
- return unwrapped;
33
- }
34
-
35
- #rawKey: Uint8Array;
36
-
37
- public get rawKey(): Uint8Array {
38
- return this.#rawKey;
39
- }
40
-
41
- #derKey: DerEncodedPublicKey;
42
-
43
- public get derKey(): DerEncodedPublicKey {
44
- return this.#derKey;
45
- }
46
-
47
- // `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
48
- private constructor(key: Uint8Array) {
49
- if (key.byteLength !== Ed25519PublicKey.RAW_KEY_LENGTH) {
50
- throw InputError.fromCode(
51
- new DerDecodeErrorCode('An Ed25519 public key must be exactly 32 bytes long'),
52
- );
53
- }
54
- this.#rawKey = key;
55
- this.#derKey = Ed25519PublicKey.derEncode(key);
56
- }
57
-
58
- public toDer(): DerEncodedPublicKey {
59
- return this.derKey;
60
- }
61
-
62
- public toRaw(): Uint8Array {
63
- return this.rawKey;
64
- }
65
- }
@@ -1,99 +0,0 @@
1
- import { lebEncode, compare } from '#candid';
2
- import type { Principal } from '#principal';
3
- import { HashValueErrorCode, InputError } from './errors.ts';
4
- import { uint8FromBufLike } from './utils/buffer.ts';
5
- import { concatBytes } from '@noble/hashes/utils';
6
- import { sha256 } from '@noble/hashes/sha2';
7
-
8
- export type RequestId = Uint8Array & { __requestId__: void };
9
-
10
- interface ToHashable {
11
- toHash(): unknown;
12
- }
13
-
14
- /**
15
- *
16
- * @param value unknown value
17
- * @returns Uint8Array
18
- */
19
- export function hashValue(value: unknown): Uint8Array {
20
- if (typeof value === 'string') {
21
- return hashString(value);
22
- }
23
- if (typeof value === 'number') {
24
- return sha256(lebEncode(value));
25
- }
26
- if (value instanceof Uint8Array || ArrayBuffer.isView(value)) {
27
- return sha256(uint8FromBufLike(value));
28
- }
29
- if (Array.isArray(value)) {
30
- const vals = value.map(hashValue);
31
- return sha256(concatBytes(...vals));
32
- }
33
- if (value && typeof value === 'object' && (value as Principal)._isPrincipal) {
34
- return sha256((value as Principal).toUint8Array());
35
- }
36
- if (
37
- typeof value === 'object' &&
38
- value !== null &&
39
- typeof (value as ToHashable).toHash === 'function'
40
- ) {
41
- return hashValue((value as ToHashable).toHash());
42
- // TODO This should be move to a specific async method as the webauthn flow required
43
- // the flow to be synchronous to ensure Safari touch id works.
44
- // } else if (value instanceof Promise) {
45
- // return value.then(x => hashValue(x));
46
- }
47
- if (typeof value === 'object') {
48
- return hashOfMap(value as Record<string, unknown>);
49
- }
50
- if (typeof value === 'bigint') {
51
- // Do this check much later than the other bigint check because this one is much less
52
- // type-safe.
53
- // So we want to try all the high-assurance type guards before this 'probable' one.
54
- return sha256(lebEncode(value));
55
- }
56
- throw InputError.fromCode(new HashValueErrorCode(value));
57
- }
58
-
59
- const hashString = (value: string): Uint8Array => {
60
- const encoded = new TextEncoder().encode(value);
61
- return sha256(encoded);
62
- };
63
-
64
- /**
65
- * Get the RequestId of the provided ic-ref request.
66
- * RequestId is the result of the representation-independent-hash function.
67
- * https://sdk.dfinity.org/docs/interface-spec/index.html#hash-of-map
68
- * @param request - ic-ref request to hash into RequestId
69
- */
70
- export function requestIdOf(request: Record<string, unknown>): RequestId {
71
- return hashOfMap(request) as RequestId;
72
- }
73
-
74
- /**
75
- * Hash a map into a Uint8Array using the representation-independent-hash function.
76
- * https://sdk.dfinity.org/docs/interface-spec/index.html#hash-of-map
77
- * @param map - Any non-nested object
78
- * @returns Uint8Array
79
- */
80
- export function hashOfMap(map: Record<string, unknown>): Uint8Array {
81
- const hashed: Array<[Uint8Array, Uint8Array]> = Object.entries(map)
82
- .filter(([, value]) => value !== undefined)
83
- .map(([key, value]: [string, unknown]) => {
84
- const hashedKey = hashString(key);
85
- const hashedValue = hashValue(value);
86
-
87
- return [hashedKey, hashedValue] as [Uint8Array, Uint8Array];
88
- });
89
-
90
- const traversed: Array<[Uint8Array, Uint8Array]> = hashed;
91
-
92
- const sorted: Array<[Uint8Array, Uint8Array]> = traversed.sort(([k1], [k2]) => {
93
- return compare(k1, k2);
94
- });
95
-
96
- const concatenated = concatBytes(...sorted.map(x => concatBytes(...x)));
97
- const result = sha256(concatenated);
98
- return result;
99
- }
@@ -1,263 +0,0 @@
1
- import { Principal } from '#principal';
2
- import {
3
- CertificateVerificationErrorCode,
4
- MissingRootKeyErrorCode,
5
- ExternalError,
6
- AgentError,
7
- UnknownError,
8
- UnexpectedErrorCode,
9
- CertificateTimeErrorCode,
10
- ProtocolError,
11
- LookupErrorCode,
12
- } from '../errors.ts';
13
- import type { HttpAgent } from '../agent/http/index.ts';
14
- import {
15
- type Cert,
16
- type CanisterRanges,
17
- Certificate,
18
- lookupResultToBuffer,
19
- decodeCanisterRanges,
20
- lookup_path,
21
- LookupPathStatus,
22
- } from '../certificate.ts';
23
- import * as cbor from '../cbor.ts';
24
- import { decodeTime } from '../utils/leb.ts';
25
- import { utf8ToBytes } from '@noble/hashes/utils';
26
- import type { CustomPath } from '../utils/readState.ts';
27
- import {
28
- type BaseStatus,
29
- type BaseSubnetStatus,
30
- type SubnetNodeKeys,
31
- decodeValue,
32
- isCustomPath,
33
- lookupNodeKeysFromCertificate,
34
- IC_ROOT_SUBNET_ID,
35
- } from '../utils/readState.ts';
36
-
37
- // Re-export shared types and functions
38
- export { type DecodeStrategy, CustomPath, IC_ROOT_SUBNET_ID } from '../utils/readState.ts';
39
-
40
- export type SubnetStatus = BaseSubnetStatus & {
41
- /**
42
- * The public key of the subnet
43
- */
44
- publicKey: Uint8Array;
45
- };
46
-
47
- export type Status = BaseStatus | SubnetNodeKeys | CanisterRanges;
48
-
49
- /**
50
- * Pre-configured fields for subnet status paths
51
- */
52
- export type Path = 'time' | 'canisterRanges' | 'publicKey' | 'nodeKeys' | CustomPath;
53
-
54
- export type StatusMap = Map<Path | string, Status>;
55
-
56
- export interface SubnetStatusOptions {
57
- /**
58
- * The subnet ID to query. Use {@link IC_ROOT_SUBNET_ID} for the IC mainnet root subnet.
59
- * You can use {@link HttpAgent.getSubnetIdFromCanister} to get a subnet ID from a canister.
60
- */
61
- subnetId: Principal;
62
- /**
63
- * The agent to use to make the subnet request.
64
- */
65
- agent: HttpAgent;
66
- /**
67
- * The paths to request.
68
- * @default []
69
- */
70
- paths?: Path[] | Set<Path>;
71
- /**
72
- * Whether to disable the certificate freshness checks.
73
- * @default false
74
- */
75
- disableCertificateTimeVerification?: boolean;
76
- }
77
-
78
- /**
79
- * Requests information from a subnet's `read_state` endpoint.
80
- * Can be used to request information about the subnet's time, canister ranges, public key, node keys, and metrics.
81
- * @param {SubnetStatusOptions} options The configuration for the subnet status request.
82
- * @see {@link SubnetStatusOptions} for detailed options.
83
- * @returns {Promise<StatusMap>} A map populated with data from the requested paths. Each path is a key in the map, and the value is the data obtained from the certificate for that path.
84
- * @example
85
- * const status = await subnetStatus.request({
86
- * subnetId: IC_ROOT_SUBNET_ID,
87
- * paths: ['time', 'nodeKeys'],
88
- * agent,
89
- * });
90
- *
91
- * const time = status.get('time');
92
- * const nodeKeys = status.get('nodeKeys');
93
- */
94
- export async function request(options: SubnetStatusOptions): Promise<StatusMap> {
95
- const { agent, paths, disableCertificateTimeVerification = false } = options;
96
- const subnetId = Principal.from(options.subnetId);
97
-
98
- const uniquePaths = [...new Set(paths)];
99
- const status = new Map<string | Path, Status>();
100
-
101
- const promises = uniquePaths.map((path, index) => {
102
- const encodedPath = encodePath(path, subnetId);
103
-
104
- return (async () => {
105
- try {
106
- if (agent.rootKey === null) {
107
- throw ExternalError.fromCode(new MissingRootKeyErrorCode());
108
- }
109
-
110
- const rootKey = agent.rootKey;
111
-
112
- const response = await agent.readSubnetState(subnetId, {
113
- paths: [encodedPath],
114
- });
115
-
116
- const certificate = await Certificate.create({
117
- certificate: response.certificate,
118
- rootKey,
119
- principal: { subnetId },
120
- disableTimeVerification: disableCertificateTimeVerification,
121
- agent,
122
- });
123
-
124
- const lookup = (cert: Certificate, lookupPath: Path) => {
125
- if (lookupPath === 'nodeKeys') {
126
- // For node keys, we need to parse the certificate directly
127
- const data = lookupNodeKeysFromCertificate(cert.cert, subnetId);
128
- return {
129
- path: lookupPath,
130
- data,
131
- };
132
- }
133
- return {
134
- path: lookupPath,
135
- data: lookupResultToBuffer(cert.lookup_path(encodedPath)),
136
- };
137
- };
138
-
139
- const { path, data } = lookup(certificate, uniquePaths[index]);
140
- if (!data) {
141
- if (typeof path === 'string') {
142
- status.set(path, null);
143
- } else {
144
- status.set(path.key, null);
145
- }
146
- } else {
147
- switch (path) {
148
- case 'time': {
149
- status.set(path, decodeTime(data));
150
- break;
151
- }
152
- case 'canisterRanges': {
153
- status.set(path, decodeCanisterRanges(data));
154
- break;
155
- }
156
- case 'publicKey': {
157
- status.set(path, data);
158
- break;
159
- }
160
- case 'nodeKeys': {
161
- status.set(path, data);
162
- break;
163
- }
164
- default: {
165
- // Check for CustomPath signature
166
- if (isCustomPath(path)) {
167
- status.set(path.key, decodeValue(data, path.decodeStrategy));
168
- }
169
- }
170
- }
171
- }
172
- } catch (error) {
173
- // Throw on certificate errors
174
- if (
175
- error instanceof AgentError &&
176
- (error.hasCode(CertificateVerificationErrorCode) ||
177
- error.hasCode(CertificateTimeErrorCode))
178
- ) {
179
- throw error;
180
- }
181
- if (isCustomPath(path)) {
182
- status.set(path.key, null);
183
- } else {
184
- status.set(path, null);
185
- }
186
- }
187
- })();
188
- });
189
-
190
- // Fetch all values separately, as each option can fail
191
- await Promise.all(promises);
192
-
193
- return status;
194
- }
195
-
196
- /**
197
- * Fetch subnet info including node keys from a certificate
198
- * @param certificate the certificate bytes
199
- * @param subnetId the subnet ID
200
- * @returns SubnetStatus with subnet ID and node keys
201
- */
202
- export function lookupSubnetInfo(certificate: Uint8Array, subnetId: Principal): SubnetStatus {
203
- const cert = cbor.decode<Cert>(certificate);
204
- const nodeKeys = lookupNodeKeysFromCertificate(cert, subnetId);
205
- const publicKey = lookupSubnetPublicKey(cert, subnetId);
206
-
207
- return {
208
- subnetId: subnetId.toText(),
209
- nodeKeys,
210
- publicKey,
211
- };
212
- }
213
-
214
- function lookupSubnetPublicKey(certificate: Cert, subnetId: Principal): Uint8Array {
215
- const subnetLookupResult = lookup_path(
216
- ['subnet', subnetId.toUint8Array(), 'public_key'],
217
- certificate.tree,
218
- );
219
- if (subnetLookupResult.status !== LookupPathStatus.Found) {
220
- throw ProtocolError.fromCode(
221
- new LookupErrorCode('Public key not found', subnetLookupResult.status),
222
- );
223
- }
224
- return subnetLookupResult.value;
225
- }
226
-
227
- /**
228
- * Encode a path for subnet state queries
229
- * @param path the path to encode
230
- * @param subnetId the subnet ID
231
- * @returns the encoded path as an array of Uint8Arrays
232
- */
233
- export function encodePath(path: Path, subnetId: Principal): Uint8Array[] {
234
- const subnetUint8Array = subnetId.toUint8Array();
235
- switch (path) {
236
- case 'time':
237
- return [utf8ToBytes('time')];
238
- case 'canisterRanges':
239
- return [utf8ToBytes('canister_ranges'), subnetUint8Array];
240
- case 'publicKey':
241
- return [utf8ToBytes('subnet'), subnetUint8Array, utf8ToBytes('public_key')];
242
- case 'nodeKeys':
243
- return [utf8ToBytes('subnet'), subnetUint8Array, utf8ToBytes('node')];
244
- default: {
245
- // Check for CustomPath signature
246
- if (isCustomPath(path)) {
247
- if (typeof path['path'] === 'string' || path['path'] instanceof Uint8Array) {
248
- // For string paths, treat as a subnet path segment
249
- const encoded =
250
- typeof path['path'] === 'string' ? utf8ToBytes(path['path']) : path['path'];
251
- return [utf8ToBytes('subnet'), subnetUint8Array, encoded];
252
- }
253
- // For non-simple paths, return the provided custom path
254
- return path['path'];
255
- }
256
- }
257
- }
258
- throw UnknownError.fromCode(
259
- new UnexpectedErrorCode(
260
- `Error while encoding your path for subnet status. Please ensure that your path ${path} was formatted correctly.`,
261
- ),
262
- );
263
- }
@@ -1,18 +0,0 @@
1
- import { bls12_381 } from '@noble/curves/bls12-381';
2
- import { bytesToHex } from '@noble/hashes/utils';
3
-
4
- export let verify: (pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) => boolean;
5
-
6
- /**
7
- *
8
- * @param pk primary key: Uint8Array
9
- * @param sig signature: Uint8Array
10
- * @param msg message: Uint8Array
11
- * @returns boolean
12
- */
13
- export function blsVerify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array): boolean {
14
- const primaryKey = typeof pk === 'string' ? pk : bytesToHex(pk);
15
- const signature = typeof sig === 'string' ? sig : bytesToHex(sig);
16
- const message = typeof msg === 'string' ? msg : bytesToHex(msg);
17
- return bls12_381.verifyShortSignature(signature, message, primaryKey);
18
- }
@@ -1,64 +0,0 @@
1
- /**
2
- * Returns a true Uint8Array from an ArrayBufferLike object.
3
- * @param bufLike a buffer-like object
4
- * @returns Uint8Array
5
- */
6
- export function uint8FromBufLike(
7
- bufLike:
8
- | ArrayBuffer
9
- | Uint8Array
10
- | DataView
11
- | ArrayBufferView
12
- | ArrayBufferLike
13
- | [number]
14
- | number[]
15
- | { buffer: ArrayBuffer },
16
- ): Uint8Array {
17
- if (!bufLike) {
18
- throw new Error('Input cannot be null or undefined');
19
- }
20
-
21
- if (bufLike instanceof Uint8Array) {
22
- return bufLike;
23
- }
24
- if (bufLike instanceof ArrayBuffer) {
25
- return new Uint8Array(bufLike);
26
- }
27
- if (Array.isArray(bufLike)) {
28
- return new Uint8Array(bufLike);
29
- }
30
- if ('buffer' in bufLike) {
31
- return uint8FromBufLike(bufLike.buffer);
32
- }
33
- return new Uint8Array(bufLike);
34
- }
35
-
36
- /**
37
- * Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
38
- * @param {Uint8Array} arr Uint8Array to convert
39
- * @returns ArrayBuffer
40
- */
41
- export function uint8ToBuf(arr: Uint8Array): ArrayBuffer {
42
- const buf = new ArrayBuffer(arr.byteLength);
43
- const view = new Uint8Array(buf);
44
- view.set(arr);
45
- return buf;
46
- }
47
-
48
- /**
49
- * Compares two Uint8Arrays for equality.
50
- * @param a The first Uint8Array.
51
- * @param b The second Uint8Array.
52
- * @returns True if the Uint8Arrays are equal, false otherwise.
53
- */
54
- export function uint8Equals(a: Uint8Array, b: Uint8Array): boolean {
55
- if (a.length !== b.length) {
56
- return false;
57
- }
58
- for (let i = 0; i < a.length; i++) {
59
- if (a[i] !== b[i]) {
60
- return false;
61
- }
62
- }
63
- return true;
64
- }