mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
launchpad-ledger: Add Node.js demo
This commit is contained in:
parent
4dd94fe115
commit
0d4baf1fe3
14
packages/launchpad-ledger/demo/node.js
Normal file
14
packages/launchpad-ledger/demo/node.js
Normal 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);
|
47
packages/launchpad-ledger/src/demo/node.ts
Normal file
47
packages/launchpad-ledger/src/demo/node.ts
Normal 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));
|
||||
}
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user