-
Notifications
You must be signed in to change notification settings - Fork 8
/
viteConnect.ts
85 lines (77 loc) · 2.12 KB
/
viteConnect.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
import Connector from '@vite/connector';
export const VCSessionKey = 'vcSession';
export class VC extends Connector {
constructor(session?: object, meta?: { session: object; bridge: string }) {
super(session, meta);
this.on('connect', (err: Error, payload: { params: [{ accounts: string[] }] }) => {
const { accounts } = payload.params[0];
this.setAccState(accounts);
});
this.on('disconnect', () => {
this.stopBizHeartBeat(); // stop heart beat when disconnected
localStorage.removeItem(VCSessionKey);
});
this.on('session_update', (update: null | { session: { accounts: string[] } }) => {
if (update) {
const { session } = update;
if (session && session.accounts) {
this.setAccState(session.accounts);
}
}
});
}
setAccState(accounts: string[] = []) {
if (!accounts || !accounts[0]) throw new Error('address is null');
this.saveSession();
}
saveSession() {
const sessionData = {
session: this.session,
timestamp: new Date().getTime(),
};
localStorage.setItem(VCSessionKey, JSON.stringify(sessionData));
}
async createSession() {
await super.createSession();
return this.uri;
}
async signAndSendTx(params: object[]) {
return new Promise((resolve, reject) => {
this.sendCustomRequest({ method: 'vite_signAndSendTx', params })
.then((result: object) => {
this.saveSession();
resolve(result);
})
.catch((e: Error) => reject(e));
});
}
}
export function getValidVCSession() {
let sessionData = null;
let session = null;
try {
const tm = localStorage.getItem(VCSessionKey);
if (tm) {
sessionData = JSON.parse(tm);
}
} catch (err) {
console.warn(err);
}
if (sessionData && sessionData.timestamp) {
// 60 minutes
if (new Date().getTime() - sessionData.timestamp < 1000 * 60 * 10) {
// console.log('Found session on localStorage.');
session = sessionData.session;
} else {
// console.log('Found session on localStorage, but it has expired.');
localStorage.removeItem(VCSessionKey);
}
}
return session;
}
export function initViteConnect(session?: object) {
return new VC({
session,
bridge: 'wss://biforst.vite.net',
});
}