@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,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
- };