@icp-sdk/core 5.2.0 → 5.3.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 (53) hide show
  1. package/lib/cjs/agent/agent/http/index.d.ts +1 -19
  2. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  3. package/lib/cjs/agent/agent/http/index.js +15 -24
  4. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  5. package/lib/cjs/agent/agent/http/types.d.ts +18 -0
  6. package/lib/cjs/agent/agent/http/types.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/http/types.js +20 -1
  8. package/lib/cjs/agent/agent/http/types.js.map +1 -1
  9. package/lib/cjs/agent/errors.d.ts +11 -2
  10. package/lib/cjs/agent/errors.d.ts.map +1 -1
  11. package/lib/cjs/agent/errors.js +20 -2
  12. package/lib/cjs/agent/errors.js.map +1 -1
  13. package/lib/cjs/agent/polling/index.js +8 -8
  14. package/lib/cjs/agent/polling/types.d.ts +1 -1
  15. package/lib/cjs/identity/identity/attributes.d.ts +58 -0
  16. package/lib/cjs/identity/identity/attributes.d.ts.map +1 -0
  17. package/lib/cjs/identity/identity/attributes.js +56 -0
  18. package/lib/cjs/identity/identity/attributes.js.map +1 -0
  19. package/lib/cjs/identity/index.d.ts +5 -0
  20. package/lib/cjs/identity/index.d.ts.map +1 -1
  21. package/lib/cjs/identity/index.js +5 -0
  22. package/lib/cjs/identity/index.js.map +1 -1
  23. package/lib/esm/agent/agent/http/index.d.ts +1 -19
  24. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  25. package/lib/esm/agent/agent/http/index.js +14 -24
  26. package/lib/esm/agent/agent/http/index.js.map +1 -1
  27. package/lib/esm/agent/agent/http/types.d.ts +18 -0
  28. package/lib/esm/agent/agent/http/types.d.ts.map +1 -1
  29. package/lib/esm/agent/agent/http/types.js +19 -0
  30. package/lib/esm/agent/agent/http/types.js.map +1 -1
  31. package/lib/esm/agent/errors.d.ts +11 -2
  32. package/lib/esm/agent/errors.d.ts.map +1 -1
  33. package/lib/esm/agent/errors.js +17 -0
  34. package/lib/esm/agent/errors.js.map +1 -1
  35. package/lib/esm/agent/polling/index.js +1 -1
  36. package/lib/esm/agent/polling/types.d.ts +1 -1
  37. package/lib/esm/identity/identity/attributes.d.ts +58 -0
  38. package/lib/esm/identity/identity/attributes.d.ts.map +1 -0
  39. package/lib/esm/identity/identity/attributes.js +52 -0
  40. package/lib/esm/identity/identity/attributes.js.map +1 -0
  41. package/lib/esm/identity/index.d.ts +5 -0
  42. package/lib/esm/identity/index.d.ts.map +1 -1
  43. package/lib/esm/identity/index.js +5 -0
  44. package/lib/esm/identity/index.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/agent/agent/http/index.ts +25 -23
  47. package/src/agent/agent/http/types.ts +19 -0
  48. package/src/agent/errors.ts +31 -2
  49. package/src/agent/polling/index.ts +1 -1
  50. package/src/agent/polling/strategy.ts +1 -1
  51. package/src/agent/polling/types.ts +1 -1
  52. package/src/identity/identity/attributes.ts +84 -0
  53. package/src/identity/index.ts +5 -0
@@ -19,6 +19,10 @@
19
19
  *
20
20
  * The `PartialIdentity` is not typically used directly, but is used by the `DelegationIdentity` and `WebAuthnIdentity` classes.
21
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
+ *
22
26
  * ### Secp256k1KeyIdentity
23
27
  *
