-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-store.ts
452 lines (402 loc) · 12.6 KB
/
auth-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { writable, get, Writable, Readable, Updater, Subscriber, Invalidator, Unsubscriber } from "svelte/store";
import type { Principal } from "@dfinity/principal";
import { Actor, ActorSubclass, HttpAgent, Identity } from "@dfinity/agent";
import { StoicIdentity } from "ic-stoic-identity";
import { AccountIdentifier } from "@dfinity/ledger-icp";
import { InterfaceFactory } from "@dfinity/candid/lib/cjs/idl";
import {
createActor as createLedgerActor,
idlFactory as ledgerIdlFactory,
} from "./declarations/ledger";
import { _SERVICE as LedgerService } from "./declarations/ledger/ledger.did";
import { createActor } from './create-actor';
export type AuthState = {
isAuthed: "stoic" | "plug" | "bitfinity" | null;
principal: Principal | null;
accountId: string;
isLoading: boolean;
balance: number;
};
type LedgerActor = ActorSubclass<LedgerService>;
let ledgerCanisterId = "ryjl3-tyaaa-aaaaa-aaaba-cai";
class AuthStoreClass implements Readable<AuthState> {
state: Writable<AuthState>;
whitelist: string[] = [];
host: string = '';
ledgerActor: LedgerActor;
stoicIdentity: Identity & { accounts(): string };
constructor({ host, whitelist }: { host?: string; whitelist?: string[]; }) {
this.host = host || (process.env.DFX_NETWORK === "local" ? "http://localhost:4943" : "https://icp0.io");
this.whitelist = whitelist || [];
this.state = writable<AuthState>(this.getDefaultState());
this.ledgerActor = createLedgerActor(ledgerCanisterId, {
agentOptions: {host: this.host},
});
}
getDefaultState(): AuthState {
return {
isAuthed: null,
principal: null,
accountId: "",
isLoading: false,
balance: 0,
};
}
subscribe(run: Subscriber<AuthState>, invalidate?: Invalidator<AuthState>): Unsubscriber {
return this.state.subscribe(run, invalidate);
}
update(updater: Updater<AuthState>) {
return this.state.update(updater);
}
set(value: AuthState) {
return this.state.set(value);
}
// create an actor using the currently connected wallet
async createActor<T extends Actor>(canisterId: string, idlFactory: InterfaceFactory): Promise<T> {
const store = get({ subscribe: this.state.subscribe });
if (store.isAuthed === "stoic") {
return createActor(canisterId, idlFactory, {
agentOptions: {
identity: this.stoicIdentity,
host: this.host,
},
});
}
else if (store.isAuthed === "plug") {
return (await window.ic?.plug.createActor({
canisterId: canisterId,
interfaceFactory: idlFactory,
})) as T;
}
else if (store.isAuthed === "bitfinity") {
return (await window.ic.bitfinityWallet.createActor({
canisterId: canisterId,
interfaceFactory: idlFactory,
host: this.host,
})) as T;
}
else {
return createActor(canisterId, idlFactory, {
agentOptions: {
host: this.host,
},
});
}
}
async checkConnections() {
await this.checkStoicConnection();
await this.checkPlugConnection();
await this.checkBitfinityConnection();
}
async checkStoicConnection() {
StoicIdentity.load().then(async (identity) => {
if (identity !== false) {
// ID is a already connected wallet!
await this.stoicConnect();
}
});
}
async checkPlugConnection() {
const connected = await window.ic?.plug?.isConnected();
if (connected) {
console.log("plug connection detected");
await this.plugConnect();
}
}
async checkBitfinityConnection() {
const connected = await window.ic?.bitfinityWallet?.isConnected();
if (connected) {
console.log("bitfinity connection detected");
await this.bitfinityConnect();
}
}
async stoicConnect() {
StoicIdentity.load().then(async (identity) => {
if (identity !== false) {
// ID is a already connected wallet!
} else {
// No existing connection, lets make one!
identity = await StoicIdentity.connect();
}
this.stoicIdentity = identity;
this.initStoic();
});
}
async initStoic() {
this.ledgerActor = createLedgerActor(ledgerCanisterId, {
agentOptions: {
identity: this.stoicIdentity,
host: this.host,
},
});
if (!this.ledgerActor) {
console.warn("couldn't create actors");
return;
}
// the stoic agent provides an `accounts()` method that returns
// accounts assocaited with the principal
let accounts = JSON.parse(await this.stoicIdentity.accounts());
this.state.update((state) => ({
...state,
principal: this.stoicIdentity.getPrincipal(),
accountId: accounts[0].address, // we take the default account associated with the identity
isAuthed: "stoic",
}));
await this.updateBalance();
}
async plugConnect() {
// check if plug is installed in the browser
if (window.ic?.plug === undefined) {
window.open("https://plugwallet.ooo/", "_blank");
return;
}
// check if plug is connected
const plugConnected = await window.ic?.plug?.isConnected();
if (!plugConnected) {
try {
await window.ic?.plug.requestConnect({
whitelist: this.whitelist,
host: this.host,
});
console.log("plug connected");
} catch (e) {
console.warn(e);
return;
}
}
await this.initPlug();
}
async initPlug() {
// check wether agent is present
// if not create it
if (!window.ic?.plug?.agent) {
console.warn("no agent found");
const result = await window.ic?.plug?.createAgent({
whitelist: this.whitelist,
host: this.host,
});
result
? console.log("agent created")
: console.warn("agent creation failed");
}
// check of if createActor method is available
if (!window.ic?.plug?.createActor) {
console.warn("no createActor found");
return;
}
// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
await window.ic.plug.agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}
const principal = await window.ic.plug.agent.getPrincipal();
this.state.update((state) => ({
...state,
principal,
accountId: window.ic.plug.sessionManager.sessionData.accountId,
isAuthed: "plug",
}));
await this.updateBalance();
console.log("plug is authed");
}
async bitfinityConnect() {
// check if bitfinity is installed in the browser
if (window.ic?.bitfinityWallet === undefined) {
window.open("https://wallet.infinityswap.one/", "_blank");
return;
}
// check if bitfinity is connected
const bitfinityConnected = await window.ic?.bitfinityWallet?.isConnected();
if (!bitfinityConnected) {
try {
await window.ic?.bitfinityWallet.requestConnect({ whitelist: this.whitelist });
console.log("bitfinity connected");
} catch (e) {
console.warn(e);
return;
}
}
await this.initBitfinity();
}
async initBitfinity() {
this.ledgerActor = (await window.ic.bitfinityWallet.createActor({
canisterId: ledgerCanisterId,
interfaceFactory: ledgerIdlFactory,
host: this.host,
})) as LedgerActor;
if (!this.ledgerActor) {
console.warn("couldn't create actors");
return;
}
const principal = await window.ic.bitfinityWallet.getPrincipal();
const accountId = await window.ic.bitfinityWallet.getAccountID();
this.state.update((state) => ({
...state,
principal,
accountId,
isAuthed: "bitfinity",
}));
await this.updateBalance();
console.log("bitfinity is authed");
}
async updateBalance() {
const store = get({ subscribe: this.state.subscribe });
let balance: number = 0;
if (store.isAuthed === "plug") {
let result = await window.ic.plug.requestBalance();
let ICP = result.find((asset) => asset.symbol === "ICP");
if (ICP) {
balance = ICP.amount;
}
} else if (store.isAuthed === "stoic") {
let res = await this.ledgerActor.account_balance({
account: AccountIdentifier.fromHex(store.accountId).toNumbers(),
});
balance = Number(res.e8s) / 1e8;
} else if (store.isAuthed === "bitfinity") {
if (process.env.DFX_NETWORK !== "ic") {
let res = await this.ledgerActor.account_balance({
account: AccountIdentifier.fromHex(store.accountId).toNumbers(),
});
balance = Number(res.e8s) / 1e8;
} else {
let result = await window.ic.bitfinityWallet.getUserAssets();
let ICP = result.find((asset) => asset.symbol === "ICP");
if (ICP) {
balance = Number(ICP.balance) / 1e8;
}
}
}
console.log("balance", balance);
this.state.update((prevState) => ({ ...prevState, balance }));
}
async transfer(toAddress: string, amount: bigint) {
const store = get({ subscribe: this.state.subscribe });
if (store.isAuthed === "plug") {
let height = await window.ic.plug.requestTransfer({
to: toAddress,
amount: Number(amount),
opts: {
fee: 10000,
},
});
console.log("sent", height);
} else if (store.isAuthed === "stoic" || store.isAuthed === "bitfinity") {
console.log("transfer...");
let res = await this.ledgerActor.transfer({
from_subaccount: [],
to: AccountIdentifier.fromHex(toAddress).toNumbers(),
amount: { e8s: amount },
fee: { e8s: 10000n },
memo: 0n,
created_at_time: [],
});
console.log("sent", res);
}
await this.updateBalance();
console.log("updated balance");
}
async disconnect() {
const store = get({ subscribe: this.state.subscribe });
if (store.isAuthed === "stoic") {
StoicIdentity.disconnect();
} else if (store.isAuthed === "plug") {
// awaiting this fails, promise never returns
window.ic.plug.disconnect();
} else if (store.isAuthed === "bitfinity") {
await window.ic.bitfinityWallet.disconnect();
}
console.log("disconnected");
this.state.update((prevState) => {
return {
...this.getDefaultState(),
};
});
}
}
declare global {
interface Window {
ic: {
bitfinityWallet: {
requestConnect: (options?: {
whitelist?: string[];
timeout?: number;
}) => Promise<{ derKey: Uint8Array; rawKey: Uint8Array }>;
isConnected: () => Promise<boolean>;
createActor: (options: {
canisterId: string;
interfaceFactory: InterfaceFactory;
host: string;
}) => Promise<Actor>;
getPrincipal: () => Promise<Principal>;
disconnect: () => Promise<boolean>;
getAccountID: () => Promise<string>;
getUserAssets: () => Promise<
{
id: string;
name: string;
fee: string;
symbol: string;
balance: string;
decimals: number;
hide: boolean;
isTestToken: boolean;
logo: string;
standard: string;
}[]
>;
};
plug: {
agent: HttpAgent;
sessionManager: {
sessionData: {
accountId: string;
};
};
getPrincipal: () => Promise<Principal>;
deleteAgent: () => void;
requestConnect: (options?: {
whitelist?: string[];
host?: string;
}) => Promise<any>;
createActor: (options: {}) => Promise<Actor>;
isConnected: () => Promise<boolean>;
disconnect: () => Promise<void>;
createAgent: (args?: {
whitelist: string[];
host?: string;
}) => Promise<undefined>;
requestBalance: () => Promise<
Array<{
amount: number;
canisterId: string | null;
image: string;
name: string;
symbol: string;
value: number | null;
}>
>;
requestTransfer: (arg: {
to: string;
amount: number;
opts?: {
fee?: number;
memo?: string;
from_subaccount?: number;
created_at_time?: {
timestamp_nanos: number;
};
};
}) => Promise<{ height: number }>;
};
};
}
}
export let createAuthStore = ({ host, whitelist }: { host?: string; whitelist?: string[]; }): AuthStore => {
return new AuthStoreClass({host, whitelist});
}
export type AuthStore = AuthStoreClass & Readable<AuthState>;