From 0561dd5dd5e57ff429e78a3c2d85c93300edf894 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 14 Jul 2020 12:36:23 +0200 Subject: [PATCH] cli: Update for sdk38 OfflineSigner --- packages/cli/examples/delegate.ts | 12 ++--- packages/cli/examples/faucet_addresses.ts | 8 ++-- packages/cli/examples/generate_address.ts | 5 +-- packages/cli/examples/helpers.ts | 54 +++++++++++------------ packages/cli/examples/local_faucet.ts | 2 +- packages/cli/src/cli.ts | 11 +++-- 6 files changed, 45 insertions(+), 47 deletions(-) diff --git a/packages/cli/examples/delegate.ts b/packages/cli/examples/delegate.ts index 9d6fc327..dd1dd2d9 100644 --- a/packages/cli/examples/delegate.ts +++ b/packages/cli/examples/delegate.ts @@ -1,5 +1,7 @@ -const pen = await Secp256k1Pen.fromMnemonic("enlist hip relief stomach skate base shallow young switch frequent cry park"); -const senderAddress = pen.address("cosmos"); +const wallet = await Secp256k1OfflineWallet.fromMnemonic( + "enlist hip relief stomach skate base shallow young switch frequent cry park", +); +const [{ address: senderAddress }] = await wallet.getAccounts(); const client = new CosmosClient("http://localhost:1317"); @@ -11,8 +13,8 @@ const msg: MsgDelegate = { // curl http://localhost:1317/staking/validators | jq '.result[0].operator_address' validator_address: "cosmosvaloper1gjvanqxc774u6ed9thj4gpn9gj5zus5u32enqn", amount: coin(300000, "ustake"), - } -} + }, +}; const fee = { amount: coins(2000, "ucosm"), gas: "120000", // 120k @@ -26,7 +28,7 @@ const { accountNumber, sequence } = await client.getNonce(senderAddress); console.log("Account/sequence:", accountNumber, sequence); const signBytes = makeSignBytes([msg], fee, chainId, memo, accountNumber, sequence); -const signature = await pen.sign(signBytes); +const signature = await wallet.sign(senderAddress, signBytes); const signedTx: StdTx = { msg: [msg], fee: fee, diff --git a/packages/cli/examples/faucet_addresses.ts b/packages/cli/examples/faucet_addresses.ts index a7be953b..603b9da9 100644 --- a/packages/cli/examples/faucet_addresses.ts +++ b/packages/cli/examples/faucet_addresses.ts @@ -1,9 +1,9 @@ -const mnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; +const mnemonic = + "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; for (let i of [0, 1, 2, 3, 4]) { - const pen = await Secp256k1Pen.fromMnemonic(mnemonic, makeCosmoshubPath(i)); - const pubkey = toBase64(pen.pubkey); - const address = pubkeyToAddress(encodeSecp256k1Pubkey(pen.pubkey), "cosmos"); + const wallet = await Secp256k1OfflineWallet.fromMnemonic(mnemonic, makeCosmoshubPath(i), "cosmos"); + const [{ address, pubkey }] = await wallet.getAccounts(); console.info(`Address ${i}: ${address}`); console.info(`Pubkey ${i}: ${pubkey}`); } diff --git a/packages/cli/examples/generate_address.ts b/packages/cli/examples/generate_address.ts index e0ccfb0e..88ca6bb9 100644 --- a/packages/cli/examples/generate_address.ts +++ b/packages/cli/examples/generate_address.ts @@ -1,7 +1,6 @@ const mnemonic = Bip39.encode(Random.getBytes(16)).toString(); -const pen = await Secp256k1Pen.fromMnemonic(mnemonic); -const pubkey = encodeSecp256k1Pubkey(pen.pubkey); -const address = pubkeyToAddress(pubkey, "cosmos"); +const wallet = await Secp256k1OfflineWallet.fromMnemonic(mnemonic); +const [{ address, pubkey }] = await wallet.getAccounts(); console.info("mnemonic:", mnemonic); console.info("pubkey:", pubkey); diff --git a/packages/cli/examples/helpers.ts b/packages/cli/examples/helpers.ts index c52ddc40..69cbf57e 100644 --- a/packages/cli/examples/helpers.ts +++ b/packages/cli/examples/helpers.ts @@ -4,7 +4,7 @@ interface Options { feeToken: string; gasPrice: number; bech32prefix: string; -}; +} const defaultOptions: Options = { httpUrl: "https://lcd.demo-09.cosmwasm.com", @@ -12,7 +12,7 @@ const defaultOptions: Options = { feeToken: "ucosm", gasPrice: 0.025, bech32prefix: "cosmos", -} +}; const defaultFaucetUrl = "https://faucet.demo-09.cosmwasm.com/credit"; @@ -22,7 +22,7 @@ const buildFeeTable = (feeToken: string, gasPrice: number): FeeTable => { return { amount: [{ amount: amount.toString(), denom: denom }], gas: gas.toString(), - } + }; }; return { @@ -32,7 +32,7 @@ const buildFeeTable = (feeToken: string, gasPrice: number): FeeTable => { exec: stdFee(200000, feeToken, gasPrice), send: stdFee(80000, feeToken, gasPrice), changeAdmin: stdFee(80000, feeToken, gasPrice), - } + }; }; // TODO: hit faucet @@ -43,22 +43,20 @@ const buildFeeTable = (feeToken: string, gasPrice: number): FeeTable => { // } // } -const penAddress = (pen: Secp256k1Pen, prefix: string): string => { - const pubkey = encodeSecp256k1Pubkey(pen.pubkey); - return pubkeyToAddress(pubkey, prefix); -} - -const connect = async (mnemonic: string, opts: Partial): Promise<{ - client: SigningCosmWasmClient, - address: string, +const connect = async ( + mnemonic: string, + opts: Partial, +): Promise<{ + client: SigningCosmWasmClient; + address: string; }> => { - const options: Options = {...defaultOptions, ...opts}; + const options: Options = { ...defaultOptions, ...opts }; const feeTable = buildFeeTable(options.feeToken, options.gasPrice); - const pen = await Secp256k1Pen.fromMnemonic(mnemonic); - const address = penAddress(pen, options.bech32prefix); + const wallet = await Secp256k1OfflineWallet.fromMnemonic(mnemonic); + const [{ address }] = await wallet.getAccounts(); - const client = new SigningCosmWasmClient(options.httpUrl, address, signBytes => pen.sign(signBytes), feeTable); - return {client, address}; + const client = new SigningCosmWasmClient(options.httpUrl, address, wallet, feeTable); + return { client, address }; }; // smartQuery assumes the content is proper JSON data and parses before returning it @@ -80,32 +78,32 @@ const loadOrCreateMnemonic = (filename: string): string => { fs.writeFileSync(filename, mnemonic, "utf8"); return mnemonic; } -} +}; const hitFaucet = async (faucetUrl: string, address: string, ticker: string): Promise => { const r = await axios.post(defaultFaucetUrl, { ticker, address }); console.log(r.status); console.log(r.data); -} +}; const randomAddress = async (prefix: string): Promise => { const mnemonic = Bip39.encode(Random.getBytes(16)).toString(); return mnemonicToAddress(prefix, mnemonic); -} +}; const mnemonicToAddress = async (prefix: string, mnemonic: string): Promise => { - const pen = await Secp256k1Pen.fromMnemonic(mnemonic); - const pubkey = encodeSecp256k1Pubkey(pen.pubkey); - return pubkeyToAddress(pubkey, prefix); -} + const wallet = await Secp256k1OfflineWallet.fromMnemonic(mnemonic); + const [{ address }] = await wallet.getAccounts(); + return address; +}; const downloadWasm = async (url: string): Promise => { - const r = await axios.get(url, { responseType: "arraybuffer"}); + const r = await axios.get(url, { responseType: "arraybuffer" }); if (r.status !== 200) { throw new Error(`Download error: ${r.status}`); } return r.data; -} +}; -const getAttibute = (logs: readonly logs.Log[], key: string): string|undefined => - logs[0].events[0].attributes.find(x => x.key == key)?.value +const getAttibute = (logs: readonly logs.Log[], key: string): string | undefined => + logs[0].events[0].attributes.find((x) => x.key == key)?.value; diff --git a/packages/cli/examples/local_faucet.ts b/packages/cli/examples/local_faucet.ts index 687e3512..44301d71 100644 --- a/packages/cli/examples/local_faucet.ts +++ b/packages/cli/examples/local_faucet.ts @@ -14,5 +14,5 @@ const faucetMnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6"; -const pen = await Secp256k1Pen.fromMnemonic(faucetMnemonic); +const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucetMnemonic); const client = new LcdClient(defaultHttpUrl); diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index d5ead030..765eea7b 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -97,10 +97,10 @@ export function main(originalArgs: readonly string[]): void { "MsgDelegate", "MsgSend", "LcdClient", - "Pen", + "OfflineSigner", "PubKey", "pubkeyToAddress", - "Secp256k1Pen", + "Secp256k1OfflineWallet", "SigningCosmosClient", "StdFee", "StdTx", @@ -145,11 +145,10 @@ export function main(originalArgs: readonly string[]): void { assert(Decimal.fromAtomics("12870000", 6).toString() === "12.87"); const mnemonic = Bip39.encode(Random.getBytes(16)).toString(); - const pen = await Secp256k1Pen.fromMnemonic(mnemonic, makeCosmoshubPath(0)); - const pubkey = encodeSecp256k1Pubkey(pen.pubkey); - const address = pubkeyToAddress(pubkey, "cosmos"); + const wallet = await Secp256k1OfflineWallet.fromMnemonic(mnemonic, makeCosmoshubPath(0)); + const [{ address }] = await wallet.getAccounts(); const data = toAscii("foo bar"); - const signature = await pen.sign(data); + const signature = await wallet.sign(address, data); console.info("Done testing, will exit now."); process.exit(0);