Skip to content

Commit f0d63d4

Browse files
authored
Merge pull request #1948 from cosmos/ledger-demo-fixes
Ledger web demo fixes
2 parents 1d8dc72 + 4d6724a commit f0d63d4

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

packages/ledger-amino/demo/index.css

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
section {
2-
display: flex;
3-
align-items: center;
4-
justify-content: center;
2+
max-width: 500px;
53
margin: auto;
64
padding: 0.5em;
75
}
@@ -20,5 +18,9 @@ input {
2018

2119
textarea {
2220
width: 32em;
23-
height: 28em;
21+
height: 5em;
22+
}
23+
24+
#accounts {
25+
white-space: pre-wrap;
2426
}

packages/ledger-amino/demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h1>Ledger Demo</h1>
2222
<button onclick="createSigner().then((signer) => {window.signer = signer; console.log('Connected')}, console.error)">
2323
Connect to Ledger
2424
</button>
25-
</div>
25+
</section>
2626
<section>
2727
<h2>Accounts</h2>
2828
<button onclick="getAccounts(window.signer)">

packages/ledger-amino/src/demo/web.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AccountData, makeCosmoshubPath, StdSignDoc } from "@cosmjs/amino";
2-
import { pathToString, stringToPath } from "@cosmjs/crypto";
1+
import { AccountData, StdSignDoc } from "@cosmjs/amino";
2+
import { HdPath, pathToString, stringToPath } from "@cosmjs/crypto";
33
import { toBase64 } from "@cosmjs/encoding";
44
import { Uint53 } from "@cosmjs/math";
55
import { assert } from "@cosmjs/utils";
@@ -8,14 +8,33 @@ import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
88

99
import { LedgerSigner } from "../ledgersigner";
1010

11-
const getElement = (id: string): HTMLInputElement => {
11+
function getElement(id: string): HTMLElement {
1212
const e = document.getElementById(id);
13-
assert(e instanceof HTMLInputElement, "got the wrong element!");
13+
assert(e, `Element with ID '${id}' not found`);
1414
return e;
15-
};
15+
}
16+
17+
function getInputElement(id: string): HTMLInputElement {
18+
const e = getElement(id);
19+
assert(e instanceof HTMLInputElement, `Element with ID '${id}' is not an input element`);
20+
return e;
21+
}
22+
23+
function getTextAreaElement(id: string): HTMLTextAreaElement {
24+
const e = getElement(id);
25+
assert(e instanceof HTMLTextAreaElement, `Element with ID '${id}' is not a text area element`);
26+
return e;
27+
}
1628

17-
const accountNumbers = [0, 1, 2, 10];
18-
const paths = accountNumbers.map(makeCosmoshubPath);
29+
const paths: HdPath[] = [
30+
stringToPath("m/44'/118'/0'/0/0"),
31+
stringToPath("m/44'/118'/1'/0/0"),
32+
stringToPath("m/44'/118'/2'/0/0"),
33+
stringToPath("m/44'/118'/3'/0/0"),
34+
stringToPath("m/44'/118'/4'/0/0"),
35+
stringToPath("m/44'/118'/5'/0/0"),
36+
stringToPath("m/44'/118'/6'/0/0"),
37+
];
1938

2039
let accounts: readonly AccountData[] = [];
2140

@@ -57,9 +76,9 @@ const updateMessage = (accountNumberInput: unknown): void => {
5776
}
5877

5978
const address = accounts[accountNumber].address;
60-
const addressInput = getElement("address");
79+
const addressInput = getInputElement("address");
6180
addressInput.value = address;
62-
const signDocTextArea = getElement("sign-doc");
81+
const signDocTextArea = getTextAreaElement("sign-doc");
6382
signDocTextArea.textContent = createSignDoc(accountNumber, address);
6483
};
6584

@@ -68,7 +87,7 @@ const setPath = (accountNumberInput: unknown): void => {
6887
const accountNumber = Uint53.fromString(accountNumberInput).toNumber();
6988

7089
const path = pathToString(paths[accountNumber]);
71-
const pathInput = getElement("path");
90+
const pathInput = getInputElement("path");
7291
pathInput.value = path;
7392
};
7493

@@ -87,11 +106,11 @@ const getAccounts = async function getAccounts(signer: LedgerSigner | undefined)
87106
console.error("Please wait for transport to connect");
88107
return;
89108
}
90-
const accountNumberInput1 = getElement("account-number1");
91-
const accountNumberInput2 = getElement("account-number2");
92-
const addressInput = getElement("address");
109+
const accountNumberInput1 = getInputElement("account-number1");
110+
const accountNumberInput2 = getInputElement("account-number2");
111+
const addressInput = getInputElement("address");
93112
const accountsDiv = getElement("accounts");
94-
const signDocTextArea = getElement("sign-doc");
113+
const signDocTextArea = getTextAreaElement("sign-doc");
95114
accountsDiv.textContent = "Loading...";
96115

97116
try {
@@ -100,7 +119,7 @@ const getAccounts = async function getAccounts(signer: LedgerSigner | undefined)
100119
...account,
101120
pubkey: toBase64(account.pubkey),
102121
}));
103-
accountsDiv.textContent = JSON.stringify(prettyAccounts, null, "\n");
122+
accountsDiv.textContent = prettyAccounts.map((pa) => JSON.stringify(pa, null, 4)).join("\n\n");
104123
const accountNumber = 0;
105124

106125
// Show address block
@@ -125,7 +144,7 @@ const showAddress = async function showAddress(signer: LedgerSigner | undefined)
125144
console.error("Please wait for transport to connect");
126145
return;
127146
}
128-
const path = stringToPath(getElement("path").value);
147+
const path = stringToPath(getInputElement("path").value);
129148
await signer.showAddress(path);
130149
};
131150

@@ -138,8 +157,8 @@ const sign = async function sign(signer: LedgerSigner | undefined): Promise<void
138157
signatureDiv.textContent = "Loading...";
139158

140159
try {
141-
const address = getElement("address").value;
142-
const signDocJson = getElement("sign-doc").textContent;
160+
const address = getInputElement("address").value;
161+
const signDocJson = getTextAreaElement("sign-doc").textContent;
143162
const signDoc: StdSignDoc = JSON.parse(signDocJson);
144163
const signature = await signer.signAmino(address, signDoc);
145164
signatureDiv.textContent = JSON.stringify(signature, null, "\t");

0 commit comments

Comments
 (0)