@icp-sdk/core 5.4.0 → 6.1.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 +71 -10
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/api.js +6 -1
  8. package/lib/cjs/agent/agent/api.js.map +1 -1
  9. package/lib/cjs/agent/agent/http/index.d.ts +9 -12
  10. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  11. package/lib/cjs/agent/agent/http/index.js +224 -176
  12. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  13. package/lib/cjs/agent/auth.d.ts +6 -5
  14. package/lib/cjs/agent/auth.d.ts.map +1 -1
  15. package/lib/cjs/agent/auth.js +3 -3
  16. package/lib/cjs/agent/auth.js.map +1 -1
  17. package/lib/cjs/agent/canisterStatus/index.d.ts +2 -3
  18. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  19. package/lib/cjs/agent/canisterStatus/index.js +65 -122
  20. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  21. package/lib/cjs/agent/cbor.d.ts +2 -1
  22. package/lib/cjs/agent/cbor.d.ts.map +1 -1
  23. package/lib/cjs/agent/cbor.js +4 -1
  24. package/lib/cjs/agent/cbor.js.map +1 -1
  25. package/lib/cjs/agent/certificate.d.ts +6 -5
  26. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  27. package/lib/cjs/agent/certificate.js +14 -18
  28. package/lib/cjs/agent/certificate.js.map +1 -1
  29. package/lib/cjs/agent/der.d.ts +3 -2
  30. package/lib/cjs/agent/der.d.ts.map +1 -1
  31. package/lib/cjs/agent/der.js.map +1 -1
  32. package/lib/cjs/agent/errors.d.ts +7 -1
  33. package/lib/cjs/agent/errors.d.ts.map +1 -1
  34. package/lib/cjs/agent/errors.js +34 -16
  35. package/lib/cjs/agent/errors.js.map +1 -1
  36. package/lib/cjs/agent/polling/index.d.ts +7 -7
  37. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  38. package/lib/cjs/agent/polling/index.js +19 -20
  39. package/lib/cjs/agent/polling/index.js.map +1 -1
  40. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  41. package/lib/cjs/agent/polling/strategy.js +6 -6
  42. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  43. package/lib/cjs/agent/polling/types.d.ts +3 -4
  44. package/lib/cjs/agent/polling/types.d.ts.map +1 -1
  45. package/lib/cjs/agent/public_key.d.ts +3 -2
  46. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  47. package/lib/cjs/agent/public_key.js +2 -2
  48. package/lib/cjs/agent/public_key.js.map +1 -1
  49. package/lib/cjs/agent/request_id.js +10 -10
  50. package/lib/cjs/agent/request_id.js.map +1 -1
  51. package/lib/cjs/agent/subnetStatus/index.d.ts +3 -2
  52. package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
  53. package/lib/cjs/agent/subnetStatus/index.js +65 -88
  54. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  55. package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
  56. package/lib/cjs/agent/utils/bls.js +7 -6
  57. package/lib/cjs/agent/utils/bls.js.map +1 -1
  58. package/lib/cjs/agent/utils/readState.d.ts +104 -15
  59. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  60. package/lib/cjs/agent/utils/readState.js +106 -17
  61. package/lib/cjs/agent/utils/readState.js.map +1 -1
  62. package/lib/cjs/agent/utils/target.d.ts +15 -0
  63. package/lib/cjs/agent/utils/target.d.ts.map +1 -0
  64. package/lib/cjs/agent/utils/target.js +27 -0
  65. package/lib/cjs/agent/utils/target.js.map +1 -0
  66. package/lib/cjs/candid/utils/buffer.d.ts +2 -1
  67. package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
  68. package/lib/cjs/candid/utils/buffer.js +5 -2
  69. package/lib/cjs/candid/utils/buffer.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 +71 -10
  104. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  105. package/lib/esm/agent/agent/api.js +1 -0
  106. package/lib/esm/agent/agent/api.js.map +1 -1
  107. package/lib/esm/agent/agent/http/index.d.ts +9 -12
  108. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  109. package/lib/esm/agent/agent/http/index.js +218 -170
  110. package/lib/esm/agent/agent/http/index.js.map +1 -1
  111. package/lib/esm/agent/auth.d.ts +6 -5
  112. package/lib/esm/agent/auth.d.ts.map +1 -1
  113. package/lib/esm/agent/auth.js +1 -1
  114. package/lib/esm/agent/auth.js.map +1 -1
  115. package/lib/esm/agent/canister-env/index.js +1 -1
  116. package/lib/esm/agent/canister-env/index.js.map +1 -1
  117. package/lib/esm/agent/canisterStatus/index.d.ts +2 -3
  118. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  119. package/lib/esm/agent/canisterStatus/index.js +66 -122
  120. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  121. package/lib/esm/agent/cbor.d.ts +2 -1
  122. package/lib/esm/agent/cbor.d.ts.map +1 -1
  123. package/lib/esm/agent/cbor.js +4 -1
  124. package/lib/esm/agent/cbor.js.map +1 -1
  125. package/lib/esm/agent/certificate.d.ts +6 -5
  126. package/lib/esm/agent/certificate.d.ts.map +1 -1
  127. package/lib/esm/agent/certificate.js +5 -9
  128. package/lib/esm/agent/certificate.js.map +1 -1
  129. package/lib/esm/agent/der.d.ts +3 -2
  130. package/lib/esm/agent/der.d.ts.map +1 -1
  131. package/lib/esm/agent/der.js.map +1 -1
  132. package/lib/esm/agent/errors.d.ts +7 -1
  133. package/lib/esm/agent/errors.d.ts.map +1 -1
  134. package/lib/esm/agent/errors.js +18 -1
  135. package/lib/esm/agent/errors.js.map +1 -1
  136. package/lib/esm/agent/polling/index.d.ts +7 -7
  137. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  138. package/lib/esm/agent/polling/index.js +19 -20
  139. package/lib/esm/agent/polling/index.js.map +1 -1
  140. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  141. package/lib/esm/agent/polling/strategy.js +6 -6
  142. package/lib/esm/agent/polling/strategy.js.map +1 -1
  143. package/lib/esm/agent/polling/types.d.ts +3 -4
  144. package/lib/esm/agent/polling/types.d.ts.map +1 -1
  145. package/lib/esm/agent/public_key.d.ts +3 -2
  146. package/lib/esm/agent/public_key.d.ts.map +1 -1
  147. package/lib/esm/agent/public_key.js +2 -2
  148. package/lib/esm/agent/public_key.js.map +1 -1
  149. package/lib/esm/agent/request_id.js +2 -2
  150. package/lib/esm/agent/request_id.js.map +1 -1
  151. package/lib/esm/agent/subnetStatus/index.d.ts +3 -2
  152. package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
  153. package/lib/esm/agent/subnetStatus/index.js +63 -86
  154. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  155. package/lib/esm/agent/utils/bls.d.ts.map +1 -1
  156. package/lib/esm/agent/utils/bls.js +7 -6
  157. package/lib/esm/agent/utils/bls.js.map +1 -1
  158. package/lib/esm/agent/utils/readState.d.ts +104 -15
  159. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  160. package/lib/esm/agent/utils/readState.js +103 -16
  161. package/lib/esm/agent/utils/readState.js.map +1 -1
  162. package/lib/esm/agent/utils/target.d.ts +15 -0
  163. package/lib/esm/agent/utils/target.d.ts.map +1 -0
  164. package/lib/esm/agent/utils/target.js +23 -0
  165. package/lib/esm/agent/utils/target.js.map +1 -0
  166. package/lib/esm/candid/utils/buffer.d.ts +2 -1
  167. package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
  168. package/lib/esm/candid/utils/buffer.js +5 -2
  169. package/lib/esm/candid/utils/buffer.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 -310
  202. package/src/agent/agent/http/expiry.ts +0 -107
  203. package/src/agent/agent/http/index.ts +0 -1724
  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 -91
  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,24 +0,0 @@
