launchpad-ledger: Add Node.js demo

This commit is contained in:
willclarktech 2020-09-15 18:51:11 +02:00
parent 4dd94fe115
commit 0d4baf1fe3
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 62 additions and 1 deletions

View File

@ -0,0 +1,14 @@
const demo = require("../build/demo/node");
async function run() {
const accounts = await demo.getAccounts();
console.info("Accounts from Ledger device:");
console.table(accounts);
const address = accounts[0].address;
const signature = await demo.sign(address, address);
console.info("Signature from Ledger device:");
console.info(signature);
}
run().catch(console.error);

View File

@ -0,0 +1,47 @@
import { toHex, toUtf8 } from "@cosmjs/encoding";
import { StdSignature } from "@cosmjs/launchpad";
import { LedgerSigner } from "../ledgersigner";
function createMessage(fromAddress: string, toAddress: string): string {
return `{
"account_number": 0,
"chain_id": "testing",
"fee": {
"amount": [{ "amount": 100, "denom": "ucosm" }],
"gas": 250
},
"memo": "Some memo",
"msgs": [{
"type": "cosmos-sdk/MsgSend",
"value": {
"amount": [{
"amount": "1234567",
"denom": "ucosm"
}],
"from_address": "${fromAddress}",
"to_address": "${toAddress}"
}
}],
"sequence": 0
}`;
}
const signer = new LedgerSigner({ testModeAllowed: true });
export async function getAccounts(): Promise<
ReadonlyArray<{
readonly algo: string;
readonly address: string;
readonly pubkey: string;
}>
> {
const accounts = await signer.getAccounts();
return accounts.map((account) => ({ ...account, pubkey: toHex(account.pubkey) }));
}
export async function sign(fromAddress: string, toAddress: string): Promise<StdSignature> {
const rawMessage = createMessage(fromAddress, toAddress);
const message = JSON.stringify(JSON.parse(rawMessage));
return signer.sign(fromAddress, toUtf8(message));
}

View File

@ -8,7 +8,7 @@ module.exports = [
{
// bundle used for Ledger demo
target: target,
entry: glob.sync("./build/demo/index.js"),
entry: glob.sync("./build/demo/web.js"),
output: {
path: demodir,
filename: "ledger.js",