ic-siwa 0.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.
package/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # IC-SIWA TypeScript Library
2
+
3
+ Sign in with Avalanche for the Internet Computer.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add ic-siwa
9
+ # or
10
+ npm install ic-siwa
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```typescript
16
+ import {SiwaClient} from "ic-siwa";
17
+ import {createWalletClient, http} from "viem";
18
+ import {avalanche} from "viem/chains";
19
+
20
+ // Create SIWA client
21
+ const siwa = new SiwaClient({
22
+ canisterId: "xxxxx-xxxxx-xxxxx-xxxxx-xxx",
23
+ });
24
+
25
+ // Create wallet client (using viem)
26
+ const walletClient = createWalletClient({
27
+ chain: avalanche,
28
+ transport: http(),
29
+ });
30
+
31
+ // Login with wallet
32
+ const result = await siwa.loginWithWallet(walletClient);
33
+ console.log("Logged in as:", result.principal.toText());
34
+
35
+ // Check if authenticated
36
+ if (await siwa.isAuthenticated()) {
37
+ const principal = await siwa.getPrincipal();
38
+ console.log("Current principal:", principal?.toText());
39
+ }
40
+
41
+ // Make authenticated calls
42
+ const actor = await siwa.createActor(canisterId, idlFactory);
43
+ const data = await actor.getData();
44
+
45
+ // Logout
46
+ await siwa.logout();
47
+ ```
48
+
49
+ ## Manual Login Flow
50
+
51
+ If you need more control over the login process:
52
+
53
+ ```typescript
54
+ const siwa = new SiwaClient({
55
+ canisterId: "xxxxx-xxxxx-xxxxx-xxxxx-xxx",
56
+ });
57
+
58
+ // 1. Prepare login message
59
+ const address = "0x1234...abcd";
60
+ const prepared = await siwa.prepareLogin(address);
61
+
62
+ // 2. Sign message with your wallet
63
+ const signature = await wallet.signMessage(prepared.message);
64
+
65
+ // 3. Complete login
66
+ const result = await siwa.login(signature, address);
67
+ ```
68
+
69
+ ## Configuration Options
70
+
71
+ ```typescript
72
+ const siwa = new SiwaClient({
73
+ // Required: IC-SIWA Provider canister ID
74
+ canisterId: "xxxxx-xxxxx-xxxxx-xxxxx-xxx",
75
+
76
+ // Optional: IC host (default: https://ic0.app)
77
+ host: "https://ic0.app",
78
+
79
+ // Optional: Custom storage provider (default: localStorage)
80
+ storage: new MemoryStorageProvider(),
81
+
82
+ // Optional: Auto-refresh delegation before expiry (default: true)
83
+ autoRefresh: true,
84
+ });
85
+ ```
86
+
87
+ ## Storage Providers
88
+
89
+ The library includes two storage providers:
90
+
91
+ - `LocalStorageProvider` (default) - Persists identity in browser localStorage
92
+ - `MemoryStorageProvider` - In-memory storage for testing or SSR
93
+
94
+ You can also implement your own by implementing the `StorageProvider` interface.
95
+
96
+ ## Error Handling
97
+
98
+ ```typescript
99
+ import {SiwaError, SiwaErrorCode} from "ic-siwa";
100
+
101
+ try {
102
+ await siwa.login(signature, address);
103
+ } catch (error) {
104
+ if (error instanceof SiwaError) {
105
+ switch (error.code) {
106
+ case SiwaErrorCode.InvalidSignature:
107
+ console.error("Invalid signature");
108
+ break;
109
+ case SiwaErrorCode.MessageExpired:
110
+ console.error("Login message expired");
111
+ break;
112
+ default:
113
+ console.error("Login failed:", error.message);
114
+ }
115
+ }
116
+ }
117
+ ```
118
+
119
+ ## Astro Component
120
+
121
+ The library includes a ready-to-use Astro component for the login button.
122
+
123
+ ### Basic Usage
124
+
125
+ ```astro
126
+ ---
127
+ import LoginButton from 'ic-siwa/astro';
128
+ ---
129
+
130
+ <LoginButton canisterId="xxxxx-xxxxx-xxxxx-xxxxx-xxx" />
131
+ ```
132
+
133
+ ### Props
134
+
135
+ | Prop | Type | Default | Description |
136
+ | ------------- | ------------------------------------- | ------------------------ | ---------------------------- |
137
+ | `canisterId` | `string` | required | IC-SIWA Provider canister ID |
138
+ | `host` | `string` | `https://ic0.app` | IC host URL |
139
+ | `label` | `string` | `Sign in with Avalanche` | Button text |
140
+ | `size` | `xs\|sm\|md\|lg\|xl` | `md` | Button size |
141
+ | `variant` | `primary\|secondary\|accent\|neutral` | `primary` | Color variant |
142
+ | `style` | `solid\|outline\|soft\|ghost` | `solid` | Button style |
143
+ | `showAddress` | `boolean` | `true` | Show address when logged in |
144
+ | `class` | `string` | `""` | Additional CSS classes |
145
+ | `id` | `string` | `siwa-login-btn` | Custom element ID |
146
+
147
+ ### Custom Styling
148
+
149
+ ```astro
150
+ <LoginButton
151
+ canisterId="xxxxx-xxxxx-xxxxx-xxxxx-xxx"
152
+ label="Connect Wallet"
153
+ variant="accent"
154
+ style="outline"
155
+ size="lg"
156
+ class="my-custom-class"
157
+ />
158
+ ```
159
+
160
+ ### Events
161
+
162
+ The component emits custom events you can listen to:
163
+
164
+ ```astro
165
+ ---
166
+ import LoginButton from 'ic-siwa/astro';
167
+ ---
168
+
169
+ <div id="auth-container">
170
+ <LoginButton canisterId="xxxxx-xxxxx-xxxxx-xxxxx-xxx" />
171
+ </div>
172
+
173
+ <script>
174
+ const container = document.getElementById('auth-container');
175
+
176
+ container.addEventListener('siwa:login-start', () => {
177
+ console.log('Login started...');
178
+ });
179
+
180
+ container.addEventListener('siwa:login-success', (e) => {
181
+ console.log('Logged in as:', e.detail.principal);
182
+ console.log('Address:', e.detail.address);
183
+ });
184
+
185
+ container.addEventListener('siwa:login-error', (e) => {
186
+ console.error('Login failed:', e.detail.error);
187
+ });
188
+
189
+ container.addEventListener('siwa:logout', () => {
190
+ console.log('User logged out');
191
+ });
192
+ </script>
193
+ ```
194
+
195
+ ### Custom Icon
196
+
197
+ You can replace the default Avalanche logo with a custom icon:
198
+
199
+ ```astro
200
+ <LoginButton canisterId="xxxxx-xxxxx-xxxxx-xxxxx-xxx">
201
+ <svg slot="icon" ...>...</svg>
202
+ </LoginButton>
203
+ ```
204
+
205
+ ## Requirements
206
+
207
+ - DaisyUI and Tailwind CSS for the Astro component styling
208
+ - An Avalanche-compatible wallet (Core, MetaMask, etc.)
209
+ - Deployed `ic_siwa_provider` canister
210
+
211
+ ## License
212
+
213
+ MIT
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Astro components for IC-SIWA
3
+ *
4
+ * Usage:
5
+ * ```astro
6
+ * ---
7
+ * import { LoginButton } from 'ic-siwa/astro';
8
+ * ---
9
+ *
10
+ * <LoginButton canisterId="xxxxx-xxxxx-xxxxx-xxxxx-xxx" />
11
+ * ```
12
+ */
13
+ export interface LoginButtonProps {
14
+ /** Canister ID of the ic_siwa_provider */
15
+ canisterId: string;
16
+ /** IC host URL (default: https://ic0.app) */
17
+ host?: string;
18
+ /** Button label text */
19
+ label?: string;
20
+ /** Button size: xs, sm, md, lg, xl */
21
+ size?: "xs" | "sm" | "md" | "lg" | "xl";
22
+ /** Button color variant */
23
+ variant?: "primary" | "secondary" | "accent" | "neutral";
24
+ /** Button style */
25
+ style?: "solid" | "outline" | "soft" | "ghost";
26
+ /** Additional CSS classes */
27
+ class?: string;
28
+ /** Show wallet address when logged in */
29
+ showAddress?: boolean;
30
+ /** Custom ID for the button element */
31
+ id?: string;
32
+ }
33
+ /**
34
+ * Custom events emitted by LoginButton:
35
+ *
36
+ * - `siwa:login-start` - Emitted when login process begins
37
+ * - `siwa:login-success` - Emitted on successful login
38
+ * - detail: { principal: string, address: string }
39
+ * - `siwa:login-error` - Emitted on login failure
40
+ * - detail: { error: string }
41
+ * - `siwa:logout` - Emitted when user logs out
42
+ *
43
+ * @example
44
+ * ```html
45
+ * <div id="login-container">
46
+ * <LoginButton canisterId="..." />
47
+ * </div>
48
+ *
49
+ * <script>
50
+ * document.getElementById('login-container')
51
+ * .addEventListener('siwa:login-success', (e) => {
52
+ * console.log('Logged in:', e.detail.principal);
53
+ * });
54
+ * </script>
55
+ * ```
56
+ */
57
+ export type LoginButtonEvents = {
58
+ "siwa:login-start": CustomEvent<void>;
59
+ "siwa:login-success": CustomEvent<{
60
+ principal: string;
61
+ address: string;
62
+ }>;
63
+ "siwa:login-error": CustomEvent<{
64
+ error: string;
65
+ }>;
66
+ "siwa:logout": CustomEvent<void>;
67
+ };
68
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/astro/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,MAAM,WAAW,gBAAgB;IAC/B,0CAA0C;IAC1C,UAAU,EAAE,MAAM,CAAC;IACnB,6CAA6C;IAC7C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wBAAwB;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sCAAsC;IACtC,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IACxC,2BAA2B;IAC3B,OAAO,CAAC,EAAE,SAAS,GAAG,WAAW,GAAG,QAAQ,GAAG,SAAS,CAAC;IACzD,mBAAmB;IACnB,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,CAAC;IAC/C,6BAA6B;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,uCAAuC;IACvC,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,kBAAkB,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC;IACtC,oBAAoB,EAAE,WAAW,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAC,CAAC,CAAC;IACxE,kBAAkB,EAAE,WAAW,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAC,CAAC,CAAC;IACjD,aAAa,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC;CAClC,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Astro components for IC-SIWA
3
+ *
4
+ * Usage:
5
+ * ```astro
6
+ * ---
7
+ * import { LoginButton } from 'ic-siwa/astro';
8
+ * ---
9
+ *
10
+ * <LoginButton canisterId="xxxxx-xxxxx-xxxxx-xxxxx-xxx" />
11
+ * ```
12
+ */
13
+ export {};
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/astro/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Auto-generated Candid bindings for ic_siwa_provider canister
3
+ * Generated from: canisters/ic_siwa_provider/ic_siwa_provider.did
4
+ * DO NOT EDIT MANUALLY - regenerate with: ic-siwa candid
5
+ */
6
+ import type { Principal } from '@dfinity/principal';
7
+ import type { ActorMethod } from '@dfinity/agent';
8
+ export interface Delegation {
9
+ 'pubkey': Uint8Array | number[];
10
+ 'targets': [] | [Array<Principal>];
11
+ 'expiration': bigint;
12
+ }
13
+ export interface InitArgs {
14
+ 'uri': string;
15
+ 'domain': string;
16
+ 'salt': string;
17
+ 'chain_id': bigint;
18
+ 'allowed_domains': [] | [Array<string>];
19
+ 'allowed_canisters': [] | [Array<Principal>];
20
+ 'session_expiration_time': bigint;
21
+ }
22
+ export interface LoginResponse {
23
+ 'user_principal': Principal;
24
+ 'expiration': bigint;
25
+ }
26
+ export interface PrepareLoginResponse {
27
+ 'expiration': bigint;
28
+ 'message': string;
29
+ 'nonce': string;
30
+ }
31
+ export type Result = {
32
+ 'Ok': string;
33
+ } | {
34
+ 'Err': string;
35
+ };
36
+ export type Result_1 = {
37
+ 'Ok': Principal;
38
+ } | {
39
+ 'Err': string;
40
+ };
41
+ export type Result_2 = {
42
+ 'Ok': SignedDelegation;
43
+ } | {
44
+ 'Err': string;
45
+ };
46
+ export type Result_3 = {
47
+ 'Ok': LoginResponse;
48
+ } | {
49
+ 'Err': string;
50
+ };
51
+ export type Result_4 = {
52
+ 'Ok': PrepareLoginResponse;
53
+ } | {
54
+ 'Err': string;
55
+ };
56
+ export interface SignedDelegation {
57
+ 'signature': Uint8Array | number[];
58
+ 'delegation': Delegation;
59
+ }
60
+ export interface _SERVICE {
61
+ /**
62
+ * Get Avalanche address for ICP principal
63
+ */
64
+ 'get_address': ActorMethod<[Principal], Result>;
65
+ /**
66
+ * Get Avalanche address for caller
67
+ */
68
+ 'get_caller_address': ActorMethod<[], Result>;
69
+ /**
70
+ * Get ICP principal for Avalanche address
71
+ */
72
+ 'get_principal': ActorMethod<[string], Result_1>;
73
+ /**
74
+ * Get delegation for authenticated principal
75
+ *
76
+ * # Arguments
77
+ * * `address` - The Avalanche address
78
+ * * `session_key` - The session public key from login
79
+ * * `expiration` - Requested expiration timestamp (may be capped)
80
+ *
81
+ * # Returns
82
+ * * `Ok(SignedDelegation)` - The signed delegation for the session
83
+ * * `Err(String)` - Error if not authenticated or expired
84
+ */
85
+ 'siwa_get_delegation': ActorMethod<[
86
+ string,
87
+ Uint8Array | number[],
88
+ bigint
89
+ ], Result_2>;
90
+ /**
91
+ * Complete SIWA login with signed message
92
+ *
93
+ * # Arguments
94
+ * * `signature` - Hex-encoded signature from the user's wallet
95
+ * * `address` - The Avalanche address that signed the message
96
+ * * `session_key` - The session public key to bind to this authentication
97
+ *
98
+ * # Returns
99
+ * * `Ok(LoginResponse)` - The derived principal and session expiration
100
+ * * `Err(String)` - Error if signature is invalid or session expired
101
+ */
102
+ 'siwa_login': ActorMethod<[string, string, Uint8Array | number[]], Result_3>;
103
+ /**
104
+ * Prepare a SIWA login message for signing
105
+ *
106
+ * # Arguments
107
+ * * `address` - The Avalanche address (0x-prefixed, EIP-55 checksummed)
108
+ *
109
+ * # Returns
110
+ * * `Ok(PrepareLoginResponse)` - The message to sign, nonce, and expiration
111
+ * * `Err(String)` - Error description if address is invalid
112
+ */
113
+ 'siwa_prepare_login': ActorMethod<[string], Result_4>;
114
+ }
115
+ export declare const idlFactory: ({ IDL }: {
116
+ IDL: any;
117
+ }) => any;
118
+ export declare const init: ({ IDL }: {
119
+ IDL: any;
120
+ }) => any[];
121
+ //# sourceMappingURL=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,GAAG,CAAA;CAAE,UAWzC,CAAC"}
@@ -0,0 +1,64 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Auto-generated Candid bindings for ic_siwa_provider canister
4
+ * Generated from: canisters/ic_siwa_provider/ic_siwa_provider.did
5
+ * DO NOT EDIT MANUALLY - regenerate with: ic-siwa candid
6
+ */
7
+ export const idlFactory = ({ IDL }) => {
8
+ const InitArgs = IDL.Record({
9
+ 'uri': IDL.Text,
10
+ 'domain': IDL.Text,
11
+ 'salt': IDL.Text,
12
+ 'chain_id': IDL.Nat64,
13
+ 'allowed_domains': IDL.Opt(IDL.Vec(IDL.Text)),
14
+ 'allowed_canisters': IDL.Opt(IDL.Vec(IDL.Principal)),
15
+ 'session_expiration_time': IDL.Nat64,
16
+ });
17
+ const Result = IDL.Variant({ 'Ok': IDL.Text, 'Err': IDL.Text });
18
+ const Result_1 = IDL.Variant({ 'Ok': IDL.Principal, 'Err': IDL.Text });
19
+ const Delegation = IDL.Record({
20
+ 'pubkey': IDL.Vec(IDL.Nat8),
21
+ 'targets': IDL.Opt(IDL.Vec(IDL.Principal)),
22
+ 'expiration': IDL.Nat64,
23
+ });
24
+ const SignedDelegation = IDL.Record({
25
+ 'signature': IDL.Vec(IDL.Nat8),
26
+ 'delegation': Delegation,
27
+ });
28
+ const Result_2 = IDL.Variant({ 'Ok': SignedDelegation, 'Err': IDL.Text });
29
+ const LoginResponse = IDL.Record({
30
+ 'user_principal': IDL.Principal,
31
+ 'expiration': IDL.Nat64,
32
+ });
33
+ const Result_3 = IDL.Variant({ 'Ok': LoginResponse, 'Err': IDL.Text });
34
+ const PrepareLoginResponse = IDL.Record({
35
+ 'expiration': IDL.Nat64,
36
+ 'message': IDL.Text,
37
+ 'nonce': IDL.Text,
38
+ });
39
+ const Result_4 = IDL.Variant({
40
+ 'Ok': PrepareLoginResponse,
41
+ 'Err': IDL.Text,
42
+ });
43
+ return IDL.Service({
44
+ 'get_address': IDL.Func([IDL.Principal], [Result], ['query']),
45
+ 'get_caller_address': IDL.Func([], [Result], ['query']),
46
+ 'get_principal': IDL.Func([IDL.Text], [Result_1], ['query']),
47
+ 'siwa_get_delegation': IDL.Func([IDL.Text, IDL.Vec(IDL.Nat8), IDL.Nat64], [Result_2], ['query']),
48
+ 'siwa_login': IDL.Func([IDL.Text, IDL.Text, IDL.Vec(IDL.Nat8)], [Result_3], []),
49
+ 'siwa_prepare_login': IDL.Func([IDL.Text], [Result_4], []),
50
+ });
51
+ };
52
+ export const init = ({ IDL }) => {
53
+ const InitArgs = IDL.Record({
54
+ 'uri': IDL.Text,
55
+ 'domain': IDL.Text,
56
+ 'salt': IDL.Text,
57
+ 'chain_id': IDL.Nat64,
58
+ 'allowed_domains': IDL.Opt(IDL.Vec(IDL.Text)),
59
+ 'allowed_canisters': IDL.Opt(IDL.Vec(IDL.Principal)),
60
+ 'session_expiration_time': IDL.Nat64,
61
+ });
62
+ return [InitArgs];
63
+ };
64
+ //# sourceMappingURL=ic_siwa_provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ic_siwa_provider.js","sourceRoot":"","sources":["../../src/candid/ic_siwa_provider.ts"],"names":[],"mappings":"AAAA,cAAc;AACd;;;;GAIG;AAoGH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,EAAE,GAAG,EAAgB,EAAE,EAAE;IAClD,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC;QAC1B,KAAK,EAAG,GAAG,CAAC,IAAI;QAChB,QAAQ,EAAG,GAAG,CAAC,IAAI;QACnB,MAAM,EAAG,GAAG,CAAC,IAAI;QACjB,UAAU,EAAG,GAAG,CAAC,KAAK;QACtB,iBAAiB,EAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC9C,mBAAmB,EAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACrD,yBAAyB,EAAG,G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;IAC5C,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC;QAC1B,KAAK,EAAG,GAAG,CAAC,IAAI;QAChB,QAAQ,EAAG,GAAG,CAAC,IAAI;QACnB,MAAM,EAAG,GAAG,CAAC,IAAI;QACjB,UAAU,EAAG,GAAG,CAAC,KAAK;QACtB,iBAAiB,EAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC9C,mBAAmB,EAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACrD,yBAAyB,EAAG,GAAG,CAAC,KAAK;KACtC,CAAC,CAAC;IACH,OAAO,CAAC,QAAQ,CAAC,CAAC;AACpB,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Candid TypeScript declarations
3
+ * Auto-generated - DO NOT EDIT MANUALLY
4
+ */
5
+ export * from './ic_siwa_provider';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/candid/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Candid TypeScript declarations
3
+ * Auto-generated - DO NOT EDIT MANUALLY
4
+ */
5
+ export * from './ic_siwa_provider';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/candid/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,124 @@
1
+ /**
2
+ * SIWA Client for browser-based authentication
3
+ */
4
+ import { HttpAgent } from "@dfinity/agent";
5
+ import { Ed25519KeyIdentity } from "@dfinity/identity";
6
+ import { Principal } from "@dfinity/principal";
7
+ import { type SiwaIdentity } from "./identity";
8
+ import { type StorageProvider } from "./storage";
9
+ import type { PreparedLogin, LoginResult } from "./types";
10
+ /**
11
+ * SIWA Client configuration options
12
+ */
13
+ export interface SiwaClientOptions {
14
+ /** Canister ID of the ic_siwa_provider */
15
+ canisterId: string;
16
+ /** IC host URL (default: https://ic0.app) */
17
+ host?: string;
18
+ /** Storage provider for caching identity (default: localStorage) */
19
+ storage?: StorageProvider;
20
+ /** Auto-refresh delegation before expiry */
21
+ autoRefresh?: boolean;
22
+ /** Refresh threshold in milliseconds (default: 5 minutes before expiry) */
23
+ refreshThreshold?: number;
24
+ }
25
+ /**
26
+ * SIWA Client for authenticating with Avalanche wallets
27
+ */
28
+ export declare class SiwaClient {
29
+ private canisterId;
30
+ private host;
31
+ private storage;
32
+ private autoRefresh;
33
+ private refreshThreshold;
34
+ private identity;
35
+ private agent;
36
+ private refreshTimer;
37
+ private sessionKey;
38
+ constructor(options: SiwaClientOptions);
39
+ /**
40
+ * Check if user is currently authenticated
41
+ */
42
+ isAuthenticated(): Promise<boolean>;
43
+ /**
44
+ * Get the current identity if authenticated
45
+ */
46
+ getIdentity(): Promise<SiwaIdentity | null>;
47
+ /**
48
+ * Get the current principal if authenticated
49
+ */
50
+ getPrincipal(): Promise<Principal | null>;
51
+ /**
52
+ * Get the current Avalanche address if authenticated
53
+ */
54
+ getAddress(): Promise<string | null>;
55
+ /**
56
+ * Create an anonymous agent for canister calls
57
+ */
58
+ private createAnonymousAgent;
59
+ /**
60
+ * Create an actor for the SIWA provider canister
61
+ */
62
+ private createProviderActor;
63
+ /**
64
+ * Prepare a login message for signing
65
+ */
66
+ prepareLogin(address: string): Promise<PreparedLogin>;
67
+ /**
68
+ * Complete login with signed message
69
+ *
70
+ * @param signature - The signed message from the wallet
71
+ * @param address - The Avalanche address
72
+ * @param sessionKey - Optional session key (generated if not provided)
73
+ */
74
+ login(signature: string, address: string, sessionKey?: Ed25519KeyIdentity): Promise<LoginResult>;
75
+ /**
76
+ * Build delegation chain from canister response
77
+ *
78
+ * @param candidDelegation - Delegation record from canister
79
+ * @param signatureBytes - Signature from canister
80
+ */
81
+ private buildDelegationChain;
82
+ /**
83
+ * Refresh the current delegation
84
+ */
85
+ refreshDelegation(): Promise<LoginResult | null>;
86
+ /**
87
+ * Schedule automatic delegation refresh
88
+ */
89
+ private scheduleRefresh;
90
+ /**
91
+ * Login with a wallet client (convenience method)
92
+ *
93
+ * @param walletClient - A viem wallet client or similar
94
+ */
95
+ loginWithWallet(walletClient: {
96
+ account: {
97
+ address: string;
98
+ };
99
+ signMessage: (args: {
100
+ message: string;
101
+ }) => Promise<string>;
102
+ }): Promise<LoginResult>;
103
+ /**
104
+ * Logout and clear stored identity
105
+ */
106
+ logout(): Promise<void>;
107
+ /**
108
+ * Get an HttpAgent for making authenticated calls
109
+ */
110
+ getAgent(): Promise<HttpAgent>;
111
+ /**
112
+ * Create an actor for calling a canister
113
+ */
114
+ createActor<T>(canisterId: string | Principal, idlFactory: unknown): Promise<T>;
115
+ /**
116
+ * Get the canister ID
117
+ */
118
+ getCanisterId(): Principal;
119
+ /**
120
+ * Get the host URL
121
+ */
122
+ getHost(): string;
123
+ }
124
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAQ,SAAS,EAAgC,MAAM,gBAAgB,CAAC;AAC/E,OAAO,EAGL,kBAAkB,EACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAC,SAAS,EAAC,MAAM,oBAAoB,CAAC;AAU7C,OAAO,EAGL,KAAK,YAAY,EAElB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAuB,KAAK,eAAe,EAAC,MAAM,WAAW,CAAC;AACrE,OAAO,KAAK,EAAC,aAAa,EAAE,WAAW,EAAC,MAAM,SAAS,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,0CAA0C;IAC1C,UAAU,EAAE,MAA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