1
- // Default delta for ingress expiry is 5 minutes.
2
- export const DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS = 5 * 60 * 1000;
3
-
4
- /**
5
- * The `\x0Dic-state-root` domain separator used in the root hash of IC certificates.
6
- */
7
- export const IC_STATE_ROOT_DOMAIN_SEPARATOR = new TextEncoder().encode('\x0Dic-state-root');
8
-
9
- /**
10
- * The `\x0Aic-request` domain separator used in the signature of IC requests.
11
- */
12
- export const IC_REQUEST_DOMAIN_SEPARATOR = new TextEncoder().encode('\x0Aic-request');
13
-
14
- /**
15
- * The `\x0Bic-response` domain separator used in the signature of IC responses.
16
- */
17
- export const IC_RESPONSE_DOMAIN_SEPARATOR = new TextEncoder().encode('\x0Bic-response');
18
-
19
- /**
20
- * The `\x1Aic-request-auth-delegation` domain separator used in the signature of delegations.
21
- */
22
- export const IC_REQUEST_AUTH_DELEGATION_DOMAIN_SEPARATOR = new TextEncoder().encode(
23
- '\x1Aic-request-auth-delegation',
24
- );
package/src/agent/der.ts DELETED
@@ -1,191 +0,0 @@
1
- import {
2
- DerDecodeErrorCode,
3
- DerDecodeLengthMismatchErrorCode,
4
- DerEncodeErrorCode,
5
- InputError,
6
- } from './errors.ts';
7
- import { uint8Equals } from './utils/buffer.ts';
8
-
9
- export const encodeLenBytes = (len: number): number => {
10
- if (len <= 0x7f) {
11
- return 1;
12
- }
13
- if (len <= 0xff) {
14
- return 2;
15
- }
16
- if (len <= 0xffff) {
17
- return 3;
18
- }
19
- if (len <= 0xffffff) {
20
- return 4;
21
- }
22
- throw InputError.fromCode(new DerEncodeErrorCode('Length too long (> 4 bytes)'));
23
- };
24
-
25
- export const encodeLen = (buf: Uint8Array, offset: number, len: number): number => {
26
- if (len <= 0x7f) {
27
- buf[offset] = len;
28
- return 1;
29
- }
30
- if (len <= 0xff) {
31
- buf[offset] = 0x81;
32
- buf[offset + 1] = len;
33
- return 2;
34
- }
35
- if (len <= 0xffff) {
36
- buf[offset] = 0x82;
37
- buf[offset + 1] = len >> 8;
38
- buf[offset + 2] = len;
39
- return 3;
40
- }
41
- if (len <= 0xffffff) {
42
- buf[offset] = 0x83;
43
- buf[offset + 1] = len >> 16;
44
- buf[offset + 2] = len >> 8;
45
- buf[offset + 3] = len;
46
- return 4;
47
- }
48
- throw InputError.fromCode(new DerEncodeErrorCode('Length too long (> 4 bytes)'));
49
- };
50
-
51
- export const decodeLenBytes = (buf: Uint8Array, offset: number): number => {
52
- if (buf[offset] < 0x80) {
53
- return 1;
54
- }
55
- if (buf[offset] === 0x80) {
56
- throw InputError.fromCode(new DerDecodeErrorCode('Invalid length 0'));
57
- }
58
- if (buf[offset] === 0x81) {
59
- return 2;
60
- }
61
- if (buf[offset] === 0x82) {
62
- return 3;
63
- }
64
- if (buf[offset] === 0x83) {
65
- return 4;
66
- }
67
- throw InputError.fromCode(new DerDecodeErrorCode('Length too long (> 4 bytes)'));
68
- };
69
-
70
- export const decodeLen = (buf: Uint8Array, offset: number): number => {
71
- const lenBytes = decodeLenBytes(buf, offset);
72
- if (lenBytes === 1) {
73
- return buf[offset];
74
- }
75
- if (lenBytes === 2) {
76
- return buf[offset + 1];
77
- }
78
- if (lenBytes === 3) {
79
- return (buf[offset + 1] << 8) + buf[offset + 2];
80
- }
81
- if (lenBytes === 4) {
82
- return (buf[offset + 1] << 16) + (buf[offset + 2] << 8) + buf[offset + 3];
83
- }
84
- throw InputError.fromCode(new DerDecodeErrorCode('Length too long (> 4 bytes)'));
85
- };
86
-
87
- /**
88
- * A DER encoded `SEQUENCE(OID)` for DER-encoded-COSE
89
- */
90
- export const DER_COSE_OID = Uint8Array.from([
91
- ...[0x30, 0x0c], // SEQUENCE
92
- ...[0x06, 0x0a], // OID with 10 bytes
93
- ...[0x2b, 0x06, 0x01, 0x04, 0x01, 0x83, 0xb8, 0x43, 0x01, 0x01], // DER encoded COSE
94
- ]);
95
-
96
- /**
97
- * A DER encoded `SEQUENCE(OID)` for the Ed25519 algorithm
98
- */
99
- export const ED25519_OID = Uint8Array.from([
100
- ...[0x30, 0x05], // SEQUENCE
101
- ...[0x06, 0x03], // OID with 3 bytes
102
- ...[0x2b, 0x65, 0x70], // id-Ed25519 OID
103
- ]);
104
-
105
- /**
106
- * A DER encoded `SEQUENCE(OID)` for secp256k1 with the ECDSA algorithm
107
- */
108
- export const SECP256K1_OID = Uint8Array.from([
109
- ...[0x30, 0x10], // SEQUENCE
110
- ...[0x06, 0x07], // OID with 7 bytes
111
- ...[0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01], // OID ECDSA
112
- ...[0x06, 0x05], // OID with 5 bytes
113
- ...[0x2b, 0x81, 0x04, 0x00, 0x0a], // OID secp256k1
114
- ]);
115
-
116
- export const BLS12_381_G2_OID = Uint8Array.from([
117
- ...[0x30, 0x1d], // SEQUENCE, length 29 bytes
118
- // Algorithm OID
119
- ...[0x06, 0x0d],
120
- ...[0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xdc, 0x7c, 0x05, 0x03, 0x01, 0x02, 0x01],
121
- // Curve OID
122
- ...[0x06, 0x0c],
123
- ...[0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xdc, 0x7c, 0x05, 0x03, 0x02, 0x01],
124
- ]);
125
-
126
- /**
127
- * Wraps the given `payload` in a DER encoding tagged with the given encoded `oid` like so:
128
- * `SEQUENCE(oid, BITSTRING(payload))`
129
- * @param payload The payload to encode as the bit string
130
- * @param oid The DER encoded (and SEQUENCE wrapped!) OID to tag the payload with
131
- */
132
- export function wrapDER(payload: Uint8Array, oid: Uint8Array): Uint8Array {
133
- // The Bit String header needs to include the unused bit count byte in its length
134
- const bitStringHeaderLength = 2 + encodeLenBytes(payload.byteLength + 1);
135
- const len = oid.byteLength + bitStringHeaderLength + payload.byteLength;
136
- let offset = 0;
137
- const buf = new Uint8Array(1 + encodeLenBytes(len) + len);
138
- // Sequence
139
- buf[offset++] = 0x30;
140
- // Sequence Length
141
- offset += encodeLen(buf, offset, len);
142
-
143
- // OID
144
- buf.set(oid, offset);
145
- offset += oid.byteLength;
146
-
147
- // Bit String Header
148
- buf[offset++] = 0x03;
149
- offset += encodeLen(buf, offset, payload.byteLength + 1);
150
- // 0 padding
151
- buf[offset++] = 0x00;
152
- buf.set(new Uint8Array(payload), offset);
153
-
154
- return buf;
155
- }
156
-
157
- /**
158
- * Extracts a payload from the given `derEncoded` data, and checks that it was tagged with the given `oid`.
159
- *
160
- * `derEncoded = SEQUENCE(oid, BITSTRING(payload))`
161
- * @param derEncoded The DER encoded and tagged data
162
- * @param oid The DER encoded (and SEQUENCE wrapped!) expected OID
163
- * @returns The unwrapped payload
164
- */
165
- export const unwrapDER = (derEncoded: Uint8Array, oid: Uint8Array): Uint8Array => {
166
- let offset = 0;
167
- const expect = (n: number, msg: string) => {
168
- if (buf[offset++] !== n) {
169
- throw InputError.fromCode(new DerDecodeErrorCode(`Expected ${msg} at offset ${offset}`));
170
- }
171
- };
172
-
173
- const buf = new Uint8Array(derEncoded);
174
- expect(0x30, 'sequence');
175
- offset += decodeLenBytes(buf, offset);
176
-
177
- if (!uint8Equals(buf.slice(offset, offset + oid.byteLength), oid)) {
178
- throw InputError.fromCode(new DerDecodeErrorCode('Not the expected OID.'));
179
- }
180
- offset += oid.byteLength;
181
-
182
- expect(0x03, 'bit string');
183
- const payloadLen = decodeLen(buf, offset) - 1; // Subtracting 1 to account for the 0 padding
184
- offset += decodeLenBytes(buf, offset);
185
- expect(0x00, '0 padding');
186
- const result = buf.slice(offset);
187
- if (payloadLen !== result.length) {
188
- throw InputError.fromCode(new DerDecodeLengthMismatchErrorCode(payloadLen, result.length));
189
- }
190
- return result;
191
- };