forked from nimiq/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiframe.ts
More file actions
68 lines (58 loc) · 2.52 KB
/
Copy pathiframe.ts
File metadata and controls
68 lines (58 loc) · 2.52 KB
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
import { RpcServer } from '@nimiq/rpc';
import { BrowserDetection } from '@nimiq/utils';
import { WalletStore } from '@/lib/WalletStore';
import { WalletInfoEntry, WalletInfo } from '@/lib/WalletInfo';
import CookieJar from '@/lib/CookieJar';
import Config from 'config';
import { Account, Cashlink as PublicCashlink } from './lib/PublicRequestTypes';
import Cashlink from './lib/Cashlink';
import { CashlinkStore } from './lib/CashlinkStore';
import { RequestType } from './lib/RequestTypes';
class IFrameApi {
public static run() {
const rpcServer = new RpcServer(Config.privilegedOrigins);
// Register handlers
rpcServer.onRequest(RequestType.LIST, IFrameApi.list);
rpcServer.onRequest(RequestType.LIST_CASHLINKS, IFrameApi.cashlinks);
rpcServer.init();
}
public static async list(): Promise<Account[]> {
let wallets: WalletInfoEntry[];
if (BrowserDetection.isIOS() || BrowserDetection.isSafari()) {
wallets = await CookieJar.eat();
} else {
wallets = await WalletStore.Instance.list();
}
if (wallets.length > 0) {
return wallets
.filter((wallet) => !wallet.keyMissing)
.map((wallet) => WalletInfo.objectToAccountType(wallet));
}
// If no wallets exist, see if the Keyguard has keys
const client = new (await import('@nimiq/keyguard-client')).KeyguardClient(Config.keyguardEndpoint);
const hasKeys = await client.hasKeys();
if (hasKeys.success) {
throw new Error('ACCOUNTS_LOST');
}
// If no keys exist, check for legacy accounts
const hasLegacyAccounts = await client.hasLegacyAccounts();
if (hasLegacyAccounts.success) {
throw new Error('MIGRATION_REQUIRED');
}
return [];
}
public static async cashlinks(): Promise<PublicCashlink[]> {
// Cashlinks are not stored in cookies on iOS/Safari, because they would take up too much space.
// TODO: Use Storage Access API on iOS/Safari to access IndexedDB in the iframe.
if (BrowserDetection.isIOS() || BrowserDetection.isSafari()) return [];
const cashlinksEntries = await CashlinkStore.Instance.list();
return cashlinksEntries.map((cashlink) => ({
address: cashlink.address,
message: cashlink.message,
value: cashlink.value,
status: cashlink.state,
theme: cashlink.theme || Cashlink.DEFAULT_THEME,
}));
}
}
IFrameApi.run();