-
Notifications
You must be signed in to change notification settings - Fork 58
/
popup.js
202 lines (160 loc) · 5.96 KB
/
popup.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
'use strict';
//this port communicate with background
var port = chrome.runtime.connect({name: "popup"});
port.postMessage({src: "popup",dst:"background"});
port.onMessage.addListener(function(msg) {
console.log("msg listened: " +JSON.stringify(msg));
if (!! msg.unapprovedTxs) {
processTx(msg.unapprovedTxs);
// var length = msg.unapprovedTxs.length
// if(msg.unapprovedTxs.length > 0) {
// var tx = msg.unapprovedTxs[length - 1].data
// processTx(tx);
// }else{
// console.log("no more unapprovedTxs")
// $(".icon-address.to input").val('');
// $("#amount").val('');
// }
}
});
//just for debug, listen to port disconnect event
port.onDisconnect.addListener(function(message) {
console.log("Port disconnected: " + JSON.stringify(message))
});
//post message to background
function messageToBackground(name,msg) {
var data = {}
data[name] = msg
port.postMessage({
src: "popup",dst:"background",
serialNumber: txTobeProcessed.serialNumber || "",
data : data
// data: {
// name : msg
// }
});
}
var txTobeProcessed = ""
//var serialNumber
function processTx(unapprovedTxs) {
var length = unapprovedTxs.length;
if(length > 0) {
var tx = unapprovedTxs[length - 1].data;
txTobeProcessed = tx
console.log("to address: " + tx.to + ", mount: " + tx.value)
$(".icon-address.to input").val(tx.to).trigger("input");
$("#amount").val(tx.value).trigger("input");
if(tx.gasLimit) $("#limit").val(tx.gasLimit).trigger("input");
if(tx.gasPrice) $("#price").val(tx.gasPrice).trigger("input");
if (length > 1)
$("#rejectAll").show();
if (tx.serialNumber) //value send by nebPay is using unit of Wei
$("#amount").val(Unit.fromBasic(tx.value, "nas")).trigger("input");
//serialNumber = tx.serialNumber || "";
if (!!tx.contract) {
//$("#contract_div").css("display", "unset");
$("#contract_div").show();
$("#contract").val(JSON.stringify(tx.contract));
$(".icon-address.to input").attr("disabled","disabled")
$("#amount").attr("disabled","disabled")
if(tx.contract.function && tx.contract.function.toLowerCase() == 'approve'){
$(".risk-tips").css('display','initial');
$(".risk-text").show();
$("#generate").css('background','#ff4d4f');
}
}
else {
//$("#contract_div").css("display", "none");
$("#contract_div").hide();
$("#contract").val("")
}
} else {
console.log("no more unapprovedTxs")
$(".icon-address.to input").val('').trigger("input");
$("#amount").val('').trigger("input");
$("#contract_div").hide();
$("#rejectAll").hide();
}
}
//load stored keyfle info from chrome.storage.local
document.addEventListener("DOMContentLoaded", function() {
console.log("popout page loaded...")
changeNetwork()
restoreAccount()
$("#contract_div").css("display","none");
});
var AccAddress ;
function getNextTx() {
console.log("to get next unapprovedTxs")
messageToBackground("getNextTx","true")
}
//tell background to check if the network is changed
function changeNetwork() {
var url = localSave.getItem("apiPrefix")
//var chainId = localSave.getItem("chainId")
console.log("to change network")
messageToBackground("changeNetwork",url)
}
//
function restoreAccount() {
chrome.storage.local.get(['keyInfo'], function(result) {
try {
console.log('keyInfo Value is :' + JSON.stringify(result.keyInfo));
result = JSON.parse(result.keyInfo)
} catch(e) {
console.log(e);
result = null;
}
if(!!result){
$(".container select-wallet-file").addClass("active1")
console.log("unlockFile:")
UnlockFile(result.fileJson, result.password)
if(typeof(hideKeyFileInput) === "function") hideKeyFileInput()
getNextTx()
}
});
}
function UnlockFile( fileJson, password) {
console.log("\tfileJson: " + JSON.stringify(fileJson) )
try {
var address;
var Account = require("nebulas").Account
var account = Account.fromAddress(fileJson.address)
account.fromKey(fileJson, password);
address = account.getAddressString();
gAccount = account;
AccAddress = address;
console.log("AccAddress got...")
port.postMessage({
src: "popup",
dst:"background",
data: {
AccAddress : AccAddress
}
});
console.log("\tfileJson: " + JSON.stringify(gAccount) )
$(".icon-address.from input").val(address).trigger("input"); // gen icon from addr, needs trigger 'input' event if via set o.value
$("#unlock").hide();
$("#send").show();
neb.api.getAccountState(address)
.then(function (resp) {
var nas = require("nebulas").Unit.fromBasic(resp.balance, "nas").toNumber();
console.log("\tbalance: " + nas +", nonce: " + resp.nonce)
$("#balance").val(nas).trigger("input"); // add comma & unit from value, needs trigger 'input' event if via set o.value
$("#nonce").val(parseInt(resp.nonce || 0) + 1);
})
.catch(function (e) {
// this catches e thrown by nebulas.js!neb
bootbox.dialog({
backdrop: true,
onEscape: true,
message: i18n.apiErrorToText(e.message),
size: "large",
title: "Error"
});
});
} catch (e) {
// this catches e thrown by nebulas.js!account
console.log("unlockFile error:" + JSON.stringify(e))
}
}