@icp-sdk/core 5.4.0 → 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 (251) 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 +19 -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 +8 -11
  9. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  10. package/lib/cjs/agent/agent/http/index.js +190 -166
  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/delegation.d.ts +10 -3
  67. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  68. package/lib/cjs/identity/identity/delegation.js +25 -12
  69. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  70. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  71. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  72. package/lib/cjs/identity/identity/ecdsa.js +3 -2
  73. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  74. package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
  75. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  76. package/lib/cjs/identity/identity/ed25519.js +18 -15
  77. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  78. package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
  79. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
  80. package/lib/cjs/identity/identity/webauthn.js +12 -11
  81. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  82. package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
  83. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  84. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  85. package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
  86. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
  87. package/lib/cjs/identity/secp256k1/pem.js +2 -2
  88. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  89. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
  90. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  91. package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
  92. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  93. package/lib/cjs/principal/principal.js +5 -5
  94. package/lib/cjs/principal/principal.js.map +1 -1
  95. package/lib/esm/agent/actor.d.ts +7 -3
  96. package/lib/esm/agent/actor.d.ts.map +1 -1
  97. package/lib/esm/agent/actor.js +18 -5
  98. package/lib/esm/agent/actor.js.map +1 -1
  99. package/lib/esm/agent/agent/api.d.ts +19 -6
  100. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  101. package/lib/esm/agent/agent/api.js.map +1 -1
  102. package/lib/esm/agent/agent/http/index.d.ts +8 -11
  103. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  104. package/lib/esm/agent/agent/http/index.js +183 -159
  105. package/lib/esm/agent/agent/http/index.js.map +1 -1
  106. package/lib/esm/agent/auth.d.ts +6 -5
  107. package/lib/esm/agent/auth.d.ts.map +1 -1
  108. package/lib/esm/agent/auth.js +1 -1
  109. package/lib/esm/agent/auth.js.map +1 -1
  110. package/lib/esm/agent/canister-env/index.js +1 -1
  111. package/lib/esm/agent/canister-env/index.js.map +1 -1
  112. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  113. package/lib/esm/agent/canisterStatus/index.js +2 -2
  114. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  115. package/lib/esm/agent/cbor.d.ts +2 -1
  116. package/lib/esm/agent/cbor.d.ts.map +1 -1
  117. package/lib/esm/agent/cbor.js +4 -1
  118. package/lib/esm/agent/cbor.js.map +1 -1
  119. package/lib/esm/agent/certificate.d.ts +5 -4
  120. package/lib/esm/agent/certificate.d.ts.map +1 -1
  121. package/lib/esm/agent/certificate.js +5 -9
  122. package/lib/esm/agent/certificate.js.map +1 -1
  123. package/lib/esm/agent/der.d.ts +3 -2
  124. package/lib/esm/agent/der.d.ts.map +1 -1
  125. package/lib/esm/agent/der.js.map +1 -1
  126. package/lib/esm/agent/errors.d.ts +7 -1
  127. package/lib/esm/agent/errors.d.ts.map +1 -1
  128. package/lib/esm/agent/errors.js +18 -1
  129. package/lib/esm/agent/errors.js.map +1 -1
  130. package/lib/esm/agent/polling/index.d.ts +3 -4
  131. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  132. package/lib/esm/agent/polling/index.js +15 -8
  133. package/lib/esm/agent/polling/index.js.map +1 -1
  134. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  135. package/lib/esm/agent/polling/strategy.js +6 -6
  136. package/lib/esm/agent/polling/strategy.js.map +1 -1
  137. package/lib/esm/agent/polling/types.d.ts +3 -4
  138. package/lib/esm/agent/polling/types.d.ts.map +1 -1
  139. package/lib/esm/agent/public_key.d.ts +3 -2
  140. package/lib/esm/agent/public_key.d.ts.map +1 -1
  141. package/lib/esm/agent/public_key.js +2 -2
  142. package/lib/esm/agent/public_key.js.map +1 -1
  143. package/lib/esm/agent/request_id.js +2 -2
  144. package/lib/esm/agent/request_id.js.map +1 -1
  145. package/lib/esm/agent/subnetStatus/index.js +1 -1
  146. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  147. package/lib/esm/agent/utils/bls.d.ts.map +1 -1
  148. package/lib/esm/agent/utils/bls.js +7 -6
  149. package/lib/esm/agent/utils/bls.js.map +1 -1
  150. package/lib/esm/agent/utils/readState.d.ts +2 -2
  151. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  152. package/lib/esm/agent/utils/readState.js +1 -1
  153. package/lib/esm/agent/utils/readState.js.map +1 -1
  154. package/lib/esm/agent/utils/target.d.ts +15 -0
  155. package/lib/esm/agent/utils/target.d.ts.map +1 -0
  156. package/lib/esm/agent/utils/target.js +23 -0
  157. package/lib/esm/agent/utils/target.js.map +1 -0
  158. package/lib/esm/candid/utils/buffer.d.ts +2 -1
  159. package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
  160. package/lib/esm/candid/utils/buffer.js +5 -2
  161. package/lib/esm/candid/utils/buffer.js.map +1 -1
  162. package/lib/esm/identity/identity/delegation.d.ts +10 -3
  163. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  164. package/lib/esm/identity/identity/delegation.js +22 -9
  165. package/lib/esm/identity/identity/delegation.js.map +1 -1
  166. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  167. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  168. package/lib/esm/identity/identity/ecdsa.js +3 -2
  169. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  170. package/lib/esm/identity/identity/ed25519.d.ts +3 -2
  171. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  172. package/lib/esm/identity/identity/ed25519.js +10 -7
  173. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  174. package/lib/esm/identity/identity/webauthn.d.ts +4 -3
  175. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
  176. package/lib/esm/identity/identity/webauthn.js +8 -7
  177. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  178. package/lib/esm/identity/secp256k1/der.d.ts +2 -1
  179. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  180. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  181. package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
  182. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
  183. package/lib/esm/identity/secp256k1/pem.js +2 -2
  184. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  185. package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
  186. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  187. package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
  188. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  189. package/lib/esm/principal/principal.js +2 -2
  190. package/lib/esm/principal/principal.js.map +1 -1
  191. package/package.json +20 -10
  192. package/src/agent/actor.ts +0 -524
  193. package/src/agent/agent/api.ts +0 -310
  194. package/src/agent/agent/http/expiry.ts +0 -107
  195. package/src/agent/agent/http/index.ts +0 -1724
  196. package/src/agent/agent/http/transforms.ts +0 -54
  197. package/src/agent/agent/http/types.ts +0 -145
  198. package/src/agent/agent/index.ts +0 -4
  199. package/src/agent/auth.ts +0 -147
  200. package/src/agent/canister-env/index.ts +0 -192
  201. package/src/agent/canisterStatus/index.ts +0 -260
  202. package/src/agent/cbor.ts +0 -63
  203. package/src/agent/certificate.ts +0 -1058
  204. package/src/agent/constants.ts +0 -24
  205. package/src/agent/der.ts +0 -191
  206. package/src/agent/errors.ts +0 -962
  207. package/src/agent/fetch_candid.ts +0 -37
  208. package/src/agent/index.ts +0 -130
  209. package/src/agent/observable.ts +0 -49
  210. package/src/agent/polling/backoff.ts +0 -129
  211. package/src/agent/polling/index.ts +0 -238
  212. package/src/agent/polling/strategy.ts +0 -140
  213. package/src/agent/polling/types.ts +0 -28
  214. package/src/agent/public_key.ts +0 -65
  215. package/src/agent/request_id.ts +0 -99
  216. package/src/agent/subnetStatus/index.ts +0 -263
  217. package/src/agent/utils/bls.ts +0 -18
  218. package/src/agent/utils/buffer.ts +0 -64
  219. package/src/agent/utils/certificateReject.ts +0 -25
  220. package/src/agent/utils/createExpirableStore.ts +0 -31
  221. package/src/agent/utils/expirableStore.ts +0 -47
  222. package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
  223. package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
  224. package/src/agent/utils/leb.ts +0 -14
  225. package/src/agent/utils/random.ts +0 -30
  226. package/src/agent/utils/readState.ts +0 -180
  227. package/src/candid/candid-core.ts +0 -274
  228. package/src/candid/candid-ui.ts +0 -281
  229. package/src/candid/idl.ts +0 -2654
  230. package/src/candid/index.ts +0 -35
  231. package/src/candid/types.ts +0 -7
  232. package/src/candid/utils/bigint-math.ts +0 -26
  233. package/src/candid/utils/buffer.ts +0 -221
  234. package/src/candid/utils/hash.ts +0 -31
  235. package/src/candid/utils/leb128.ts +0 -233
  236. package/src/identity/identity/attributes.ts +0 -91
  237. package/src/identity/identity/delegation.ts +0 -411
  238. package/src/identity/identity/ecdsa.ts +0 -162
  239. package/src/identity/identity/ed25519.ts +0 -246
  240. package/src/identity/identity/partial.ts +0 -60
  241. package/src/identity/identity/webauthn.ts +0 -265
  242. package/src/identity/index.ts +0 -44
  243. package/src/identity/secp256k1/der.ts +0 -177
  244. package/src/identity/secp256k1/index.ts +0 -52
  245. package/src/identity/secp256k1/pem.ts +0 -39
  246. package/src/identity/secp256k1/secp256k1.ts +0 -276
  247. package/src/index.ts +0 -3
  248. package/src/principal/index.ts +0 -19
  249. package/src/principal/principal.ts +0 -181
  250. package/src/principal/utils/base32.ts +0 -101
  251. package/src/principal/utils/getCrc.ts +0 -52
