-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
165 lines (136 loc) · 4.52 KB
/
Copy pathindex.ts
File metadata and controls
165 lines (136 loc) · 4.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
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
import AlgoSignerWallet from "./wallets/algosigner";
import MyAlgoConnectWallet from "./wallets/myalgoconnect";
import InsecureWallet from "./wallets/insecure";
import WC from "./wallets/walletconnect";
import { PermissionCallback, Wallet, SignedTxn } from "./wallets/wallet";
import { Transaction, TransactionSigner } from "algosdk";
export {
PermissionResult,
PermissionCallback,
Wallet,
SignedTxn,
} from "./wallets/wallet";
export const allowedWallets = {
"wallet-connect": WC,
"algo-signer": AlgoSignerWallet,
"my-algo-connect": MyAlgoConnectWallet,
"insecure-wallet": InsecureWallet,
};
const walletPreferenceKey = "wallet-preference";
const acctListKey = "acct-list";
const acctPreferenceKey = "acct-preference";
const mnemonicKey = "mnemonic";
export class SessionWallet {
wallet: Wallet;
wname: string;
network: string;
permissionCallback?: PermissionCallback;
constructor(
network: string,
permissionCallback?: PermissionCallback,
wname?: string
) {
if (wname) this.setWalletPreference(wname);
this.network = network;
this.wname = this.walletPreference();
if (permissionCallback) this.permissionCallback = permissionCallback;
if (!(this.wname in allowedWallets)) return;
this.wallet = new allowedWallets[this.wname](network);
this.wallet.permissionCallback = this.permissionCallback;
this.wallet.accounts = this.accountList();
this.wallet.defaultAccount = this.accountIndex();
}
async connect(): Promise<boolean> {
if (this.wallet === undefined) return false;
switch (this.wname) {
case "insecure-wallet":
const storedMnemonic = this.mnemonic();
const mnemonic = storedMnemonic
? storedMnemonic
: prompt(
"Paste your mnemonic space delimited (DO NOT USE WITH MAINNET ACCOUNTS)"
);
if (!mnemonic) return false;
if (await this.wallet.connect(mnemonic)) {
this.setMnemonic(mnemonic);
this.setAccountList(this.wallet.accounts);
this.wallet.defaultAccount = this.accountIndex();
return true;
}
break;
case "wallet-connect":
await this.wallet.connect((acctList) => {
this.setAccountList(acctList);
this.wallet.defaultAccount = this.accountIndex();
});
return true;
default:
if (await this.wallet.connect()) {
this.setAccountList(this.wallet.accounts);
this.wallet.defaultAccount = this.accountIndex();
return true;
}
break;
}
// Fail
this.disconnect();
return false;
}
connected(): boolean {
return this.wallet !== undefined && this.wallet.isConnected();
}
getSigner(): TransactionSigner {
return (txnGroup: Transaction[], indexesToSign: number[]) => {
return Promise.resolve(this.signTxn(txnGroup)).then((txns) => {
return txns.map((tx) => {
return tx.blob;
}).filter((_, index) => indexesToSign.includes(index));
});
};
}
setAccountList(accts: string[]) {
sessionStorage.setItem(acctListKey, JSON.stringify(accts));
}
accountList(): string[] {
const accts = sessionStorage.getItem(acctListKey);
return accts === "" || accts === null ? [] : JSON.parse(accts);
}
setAccountIndex(idx: number) {
this.wallet.defaultAccount = idx;
sessionStorage.setItem(acctPreferenceKey, idx.toString());
}
accountIndex(): number {
const idx = sessionStorage.getItem(acctPreferenceKey);
return idx === null || idx === "" ? 0 : parseInt(idx, 10);
}
setWalletPreference(wname: string) {
this.wname = wname;
sessionStorage.setItem(walletPreferenceKey, wname);
}
walletPreference(): string {
const wp = sessionStorage.getItem(walletPreferenceKey);
return wp === null ? "" : wp;
}
setMnemonic(m: string) {
sessionStorage.setItem(mnemonicKey, m);
}
mnemonic(): string {
const mn = sessionStorage.getItem(mnemonicKey);
return mn === null ? "" : mn;
}
disconnect() {
if (this.wallet !== undefined) this.wallet.disconnect();
sessionStorage.setItem(walletPreferenceKey, "");
sessionStorage.setItem(acctPreferenceKey, "");
sessionStorage.setItem(acctListKey, "");
sessionStorage.setItem(mnemonicKey, "");
}
getDefaultAccount(): string {
if (!this.connected()) return "";
return this.wallet.getDefaultAccount();
}
async signTxn(txns: Transaction[]): Promise<SignedTxn[]> {
if (!this.connected() && !(await this.connect())) return [];
return this.wallet.signTxn(txns);
}
}