Skip to content

Commit f13dc99

Browse files
author
Booyoun-Kim
committed
Supported multiple keychain accounts
1 parent 2fae2e0 commit f13dc99

File tree

9 files changed

+51
-9
lines changed

9 files changed

+51
-9
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var keystation = new Keystation();
3232
keystation.client = "YOUR_WEB_URL";
3333
keystation.lcd = "https://lcd-do-not-abuse.cosmostation.io";
3434
keystation.path = "44/118/0/0/0";
35+
36+
// The account parameter is required for users having multiple keychain accounts.
37+
var keystationAccount = "";
3538
```
3639

3740
```js
@@ -45,16 +48,19 @@ var popup = keystation.openWindow("signin", prefix);
4548
var txJson = {"account_number":"18012","chain_id":"cosmoshub-2","fee":{"amount":[{"amount":"5000","denom":"uatom"}],"gas":"200000"},"memo":"","msgs":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[{"amount":"10000","denom":"uatom"}],"from_address":"cosmos1z67fshyr48pa9a6htdz4qd0zullfk6y0fgvxv7","to_address":"cosmos10nv3yj0jdxf02vxyc0tavf97fdvppdth6wmcn3"}}],"sequence":"24"};
4649

4750
var txJsonStr = JSON.stringify(txJson);
48-
var popup = keystation.openWindow("transaction", txJsonStr);
51+
var popup = keystation.openWindow("transaction", txJsonStr, keystationAccount);
4952
```
5053

5154
```js
5255
// add an EventListener
5356
window.addEventListener("message", function(e) {
5457
if (e.origin != "https://keystation.cosmostation.io") return;
5558
console.log(e.data);
59+
// e.data.account : User's keychain account. Remember this account!
60+
keystationAccount = e.data.account;
5661
} , false);
5762
```
63+
If the user is log in as Alice, he or she should use Alice account to sign transaction.
5864

5965
## Supporting Transaction Message Types
6066

example/index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
keystation.lcd = "https://lcd-do-not-abuse.cosmostation.io";
2222
keystation.path = "44/118/0/0/0";
2323

24+
// The account parameter is required for users having multiple keychain accounts.
25+
// Remember it and use to sign a transaction
26+
var keystationAccount = "";
27+
2428
function login() {
2529
var prefix = "cosmos";
2630
var popup = keystation.openWindow("signin", prefix);
@@ -34,7 +38,7 @@
3438

3539
function send() {
3640
var txJsonStr = $.trim($("textarea").val());
37-
var popup = keystation.openWindow("transaction", txJsonStr);
41+
var popup = keystation.openWindow("transaction", txJsonStr, keystationAccount);
3842
var popupTick = setInterval(function() {
3943
if (popup.closed) {
4044
clearInterval(popupTick);
@@ -46,6 +50,8 @@
4650
window.addEventListener("message", function(e) {
4751
if (e.origin != "https://keystation.cosmostation.io") return;
4852
console.log(e.data);
53+
// e.data.account : User's keychain account. Remember this account!
54+
keystationAccount = e.data.account;
4955
document.getElementById("result").innerHTML = JSON.stringify(e.data);
5056
} , false);
5157
</script>

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ func main() {
3636
HandlerFunc(signInHandler).
3737
Methods("GET")
3838
r.Path("/session").
39+
Queries("account", "{account}").
3940
Queries("client", "{client}").
4041
Queries("lcd", "{lcd}").
4142
Queries("path", "{path}").
4243
Queries("payload", "{payload}").
4344
HandlerFunc(sessionInHandler).
4445
Methods("GET")
4546
r.Path("/tx").
47+
Queries("account", "{account}").
4648
Queries("client", "{client}").
4749
Queries("lcd", "{lcd}").
4850
Queries("path", "{path}").

model.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ type importTemplateParams struct {
2323
type sessionTemplateParams struct {
2424
QueryUrl string
2525
Payload string
26+
Account string
2627
}
2728

2829
type ImportForm struct {
30+
Account string
2931
Client string
3032
Lcd string
3133
Path string
3234
Payload string
3335
}
3436

3537
type txTemplateParams struct {
38+
Account string
3639
Client string
3740
Lcd string
3841
Path string

service.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,18 @@ func signInHandler(w http.ResponseWriter, r *http.Request) {
9898
func sessionInHandler(w http.ResponseWriter, r *http.Request) {
9999
// HTML 폼 전송 처리
100100
importForm := ImportForm{
101+
Account: r.FormValue("account"),
101102
Client: r.FormValue("client"),
102103
Path: r.FormValue("path"),
103104
Payload: r.FormValue("payload"),
104105
}
105106

107+
account, err := url.QueryUnescape(importForm.Account)
108+
if err != nil {
109+
http.Error(w, err.Error(), http.StatusInternalServerError)
110+
return
111+
}
112+
106113
client, err := url.QueryUnescape(importForm.Client)
107114
if err != nil {
108115
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -139,6 +146,7 @@ func sessionInHandler(w http.ResponseWriter, r *http.Request) {
139146
params := sessionTemplateParams{}
140147
params.QueryUrl = "import?client=" + url.QueryEscape(client) + "&lcd=" + url.QueryEscape(lcd) + "&path=" + url.QueryEscape(path) + "&payload=" + url.QueryEscape(payloadForQuery)
141148
params.Payload = payload // address
149+
params.Account = account // keychain account
142150

143151
sessionTemplate.Execute(w, params)
144152
return
@@ -147,6 +155,12 @@ func sessionInHandler(w http.ResponseWriter, r *http.Request) {
147155
func txHandler(w http.ResponseWriter, r *http.Request) {
148156
vars := mux.Vars(r)
149157

158+
account, err := url.QueryUnescape(vars["account"])
159+
if err != nil {
160+
http.Error(w, err.Error(), http.StatusInternalServerError)
161+
return
162+
}
163+
150164
client, err := url.QueryUnescape(vars["client"])
151165
if err != nil {
152166
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -172,6 +186,7 @@ func txHandler(w http.ResponseWriter, r *http.Request) {
172186
}
173187

174188
params := txTemplateParams{}
189+
params.Account = account
175190
params.Client = client
176191
params.Lcd = lcd
177192
params.Path = path

www/js/pin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,13 @@
152152
var prefix = getParameterByName('payload');
153153
var address = getKeyStationMainAddress(decryptedMnemonics, hdPathResult, prefix);
154154

155+
var msgObj = {
156+
"address": address,
157+
"account": $(".input-account").val()
158+
}
159+
155160
setTimeout(function () {
156-
window.opener.postMessage(address, "*");
161+
window.opener.postMessage(msgObj, "*");
157162
window.close();
158163
}, 500);
159164
} else if (window.pinType == "tx") {

www/lib/keystation.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function Keystation(client, lcd, path) {
3030
this.keystationUrl = "https://keystation.cosmostation.io";
3131
}
3232

33-
Keystation.prototype.openWindow = function(type, payload) {
33+
Keystation.prototype.openWindow = function(type, payload, account = "") {
34+
// The account parameter is required for users having multiple keychain accounts.
3435
var apiUrl = "";
3536
switch (type) {
3637
case "signin":
@@ -41,5 +42,5 @@ Keystation.prototype.openWindow = function(type, payload) {
4142
break;
4243
}
4344

44-
return PopupCenter(this.keystationUrl + '/' + apiUrl + '?client=' + encodeURIComponent(this.client) + '&lcd=' + encodeURIComponent(this.lcd) + '&path=' + encodeURIComponent(this.path) + '&payload=' + encodeURIComponent(payload),'','400','690', {toolbar:1, resizable:1, location:1, menubar:1, status:1});
45+
return PopupCenter(this.keystationUrl + '/' + apiUrl + '?account=' + encodeURIComponent(account) + '&client=' + encodeURIComponent(this.client) + '&lcd=' + encodeURIComponent(this.lcd) + '&path=' + encodeURIComponent(this.path) + '&payload=' + encodeURIComponent(payload),'','400','690', {toolbar:1, resizable:1, location:1, menubar:1, status:1});
4546
}

www/session.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
gtag('config', 'UA-146700011-1');
2424
</script>
2525
<script>
26-
function sendEventToParent(data) {
27-
window.opener.postMessage(data, "*");
26+
function sendEventToParent(address, account) {
27+
var msgObj = {
28+
"address": address,
29+
"account": account
30+
}
31+
window.opener.postMessage(msgObj, "*");
2832
window.close();
2933
}
3034
</script>
@@ -38,7 +42,7 @@ <h2 id="session-page-title">Save your Account to Keychain</h2>
3842
<form class="keystation-form">
3943
<img src="img/keychain.png" alt="" width="100%"><br><br>
4044
<p>You MUST press "Save" in order to complete registration of your account. If this pop-up does not appear, please press the Key logo on the top right corner of your browser.</p>
41-
<button type="button" onclick="sendEventToParent({{.Payload}})">Next</button>
45+
<button type="button" onclick="sendEventToParent({{.Payload}}, {{.Account}})">Next</button>
4246
</form>
4347
<a href="https://support.google.com/chrome/answer/95606?co=GENIE.Platform%3DDesktop&hl=en" target="_blank">How can I manage saved mnemonics?</a>
4448
<div class="form-fotter">

www/transaction.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1><img src="img/keystation_logo_.png" alt="" width="145"></h1>
3434
<h2>Sign Transaction</h2>
3535
<form class="keystation-form">
3636
<input style="display:none;" type="text" tabindex="-1"
37-
spellcheck="false">
37+
spellcheck="false" name="account" value="{{.Account}}">
3838
<input style="display:none;" type="password" autocomplete="current-password" tabindex="-1"
3939
spellcheck="false">
4040
<div class="keystation-url-info">

0 commit comments

Comments
 (0)