24
28
  * See [@icp-sdk/core/identity/secp256k1](https://js.icp.build/core/latest/libs/identity-secp256k1/api/) for more information.
@@ -34,6 +38,7 @@ export * from './identity/ed25519.ts';
34
38
  export * from './identity/ecdsa.ts';
35
39
  export * from './identity/delegation.ts';
36
40
  export * from './identity/partial.ts';
41
+ export * from './identity/attributes.ts';
37
42
  export { WebAuthnIdentity } from './identity/webauthn.ts';
38
43
  export { wrapDER, unwrapDER, DER_COSE_OID, ED25519_OID } from '../agent/index.js';
39
44
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/identity/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/identity/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC"}
@@ -19,6 +19,10 @@
19
19
  *
20
20
  * The `PartialIdentity` is not typically used directly, but is used by the `DelegationIdentity` and `WebAuthnIdentity` classes.
21
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
+ *
22
26
  * ### Secp256k1KeyIdentity
23
27
  *
24
28
  * See [@icp-sdk/core/identity/secp256k1](https://js.icp.build/core/latest/libs/identity-secp256k1/api/) for more information.
@@ -34,6 +38,7 @@ export * from "./identity/ed25519.js";
34
38
  export * from "./identity/ecdsa.js";
35
39
  export * from "./identity/delegation.js";
36
40
  export * from "./identity/partial.js";
41
+ export * from "./identity/attributes.js";
37
42
  export { WebAuthnIdentity } from "./identity/webauthn.js";
38
43
  export { wrapDER, unwrapDER, DER_COSE_OID, ED25519_OID } from '../agent/index.js';
39
44
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/identity/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/identity/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icp-sdk/core",
3
- "version": "5.2.0",
3
+ "version": "5.3.0",
4
4
  "author": "DFINITY Stiftung <sdk@dfinity.org>",
5
5
  "license": "Apache-2.0",
6
6
  "description": "JavaScript and TypeScript library to interact with the Internet Computer",
@@ -93,7 +93,7 @@
93
93
  },
94
94
  "dependencies": {
95
95
  "@dfinity/cbor": "^0.2.2",
96
- "@noble/curves": "^1.9.2",
96
+ "@noble/curves": "^1.9.7",
97
97
  "@noble/hashes": "^1.8.0",
98
98
  "@scure/bip32": "^1.7.0",
99
99
  "@scure/bip39": "^1.6.0",
@@ -20,6 +20,7 @@ import {
20
20
  TrustError,
21
21
  UncertifiedRejectUpdateErrorCode,
22
22
  UnexpectedErrorCode,
23
+ UnexpectedV4StatusErrorCode,
23
24
  UnknownError,
24
25
  HttpErrorCode,
25
26
  HttpV4ApiNotSupportedErrorCode,
@@ -91,28 +92,12 @@ import {
91
92
  } from '../../polling/backoff.ts';
92
93
  import { decodeTime } from '../../utils/leb.ts';
93
94
  import { pollForResponse, type PollingOptions } from '../../polling/index.ts';
94
- import { concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
95
+ import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
95
96
  import { uint8Equals, uint8FromBufLike } from '../../utils/buffer.ts';
96
97
  import { IC_RESPONSE_DOMAIN_SEPARATOR } from '../../constants.ts';
97
98
 
98
- /**
99
- * Possible values for the request status in the IC state tree.
100
- * @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-request-status
101
- */
102
- export enum RequestStatusResponseStatus {
103
- /** The call has made it past the endpoint into the IC's state. */
104
- Received = 'received',
105
- /** The initial effect of the call has happened or will happen. */
106
- Processing = 'processing',
107
- /** The call completed successfully; the reply is available at `/request_status/<id>/reply`. */
108
- Replied = 'replied',
109
- /** The call failed; reject code and message are available at `/request_status/<id>/reject_code` and `/request_status/<id>/reject_message`. */
110
- Rejected = 'rejected',
111
- /** The request status path is absent from the state tree, the request is unknown to the IC (never received or pruned after expiry). */
112
- Unknown = 'unknown',
113
- /** The IC has pruned the response data but remembers the request to prevent replay attacks. */
114
- Done = 'done',
115
- }
99
+ import { RequestStatusResponseStatus } from './types.ts';
100
+ export { RequestStatusResponseStatus } from './types.ts';
116
101
 
117
102
  const MINUTE_TO_MSECS = 60 * 1_000;
118
103
  const MSECS_TO_NANOSECONDS = 1_000_000;
@@ -762,16 +747,33 @@ export class HttpAgent implements Agent {
762
747
  };
763
748
  throw RejectError.fromCode(error);
764
749
  }
750
+ case undefined: {
751
+ // The certificate does not contain an entry for our request ID status.
752
+ // Fall back to polling via read state requests.
753
+ this.log.warn(
754
+ `v4 sync response certificate does not contain request ID ${bytesToHex(requestId)} status. Falling back to polling.`,
755
+ );
756
+ const pollResult = await pollForResponse(
757
+ this,
758
+ effectiveCanisterId,
759
+ requestId,
760
+ pollingOptions,
761
+ );
762
+ return { ...pollResult, requestDetails, callResponse: response };
763
+ }
765
764
  default: {
766
- const unexpectedErrorCode = new UnexpectedErrorCode(
767
- `Unexpected request status in v4 sync response: "${status}"`,
765
+ const errorCode = new UnexpectedV4StatusErrorCode(
766
+ status,
767
+ requestId,
768
+ response,
769
+ rawCertificate,
768
770
  );
769
- unexpectedErrorCode.callContext = {
771
+ errorCode.callContext = {
770
772
  canisterId: effectiveCanisterId,
771
773
  methodName,
772
774
  httpDetails,
773
775
  };
774
- throw UnknownError.fromCode(unexpectedErrorCode);
776
+ throw UnknownError.fromCode(errorCode);
775
777
  }
776
778
  }
777
779
  }
@@ -102,6 +102,25 @@ export interface ReadStateRequest extends Record<string, any> {
102
102
 
103
103
  export type ReadRequest = QueryRequest | ReadStateRequest;
104
104
 
105
+ /**
106
+ * Possible values for the request status in the IC state tree.
107
+ * @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-request-status
108
+ */
109
+ export enum RequestStatusResponseStatus {
110
+ /** The call has made it past the endpoint into the IC's state. */
111
+ Received = 'received',
112
+ /** The initial effect of the call has happened or will happen. */
113
+ Processing = 'processing',
114
+ /** The call completed successfully; the reply is available at `/request_status/<id>/reply`. */
115
+ Replied = 'replied',
116
+ /** The call failed; reject code and message are available at `/request_status/<id>/reject_code` and `/request_status/<id>/reject_message`. */
117
+ Rejected = 'rejected',
118
+ /** The request status path is absent from the state tree, the request is unknown to the IC (never received or pruned after expiry). */
119
+ Unknown = 'unknown',
120
+ /** The IC has pruned the response data but remembers the request to prevent replay attacks. */
121
+ Done = 'done',
122
+ }
123
+
105
124
  // A Nonce that can be used for calls.
106
125
  export type Nonce = Uint8Array & { __nonce__: void };
107
126
 
@@ -1,7 +1,12 @@
1
1
  import type { Principal } from '#principal';
2
- import type { HttpDetailsResponse, NodeSignature, ReplicaRejectCode } from './agent/api.ts';
2
+ import type {
3
+ HttpDetailsResponse,
4
+ NodeSignature,
5
+ ReplicaRejectCode,
6
+ SubmitResponse,
7
+ } from './agent/api.ts';
3
8
  import type { RequestId } from './request_id.ts';
4
- import type { RequestStatusResponseStatus } from './agent/http/index.ts';
9
+ import type { RequestStatusResponseStatus } from './agent/http/types.ts';
5
10
  import type { Expiry } from './agent/http/expiry.ts';
6
11
  import type { HttpHeaderField } from './agent/http/types.ts';
7
12
  import type { LookupPathStatus, LookupSubtreeStatus } from './certificate.ts';
@@ -769,6 +774,30 @@ export class UnexpectedErrorCode extends ErrorCode {
769
774
  }
770
775
  }
771
776
 
777
+ export class UnexpectedV4StatusErrorCode extends ErrorCode {
778
+ public name = 'UnexpectedV4StatusErrorCode';
779
+
780
+ constructor(
781
+ public readonly status: string | undefined,
782
+ public readonly requestId: RequestId,
783
+ public readonly response: SubmitResponse['response'],
784
+ public readonly rawCertificate: Uint8Array,
785
+ ) {
786
+ super();
787
+ Object.setPrototypeOf(this, UnexpectedV4StatusErrorCode.prototype);
788
+ }
789
+
790
+ public toErrorMessage(): string {
791
+ const replacer = (_k: string, v: unknown) =>
792
+ v instanceof Uint8Array ? `hex(${v.length}):${bytesToHex(v)}` : v;
793
+ return (
794
+ `Unexpected request status in v4 sync response: "${this.status}"\n` +
795
+ ` Request ID (hex): ${bytesToHex(this.requestId)}\n` +
796
+ ` Response: ${JSON.stringify(this.response, replacer, 2)}`
797
+ );
798
+ }
799
+ }
800
+
772
801
  export class HashTreeDecodeErrorCode extends ErrorCode {
773
802
  public name = 'HashTreeDecodeErrorCode';
774
803
 
@@ -22,7 +22,7 @@ export * as strategy from './strategy.ts';
22
22
  import { defaultStrategy } from './strategy.ts';
23
23
  import type { PollStrategy, PollForResponseResult } from './types.ts';
24
24
  import { ReadRequestType, type ReadStateRequest } from '../agent/http/types.ts';
25
- import { RequestStatusResponseStatus } from '../agent/http/index.ts';
25
+ import { RequestStatusResponseStatus } from '../agent/http/types.ts';
26
26
  import { utf8ToBytes } from '@noble/hashes/utils';
27
27
  export { defaultStrategy } from './strategy.ts';
28
28
  export type { PollStrategy, PollForResponseResult } from './types.ts';
@@ -1,5 +1,5 @@
1
1
  import type { Principal } from '#principal';
2
- import type { RequestStatusResponseStatus } from '../agent/http/index.ts';
2
+ import type { RequestStatusResponseStatus } from '../agent/http/types.ts';
3
3
  import type { RequestId } from '../request_id.ts';
4
4
  import { ProtocolError, TimeoutWaitingForResponseErrorCode } from '../errors.ts';
5
5
 
@@ -1,6 +1,6 @@
1
1
  import type { Certificate } from '../certificate.ts';
2
2
  import type { Principal } from '#principal';
3
- import type { RequestStatusResponseStatus } from '../agent/http/index.ts';
3
+ import type { RequestStatusResponseStatus } from '../agent/http/types.ts';
4
4
  import type { RequestId } from '../request_id.ts';
5
5
 
6
6
  export type PollStrategy = (
@@ -0,0 +1,84 @@
1
+ import type { Identity, 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.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const inner = DelegationIdentity.fromDelegation(key, chain);
42
+ * const identity = new AttributesIdentity({
43
+ * inner,
44
+ * attributes: { data: new Uint8Array([...]), signature: new Uint8Array([...]) },
45
+ * signer: { canisterId: Principal.fromText('aaaaa-aa') },
46
+ * });
47
+ * const agent = HttpAgent.create({ identity });
48
+ * ```
49
+ */
50
+ export class AttributesIdentity implements Identity {
51
+ readonly #inner: Identity;
52
+ readonly #attributes: SignedAttributes;
53
+ readonly #signer: Signer;
54
+
55
+ /**
56
+ * @param options - Configuration for the identity.
57
+ * @param options.inner - The inner identity to delegate signing to.
58
+ * @param options.attributes - The signed attributes to include in the request.
59
+ * @param options.signer - The canister that signed the attributes.
60
+ */
61
+ constructor(options: AttributesIdentityOptions) {
62
+ this.#inner = options.inner;
63
+ this.#attributes = options.attributes;
64
+ this.#signer = options.signer;
65
+ }
66
+
67
+ getPrincipal(): Principal {
68
+ return this.#inner.getPrincipal();
69
+ }
70
+
71
+ transformRequest(request: HttpAgentRequest): Promise<unknown> {
72
+ return this.#inner.transformRequest({
73
+ ...request,
74
+ body: {
75
+ ...request.body,
76
+ sender_info: {
77
+ signer: this.#signer.canisterId.toUint8Array(),
78
+ info: this.#attributes.data,
79
+ sig: this.#attributes.signature,
80
+ },
81
+ },
82
+ } as HttpAgentRequest);
83
+ }
84
+ }
@@ -19,6 +19,10 @@
19
19
  *
20
20
  * The `PartialIdentity` is not typically used directly, but is used by the `DelegationIdentity` and `WebAuthnIdentity` classes.
21
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
+ *
22
26
  * ### Secp256k1KeyIdentity
23
27
  *
24
28
  * See [@icp-sdk/core/identity/secp256k1](https://js.icp.build/core/latest/libs/identity-secp256k1/api/) for more information.
@@ -35,5 +39,6 @@ export * from './identity/ed25519.ts';
35
39
  export * from './identity/ecdsa.ts';
36
40
  export * from './identity/delegation.ts';
37
41
  export * from './identity/partial.ts';
42
+ export * from './identity/attributes.ts';
38
43
  export { WebAuthnIdentity } from './identity/webauthn.ts';
39
44
  export { wrapDER, unwrapDER, DER_COSE_OID, ED25519_OID } from '#agent';