Skip to content

Commit dbad9c8

Browse files
committed
update how to get user account
1 parent f3fd51c commit dbad9c8

File tree

2 files changed

+86
-16
lines changed

2 files changed

+86
-16
lines changed

README.md

+59-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,30 @@ We will keep improving this extension, and any suggestions are welcome!
2424

2525
Now WebExtensionApp supports two different ways to communicate with Dapp page.
2626

27-
#### 3.1 Using `postMessage`
27+
28+
#### 3.1 Using NebPay SDK
29+
30+
Please refer to Dapp Example [SuperDictionary](https://github.com/15010159959/super-dictionary) to learn how to use this extension.
31+
32+
When developing your Dapp page, you can use [NebPay SDK](https://github.com/nebulasio/nebPay) to communicate with ExtensionWallet. Just as the example below.
33+
34+
To call a SmartContract through extensionWallet, you should use [`nebpay.call`](https://github.com/nebulasio/nebPay/blob/master/doc/NebPay_Introduction.md#call) or [`nebpay.simulateCall`](https://github.com/nebulasio/nebPay/blob/master/doc/NebPay_Introduction.md#simulatecall) to send a transaction as below:
35+
```js
36+
nebPay.call(to, value, callFunction, callArgs, {
37+
qrcode: {
38+
showQRCode: true
39+
},
40+
listener: cbCallDapp //specify a listener to handle the transaction result
41+
});
42+
43+
function cbCallDapp(resp){
44+
console.log("response: " + JSON.stringify(resp))
45+
}
46+
47+
```
48+
49+
50+
#### 3.2 Using `postMessage`
2851

2952
When developing your Dapp page, you can use `postMessage` API to communicate with ExtensionWallet, and use `window.addEventListener` to listen the message answer. Just as the example below.
3053

@@ -54,27 +77,47 @@ window.addEventListener('message', function(e) {
5477
})
5578
```
5679

57-
#### 3.2 Using NebPay SDK
80+
### 4 how to get account address
5881

59-
Please refer to Dapp Example [SuperDictionary](https://github.com/15010159959/super-dictionary) to learn how to use this extension.
60-
61-
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.
82+
It is useful for Dapp to get the current account address in the extension. Here is the explanation on how to achive this.
6283

63-
To call a SmartContract through extensionWallet, you should use [`nebpay.call`](https://github.com/nebulasio/nebPay/blob/master/doc/NebPay_Introduction.md#call) or [`nebpay.simulateCall`](https://github.com/nebulasio/nebPay/blob/master/doc/NebPay_Introduction.md#simulatecall) to send a transaction as below:
84+
Method 1:
6485
```js
65-
nebPay.call(to, value, callFunction, callArgs, {
66-
qrcode: {
67-
showQRCode: true
68-
},
69-
listener: cbCallDapp //specify a listener to handle the transaction result
70-
});
71-
72-
function cbCallDapp(resp){
73-
console.log("response: " + JSON.stringify(resp))
86+
var userAddrerss;
87+
88+
function getUserAddress() {
89+
console.log("********* get account ************")
90+
window.postMessage({
91+
"target": "contentscript",
92+
"data":{
93+
},
94+
"method": "getAccount",
95+
}, "*");
96+
}
97+
// listen message from contentscript
98+
window.addEventListener('message', function(e) {
99+
// e.detail contains the transferred data (can
100+
console.log("recived by page:" + e + ", e.data:" + JSON.stringify(e.data));
101+
if (!!e.data.data.account) {
102+
userAddrerss = e.data.data.account;
74103
}
75-
104+
})
105+
106+
```
107+
108+
Method 2:
109+
A module `NasExtWallet` is injected to your page if NasExtWallet is installed, then you can use the code below to get user account:
110+
```js
111+
var userAddrerss;
112+
function callback(addr){
113+
userAddrerss = addr;
114+
}
115+
116+
NasExtWallet.getUserAddress(callback)
117+
76118
```
77119

120+
78121
### example page
79122
And you can use `example/TestPage.html` to take a test.
80123

inpage.js

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ var webExtensionWallet = "for nebulas";
44

55
console.log("webExtensionWallet is defined:" + webExtensionWallet);
66

7+
var _NasExtWallet = function () {
8+
this.getUserAddressCallback ;
9+
10+
this.getUserAddress = function(callback) {
11+
//console.log("********* get account ************")
12+
getUserAddressCallback = callback
13+
window.postMessage({
14+
"target": "contentscript",
15+
"data":{
16+
},
17+
"method": "getAccount",
18+
}, "*");
19+
}
20+
21+
// listen message from contentscript
22+
window.addEventListener('message', function(e) {
23+
// e.detail contains the transferred data (can
24+
if (!!e.data.data.account) {
25+
userAddrerss = e.data.data.account;
26+
if(typeof getUserAddressCallback === 'function'){
27+
getUserAddressCallback(userAddrerss)
28+
}
29+
}
30+
})
31+
}
32+
33+
var NasExtWallet = new _NasExtWallet()
734

835

936

0 commit comments

Comments
 (0)