launchpad-ledger: Add ledger demo and wallet

This commit is contained in:
willclarktech 2020-09-10 11:32:43 +02:00
parent 59c5ae5c8b
commit a5891f147f
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
7 changed files with 116 additions and 7 deletions

View File

@ -1 +1,2 @@
export { LaunchpadLedger } from "./launchpadledger";
export { LedgerWallet } from "./ledgerwallet";

View File

@ -0,0 +1,38 @@
import { toHex, toUtf8 } from "@cosmjs/encoding";
import { LedgerWallet } from "./ledgerwallet";
declare const window: any;
declare const document: any;
const ledgerWallet = new LedgerWallet({ testModeAllowed: true });
window.getAccounts = async function getAccounts(): Promise<void> {
const addressInput = document.getElementById("address");
const accountsDiv = document.getElementById("accounts");
accountsDiv.textContent = "Loading...";
try {
const accounts = await ledgerWallet.getAccounts();
const prettyAccounts = accounts.map((account) => ({ ...account, pubkey: toHex(account.pubkey) }));
accountsDiv.textContent = JSON.stringify(prettyAccounts, null, "\t");
addressInput.value = accounts[0].address;
} catch (error) {
accountsDiv.textContent = error;
}
};
window.sign = async function sign(): Promise<void> {
const signatureDiv = document.getElementById("signature");
signatureDiv.textContent = "Loading...";
try {
const address = document.getElementById("address").value;
const rawMessage = document.getElementById("message").textContent;
const message = JSON.stringify(JSON.parse(rawMessage));
const signature = await ledgerWallet.sign(address, toUtf8(message));
signatureDiv.textContent = JSON.stringify(signature, null, "\t");
} catch (error) {
signatureDiv.textContent = error;
}
};

View File

@ -0,0 +1,45 @@
import { AccountData, encodeSecp256k1Signature, OfflineSigner, StdSignature } from "@cosmjs/launchpad";
import { LaunchpadLedger } from "./launchpadledger";
interface LedgerWalletOptions {
readonly testModeAllowed: boolean;
}
export class LedgerWallet implements OfflineSigner {
private readonly ledger: LaunchpadLedger;
private address: string | undefined;
private pubkey: Uint8Array | undefined;
constructor(options?: LedgerWalletOptions) {
this.ledger = new LaunchpadLedger(options);
}
public async getAccounts(): Promise<readonly AccountData[]> {
await this.ledger.connect();
const address = (this.address = this.address || (await this.ledger.getCosmosAddress()));
const pubkey = (this.pubkey = this.pubkey || (await this.ledger.getPubKey()));
return [
{
algo: "secp256k1",
address: address,
pubkey: pubkey,
},
];
}
public async sign(address: string, message: Uint8Array): Promise<StdSignature> {
await this.ledger.connect();
const thisAddress = (this.address = this.address || (await this.ledger.getCosmosAddress()));
if (address !== thisAddress) {
throw new Error(`Address ${address} not found in wallet`);
}
const signature = await this.ledger.sign(message);
const pubkey = (this.pubkey = this.pubkey || (await this.ledger.getPubKey()));
return encodeSecp256k1Signature(pubkey, signature);
}
}

View File

@ -1 +1,2 @@
export { LaunchpadLedger } from "./launchpadledger";
export { LedgerWallet } from "./ledgerwallet";

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1,13 @@
import { AccountData, OfflineSigner, StdSignature } from "@cosmjs/launchpad";
interface LedgerWalletOptions {
readonly testModeAllowed: boolean;
}
export declare class LedgerWallet implements OfflineSigner {
private readonly ledger;
private address;
private pubkey;
constructor(options?: LedgerWalletOptions);
getAccounts(): Promise<readonly AccountData[]>;
sign(address: string, message: Uint8Array): Promise<StdSignature>;
}
export {};

View File

@ -1,19 +1,29 @@
const glob = require("glob");
const path = require("path");
const webpack = require("webpack");
// const webpack = require("webpack");
const target = "web";
const distdir = path.join(__dirname, "dist", "web");
// const distdir = path.join(__dirname, "dist", "web");
const demodir = path.join(__dirname, "dist", "demo");
module.exports = [
// {
// // bundle used for Karma tests
// target: target,
// entry: glob.sync("./build/**/*.spec.js"),
// output: {
// path: distdir,
// filename: "tests.js",
// },
// plugins: [new webpack.EnvironmentPlugin(["WASMD_ENABLED"])],
// },
{
// bundle used for Karma tests
// bundle used for Ledger demo
target: target,
entry: glob.sync("./build/**/*.spec.js"),
entry: glob.sync("./build/**/*.demo.js"),
output: {
path: distdir,
filename: "tests.js",
path: demodir,
filename: "ledger.js",
},
plugins: [new webpack.EnvironmentPlugin(["WASMD_ENABLED"])],
},
];