@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,246 +0,0 @@
1
- import {
2
- type DerEncodedPublicKey,
3
- type KeyPair,
4
- type PublicKey,
5
- type Signature,
6
- SignIdentity,
7
- ED25519_OID,
8
- unwrapDER,
9
- wrapDER,
10
- } from '#agent';
11
- import { uint8Equals, uint8FromBufLike } from '#candid';
12
- import { ed25519 } from '@noble/curves/ed25519';
13
- import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
14
-
15
- declare type KeyLike = PublicKey | DerEncodedPublicKey | ArrayBuffer | ArrayBufferView;
16
-
17
- function isObject(value: unknown) {
18
- return value !== null && typeof value === 'object';
19
- }
20
-
21
- export class Ed25519PublicKey implements PublicKey {
22
- /**
23
- * Construct Ed25519PublicKey from an existing PublicKey
24
- * @param {unknown} maybeKey - existing PublicKey, ArrayBuffer, DerEncodedPublicKey, or hex string
25
- * @returns {Ed25519PublicKey} Instance of Ed25519PublicKey
26
- */
27
- public static from(maybeKey: unknown): Ed25519PublicKey {
28
- if (typeof maybeKey === 'string') {
29
- const key = hexToBytes(maybeKey);
30
- return this.fromRaw(key);
31
- }
32
- if (isObject(maybeKey)) {
33
- const key = maybeKey as KeyLike;
34
- if (isObject(key) && Object.hasOwnProperty.call(key, '__derEncodedPublicKey__')) {
35
- return this.fromDer(key as DerEncodedPublicKey);
36
- }
37
- if (ArrayBuffer.isView(key)) {
38
- const view = key as ArrayBufferView;
39
- return this.fromRaw(uint8FromBufLike(view.buffer));
40
- }
41
- if (key instanceof ArrayBuffer) {
42
- return this.fromRaw(uint8FromBufLike(key));
43
- }
44
- if ('rawKey' in key && key.rawKey instanceof Uint8Array) {
45
- return this.fromRaw(key.rawKey);
46
- }
47
- if ('derKey' in key) {
48
- return this.fromDer(key.derKey as DerEncodedPublicKey);
49
- }
50
- if ('toDer' in key) {
51
- return this.fromDer(key.toDer());
52
- }
53
- }
54
- throw new Error('Cannot construct Ed25519PublicKey from the provided key.');
55
- }
56
-
57
- public static fromRaw(rawKey: Uint8Array): Ed25519PublicKey {
58
- return new Ed25519PublicKey(rawKey);
59
- }
60
-
61
- public static fromDer(derKey: DerEncodedPublicKey): Ed25519PublicKey {
62
- return new Ed25519PublicKey(this.derDecode(derKey));
63
- }
64
-
65
- // The length of Ed25519 public keys is always 32 bytes.
66
- private static RAW_KEY_LENGTH = 32;
67
-
68
- private static derEncode(publicKey: Uint8Array): DerEncodedPublicKey {
69
- const key = wrapDER(publicKey, ED25519_OID) as DerEncodedPublicKey;
70
- key.__derEncodedPublicKey__ = undefined;
71
- return key;
72
- }
73
-
74
- private static derDecode(key: DerEncodedPublicKey): Uint8Array {
75
- const unwrapped = unwrapDER(key, ED25519_OID);
76
- if (unwrapped.length !== this.RAW_KEY_LENGTH) {
77
- throw new Error('An Ed25519 public key must be exactly 32bytes long');
78
- }
79
- return unwrapped;
80
- }
81
-
82
- #rawKey: Uint8Array;
83
-
84
- public get rawKey(): Uint8Array {
85
- return this.#rawKey;
86
- }
87
-
88
- #derKey: DerEncodedPublicKey;
89
-
90
- public get derKey(): DerEncodedPublicKey {
91
- return this.#derKey;
92
- }
93
-
94
- // `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
95
- private constructor(key: Uint8Array) {
96
- if (key.byteLength !== Ed25519PublicKey.RAW_KEY_LENGTH) {
97
- throw new Error('An Ed25519 public key must be exactly 32bytes long');
98
- }
99
- this.#rawKey = key;
100
- this.#derKey = Ed25519PublicKey.derEncode(key);
101
- }
102
-
103
- public toDer(): DerEncodedPublicKey {
104
- return this.derKey;
105
- }
106
-
107
- public toRaw(): Uint8Array {
108
- return this.rawKey;
109
- }
110
- }
111
-
112
- /**
113
- * Ed25519KeyIdentity is an implementation of SignIdentity that uses Ed25519 keys. This class is used to sign and verify messages for an agent.
114
- */
115
- export class Ed25519KeyIdentity extends SignIdentity {
116
- /**
117
- * Generate a new Ed25519KeyIdentity.
118
- * @param seed a 32-byte seed for the private key. If not provided, a random seed will be generated.
119
- * @returns Ed25519KeyIdentity
120
- */
121
- public static generate(seed?: Uint8Array): Ed25519KeyIdentity {
122
- if (seed && seed.length !== 32) {
123
- throw new Error('Ed25519 Seed needs to be 32 bytes long.');
124
- }
125
- if (!seed) {
126
- seed = ed25519.utils.randomPrivateKey();
127
- }
128
- // Check if the seed is all zeros
129
- if (uint8Equals(seed, new Uint8Array(new Array(32).fill(0)))) {
130
- console.warn(
131
- 'Seed is all zeros. This is not a secure seed. Please provide a seed with sufficient entropy if this is a production environment.',
132
- );
133
- }
134
- const sk = new Uint8Array(32);
135
- for (let i = 0; i < 32; i++) {
136
- sk[i] = seed[i];
137
- }
138
-
139
- const pk = ed25519.getPublicKey(sk);
140
- return Ed25519KeyIdentity.fromKeyPair(pk, sk);
141
- }
142
-
143
- public static fromParsedJson(obj: JsonnableEd25519KeyIdentity): Ed25519KeyIdentity {
144
- const [publicKeyDer, privateKeyRaw] = obj;
145
- return new Ed25519KeyIdentity(
146
- Ed25519PublicKey.fromDer(hexToBytes(publicKeyDer) as DerEncodedPublicKey),
147
- hexToBytes(privateKeyRaw),
148
- );
149
- }
150
-
151
- public static fromJSON(json: string): Ed25519KeyIdentity {
152
- const parsed = JSON.parse(json);
153
- if (Array.isArray(parsed)) {
154
- if (typeof parsed[0] === 'string' && typeof parsed[1] === 'string') {
155
- return this.fromParsedJson([parsed[0], parsed[1]]);
156
- }
157
- throw new Error('Deserialization error: JSON must have at least 2 items.');
158
- }
159
- throw new Error(`Deserialization error: Invalid JSON type for string: ${JSON.stringify(json)}`);
160
- }
161
-
162
- public static fromKeyPair(publicKey: Uint8Array, privateKey: Uint8Array): Ed25519KeyIdentity {
163
- return new Ed25519KeyIdentity(Ed25519PublicKey.fromRaw(publicKey), privateKey);
164
- }
165
-
166
- public static fromSecretKey(secretKey: Uint8Array): Ed25519KeyIdentity {
167
- const publicKey = ed25519.getPublicKey(secretKey);
168
- return Ed25519KeyIdentity.fromKeyPair(publicKey, secretKey);
169
- }
170
-
171
- #publicKey: Ed25519PublicKey;
172
- #privateKey: Uint8Array;
173
-
174
- // `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
175
- protected constructor(publicKey: PublicKey, privateKey: Uint8Array) {
176
- super();
177
- this.#publicKey = Ed25519PublicKey.from(publicKey);
178
- this.#privateKey = privateKey;
179
- }
180
-
181
- /**
182
- * Serialize this key to JSON.
183
- */
184
- public toJSON(): JsonnableEd25519KeyIdentity {
185
- return [bytesToHex(this.#publicKey.toDer()), bytesToHex(this.#privateKey)];
186
- }
187
-
188
- /**
189
- * Return a copy of the key pair.
190
- */
191
- public getKeyPair(): KeyPair {
192
- return {
193
- secretKey: this.#privateKey,
194
- publicKey: this.#publicKey,
195
- };
196
- }
197
-
198
- /**
199
- * Return the public key.
200
- */
201
- public getPublicKey(): Required<PublicKey> {
202
- return this.#publicKey;
203
- }
204
-
205
- /**
206
- * Signs a blob of data, with this identity's private key.
207
- * @param challenge - challenge to sign with this identity's secretKey, producing a signature
208
- */
209
- public async sign(challenge: Uint8Array): Promise<Signature> {
210
- // Some implementations of Ed25519 private keys append a public key to the end of the private key. We only want the private key.
211
- const signature = ed25519.sign(challenge, this.#privateKey.slice(0, 32));
212
- // add { __signature__: void; } to the signature to make it compatible with the agent
213
-
214
- Object.defineProperty(signature, '__signature__', {
215
- enumerable: false,
216
- value: undefined,
217
- });
218
-
219
- return signature as Signature;
220
- }
221
-
222
- /**
223
- * Verify
224
- * @param sig - signature to verify
225
- * @param msg - message to verify
226
- * @param pk - public key
227
- * @returns - true if the signature is valid, false otherwise
228
- */
229
- public static verify(
230
- sig: ArrayBuffer | Uint8Array | string,
231
- msg: ArrayBuffer | Uint8Array | string,
232
- pk: ArrayBuffer | Uint8Array | string,
233
- ) {
234
- const [signature, message, publicKey] = [sig, msg, pk].map(x => {
235
- if (typeof x === 'string') {
236
- x = hexToBytes(x);
237
- }
238
- return uint8FromBufLike(x);
239
- });
240
- return ed25519.verify(signature, message, publicKey);
241
- }
242
- }
243
-
244
- type PublicKeyHex = string;
245
- type SecretKeyHex = string;
246
- export type JsonnableEd25519KeyIdentity = [PublicKeyHex, SecretKeyHex];
@@ -1,60 +0,0 @@
1
- import type { Identity, PublicKey } from '#agent';
2
- import { Principal } from '#principal';
3
-
4
- /**
5
- * A partial delegated identity, representing a delegation chain and the public key that it targets
6
- */
7
- export class PartialIdentity implements Identity {
8
- #inner: PublicKey;
9
-
10
- /**
11
- * The raw public key of this identity.
12
- */
13
- get rawKey(): Uint8Array | undefined {
14
- return this.#inner.rawKey;
15
- }
16
-
17
- /**
18
- * The DER-encoded public key of this identity.
19
- */
20
- get derKey(): Uint8Array | undefined {
21
- return this.#inner.derKey;
22
- }
23
-
24
- /**
25
- * The DER-encoded public key of this identity.
26
- */
27
- public toDer(): Uint8Array {
28
- return this.#inner.toDer();
29
- }
30
-
31
- /**
32
- * The inner {@link PublicKey} used by this identity.
33
- */
34
- public getPublicKey(): PublicKey {
35
- return this.#inner;
36
- }
37
-
38
- /**
39
- * The {@link Principal} of this identity.
40
- */
41
- public getPrincipal(): Principal {
42
- if (!this.#inner.rawKey) {
43
- throw new Error('Cannot get principal from a public key without a raw key.');
44
- }
45
- return Principal.fromUint8Array(new Uint8Array(this.#inner.rawKey));
46
- }
47
-
48
- /**
49
- * Required for the Identity interface, but cannot implemented for just a public key.
50
- */
51
- public transformRequest(): Promise<never> {
52
- return Promise.reject(
53
- 'Not implemented. You are attempting to use a partial identity to sign calls, but this identity only has access to the public key.To sign calls, use a DelegationIdentity instead.',
54
- );
55
- }
56
-
57
- constructor(inner: PublicKey) {
58
- this.#inner = inner;
59
- }
60
- }
@@ -1,265 +0,0 @@
1
- import {
2
- type DerEncodedPublicKey,
3
- type PublicKey,
4
- type Signature,
5
- SignIdentity,
6
- wrapDER,
7
- DER_COSE_OID,
8
- Cbor,
9
- } from '#agent';
10
- import { bytesToHex, hexToBytes, randomBytes, bytesToUtf8 } from '@noble/hashes/utils';
11
- import { uint8FromBufLike } from '#candid';
12
-
13
- function _coseToDerEncodedBlob(cose: Uint8Array): DerEncodedPublicKey {
14
- return wrapDER(cose, DER_COSE_OID) as DerEncodedPublicKey;
15
- }
16
-
17
- type PublicKeyCredentialWithAttachment = PublicKeyCredential & {
18
- // Extends `PublicKeyCredential` with an optional field introduced in the WebAuthn level 3 spec:
19
- // https://w3c.github.io/webauthn/#dom-publickeycredential-authenticatorattachment
20
- // Already supported by Chrome, Safari and Edge
21
- // Note: `null` is included here as a possible value because Edge set this value to null in the
22
- // past.
23
- authenticatorAttachment: AuthenticatorAttachment | undefined | null;
24
- // Explicitly including toJSON method from the base PublicKeyCredential interface
25
- toJSON: () => Record<string, unknown>;
26
- };
27
-
28
- /**
29
- * From the documentation;
30
- * The authData is a byte array described in the spec. Parsing it will involve slicing bytes from
31
- * the array and converting them into usable objects.
32
- *
33
- * See https://webauthn.guide/#registration (subsection "Example: Parsing the authenticator data").
34
- * @param authData The authData field of the attestation response.
35
- * @returns The COSE key of the authData.
36
- */
37
- function _authDataToCose(authData: Uint8Array): Uint8Array {
38
- const dataView = new DataView(new ArrayBuffer(2));
39
- const idLenBytes = authData.slice(53, 55);
40
- [...new Uint8Array(idLenBytes)].forEach((v, i) => dataView.setUint8(i, v));
41
- const credentialIdLength = dataView.getUint16(0);
42
-
43
- // Get the public key object.
44
- return authData.slice(55 + credentialIdLength);
45
- }
46
-
47
- export class CosePublicKey implements PublicKey {
48
- protected _encodedKey: DerEncodedPublicKey;
49
-
50
- public constructor(protected _cose: Uint8Array) {
51
- this._encodedKey = _coseToDerEncodedBlob(_cose);
52
- }
53
-
54
- public toDer(): DerEncodedPublicKey {
55
- return this._encodedKey;
56
- }
57
-
58
- public getCose(): Uint8Array {
59
- return this._cose;
60
- }
61
- }
62
-
63
- /**
64
- * Create a challenge from a string or array. The default challenge is always the same
65
- * because we don't need to verify the authenticity of the key on the server (we don't
66
- * register our keys with the IC). Any challenge would do, even one per key, randomly
67
- * generated.
68
- * @param challenge The challenge to transform into a byte array. By default a hard
69
- * coded string.
70
- */
71
- function _createChallengeBuffer(challenge: string | Uint8Array = '<ic0.app>'): Uint8Array {
72
- if (typeof challenge === 'string') {
73
- return Uint8Array.from(challenge, c => c.charCodeAt(0));
74
- }
75
- return challenge;
76
- }
77
-
78
- /**
79
- * Create a credentials to authenticate with a server. This is necessary in order in
80
- * WebAuthn to get credentials IDs (which give us the public key and allow us to
81
- * sign), but in the case of the Internet Computer, we don't actually need to register
82
- * it, so we don't.
83
- * @param credentialCreationOptions an optional CredentialCreationOptions object
84
- */
85
- async function _createCredential(
86
- credentialCreationOptions?: CredentialCreationOptions,
87
- ): Promise<PublicKeyCredentialWithAttachment | null> {
88
- const defaultCredentialCreationOptions: CredentialCreationOptions = {
89
- publicKey: {
90
- authenticatorSelection: {
91
- userVerification: 'preferred',
92
- },
93
- attestation: 'direct',
94
- challenge: _createChallengeBuffer() as Uint8Array<ArrayBuffer>,
95
- pubKeyCredParams: [{ type: 'public-key', alg: PubKeyCoseAlgo.ECDSA_WITH_SHA256 }],
96
- rp: {
97
- name: 'Internet Identity Service',
98
- },
99
- user: {
100
- id: randomBytes(16) as Uint8Array<ArrayBuffer>,
101
- name: 'Internet Identity',
102
- displayName: 'Internet Identity',
103
- },
104
- },
105
- };
106
- const creds = (await navigator.credentials.create(
107
- credentialCreationOptions ?? defaultCredentialCreationOptions,
108
- )) as PublicKeyCredentialWithAttachment | null;
109
-
110
- if (creds === null) {
111
- return null;
112
- }
113
-
114
- return {
115
- // do _not_ use ...creds here, as creds is not enumerable in all cases
116
- id: creds.id,
117
- response: creds.response,
118
- type: creds.type,
119
- authenticatorAttachment: creds.authenticatorAttachment,
120
- getClientExtensionResults: creds.getClientExtensionResults,
121
- // Some password managers will return a Uint8Array, so we ensure we return an ArrayBuffer.
122
- rawId: creds.rawId,
123
- toJSON: creds.toJSON.bind(creds), // Ensure the toJSON method is included
124
- };
125
- }
126
-
127
- // See https://www.iana.org/assignments/cose/cose.xhtml#algorithms for a complete
128
- // list of these algorithms. We only list the ones we support here.
129
- enum PubKeyCoseAlgo {
130
- ECDSA_WITH_SHA256 = -7,
131
- }
132
-
133
- /**
134
- * A SignIdentity that uses `navigator.credentials`. See https://webauthn.guide/ for
135
- * more information about WebAuthentication.
136
- */
137
- export class WebAuthnIdentity extends SignIdentity {
138
- /**
139
- * Create an identity from a JSON serialization.
140
- * @param json - json to parse
141
- */
142
- public static fromJSON(json: string): WebAuthnIdentity {
143
- const { publicKey, rawId } = JSON.parse(json);
144
-
145
- if (typeof publicKey !== 'string' || typeof rawId !== 'string') {
146
- throw new Error('Invalid JSON string.');
147
- }
148
-
149
- return new this(hexToBytes(rawId), hexToBytes(publicKey), undefined);
150
- }
151
-
152
- /**
153
- * Create an identity.
154
- * @param credentialCreationOptions an optional CredentialCreationOptions Challenge
155
- */
156
- public static async create(
157
- credentialCreationOptions?: CredentialCreationOptions,
158
- ): Promise<WebAuthnIdentity> {
159
- const creds = await _createCredential(credentialCreationOptions);
160
-
161
- if (!creds || creds.type !== 'public-key') {
162
- throw new Error('Could not create credentials.');
163
- }
164
-
165
- const response = creds.response as AuthenticatorAttestationResponse;
166
- if (response.attestationObject === undefined) {
167
- throw new Error('Was expecting an attestation response.');
168
- }
169
-
170
- // Parse the attestationObject as CBOR.
171
- const attObject = Cbor.decode<{ authData: Uint8Array }>(
172
- new Uint8Array(response.attestationObject),
173
- );
174
-
175
- return new this(
176
- uint8FromBufLike(creds.rawId),
177
- _authDataToCose(attObject.authData),
178
- creds.authenticatorAttachment ?? undefined,
179
- );
180
- }
181
-
182
- protected _publicKey: CosePublicKey;
183
-
184
- public constructor(
185
- public readonly rawId: Uint8Array,
186
- cose: Uint8Array,
187
- protected authenticatorAttachment: AuthenticatorAttachment | undefined,
188
- ) {
189
- super();
190
- this._publicKey = new CosePublicKey(cose);
191
- }
192
-
193
- public getPublicKey(): PublicKey {
194
- return this._publicKey;
195
- }
196
-
197
- /**
198
- * WebAuthn level 3 spec introduces a new attribute on successful WebAuthn interactions,
199
- * see https://w3c.github.io/webauthn/#dom-publickeycredential-authenticatorattachment.
200
- * This attribute is already implemented for Chrome, Safari and Edge.
201
- *
202
- * Given the attribute is only available after a successful interaction, the information is
203
- * provided opportunistically and might also be `undefined`.
204
- */
205
- public getAuthenticatorAttachment(): AuthenticatorAttachment | undefined {
206
- return this.authenticatorAttachment;
207
- }
208
-
209
- public async sign(blob: Uint8Array): Promise<Signature> {
210
- const result = (await navigator.credentials.get({
211
- publicKey: {
212
- allowCredentials: [
213
- {
214
- type: 'public-key',
215
- id: this.rawId as Uint8Array<ArrayBuffer>,
216
- },
217
- ],
218
- challenge: blob as Uint8Array<ArrayBuffer>,
219
- userVerification: 'preferred',
220
- },
221
- })) as PublicKeyCredentialWithAttachment;
222
-
223
- if (result.authenticatorAttachment !== null) {
224
- this.authenticatorAttachment = result.authenticatorAttachment;
225
- }
226
-
227
- const response = result.response as AuthenticatorAssertionResponse;
228
-
229
- const encoded = Cbor.encode({
230
- authenticator_data: response.authenticatorData,
231
- client_data_json: bytesToUtf8(new Uint8Array(response.clientDataJSON)),
232
- signature: response.signature,
233
- });
234
-
235
- if (!encoded) {
236
- throw new Error('failed to encode cbor');
237
- }
238
-
239
- Object.assign(encoded, {
240
- __signature__: undefined,
241
- });
242
-
243
- return encoded as Signature;
244
- }
245
-
246
- /**
247
- * Allow for JSON serialization of all information needed to reuse this identity.
248
- */
249
- public toJSON(): JsonnableWebAuthnIdentity {
250
- return {
251
- publicKey: bytesToHex(this._publicKey.getCose()),
252
- rawId: bytesToHex(this.rawId),
253
- };
254
- }
255
- }
256
-
257
- /**
258
- * ReturnType<WebAuthnIdentity.toJSON>
259
- */
260
- export interface JsonnableWebAuthnIdentity {
261
- // The hexadecimal representation of the DER encoded public key.
262
- publicKey: string;
263
- // The string representation of the local WebAuthn Credential.id (base64url encoded).
264
- rawId: string;
265
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * JavaScript and TypeScript module to manage Identities and enable simple Web Authentication flows for applications on the [Internet Computer](https://internetcomputer.org/)
3
- *
4
- * ## Usage
5
- *
6
- * ```ts
7
- * import { ECDSAKeyIdentity, Ed25519KeyIdentity, WebAuthnIdentity } from '@icp-sdk/core/identity';
8
- *
9
- * const ecdsaIdentity = await ECDSAKeyIdentity.generate();
10
- * const ed25519Identity = Ed25519KeyIdentity.generate();
11
- * const webAuthnIdentity = await WebAuthnIdentity.create();
12
- * ```
13
- *
14
- * ### DelegationIdentity
15
- *
16
- * The `DelegationIdentity` is typically generated using the [`@icp-sdk/auth`](https://js.icp.build/auth/latest/) package.
17
- *
18
- * ### PartialIdentity
19
- *
20
- * The `PartialIdentity` is not typically used directly, but is used by the `DelegationIdentity` and `WebAuthnIdentity` classes.
21
- *
22
- * ### AttributesIdentity
23
- *
24
- * The `AttributesIdentity` is an Identity decorator that injects `sender_info` into the request content. It wraps any other identity and adds signed attributes before the inner identity signs the request.
25
- *
26
- * ### Secp256k1KeyIdentity
27
- *
28
- * See [@icp-sdk/core/identity/secp256k1](https://js.icp.build/core/latest/libs/identity-secp256k1/api/) for more information.
29
- *
30
- * ## In Node.js
31
- *
32
- * Depending on your version, you may need to use a polyfill and set `global.crypto` in a setup file. If you prefer, you can also pass in a `subtleCrypto` implementation in methods that call for it, either as a direct argument, or in a `cryptoOptions` object.
33
- *
34
- * <!-- split here -->
35
- * @module libs/identity/api
36
- */
37
-
38
- export * from './identity/ed25519.ts';
39
- export * from './identity/ecdsa.ts';
40
- export * from './identity/delegation.ts';
41
- export * from './identity/partial.ts';
42
- export * from './identity/attributes.ts';
43
- export { WebAuthnIdentity } from './identity/webauthn.ts';
44
- export { wrapDER, unwrapDER, DER_COSE_OID, ED25519_OID } from '#agent';