@icp-sdk/signer 5.2.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.
- package/LICENSE +190 -0
- package/README.md +165 -0
- package/lib/esm/agent/agent.d.ts +112 -0
- package/lib/esm/agent/agent.js +288 -0
- package/lib/esm/agent/agent.js.map +1 -0
- package/lib/esm/agent/index.d.ts +1 -0
- package/lib/esm/agent/index.js +2 -0
- package/lib/esm/agent/index.js.map +1 -0
- package/lib/esm/extension/browserExtensionChannel.d.ts +37 -0
- package/lib/esm/extension/browserExtensionChannel.js +80 -0
- package/lib/esm/extension/browserExtensionChannel.js.map +1 -0
- package/lib/esm/extension/browserExtensionTransport.d.ts +67 -0
- package/lib/esm/extension/browserExtensionTransport.js +70 -0
- package/lib/esm/extension/browserExtensionTransport.js.map +1 -0
- package/lib/esm/extension/index.d.ts +3 -0
- package/lib/esm/extension/index.js +3 -0
- package/lib/esm/extension/index.js.map +1 -0
- package/lib/esm/extension/types.d.ts +32 -0
- package/lib/esm/extension/types.js +2 -0
- package/lib/esm/extension/types.js.map +1 -0
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/signer.d.ts +180 -0
- package/lib/esm/signer.js +427 -0
- package/lib/esm/signer.js.map +1 -0
- package/lib/esm/transport.d.ts +32 -0
- package/lib/esm/transport.js +11 -0
- package/lib/esm/transport.js.map +1 -0
- package/lib/esm/web/heartbeat/client.d.ts +60 -0
- package/lib/esm/web/heartbeat/client.js +112 -0
- package/lib/esm/web/heartbeat/client.js.map +1 -0
- package/lib/esm/web/heartbeat/server.d.ts +43 -0
- package/lib/esm/web/heartbeat/server.js +82 -0
- package/lib/esm/web/heartbeat/server.js.map +1 -0
- package/lib/esm/web/index.d.ts +4 -0
- package/lib/esm/web/index.js +5 -0
- package/lib/esm/web/index.js.map +1 -0
- package/lib/esm/web/postMessageChannel.d.ts +55 -0
- package/lib/esm/web/postMessageChannel.js +109 -0
- package/lib/esm/web/postMessageChannel.js.map +1 -0
- package/lib/esm/web/postMessageTransport.d.ts +96 -0
- package/lib/esm/web/postMessageTransport.js +113 -0
- package/lib/esm/web/postMessageTransport.js.map +1 -0
- package/package.json +122 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { PublicKey } from '@icp-sdk/core/agent';
|
|
2
|
+
import { DelegationChain } from '@icp-sdk/core/identity';
|
|
3
|
+
import { Principal } from '@icp-sdk/core/principal';
|
|
4
|
+
import type { Channel, JsonRpcError, JsonRpcRequest, JsonRpcResponse, Transport } from './transport.js';
|
|
5
|
+
/**
|
|
6
|
+
* A permission scope identifies a method and optionally additional
|
|
7
|
+
* constraints (e.g. target canister IDs for delegations).
|
|
8
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_25_signer_interaction_standard.md
|
|
9
|
+
*/
|
|
10
|
+
export type PermissionScope = {
|
|
11
|
+
method: string;
|
|
12
|
+
} & Record<string, unknown>;
|
|
13
|
+
/**
|
|
14
|
+
* The state of a permission scope as reported by the signer.
|
|
15
|
+
* - `granted` — the relying party may call the method without further approval.
|
|
16
|
+
* - `denied` — the signer will reject calls to the method.
|
|
17
|
+
* - `ask_on_use` — the signer will prompt the user when the method is called.
|
|
18
|
+
*/
|
|
19
|
+
export type PermissionState = 'denied' | 'ask_on_use' | 'granted';
|
|
20
|
+
/**
|
|
21
|
+
* A standard supported by the signer, as returned by
|
|
22
|
+
* {@link Signer.getSupportedStandards}. The `name` field contains
|
|
23
|
+
* the ICRC standard identifier (e.g. `"ICRC-27"`) and `url` points
|
|
24
|
+
* to the specification.
|
|
25
|
+
*/
|
|
26
|
+
export interface SupportedStandard {
|
|
27
|
+
name: string;
|
|
28
|
+
url: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Error thrown when a signer returns a JSON-RPC error response
|
|
32
|
+
* or when a transport-level failure occurs.
|
|
33
|
+
*/
|
|
34
|
+
export declare class SignerError extends Error {
|
|
35
|
+
/** The JSON-RPC error code. */
|
|
36
|
+
code: number;
|
|
37
|
+
/** Optional additional error data from the signer. */
|
|
38
|
+
data?: JsonRpcError['data'];
|
|
39
|
+
constructor(error: JsonRpcError, options?: ErrorOptions);
|
|
40
|
+
}
|
|
41
|
+
/** Options for creating a {@link Signer} instance. */
|
|
42
|
+
export interface SignerOptions<T extends Transport> {
|
|
43
|
+
/** The transport used to communicate with the signer. */
|
|
44
|
+
transport: T;
|
|
45
|
+
/**
|
|
46
|
+
* Automatically close the transport channel after a response is received.
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
autoCloseTransportChannel?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Delay in milliseconds before auto-closing the transport channel.
|
|
52
|
+
* @default 200
|
|
53
|
+
*/
|
|
54
|
+
closeTransportChannelAfter?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Source of random UUIDs for JSON-RPC request IDs.
|
|
57
|
+
* @default globalThis.crypto
|
|
58
|
+
*/
|
|
59
|
+
crypto?: Pick<Crypto, 'randomUUID'>;
|
|
60
|
+
/**
|
|
61
|
+
* Derivation origin for ICRC-95 identity derivation.
|
|
62
|
+
* When set, all requests include an `icrc95DerivationOrigin` param.
|
|
63
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_95_derivationorigin.md
|
|
64
|
+
*/
|
|
65
|
+
derivationOrigin?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Client for interacting with an ICRC-25 compliant signer.
|
|
69
|
+
*
|
|
70
|
+
* Signers are applications that hold private keys and can sign messages
|
|
71
|
+
* on behalf of a user. They communicate over a {@link Transport} using
|
|
72
|
+
* JSON-RPC 2.0 messages as defined by the ICRC-25 standard.
|
|
73
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_25_signer_interaction_standard.md
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { Signer } from "@icp-sdk/signer";
|
|
77
|
+
* import { PostMessageTransport } from "@icp-sdk/signer/web";
|
|
78
|
+
*
|
|
79
|
+
* const transport = new PostMessageTransport({ url: "https://oisy.com/sign" });
|
|
80
|
+
* const signer = new Signer({ transport });
|
|
81
|
+
*
|
|
82
|
+
* const standards = await signer.getSupportedStandards();
|
|
83
|
+
* const accounts = await signer.getAccounts();
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare class Signer<T extends Transport = Transport> {
|
|
87
|
+
#private;
|
|
88
|
+
constructor(options: SignerOptions<T>);
|
|
89
|
+
/** The transport used to communicate with the signer. */
|
|
90
|
+
get transport(): T;
|
|
91
|
+
/**
|
|
92
|
+
* Opens a communication channel with the signer.
|
|
93
|
+
* Reuses an existing open channel if available.
|
|
94
|
+
*/
|
|
95
|
+
openChannel(): Promise<Channel>;
|
|
96
|
+
/** Closes the current communication channel, if open. */
|
|
97
|
+
closeChannel(): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Sends a JSON-RPC request over the transport channel.
|
|
100
|
+
* @param request - The JSON-RPC request to send.
|
|
101
|
+
*/
|
|
102
|
+
sendRequest(request: JsonRpcRequest): Promise<JsonRpcResponse>;
|
|
103
|
+
/**
|
|
104
|
+
* Queries which ICRC standards the signer supports.
|
|
105
|
+
* Use this to determine signer capabilities before calling other methods.
|
|
106
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_25_signer_interaction_standard.md
|
|
107
|
+
*/
|
|
108
|
+
getSupportedStandards(): Promise<SupportedStandard[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Requests the signer to grant permission for the given scopes.
|
|
111
|
+
* The signer may prompt the user for approval.
|
|
112
|
+
* @param scopes - The permission scopes to request.
|
|
113
|
+
* @returns The current state of each requested scope after the user's decision.
|
|
114
|
+
*/
|
|
115
|
+
requestPermissions(scopes: PermissionScope[]): Promise<Array<{
|
|
116
|
+
scope: PermissionScope;
|
|
117
|
+
state: PermissionState;
|
|
118
|
+
}>>;
|
|
119
|
+
/**
|
|
120
|
+
* Queries the current state of all permission scopes.
|
|
121
|
+
* @returns The current permission state for each scope the signer supports.
|
|
122
|
+
*/
|
|
123
|
+
getPermissions(): Promise<Array<{
|
|
124
|
+
scope: PermissionScope;
|
|
125
|
+
state: PermissionState;
|
|
126
|
+
}>>;
|
|
127
|
+
/**
|
|
128
|
+
* Requests the accounts managed by the signer.
|
|
129
|
+
* Each account has an owner {@link Principal} and an optional 32-byte subaccount.
|
|
130
|
+
*
|
|
131
|
+
* Requires the `icrc27_accounts` permission scope.
|
|
132
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_27_accounts.md
|
|
133
|
+
*/
|
|
134
|
+
getAccounts(): Promise<Array<{
|
|
135
|
+
owner: Principal;
|
|
136
|
+
subaccount?: Uint8Array;
|
|
137
|
+
}>>;
|
|
138
|
+
/**
|
|
139
|
+
* Requests a delegation chain from the signer for session-based authentication.
|
|
140
|
+
* This allows the relying party to sign canister calls without requiring
|
|
141
|
+
* user approval for each individual call.
|
|
142
|
+
* @param params - The delegation request parameters.
|
|
143
|
+
* @param params.publicKey - The session's public key to delegate to.
|
|
144
|
+
* @param params.targets - Optional canister IDs to restrict the delegation to.
|
|
145
|
+
* When provided, the signer creates an account delegation; otherwise a
|
|
146
|
+
* relying party delegation.
|
|
147
|
+
* @param params.maxTimeToLive - Optional maximum delegation lifetime in nanoseconds.
|
|
148
|
+
* @returns A {@link DelegationChain} that can be used with `DelegationIdentity`.
|
|
149
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_34_delegation.md
|
|
150
|
+
*/
|
|
151
|
+
requestDelegation(params: {
|
|
152
|
+
publicKey: PublicKey;
|
|
153
|
+
targets?: Principal[];
|
|
154
|
+
maxTimeToLive?: bigint;
|
|
155
|
+
}): Promise<DelegationChain>;
|
|
156
|
+
/**
|
|
157
|
+
* Requests the signer to execute a canister call on behalf of the user.
|
|
158
|
+
* The signer will prompt the user for approval before signing and
|
|
159
|
+
* submitting the call to the Internet Computer.
|
|
160
|
+
* @param params - The canister call parameters.
|
|
161
|
+
* @param params.canisterId - The target canister.
|
|
162
|
+
* @param params.sender - The principal executing the call.
|
|
163
|
+
* @param params.method - The canister method to invoke.
|
|
164
|
+
* @param params.arg - The Candid-encoded call arguments.
|
|
165
|
+
* @param params.nonce - Optional nonce (max 32 bytes) for replay protection.
|
|
166
|
+
* @returns The CBOR-encoded content map and certificate from the IC,
|
|
167
|
+
* which can be used to verify the call's execution.
|
|
168
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_49_call_canister.md
|
|
169
|
+
*/
|
|
170
|
+
callCanister(params: {
|
|
171
|
+
canisterId: Principal;
|
|
172
|
+
sender: Principal;
|
|
173
|
+
method: string;
|
|
174
|
+
arg: Uint8Array;
|
|
175
|
+
nonce?: Uint8Array;
|
|
176
|
+
}): Promise<{
|
|
177
|
+
contentMap: Uint8Array;
|
|
178
|
+
certificate: Uint8Array;
|
|
179
|
+
}>;
|
|
180
|
+
}
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { Delegation, DelegationChain } from '@icp-sdk/core/identity';
|
|
2
|
+
import { Principal } from '@icp-sdk/core/principal';
|
|
3
|
+
const GENERIC_ERROR = 1000;
|
|
4
|
+
const NETWORK_ERROR = 4000;
|
|
5
|
+
// Base64 helpers — use native Uint8Array methods when available, fallback to btoa/atob
|
|
6
|
+
const toBase64 = (bytes) => {
|
|
7
|
+
if ('toBase64' in bytes && typeof bytes.toBase64 === 'function') {
|
|
8
|
+
return bytes.toBase64();
|
|
9
|
+
}
|
|
10
|
+
let binary = '';
|
|
11
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
12
|
+
binary += String.fromCharCode(bytes[i]);
|
|
13
|
+
}
|
|
14
|
+
return globalThis.btoa(binary);
|
|
15
|
+
};
|
|
16
|
+
const fromBase64 = (str) => {
|
|
17
|
+
if ('fromBase64' in Uint8Array && typeof Uint8Array.fromBase64 === 'function') {
|
|
18
|
+
return Uint8Array.fromBase64(str);
|
|
19
|
+
}
|
|
20
|
+
const binary = globalThis.atob(str);
|
|
21
|
+
const bytes = new Uint8Array(binary.length);
|
|
22
|
+
for (let i = 0; i < binary.length; i++) {
|
|
23
|
+
bytes[i] = binary.charCodeAt(i);
|
|
24
|
+
}
|
|
25
|
+
return bytes;
|
|
26
|
+
};
|
|
27
|
+
// Helpers to safely read fields from unknown response data
|
|
28
|
+
const asRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
29
|
+
? value
|
|
30
|
+
: undefined;
|
|
31
|
+
const asString = (value) => typeof value === 'string' ? value : undefined;
|
|
32
|
+
const asArray = (value) => Array.isArray(value) ? value : undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Error thrown when a signer returns a JSON-RPC error response
|
|
35
|
+
* or when a transport-level failure occurs.
|
|
36
|
+
*/
|
|
37
|
+
export class SignerError extends Error {
|
|
38
|
+
/** The JSON-RPC error code. */
|
|
39
|
+
code;
|
|
40
|
+
/** Optional additional error data from the signer. */
|
|
41
|
+
data;
|
|
42
|
+
constructor(error, options) {
|
|
43
|
+
super(error.message, options);
|
|
44
|
+
this.code = error.code;
|
|
45
|
+
this.data = error.data;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Client for interacting with an ICRC-25 compliant signer.
|
|
50
|
+
*
|
|
51
|
+
* Signers are applications that hold private keys and can sign messages
|
|
52
|
+
* on behalf of a user. They communicate over a {@link Transport} using
|
|
53
|
+
* JSON-RPC 2.0 messages as defined by the ICRC-25 standard.
|
|
54
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_25_signer_interaction_standard.md
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { Signer } from "@icp-sdk/signer";
|
|
58
|
+
* import { PostMessageTransport } from "@icp-sdk/signer/web";
|
|
59
|
+
*
|
|
60
|
+
* const transport = new PostMessageTransport({ url: "https://oisy.com/sign" });
|
|
61
|
+
* const signer = new Signer({ transport });
|
|
62
|
+
*
|
|
63
|
+
* const standards = await signer.getSupportedStandards();
|
|
64
|
+
* const accounts = await signer.getAccounts();
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export class Signer {
|
|
68
|
+
#options;
|
|
69
|
+
#channel;
|
|
70
|
+
#establishingChannel;
|
|
71
|
+
#scheduledChannelClosure;
|
|
72
|
+
constructor(options) {
|
|
73
|
+
this.#options = {
|
|
74
|
+
autoCloseTransportChannel: true,
|
|
75
|
+
closeTransportChannelAfter: 200,
|
|
76
|
+
crypto: globalThis.crypto,
|
|
77
|
+
...options,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** The transport used to communicate with the signer. */
|
|
81
|
+
get transport() {
|
|
82
|
+
return this.#options.transport;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Opens a communication channel with the signer.
|
|
86
|
+
* Reuses an existing open channel if available.
|
|
87
|
+
*/
|
|
88
|
+
async openChannel() {
|
|
89
|
+
clearTimeout(this.#scheduledChannelClosure);
|
|
90
|
+
if (this.#establishingChannel) {
|
|
91
|
+
await this.#establishingChannel;
|
|
92
|
+
}
|
|
93
|
+
if (this.#channel && !this.#channel.closed) {
|
|
94
|
+
return this.#channel;
|
|
95
|
+
}
|
|
96
|
+
const channel = this.#options.transport.establishChannel();
|
|
97
|
+
this.#establishingChannel = channel.then(() => { }).catch(() => { });
|
|
98
|
+
this.#channel = undefined;
|
|
99
|
+
this.#channel = await channel.catch(error => {
|
|
100
|
+
throw new SignerError({
|
|
101
|
+
code: NETWORK_ERROR,
|
|
102
|
+
message: error instanceof Error ? error.message : 'Network error',
|
|
103
|
+
}, { cause: error });
|
|
104
|
+
});
|
|
105
|
+
this.#establishingChannel = undefined;
|
|
106
|
+
return this.#channel;
|
|
107
|
+
}
|
|
108
|
+
/** Closes the current communication channel, if open. */
|
|
109
|
+
async closeChannel() {
|
|
110
|
+
await this.#channel?.close();
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Sends a JSON-RPC request over the transport channel.
|
|
114
|
+
* @param request - The JSON-RPC request to send.
|
|
115
|
+
*/
|
|
116
|
+
async sendRequest(request) {
|
|
117
|
+
const channel = await this.openChannel();
|
|
118
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
119
|
+
const responseListener = channel.addEventListener('response', response => {
|
|
120
|
+
if (response.id !== request.id) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
responseListener();
|
|
124
|
+
closeListener();
|
|
125
|
+
// Validate that error responses have the expected shape,
|
|
126
|
+
// normalize invalid ones so #rpc can trust the types
|
|
127
|
+
if ('error' in response &&
|
|
128
|
+
(typeof response.error !== 'object' ||
|
|
129
|
+
response.error === null ||
|
|
130
|
+
typeof response.error.code !== 'number' ||
|
|
131
|
+
typeof response.error.message !== 'string')) {
|
|
132
|
+
resolve({
|
|
133
|
+
jsonrpc: '2.0',
|
|
134
|
+
id: response.id,
|
|
135
|
+
error: { code: GENERIC_ERROR, message: 'Invalid error response from signer' },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
resolve(response);
|
|
140
|
+
}
|
|
141
|
+
if (this.#options.autoCloseTransportChannel) {
|
|
142
|
+
this.#scheduledChannelClosure = setTimeout(() => {
|
|
143
|
+
if (!channel.closed) {
|
|
144
|
+
channel.close();
|
|
145
|
+
}
|
|
146
|
+
}, this.#options.closeTransportChannelAfter);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
const closeListener = channel.addEventListener('close', () => {
|
|
150
|
+
responseListener();
|
|
151
|
+
closeListener();
|
|
152
|
+
reject(new SignerError({
|
|
153
|
+
code: NETWORK_ERROR,
|
|
154
|
+
message: 'Channel was closed before a response was received',
|
|
155
|
+
}));
|
|
156
|
+
});
|
|
157
|
+
try {
|
|
158
|
+
await channel.send(this.#transformRequest(request));
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
responseListener();
|
|
162
|
+
closeListener();
|
|
163
|
+
reject(new SignerError({
|
|
164
|
+
code: NETWORK_ERROR,
|
|
165
|
+
message: error instanceof Error ? error.message : 'Network error',
|
|
166
|
+
}, { cause: error }));
|
|
167
|
+
}
|
|
168
|
+
return promise;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Queries which ICRC standards the signer supports.
|
|
172
|
+
* Use this to determine signer capabilities before calling other methods.
|
|
173
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_25_signer_interaction_standard.md
|
|
174
|
+
*/
|
|
175
|
+
getSupportedStandards() {
|
|
176
|
+
return this.#rpc({
|
|
177
|
+
method: 'icrc25_supported_standards',
|
|
178
|
+
decode: result => {
|
|
179
|
+
const r = asRecord(result);
|
|
180
|
+
const standards = asArray(r?.supportedStandards);
|
|
181
|
+
if (!standards) {
|
|
182
|
+
throw new Error('Expected supportedStandards array');
|
|
183
|
+
}
|
|
184
|
+
return standards.map(item => {
|
|
185
|
+
const obj = asRecord(item);
|
|
186
|
+
const name = asString(obj?.name);
|
|
187
|
+
const url = asString(obj?.url);
|
|
188
|
+
if (name === undefined || url === undefined) {
|
|
189
|
+
throw new Error('Expected { name, url }');
|
|
190
|
+
}
|
|
191
|
+
return { name, url };
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Requests the signer to grant permission for the given scopes.
|
|
198
|
+
* The signer may prompt the user for approval.
|
|
199
|
+
* @param scopes - The permission scopes to request.
|
|
200
|
+
* @returns The current state of each requested scope after the user's decision.
|
|
201
|
+
*/
|
|
202
|
+
requestPermissions(scopes) {
|
|
203
|
+
return this.#rpc({
|
|
204
|
+
method: 'icrc25_request_permissions',
|
|
205
|
+
params: scopes,
|
|
206
|
+
encode: scopes => ({ scopes }),
|
|
207
|
+
decode: result => {
|
|
208
|
+
const r = asRecord(result);
|
|
209
|
+
const scopes = asArray(r?.scopes);
|
|
210
|
+
if (!scopes) {
|
|
211
|
+
throw new Error('Expected scopes array');
|
|
212
|
+
}
|
|
213
|
+
return scopes.map(item => {
|
|
214
|
+
const obj = asRecord(item);
|
|
215
|
+
const scope = asRecord(obj?.scope);
|
|
216
|
+
const state = asString(obj?.state);
|
|
217
|
+
if (!scope || typeof scope.method !== 'string' || !state) {
|
|
218
|
+
throw new Error('Expected { scope: { method }, state }');
|
|
219
|
+
}
|
|
220
|
+
return { scope: scope, state: state };
|
|
221
|
+
});
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Queries the current state of all permission scopes.
|
|
227
|
+
* @returns The current permission state for each scope the signer supports.
|
|
228
|
+
*/
|
|
229
|
+
getPermissions() {
|
|
230
|
+
return this.#rpc({
|
|
231
|
+
method: 'icrc25_permissions',
|
|
232
|
+
decode: result => {
|
|
233
|
+
const r = asRecord(result);
|
|
234
|
+
const scopes = asArray(r?.scopes);
|
|
235
|
+
if (!scopes) {
|
|
236
|
+
throw new Error('Expected scopes array');
|
|
237
|
+
}
|
|
238
|
+
return scopes.map(item => {
|
|
239
|
+
const obj = asRecord(item);
|
|
240
|
+
const scope = asRecord(obj?.scope);
|
|
241
|
+
const state = asString(obj?.state);
|
|
242
|
+
if (!scope || typeof scope.method !== 'string' || !state) {
|
|
243
|
+
throw new Error('Expected { scope: { method }, state }');
|
|
244
|
+
}
|
|
245
|
+
return { scope: scope, state: state };
|
|
246
|
+
});
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Requests the accounts managed by the signer.
|
|
252
|
+
* Each account has an owner {@link Principal} and an optional 32-byte subaccount.
|
|
253
|
+
*
|
|
254
|
+
* Requires the `icrc27_accounts` permission scope.
|
|
255
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_27_accounts.md
|
|
256
|
+
*/
|
|
257
|
+
getAccounts() {
|
|
258
|
+
return this.#rpc({
|
|
259
|
+
method: 'icrc27_accounts',
|
|
260
|
+
decode: result => {
|
|
261
|
+
const r = asRecord(result);
|
|
262
|
+
const accounts = asArray(r?.accounts);
|
|
263
|
+
if (!accounts) {
|
|
264
|
+
throw new Error('Expected accounts array');
|
|
265
|
+
}
|
|
266
|
+
return accounts.map(item => {
|
|
267
|
+
const obj = asRecord(item);
|
|
268
|
+
const owner = asString(obj?.owner);
|
|
269
|
+
const subaccount = asString(obj?.subaccount);
|
|
270
|
+
if (!owner) {
|
|
271
|
+
throw new Error('Expected account.owner string');
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
owner: Principal.fromText(owner),
|
|
275
|
+
subaccount: subaccount !== undefined ? fromBase64(subaccount) : undefined,
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Requests a delegation chain from the signer for session-based authentication.
|
|
283
|
+
* This allows the relying party to sign canister calls without requiring
|
|
284
|
+
* user approval for each individual call.
|
|
285
|
+
* @param params - The delegation request parameters.
|
|
286
|
+
* @param params.publicKey - The session's public key to delegate to.
|
|
287
|
+
* @param params.targets - Optional canister IDs to restrict the delegation to.
|
|
288
|
+
* When provided, the signer creates an account delegation; otherwise a
|
|
289
|
+
* relying party delegation.
|
|
290
|
+
* @param params.maxTimeToLive - Optional maximum delegation lifetime in nanoseconds.
|
|
291
|
+
* @returns A {@link DelegationChain} that can be used with `DelegationIdentity`.
|
|
292
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_34_delegation.md
|
|
293
|
+
*/
|
|
294
|
+
requestDelegation(params) {
|
|
295
|
+
return this.#rpc({
|
|
296
|
+
method: 'icrc34_delegation',
|
|
297
|
+
params,
|
|
298
|
+
encode: v => ({
|
|
299
|
+
publicKey: toBase64(new Uint8Array(v.publicKey.toDer())),
|
|
300
|
+
targets: v.targets?.map(t => t.toText()),
|
|
301
|
+
maxTimeToLive: v.maxTimeToLive !== undefined ? String(v.maxTimeToLive) : undefined,
|
|
302
|
+
}),
|
|
303
|
+
decode: result => {
|
|
304
|
+
const r = asRecord(result);
|
|
305
|
+
const publicKey = asString(r?.publicKey);
|
|
306
|
+
const signerDelegation = asArray(r?.signerDelegation);
|
|
307
|
+
if (!publicKey || !signerDelegation) {
|
|
308
|
+
throw new Error('Expected { publicKey, signerDelegation }');
|
|
309
|
+
}
|
|
310
|
+
return DelegationChain.fromDelegations(signerDelegation.map(item => {
|
|
311
|
+
const obj = asRecord(item);
|
|
312
|
+
const del = asRecord(obj?.delegation);
|
|
313
|
+
const pubkey = asString(del?.pubkey);
|
|
314
|
+
const expiration = del?.expiration;
|
|
315
|
+
const signature = asString(obj?.signature);
|
|
316
|
+
if (!pubkey || expiration === undefined || !signature) {
|
|
317
|
+
throw new Error('Expected delegation { pubkey, expiration, signature }');
|
|
318
|
+
}
|
|
319
|
+
const targets = asArray(del?.targets);
|
|
320
|
+
return {
|
|
321
|
+
delegation: new Delegation(fromBase64(pubkey), BigInt(expiration), targets?.map(t => Principal.fromText(t))),
|
|
322
|
+
signature: fromBase64(signature),
|
|
323
|
+
};
|
|
324
|
+
}), fromBase64(publicKey));
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Requests the signer to execute a canister call on behalf of the user.
|
|
330
|
+
* The signer will prompt the user for approval before signing and
|
|
331
|
+
* submitting the call to the Internet Computer.
|
|
332
|
+
* @param params - The canister call parameters.
|
|
333
|
+
* @param params.canisterId - The target canister.
|
|
334
|
+
* @param params.sender - The principal executing the call.
|
|
335
|
+
* @param params.method - The canister method to invoke.
|
|
336
|
+
* @param params.arg - The Candid-encoded call arguments.
|
|
337
|
+
* @param params.nonce - Optional nonce (max 32 bytes) for replay protection.
|
|
338
|
+
* @returns The CBOR-encoded content map and certificate from the IC,
|
|
339
|
+
* which can be used to verify the call's execution.
|
|
340
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_49_call_canister.md
|
|
341
|
+
*/
|
|
342
|
+
callCanister(params) {
|
|
343
|
+
return this.#rpc({
|
|
344
|
+
method: 'icrc49_call_canister',
|
|
345
|
+
params,
|
|
346
|
+
encode: v => ({
|
|
347
|
+
canisterId: v.canisterId.toText(),
|
|
348
|
+
sender: v.sender.toText(),
|
|
349
|
+
method: v.method,
|
|
350
|
+
arg: toBase64(v.arg),
|
|
351
|
+
nonce: v.nonce !== undefined ? toBase64(v.nonce) : undefined,
|
|
352
|
+
}),
|
|
353
|
+
decode: result => {
|
|
354
|
+
const r = asRecord(result);
|
|
355
|
+
const contentMap = asString(r?.contentMap);
|
|
356
|
+
const certificate = asString(r?.certificate);
|
|
357
|
+
if (!contentMap || !certificate) {
|
|
358
|
+
throw new Error('Expected { contentMap, certificate }');
|
|
359
|
+
}
|
|
360
|
+
return { contentMap: fromBase64(contentMap), certificate: fromBase64(certificate) };
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Sends a JSON-RPC request to the signer and decodes the result.
|
|
366
|
+
* Handles encoding params, validating the response, and throwing
|
|
367
|
+
* {@link SignerError} on JSON-RPC errors or invalid results.
|
|
368
|
+
* @param args - The RPC call configuration.
|
|
369
|
+
*/
|
|
370
|
+
async #rpc(args) {
|
|
371
|
+
let params;
|
|
372
|
+
if (args.encode) {
|
|
373
|
+
try {
|
|
374
|
+
params = args.encode(args.params);
|
|
375
|
+
}
|
|
376
|
+
catch (cause) {
|
|
377
|
+
throw new SignerError({
|
|
378
|
+
code: GENERIC_ERROR,
|
|
379
|
+
message: `Failed to encode params: ${cause instanceof Error ? cause.message : cause}`,
|
|
380
|
+
}, { cause });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const response = await this.sendRequest({
|
|
384
|
+
id: this.#options.crypto.randomUUID(),
|
|
385
|
+
jsonrpc: '2.0',
|
|
386
|
+
method: args.method,
|
|
387
|
+
params,
|
|
388
|
+
});
|
|
389
|
+
if ('error' in response) {
|
|
390
|
+
throw new SignerError(response.error);
|
|
391
|
+
}
|
|
392
|
+
if ('result' in response) {
|
|
393
|
+
try {
|
|
394
|
+
return args.decode(response.result);
|
|
395
|
+
}
|
|
396
|
+
catch (cause) {
|
|
397
|
+
throw new SignerError({
|
|
398
|
+
code: GENERIC_ERROR,
|
|
399
|
+
message: `Invalid result from signer: ${cause instanceof Error ? cause.message : cause}`,
|
|
400
|
+
}, { cause });
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
throw new SignerError({
|
|
404
|
+
code: GENERIC_ERROR,
|
|
405
|
+
message: 'Response contains neither result nor error',
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Appends the ICRC-95 derivation origin to the request params
|
|
410
|
+
* when configured.
|
|
411
|
+
* @param request - The JSON-RPC request to transform.
|
|
412
|
+
* @see https://github.com/dfinity/wg-identity-authentication/blob/main/topics/icrc_95_derivationorigin.md
|
|
413
|
+
*/
|
|
414
|
+
#transformRequest(request) {
|
|
415
|
+
if (this.#options.derivationOrigin) {
|
|
416
|
+
return {
|
|
417
|
+
...request,
|
|
418
|
+
params: {
|
|
419
|
+
...request.params,
|
|
420
|
+
icrc95DerivationOrigin: this.#options.derivationOrigin,
|
|
421
|
+
},
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
return request;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
//# sourceMappingURL=signer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.js","sourceRoot":"","sources":["../../src/signer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AASpD,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,aAAa,GAAG,I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AI,CAAC,QAAQ,CAAC,gBAAgB;iBACvD;aACF,CAAC;QACJ,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface JsonRpcError {
|
|
2
|
+
code: number;
|
|
3
|
+
message: string;
|
|
4
|
+
data?: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface JsonRpcRequest {
|
|
7
|
+
jsonrpc: '2.0';
|
|
8
|
+
id?: string | number | null;
|
|
9
|
+
method: string;
|
|
10
|
+
params?: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
export type JsonRpcResponse = {
|
|
13
|
+
jsonrpc: '2.0';
|
|
14
|
+
id: string | number | null;
|
|
15
|
+
result: unknown;
|
|
16
|
+
} | {
|
|
17
|
+
jsonrpc: '2.0';
|
|
18
|
+
id: string | number | null;
|
|
19
|
+
error: JsonRpcError;
|
|
20
|
+
};
|
|
21
|
+
export interface Channel {
|
|
22
|
+
closed: boolean;
|
|
23
|
+
addEventListener(event: 'close', listener: () => void): () => void;
|
|
24
|
+
addEventListener(event: 'response', listener: (response: JsonRpcResponse) => void): () => void;
|
|
25
|
+
send(request: JsonRpcRequest): Promise<void>;
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export interface Transport {
|
|
29
|
+
establishChannel(): Promise<Channel>;
|
|
30
|
+
}
|
|
31
|
+
export declare const isJsonRpcRequest: (message: unknown) => message is JsonRpcRequest;
|
|
32
|
+
export declare const isJsonRpcResponse: (message: unknown) => message is JsonRpcResponse;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const isJsonRpcRequest = (message) => typeof message === 'object' &&
|
|
2
|
+
message !== null &&
|
|
3
|
+
message.jsonrpc === '2.0' &&
|
|
4
|
+
typeof message.method === 'string';
|
|
5
|
+
export const isJsonRpcResponse = (message) => typeof message === 'object' &&
|
|
6
|
+
message !== null &&
|
|
7
|
+
message.jsonrpc === '2.0' &&
|
|
8
|
+
'id' in message &&
|
|
9
|
+
(typeof message.id === 'string' ||
|
|
10
|
+
typeof message.id === 'number');
|
|
11
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/transport.ts"],"names":[],"mappings":"AA+BA,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,OAAgB,EAA6B,EAAE,CAC9E,OAAO,OAAO,KAAK,QAAQ;IAC3B,OAAO,KAAK,IAAI;IACf,OAAmC,CAAC,OAAO,KAAK,KAAK;IACtD,OAAQ,OAAmC,CAAC,MAAM,KAAK,QAAQ,CAAC;AAElE,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,OAAgB,EAA8B,EAAE,CAChF,OAAO,OAAO,KAAK,QAAQ;IAC3B,OAAO,KAAK,IAAI;IACf,OAAmC,CAAC,OAAO,KAAK,KAAK;IACtD,IAAI,IAAI,OAAO;IACf,CAAC,OAAQ,OAAmC,CAAC,EAAE,KAAK,QAAQ;QAC1D,OAAQ,OAAmC,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC"}
|