Skip to content

Commit 202b707

Browse files
committed
update: update with nebPay(post tx hash to server) and update UI
1 parent da19d66 commit 202b707

File tree

6 files changed

+95
-55
lines changed

6 files changed

+95
-55
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ window.addEventListener('message', function(e) {
5858
```
5959

6060
#### 3.2 Using NebPay SDK
61+
62+
Please refer to Dapp Example [SuperDictionary](https://github.com/15010159959/super-dictionary) to learn how to use this extension.
6163

6264
When developing your Dapp page, you can also use [NebPay SDK](https://github.com/nebulasio/nebPay) to communicate with ExtensionWallet. Just as the example below.
6365

background.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ chrome.runtime.onMessage.addListener(
264264
var txData = {
265265
"to":request.params.pay.to,
266266
"value":request.params.pay.value,
267-
"serialNumber":request.params.serialNumber
267+
"serialNumber":request.params.serialNumber,
268+
"callback":request.params.callback
268269
};
269270
if(type === "deploy")
270271
txData.contract ={

example/nebPay.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
22
"use strict";
33

4-
var payUrl = "http://18.221.150.42/api/pay";
4+
var payUrl = "https://pay.nebulas.io/api/pay";
55

66
module.exports = {
77
payUrl: payUrl
@@ -17,7 +17,7 @@ var openExtension = function (params) {
1717
if (params.listener) {
1818
callbackMap[params.serialNumber] = params.listener;
1919
}
20-
params.callback = undefined; //postMessage can't contains a function attr
20+
//params.callback = undefined; //postMessage can't contains a function attr
2121
params.listener = undefined; //postMessage can't contains a function attr
2222

2323
window.postMessage({
@@ -143,7 +143,7 @@ Pay.prototype = {
143143

144144
function openApp(params, options) {
145145
// if (typeof window !== "undefined") {
146-
params.callback = Config.payUrl;
146+
//params.callback = Config.payUrl;
147147
var appParams = {
148148
category: "jump",
149149
des: "confirmTransfer",
@@ -6495,8 +6495,8 @@ var defaultOptions = {
64956495
showQRCode: false,
64966496
container: undefined
64976497
},
6498-
// callback is the return url/func after payment
6499-
callback: undefined,
6498+
// callback is the return url after payment
6499+
callback: config.payUrl,
65006500
//listener:specify a listener function to handle payment feedback message(only valid for browser extension)
65016501
listener: undefined,
65026502
// if use nrc20pay ,should input nrc20 params like address, name, symbol, decimals

html/js/sendNas.js

+68-28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $("#generate").on("click", onClickGenerate);
1515
$("#reject").on("click", onClickReject);
1616
$("#modal-confirm .s").on("click", onClickModalConfirmS);
1717
$("#send_transaction").on("click", onClickSendTransaction);
18+
$("#change-wallet").on("click", onClickChangeWallet);
1819

1920
uiBlock.insert({
2021
footer: ".footer",
@@ -25,6 +26,15 @@ uiBlock.insert({
2526
selectWalletFile: [".select-wallet-file", onUnlockFile]
2627
});
2728

29+
function hideKeyFileInput(){
30+
$(".select-wallet-file").hide()
31+
$(".change_wallet").show()
32+
}
33+
34+
function onClickChangeWallet(){
35+
$(".select-wallet-file").show()
36+
$(".change_wallet").hide()
37+
}
2838

2939
function onUnlockFile(swf, fileJson, account, password) {
3040

@@ -85,13 +95,7 @@ function onUnlockFile(swf, fileJson, account, password) {
8595
}
8696

8797
function onClickReject() {
88-
// port.postMessage({
89-
// src: "popup",dst:"background",
90-
// data: {
91-
// reject : "true"
92-
// }
93-
//
94-
// })
98+
9599
messageToBackground("reject","true")
96100

97101
messageToBackground("default","Error: Transaction rejected by user")
@@ -102,12 +106,6 @@ function onClickReject() {
102106

103107
function onClickGenerate() {
104108

105-
// port.postMessage({
106-
// src: "popup",dst:"background",
107-
// data: {
108-
// generate : "true"
109-
// }
110-
// })
111109
messageToBackground("generate","true")
112110

113111
var fromAddress, toAddress, balance, amount, gaslimit, gasprice, nonce, bnAmount;
@@ -182,6 +180,56 @@ function onClickSendTransaction() {
182180
$("#value").val($("#amount").val()).trigger("input");
183181
}
184182

183+
var request = function(obj) {
184+
return new Promise((resolve, reject) => {
185+
let xhr = new XMLHttpRequest();
186+
xhr.open(obj.method || "GET", obj.url);
187+
if (obj.headers) {
188+
Object.keys(obj.headers).forEach(key => {
189+
xhr.setRequestHeader(key, obj.headers[key]);
190+
});
191+
}
192+
xhr.onload = () => {
193+
if (xhr.status >= 200 && xhr.status < 300) {
194+
resolve(xhr.response);
195+
} else {
196+
reject(xhr.statusText);
197+
}
198+
};
199+
xhr.onerror = () => reject(xhr.statusText);
200+
xhr.send(obj.body);
201+
});
202+
};
203+
204+
205+
function psotTxhashToServer(txTobeProcessed, mTxHash){
206+
207+
if(!txTobeProcessed.callback){
208+
console.log("this tx has no \"callback\"")
209+
return
210+
}
211+
var url = txTobeProcessed.callback;
212+
var payId = txTobeProcessed.serialNumber;
213+
var txHash= mTxHash;
214+
215+
url = `${url}?payId=${payId}&txHash=${txHash}`;
216+
217+
var obj = {
218+
url: url,
219+
method: "POST",
220+
body: {}
221+
};
222+
223+
request(obj).then(function (resp) {
224+
console.log("psotTxhashToServer result:" + JSON.stringify(resp))
225+
}).catch(function (err) {
226+
console.log("psotTxhashToServer error:" + JSON.stringify(err))
227+
})
228+
// .then(function () {
229+
// window.location.href = "check.html?" + mTxHash;
230+
// })
231+
}
232+
185233
function onClickModalConfirmS() {
186234
var mTxHash;
187235

@@ -192,16 +240,14 @@ function onClickModalConfirmS() {
192240

193241
console.log("txHash got...") //send txhash msg to background.js
194242

195-
// port.postMessage({
196-
// src: "popup",dst:"background",
197-
// serialNumber: serialNumber || "",
198-
// data: {
199-
// txhash : resp
200-
// }
201-
// });
202243
messageToBackground("txhash",resp)
203244

204-
window.location.href = "check.html?" + mTxHash;
245+
psotTxhashToServer(txTobeProcessed, mTxHash);
246+
247+
//window.location.href = "check.html?" + mTxHash;
248+
setTimeout(() => {
249+
window.location.href = "check.html?" + mTxHash;
250+
}, 1000)
205251

206252
return neb.api.getTransactionReceipt(mTxHash);
207253
}).then(function (resp) {
@@ -210,13 +256,7 @@ function onClickModalConfirmS() {
210256
$("#receipt_div").show();
211257

212258
console.log("txReceipt got...") //send txhash msg to background.js
213-
// port.postMessage({
214-
// src: "popup",dst:"background",
215-
// serialNumber: serialNumber || "",
216-
// data: {
217-
// receipt : resp
218-
// }
219-
// });
259+
220260
messageToBackground("receipt",resp)
221261

222262
// TODO 重新点击需要reset页面状态,清理setTimeout

html/sendNas.html

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
<div class=header></div>
5656
<div class="container select-wallet-file"></div>
5757

58+
<div class="container change_wallet active1">
59+
<div>Your wallet file is imported.</div>
60+
<button id="change-wallet" class="btn btn-block">change another wallet</button>
61+
</div>
62+
5863
<div id=send class="container send">
5964
<div class="form-group row">
6065
<div class=col-md-6>

popup.js

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12

23
//this port communicate with background
34
var port = chrome.runtime.connect({name: "popup"});
@@ -22,21 +23,22 @@ port.onDisconnect.addListener(function(message) {
2223
console.log("Port disconnected: " + JSON.stringify(message))
2324
});
2425

26+
//post message to background
2527
function messageToBackground(name,msg) {
2628
var data = {}
2729
data[name] = msg
2830
port.postMessage({
2931
src: "popup",dst:"background",
30-
serialNumber: serialNumber || "",
32+
serialNumber: txTobeProcessed.serialNumber || "",
3133
data : data
3234
// data: {
3335
// name : msg
3436
// }
3537
});
3638
}
3739

38-
var txTobeProcessed
39-
var serialNumber
40+
var txTobeProcessed = ""
41+
//var serialNumber
4042

4143
function processTx(tx) {
4244
txTobeProcessed = tx
@@ -47,7 +49,7 @@ function processTx(tx) {
4749
if(tx.serialNumber) //value send by nebPay is using unit of Wei
4850
$("#amount").val(Unit.fromBasic(tx.value, "nas"));
4951

50-
serialNumber = tx.serialNumber || "";
52+
//serialNumber = tx.serialNumber || "";
5153
if(!!tx.contract){
5254
$("#contract_div").css("display","unset");
5355
$("#contract").val(JSON.stringify(tx.contract))
@@ -71,31 +73,21 @@ var AccAddress ;
7173

7274
function getNextTx() {
7375
console.log("to get next unapprovedTxs")
74-
// port.postMessage({
75-
// src: "popup",
76-
// dst:"background",
77-
// data: {
78-
// getNextTx : 'true'
79-
// }
80-
// });
76+
8177
messageToBackground("getNextTx","true")
8278

8379
}
80+
81+
//tell background to check if the network is changed
8482
function changeNetwork() {
8583
var url = localSave.getItem("apiPrefix")
86-
var chainId = localSave.getItem("chainId")
84+
//var chainId = localSave.getItem("chainId")
8785
console.log("to change network")
88-
// port.postMessage({
89-
// src: "popup",
90-
// dst:"background",
91-
// data: {
92-
// network : url,
93-
// chainId : chainId
94-
// })
95-
// });
86+
9687
messageToBackground("changeNetwork",url)
9788
}
9889

90+
//
9991
function restoreAccount() {
10092

10193
chrome.storage.local.get(['keyInfo'], function(result) {
@@ -106,10 +98,10 @@ function restoreAccount() {
10698
$(".container select-wallet-file").addClass("active1")
10799
console.log("unlockFile:")
108100
UnlockFile(result.fileJson, result.password)
101+
hideKeyFileInput()
109102
getNextTx()
110103
}
111104

112-
113105
});
114106
}
115107

0 commit comments

Comments
 (0)