-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpwallet.js
254 lines (216 loc) · 7.21 KB
/
pwallet.js
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
import algosdk from "algosdk";
import Encrypt from "encrypt-with-password";
import Pipeline from "./index";
import { saveAs } from "file-saver";
import pipeWalletStyle from './pwalletCss.js'
import html from './pwalletHTML.js'
function pipeModalScript() {
var modal = document.getElementById("pipeWalletModal");
var span = document.getElementsByClassName("close")[0];
var crtBtn = document.getElementById("pipeWcreate");
var ldBtn = document.getElementById("pwLoad");
var selBtn = document.getElementById("pwSelect");
var signBtn = document.getElementById("pwSign");
var history = document.getElementById("pwHistory");
var historyBtn = document.getElementById("pwHistoryBtn");
var exportBtn = document.getElementById("pwExportBtn");
var exportBtn2 = document.getElementById("pwExportBtn2");
var pwExportDiv = document.getElementById("pwExport");
var responsiveInput = document.getElementById("pwInput");
responsiveInput.oninput = function () {
responsiveInput.style.width = (responsiveInput.value.length * 50).toString() + "px"
}
pwExportDiv.style.display = "none";
exportBtn2.onclick = function () {
PipeWallet.export();
};
exportBtn.onclick = function () {
if (PipeWallet.exportShow) {
pwExportDiv.style.display = "none";
PipeWallet.exportShow = false;
} else {
pwExportDiv.style.display = "block";
PipeWallet.exportShow = true;
}
};
historyBtn.onclick = function () {
if (PipeWallet.history) {
history.style.display = "none";
PipeWallet.history = false;
} else {
history.style.display = "block";
PipeWallet.history = true;
}
};
history.style.display = "none";
signBtn.onclick = function () {
PipeWallet.approve();
};
ldBtn.onclick = function () {
let decryptedWallet = PipeWallet.loadWallet(
document.getElementById("pwWord").value
);
let newHTML = '<option value="accounts">Select an account</option>">';
Object.keys(decryptedWallet).forEach((address) => {
newHTML += '<option value="' + address + '">' + address + "</option>";
});
document.getElementById("pwSelect").innerHTML = newHTML;
};
crtBtn.onclick = PipeWallet.createAccount;
span.onclick = function () {
modal.style.display = "none";
};
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
selBtn.onchange = function (event) {
let address = event.target.value;
Pipeline.address = address;
document.getElementById("selectedpwAccount").innerText = address;
PipeWallet.getHistory();
};
document
.getElementById("pwInputfile")
.addEventListener("change", function () {
var fr = new FileReader();
fr.onload = function () {
localStorage.setItem("PipeWallet", fr.result);
alert('Wallet loaded! Enter your password and press "load accounts"');
};
fr.readAsText(this.files[0]);
});
}
export default class PipeWallet {
static init() {
this.history = false
this.exportShow = false
this.approved = false
let walletDiv = document.createElement("div")
walletDiv.id = "pipeWallet"
document.body.appendChild(walletDiv)
document.getElementById("pipeWallet").innerHTML =
"<style>" + pipeWalletStyle + "</style>" + html;
Pipeline.address = "";
pipeModalScript();
}
static loadWallet(password = "testing1234") {
let cipherText = localStorage.getItem("PipeWallet");
if (cipherText !== null) {
let decipheredString = Encrypt.decrypt(cipherText, password);
return JSON.parse(decipheredString);
} else {
alert(
"No wallet detected. Please create a new wallet or restore from data"
);
return {};
}
}
static restoreFromData(data = "") {
localStorage.setItem("PipeWallet", data);
}
static export() {
return localStorage.getItem("PipeWallet");
}
static updateWallet(data = {}, password = "") {
data = JSON.stringify(data);
localStorage.setItem("PipeWallet", Encrypt.encrypt(data, password));
}
static sign(txn = undefined) {
let password = document.getElementById("pwWord").value;
let decryptedWallet = PipeWallet.loadWallet(password);
let sk = this.getSecret(Pipeline.address, decryptedWallet);
let signedTxn = algosdk.signTransaction(txn, sk);
console.log(signedTxn);
return signedTxn;
}
static getSecret(address, decryptedWallet) {
let skU8Array = new Uint8Array(64);
let skArray = decryptedWallet[address].split(",");
console.log(skArray);
for (let i = 0; i < 64; i++) {
skU8Array[i] = parseInt(skArray[i]);
}
return skU8Array;
}
static createAccount() {
let password = document.getElementById("pwWord").value;
let decryptedWallet = PipeWallet.loadWallet(password);
let newAccount = algosdk.generateAccount();
decryptedWallet[newAccount.addr] = String(newAccount.sk);
PipeWallet.updateWallet(decryptedWallet, password);
}
static openWallet() {
this.approved = false;
document.getElementById("pipeWalletModal").style.display = "block";
}
static approve() {
PipeWallet.approved = true;
}
static waitForApproval() {
return new Promise((resolve) => {
var start_time = Date.now();
function checkFlag() {
if (PipeWallet.approved) {
resolve(PipeWallet.approved);
} else if (Date.now() > start_time + 60000) {
resolve(false);
} else {
window.setTimeout(checkFlag, 100);
}
}
checkFlag();
});
}
static previewTxn(txn) {
let keys = Object.keys(txn);
let html = "";
keys.forEach((key) => {
if (typeof txn[key] === "string" || typeof txn[key] === "number")
html += "<h4>" + key + ": " + "</h4>" + txn[key];
});
document.getElementById("pwPreview").innerHTML = html;
}
static clearPreviewTxn() {
document.getElementById("pwPreview").innerText = "";
}
static close() {
document.getElementById("pipeWalletModal").style.display = "none";
}
static async getHistory() {
if (Pipeline.address !== "") {
let address = Pipeline.address;
let url = Pipeline.main
? "https://mainnet-idx.algonode.cloud/v2/accounts/" +
address +
"/transactions"
: "https://testnet-idx.algonode.cloud/v2/accounts/" +
address +
"/transactions";
let txUrl = Pipeline.main
? "https://algoexplorer.io/tx/"
: "https://testnet.algoexplorer.io/tx/";
let data = await fetch(url);
let dataJSON = await data.json();
console.log(dataJSON);
let transactions = dataJSON.transactions;
let html = "";
transactions.forEach((txn) => {
html += '<a href="' + txUrl + txn.id + '">' + txn.id + "</a><br></br>";
});
document.getElementById("pwHistory").innerHTML = html;
} else {
alert("Please load your wallet");
}
}
static export() {
let data = localStorage.getItem("PipeWallet");
let blob = new Blob([data], { type: "text/plain;charset=utf-8" });
saveAs(blob, "PipeWallet.txt");
}
static showHide(show = [], hide = []){
show.forEach(id => document.getElementById(id).style.display = "block")
hide.forEach(id => document.getElementById(id).style.display = "none")
}
}