@@ -1,35 +0,0 @@
1
- /**
2
- * JavaScript and TypeScript module to work with Candid interfaces
3
- *
4
- * ## Usage
5
- *
6
- * ```ts
7
- * import { IDL } from '@icp-sdk/core/candid';
8
- *
9
- * const MyType = IDL.Record({
10
- * name: IDL.Text,
11
- * age: IDL.Nat8,
12
- * });
13
- *
14
- * const encoded = IDL.encode([MyType], [{ name: 'John', age: 30 }]);
15
- * const decoded = IDL.decode([MyType], encoded);
16
- *
17
- * console.log(decoded); // [{ name: 'John', age: 30 }]
18
- * ```
19
- *
20
- * <!-- split here -->
21
- * @module libs/candid/api
22
- */
23
-
24
- export * from './candid-ui.ts';
25
- export * from './candid-core.ts';
26
- export * as IDL from './idl.ts';
27
- export {
28
- type GenericIdlFuncArgs,
29
- type GenericIdlFuncRets,
30
- type GenericIdlServiceFields,
31
- } from './idl.ts';
32
- export * from './utils/hash.ts';
33
- export * from './utils/leb128.ts';
34
- export * from './utils/buffer.ts';
35
- export * from './types.ts';
@@ -1,7 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-empty-object-type */
2
-
3
- export interface JsonArray extends Array<JsonValue> {}
4
-
5
- export interface JsonObject extends Record<string, JsonValue> {}
6
-
7
- export type JsonValue = boolean | string | number | bigint | JsonArray | JsonObject;
@@ -1,26 +0,0 @@
1
- /**
2
- * Equivalent to `Math.log2(n)` with support for `BigInt` values
3
- * @param n bigint or integer
4
- * @returns integer
5
- */
6
- export function ilog2(n: bigint | number): number {
7
- const nBig = BigInt(n);
8
- if (n <= 0) {
9
- throw new RangeError('Input must be positive');
10
- }
11
- return nBig.toString(2).length - 1;
12
- }
13
-
14
- /**
15
- * Equivalent to `2 ** n` with support for `BigInt` values
16
- * (necessary for browser preprocessors which replace the `**` operator with `Math.pow`)
17
- * @param n bigint or integer
18
- * @returns bigint
19
- */
20
- export function iexp2(n: bigint | number): bigint {
21
- const nBig = BigInt(n);
22
- if (n < 0) {
23
- throw new RangeError('Input must be non-negative');
24
- }
25
- return BigInt(1) << nBig;
26
- }
@@ -1,221 +0,0 @@
1
- /**
2
- * Concatenate multiple Uint8Arrays.
3
- * @param uint8Arrays The Uint8Arrays to concatenate.
4
- */
5
- export function concat(...uint8Arrays: Uint8Array[]): Uint8Array {
6
- const result = new Uint8Array(uint8Arrays.reduce((acc, curr) => acc + curr.byteLength, 0));
7
- let index = 0;
8
- for (const b of uint8Arrays) {
9
- result.set(b, index);
10
- index += b.byteLength;
11
- }
12
- return result;
13
- }
14
-
15
- /**
16
- * A class that abstracts a pipe-like Uint8Array.
17
- */
18
- export class PipeArrayBuffer {
19
- /**
20
- * The reading view. It's a sliding window as we read and write, pointing to the buffer.
21
- * @private
22
- */
23
- private _view: Uint8Array;
24
-
25
- /**
26
- * Save a checkpoint of the reading view (for backtracking)
27
- */
28
- public save(): Uint8Array {
29
- return this._view;
30
- }
31
-
32
- /**
33
- * Restore a checkpoint of the reading view (for backtracking)
34
- * @param checkPoint a previously saved checkpoint
35
- */
36
- public restore(checkPoint: Uint8Array) {
37
- if (!(checkPoint instanceof Uint8Array)) {
38
- throw new Error('Checkpoint must be a Uint8Array');
39
- }
40
- this._view = checkPoint;
41
- }
42
-
43
- /**
44
- * The actual buffer containing the bytes.
45
- * @private
46
- */
47
- private _buffer: Uint8Array;
48
-
49
- /**
50
- * Creates a new instance of a pipe
51
- * @param buffer an optional buffer to start with
52
- * @param length an optional amount of bytes to use for the length.
53
- */
54
- constructor(buffer?: Uint8Array, length = buffer?.byteLength || 0) {
55
- if (buffer && !(buffer instanceof Uint8Array)) {
56
- try {
57
- buffer = uint8FromBufLike(buffer);
58
- } catch {
59
- throw new Error('Buffer must be a Uint8Array');
60
- }
61
- }
62
- if (length < 0 || !Number.isInteger(length)) {
63
- throw new Error('Length must be a non-negative integer');
64
- }
65
- if (buffer && length > buffer.byteLength) {
66
- throw new Error('Length cannot exceed buffer length');
67
- }
68
- this._buffer = buffer || new Uint8Array(0);
69
- this._view = new Uint8Array(this._buffer.buffer, 0, length);
70
- }
71
-
72
- get buffer(): Uint8Array {
73
- // Return a copy of the buffer.
74
- return this._view.slice();
75
- }
76
-
77
- get byteLength(): number {
78
- return this._view.byteLength;
79
- }
80
-
81
- /**
82
- * Read `num` number of bytes from the front of the pipe.
83
- * @param num The number of bytes to read.
84
- */
85
- public read(num: number): Uint8Array {
86
- const result = this._view.subarray(0, num);
87
- this._view = this._view.subarray(num);
88
- return result.slice();
89
- }
90
-
91
- public readUint8(): number | undefined {
92
- if (this._view.byteLength === 0) {
93
- return undefined;
94
- }
95
- const result = this._view[0];
96
- this._view = this._view.subarray(1);
97
- return result;
98
- }
99
-
100
- /**
101
- * Write a buffer to the end of the pipe.
102
- * @param buf The bytes to write.
103
- */
104
- public write(buf: Uint8Array): void {
105
- if (!(buf instanceof Uint8Array)) {
106
- throw new Error('Buffer must be a Uint8Array');
107
- }
108
- const offset = this._view.byteLength;
109
- if (this._view.byteOffset + this._view.byteLength + buf.byteLength >= this._buffer.byteLength) {
110
- // Alloc grow the view to include the new bytes.
111
- this.alloc(buf.byteLength);
112
- } else {
113
- // Update the view to include the new bytes.
114
- this._view = new Uint8Array(
115
- this._buffer.buffer,
116
- this._view.byteOffset,
117
- this._view.byteLength + buf.byteLength,
118
- );
119
- }
120
-
121
- this._view.set(buf, offset);
122
- }
123
-
124
- /**
125
- * Whether or not there is more data to read from the buffer
126
- */
127
- public get end(): boolean {
128
- return this._view.byteLength === 0;
129
- }
130
-
131
- /**
132
- * Allocate a fixed amount of memory in the buffer. This does not affect the view.
133
- * @param amount A number of bytes to add to the buffer.
134
- */
135
- public alloc(amount: number) {
136
- if (amount <= 0 || !Number.isInteger(amount)) {
137
- throw new Error('Amount must be a positive integer');
138
- }
139
- // Add a little bit of exponential growth.
140
- const b = new Uint8Array(((this._buffer.byteLength + amount) * 1.2) | 0);
141
- const v = new Uint8Array(b.buffer, 0, this._view.byteLength + amount);
142
- v.set(this._view);
143
- this._buffer = b;
144
- this._view = v;
145
- }
146
- }
147
-
148
- /**
149
- * Returns a true Uint8Array from an ArrayBufferLike object.
150
- * @param bufLike a buffer-like object
151
- * @returns Uint8Array
152
- */
153
- export function uint8FromBufLike(
154
- bufLike:
155
- | ArrayBuffer
156
- | Uint8Array
157
- | DataView
158
- | ArrayBufferView
159
- | ArrayBufferLike
160
- | [number]
161
- | number[]
162
- | { buffer: ArrayBuffer },
163
- ): Uint8Array {
164
- if (!bufLike) {
165
- throw new Error('Input cannot be null or undefined');
166
- }
167
-
168
- if (bufLike instanceof Uint8Array) {
169
- return bufLike;
170
- }
171
- if (bufLike instanceof ArrayBuffer) {
172
- return new Uint8Array(bufLike);
173
- }
174
- if (Array.isArray(bufLike)) {
175
- return new Uint8Array(bufLike);
176
- }
177
- if ('buffer' in bufLike) {
178
- return uint8FromBufLike(bufLike.buffer);
179
- }
180
- return new Uint8Array(bufLike);
181
- }
182
-
183
- /**
184
- *
185
- * @param u1 uint8Array 1
186
- * @param u2 uint8Array 2
187
- * @returns number - negative if u1 < u2, positive if u1 > u2, 0 if u1 === u2
188
- */
189
- export function compare(u1: Uint8Array, u2: Uint8Array): number {
190
- if (u1.byteLength !== u2.byteLength) {
191
- return u1.byteLength - u2.byteLength;
192
- }
193
- for (let i = 0; i < u1.length; i++) {
194
- if (u1[i] !== u2[i]) {
195
- return u1[i] - u2[i];
196
- }
197
- }
198
- return 0;
199
- }
200
-
201
- /**
202
- * Checks two uint8Arrays for equality.
203
- * @param u1 uint8Array 1
204
- * @param u2 uint8Array 2
205
- * @returns boolean
206
- */
207
- export function uint8Equals(u1: Uint8Array, u2: Uint8Array): boolean {
208
- return compare(u1, u2) === 0;
209
- }
210
-
211
- /**
212
- * Helpers to convert a Uint8Array to a DataView.
213
- * @param uint8 Uint8Array
214
- * @returns DataView
215
- */
216
- export function uint8ToDataView(uint8: Uint8Array): DataView {
217
- if (!(uint8 instanceof Uint8Array)) {
218
- throw new Error('Input must be a Uint8Array');
219
- }
220
- return new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
221
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * Hashes a string to a number. Algorithm can be found here:
3
- * https://caml.inria.fr/pub/papers/garrigue-polymorphic_variants-ml98.pdf
4
- * @param s - string to hash
5
- * @returns number representing hashed string
6
- */
7
- function idlHash(s: string): number {
8
- const utf8encoder = new TextEncoder();
9
- const array = utf8encoder.encode(s);
10
-
11
- let h = 0;
12
- for (const c of array) {
13
- h = (h * 223 + c) % 2 ** 32;
14
- }
15
- return h;
16
- }
17
-
18
- /**
19
- *
20
- * @param label string
21
- * @returns number representing hashed label
22
- */
23
- export function idlLabelToId(label: string): number {
24
- if (/^_\d+_$/.test(label) || /^_0x[0-9a-fA-F]+_$/.test(label)) {
25
- const num = +label.slice(1, -1);
26
- if (Number.isSafeInteger(num) && num >= 0 && num < 2 ** 32) {
27
- return num;
28
- }
29
- }
30
- return idlHash(label);
31
- }
@@ -1,233 +0,0 @@
1
- // Note: this file uses buffer-pipe, which on Node only, uses the Node Buffer
2
- // implementation, which isn't compatible with the NPM buffer package
3
- // which we use everywhere else. This means that we have to transform
4
- // one into the other, hence why every function that returns a Buffer
5
- // actually return `new Buffer(pipe.buffer)`.
6
- // TODO: The best solution would be to have our own buffer type around
7
- // Uint8Array which is standard.
8
- import { PipeArrayBuffer as Pipe } from './buffer.ts';
9
- import { ilog2 } from './bigint-math.ts';
10
-
11
- function eob(): never {
12
- throw new Error('unexpected end of buffer');
13
- }
14
-
15
- /**
16
- *
17
- * @param pipe Pipe from buffer-pipe
18
- * @param num number
19
- * @returns Uint8Array
20
- */
21
- export function safeRead(pipe: Pipe, num: number): Uint8Array {
22
- if (pipe.byteLength < num) {
23
- eob();
24
- }
25
- return pipe.read(num);
26
- }
27
-
28
- /**
29
- * @param pipe - PipeArrayBuffer simulating buffer-pipe api
30
- */
31
- export function safeReadUint8(pipe: Pipe): number {
32
- const byte = pipe.readUint8();
33
- if (byte === undefined) {
34
- eob();
35
- }
36
- return byte;
37
- }
38
-
39
- /**
40
- * Encode a positive number (or bigint) into a Buffer. The number will be floored to the
41
- * nearest integer.
42
- * @param value The number to encode.
43
- */
44
- export function lebEncode(value: bigint | number): Uint8Array {
45
- if (typeof value === 'number') {
46
- value = BigInt(value);
47
- }
48
-
49
- if (value < BigInt(0)) {
50
- throw new Error('Cannot leb encode negative values.');
51
- }
52
-
53
- const byteLength = (value === BigInt(0) ? 0 : ilog2(value)) + 1;
54
- const pipe = new Pipe(new Uint8Array(byteLength), 0);
55
- while (true) {
56
- const i = Number(value & BigInt(0x7f));
57
- value /= BigInt(0x80);
58
- if (value === BigInt(0)) {
59
- pipe.write(new Uint8Array([i]));
60
- break;
61
- } else {
62
- pipe.write(new Uint8Array([i | 0x80]));
63
- }
64
- }
65
-
66
- return pipe.buffer;
67
- }
68
-
69
- /**
70
- * Decode a leb encoded buffer into a bigint. The number will always be positive (does not
71
- * support signed leb encoding).
72
- * @param pipe A Buffer containing the leb encoded bits.
73
- */
74
- export function lebDecode(pipe: Pipe): bigint {
75
- let weight = BigInt(1);
76
- let value = BigInt(0);
77
- let byte;
78
-
79
- do {
80
- byte = safeReadUint8(pipe);
81
- value += BigInt(byte & 0x7f).valueOf() * weight;
82
- weight *= BigInt(128);
83
- } while (byte >= 0x80);
84
-
85
- return value;
86
- }
87
-
88
- /**
89
- * Encode a number (or bigint) into a Buffer, with support for negative numbers. The number
90
- * will be floored to the nearest integer.
91
- * @param value The number to encode.
92
- */
93
- export function slebEncode(value: bigint | number): Uint8Array {
94
- if (typeof value === 'number') {
95
- value = BigInt(value);
96
- }
97
-
98
- const isNeg = value < BigInt(0);
99
- if (isNeg) {
100
- value = -value - BigInt(1);
101
- }
102
- const byteLength = (value === BigInt(0) ? 0 : ilog2(value)) + 1;
103
- const pipe = new Pipe(new Uint8Array(byteLength), 0);
104
- while (true) {
105
- const i = getLowerBytes(value);
106
- value /= BigInt(0x80);
107
-
108
- // prettier-ignore
109
- if ( ( isNeg && value === BigInt(0) && (i & 0x40) !== 0)
110
- || (!isNeg && value === BigInt(0) && (i & 0x40) === 0)) {
111
- pipe.write(new Uint8Array([i]));
112
- break;
113
- } else {
114
- pipe.write(new Uint8Array([i | 0x80]));
115
- }
116
- }
117
-
118
- function getLowerBytes(num: bigint): number {
119
- const bytes = num % BigInt(0x80);
120
- if (isNeg) {
121
- // We swap the bits here again, and remove 1 to do two's complement.
122
- return Number(BigInt(0x80) - bytes - BigInt(1));
123
- }
124
- return Number(bytes);
125
- }
126
- return pipe.buffer;
127
- }
128
-
129
- /**
130
- * Decode a leb encoded buffer into a bigint. The number is decoded with support for negative
131
- * signed-leb encoding.
132
- * @param pipe A Buffer containing the signed leb encoded bits.
133
- */
134
- export function slebDecode(pipe: Pipe): bigint {
135
- // Get the size of the buffer, then cut a buffer of that size.
136
- const pipeView = new Uint8Array(pipe.buffer);
137
- let len = 0;
138
- for (; len < pipeView.byteLength; len++) {
139
- if (pipeView[len] < 0x80) {
140
- // If it's a positive number, we reuse lebDecode.
141
- if ((pipeView[len] & 0x40) === 0) {
142
- return lebDecode(pipe);
143
- }
144
- break;
145
- }
146
- }
147
-
148
- const bytes = new Uint8Array(safeRead(pipe, len + 1));
149
- let value = BigInt(0);
150
- for (let i = bytes.byteLength - 1; i >= 0; i--) {
151
- value = value * BigInt(0x80) + BigInt(0x80 - (bytes[i] & 0x7f) - 1);
152
- }
153
- return -value - BigInt(1);
154
- }
155
-
156
- /**
157
- *
158
- * @param value bigint or number
159
- * @param byteLength number
160
- * @returns Uint8Array
161
- */
162
- export function writeUIntLE(value: bigint | number, byteLength: number): Uint8Array {
163
- if (BigInt(value) < BigInt(0)) {
164
- throw new Error('Cannot write negative values.');
165
- }
166
- return writeIntLE(value, byteLength);
167
- }
168
-
169
- /**
170
- *
171
- * @param value - bigint or number
172
- * @param byteLength - number
173
- * @returns Uint8Array
174
- */
175
- export function writeIntLE(value: bigint | number, byteLength: number): Uint8Array {
176
- value = BigInt(value);
177
-
178
- const pipe = new Pipe(new Uint8Array(Math.min(1, byteLength)), 0);
179
- let i = 0;
180
- let mul = BigInt(256);
181
- let sub = BigInt(0);
182
- let byte = Number(value % mul);
183
- pipe.write(new Uint8Array([byte]));
184
- while (++i < byteLength) {
185
- if (value < 0 && sub === BigInt(0) && byte !== 0) {
186
- sub = BigInt(1);
187
- }
188
- byte = Number((value / mul - sub) % BigInt(256));
189
- pipe.write(new Uint8Array([byte]));
190
- mul *= BigInt(256);
191
- }
192
-
193
- return pipe.buffer;
194
- }
195
-
196
- /**
197
- *
198
- * @param pipe Pipe from buffer-pipe
199
- * @param byteLength number
200
- * @returns bigint
201
- */
202
- export function readUIntLE(pipe: Pipe, byteLength: number): bigint {
203
- if (byteLength <= 0 || !Number.isInteger(byteLength)) {
204
- throw new Error('Byte length must be a positive integer');
205
- }
206
- let val = BigInt(safeReadUint8(pipe));
207
- let mul = BigInt(1);
208
- let i = 0;
209
- while (++i < byteLength) {
210
- mul *= BigInt(256);
211
- const byte = BigInt(safeReadUint8(pipe));
212
- val = val + mul * byte;
213
- }
214
- return val;
215
- }
216
-
217
- /**
218
- *
219
- * @param pipe Pipe from buffer-pipe
220
- * @param byteLength number
221
- * @returns bigint
222
- */
223
- export function readIntLE(pipe: Pipe, byteLength: number): bigint {
224
- if (byteLength <= 0 || !Number.isInteger(byteLength)) {
225
- throw new Error('Byte length must be a positive integer');
226
- }
227
- let val = readUIntLE(pipe, byteLength);
228
- const mul = BigInt(2) ** (BigInt(8) * BigInt(byteLength - 1) + BigInt(7));
229
- if (val >= mul) {
230
- val -= mul * BigInt(2);
231
- }
232
- return val;
233
- }
@@ -1,91 +0,0 @@
1
- import { Endpoint, type Identity, type HttpAgentRequest } from '#agent';
2
- import type { Principal } from '#principal';
3
-
4
- /**
5
- * Signed attributes to be included as `sender_info` in the request content.
6
- */
7
- export interface SignedAttributes {
8
- data: Uint8Array;
9
- signature: Uint8Array;
10
- }
11
-
12
- /**
13
- * The canister that signed the attributes.
14
- */
15
- export interface Signer {
16
- canisterId: Principal;
17
- }
18
-
19
- /**
20
- * Options for creating an {@link AttributesIdentity}.
21
- */
22
- export interface AttributesIdentityOptions {
23
- /** The inner identity to delegate signing to. */
24
- inner: Identity;
25
- /** The signed attributes to include in the request. */
26
- attributes: SignedAttributes;
27
- /** The canister that signed the attributes. */
28
- signer: Signer;
29
- }
30
-
31
- /**
32
- * An Identity decorator that injects `sender_info` into the request body
33
- * before delegating to an inner identity for signing.
34
- *
35
- * Because `sender_info` is part of the request content, it is included in the
36
- * representation-independent hash (`requestIdOf`) and covered by the sender's
37
- * signature for `call` and `query` endpoints.
38
- *
39
- * The IC does not hash `sender_info` for `read_state` requests, so the
40
- * decorator skips injection for that endpoint to avoid signature verification
41
- * failures on update-call polls.
42
- *
43
- * @example
44
- * ```ts
45
- * const inner = DelegationIdentity.fromDelegation(key, chain);
46
- * const identity = new AttributesIdentity({
47
- * inner,
48
- * attributes: { data: new Uint8Array([...]), signature: new Uint8Array([...]) },
49
- * signer: { canisterId: Principal.fromText('aaaaa-aa') },
50
- * });
51
- * const agent = HttpAgent.create({ identity });
52
- * ```
53
- */
54
- export class AttributesIdentity implements Identity {
55
- readonly #inner: Identity;
56
- readonly #attributes: SignedAttributes;
57
- readonly #signer: Signer;
58
-
59
- /**
60
- * @param options - Configuration for the identity.
61
- * @param options.inner - The inner identity to delegate signing to.
62
- * @param options.attributes - The signed attributes to include in the request.
63
- * @param options.signer - The canister that signed the attributes.
64
- */
65
- constructor(options: AttributesIdentityOptions) {
66
- this.#inner = options.inner;
67
- this.#attributes = options.attributes;
68
- this.#signer = options.signer;
69
- }
70
-
71
- getPrincipal(): Principal {
72
- return this.#inner.getPrincipal();
73
- }
74
-
75
- transformRequest(request: HttpAgentRequest): Promise<unknown> {
76
- if (request.endpoint === Endpoint.ReadState) {
77
- return this.#inner.transformRequest(request);
78
- }
79
- return this.#inner.transformRequest({
80
- ...request,
81
- body: {
82
- ...request.body,
83
- sender_info: {
84
- signer: this.#signer.canisterId.toUint8Array(),
85
- info: this.#attributes.data,
86
- sig: this.#attributes.signature,
87
- },
88
- },
89
- } as HttpAgentRequest);
90
- }
91
- }