Skip to content

Commit 8d4d92b

Browse files
lmuntanerCopilot
andauthored
Use icp-sdk instead of @dfinity npm module (#3380)
* Use icp-sdk instead of @dfinity npm module * Update demos/test-app/src/index.tsx Co-authored-by: Copilot <[email protected]> * Fix type issue * Update package json for test-app * Update signer-js version and cast * Revert changes package-lock * Fix toJSON issue with Identity library * Remove unnecessary comment * Improve logging * Revert import in declarations --------- Co-authored-by: Copilot <[email protected]>
1 parent 6ccd39e commit 8d4d92b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+173
-151
lines changed

demos/test-app/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
"type": "module",
44
"private": true,
55
"dependencies": {
6-
"@dfinity/agent": "*",
7-
"@dfinity/candid": "*",
8-
"@dfinity/identity": "*",
9-
"@dfinity/principal": "*",
10-
"@slide-computer/signer": "^3.20.0",
11-
"@slide-computer/signer-web": "^3.20.0",
6+
"@icp-sdk/core": "*",
7+
"@slide-computer/signer": "*",
8+
"@slide-computer/signer-web": "*",
9+
"@noble/hashes": "*",
1210
"buffer": "^6.0.3",
1311
"jose": "^5.1.2",
1412
"react": "^18.2.0",

demos/test-app/src/auth.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { SignIdentity, Signature } from "@dfinity/agent";
1+
import type { SignIdentity, Signature } from "@icp-sdk/core/agent";
22
import {
33
Delegation,
44
DelegationChain,
55
DelegationIdentity,
66
SignedDelegation,
7-
} from "@dfinity/identity";
8-
import { Principal } from "@dfinity/principal";
7+
} from "@icp-sdk/core/identity";
8+
import { Principal } from "@icp-sdk/core/principal";
99
import { Signer } from "@slide-computer/signer";
1010
import { PostMessageTransport } from "@slide-computer/signer-web";
1111

@@ -46,10 +46,12 @@ export const authWithII = async ({
4646
if (useIcrc25) {
4747
const transport = new PostMessageTransport({ url: url_ });
4848
const signer = new Signer({ transport, derivationOrigin });
49-
const delegation = await signer.delegation({
49+
// We need to cast the delegation from signer-js to avoid a TS issue because one type is imported from cjs and another esm:
50+
// Types of property 'delegations' are incompatible.
51+
const delegation = (await signer.delegation({
5052
maxTimeToLive,
5153
publicKey: sessionIdentity.getPublicKey().toDer(),
52-
});
54+
})) as unknown as DelegationChain;
5355
return {
5456
identity: DelegationIdentity.fromDelegation(sessionIdentity, delegation),
5557
authnMethod: "passkey",
@@ -138,7 +140,7 @@ const identityFromResponse = ({
138140

139141
const delegationChain = DelegationChain.fromDelegations(
140142
delegations,
141-
response.userPublicKey.buffer,
143+
response.userPublicKey,
142144
);
143145

144146
const identity = DelegationIdentity.fromDelegation(

demos/test-app/src/index.tsx

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { VcFlowRequestWire } from "@dfinity/internet-identity-vc-api";
2-
3-
import type { Identity, SignIdentity } from "@dfinity/agent";
4-
import { Actor, HttpAgent } from "@dfinity/agent";
2+
import { bytesToHex } from "@noble/hashes/utils";
3+
import type { Identity, SignIdentity } from "@icp-sdk/core/agent";
4+
import { Actor, HttpAgent } from "@icp-sdk/core/agent";
55
import {
66
DelegationChain,
77
DelegationIdentity,
88
Ed25519KeyIdentity,
9-
} from "@dfinity/identity";
10-
import { Principal } from "@dfinity/principal";
9+
} from "@icp-sdk/core/identity";
10+
import { Principal } from "@icp-sdk/core/principal";
1111

1212
import React, { useState } from "react";
1313
import ReactDOM from "react-dom/client";
@@ -134,11 +134,38 @@ const updateDelegationView = ({
134134
}
135135

136136
if (identity instanceof DelegationIdentity) {
137-
delegationEl.innerText = JSON.stringify(
138-
identity.getDelegation().toJSON(),
139-
undefined,
140-
2,
141-
);
137+
const delegation = identity.getDelegation();
138+
let jsonResult;
139+
try {
140+
// `toJSON` is failing due to `bytesToHex(delegation.publicKey)`
141+
// `bytesToHex` expects a Uint8Array and delegation.publicKey is a ArrayBuffer
142+
// jsonResult = delegation.toJSON();
143+
jsonResult = {
144+
delegations: delegation.delegations.map((signedDelegation) => {
145+
const { delegation, signature } = signedDelegation;
146+
const { targets } = delegation;
147+
return {
148+
delegation: {
149+
expiration: delegation.expiration.toString(16),
150+
pubkey: bytesToHex(delegation.pubkey),
151+
...(targets && {
152+
targets: targets.map((t) => t.toHex()),
153+
}),
154+
},
155+
signature: bytesToHex(new Uint8Array(signature)),
156+
};
157+
}),
158+
publicKey: bytesToHex(new Uint8Array(delegation.publicKey)),
159+
};
160+
} catch (error) {
161+
console.error("toJSON error:", error);
162+
jsonResult = {
163+
error: "toJSON failed",
164+
message: error instanceof Error ? error.message : String(error),
165+
};
166+
}
167+
168+
delegationEl.innerText = JSON.stringify(jsonResult, undefined, 2);
142169

143170
// cannot use Math.min, as we deal with bigint here
144171
const nextExpiration = identity
@@ -219,7 +246,7 @@ const readCanisterId = (): string => {
219246
const init = async () => {
220247
signInBtn.onclick = async () => {
221248
const maxTimeToLive_ = BigInt(maxTimeToLiveEl.value);
222-
// The default max TTL setin the @dfinity/auth-client library
249+
// The default max TTL set in the @icp-sdk/auth/client library
223250
const authClientDefaultMaxTTL =
224251
/* hours */ BigInt(8) * /* nanoseconds */ BigInt(3_600_000_000_000);
225252
const maxTimeToLive =

demos/using-dev-build/webapp/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* then calls the whoami canister to check the user's principal.
33
*/
44

5-
import { Actor, ActorMethod, HttpAgent } from "@dfinity/agent";
6-
import type { Principal } from "@dfinity/principal";
5+
import { Actor, ActorMethod, HttpAgent } from "@icp-sdk/core/agent";
6+
import type { Principal } from "@icp-sdk/core/principal";
77
import { AuthClient } from "@icp-sdk/auth/client";
88

99
const webapp_id = process.env.WHOAMI_CANISTER_ID;

demos/vc_issuer/app/generated/vc_issuer_types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Principal } from '@dfinity/principal';
2-
import type { ActorMethod } from '@dfinity/agent';
3-
import type { IDL } from '@dfinity/candid';
1+
import type { Principal } from '@icp-sdk/core/principal';
2+
import type { ActorMethod } from '@icp-sdk/core/agent';
3+
import type { IDL } from '@icp-sdk/core/candid';
44

55
export type ArgumentValue = { 'Int' : number } |
66
{ 'String' : string };

demos/vc_issuer/app/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AuthClient } from "@dfinity/auth-client";
1+
import { AuthClient } from "@icp-sdk/auth/client";
22

33
import React, { useState } from "react";
44
import ReactDOM from "react-dom/client";

demos/vc_issuer/app/issuer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Actor, ActorSubclass, HttpAgent } from "@dfinity/agent";
2-
import { Principal } from "@dfinity/principal";
1+
import { Actor, ActorSubclass, HttpAgent } from "@icp-sdk/core/agent";
2+
import { Principal } from "@icp-sdk/core/principal";
33

44
import { idlFactory as vc_issuer_idl } from "./generated/vc_issuer_idl";
55
import { _SERVICE } from "./generated/vc_issuer_types";

demos/vc_issuer/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
"private": true,
55
"license": "SEE LICENSE IN LICENSE.md",
66
"dependencies": {
7-
"@dfinity/agent": "*",
8-
"@dfinity/auth-client": "*",
9-
"@dfinity/candid": "*",
10-
"@dfinity/identity": "*",
11-
"@dfinity/principal": "*",
127
"@dfinity/utils": "*",
8+
"@icp-sdk/auth": "*",
9+
"@icp-sdk/core": "^4.0.4",
1310
"buffer": "^6.0.3",
1411
"react": "^18.2.0",
1512
"react-dom": "^18.2.0"

docs/ii-spec.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ sequenceDiagram
221221

222222
The client application frontend needs to be able to detect when any of the delegations in the chain has expired, and re-authorize the user in that case.
223223

224-
The [`@dfinity/auth-client`](https://www.npmjs.com/package/@dfinity/auth-client) NPM package provides helpful functionality here.
224+
The [`@icp-sdk/auth`](https://www.npmjs.com/package/@icp-sdk/auth) NPM package provides helpful functionality here.
225225

226226
The client application frontend should support delegation chains of length more than one, and delegations with `targets`, even if the present version of this spec does not use them, to be compatible with possible future versions.
227227

package-lock.json

Lines changed: 33 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)