cli: Update for sdk38 OfflineSigner

This commit is contained in:
willclarktech 2020-07-14 12:36:23 +02:00
parent 96fa0f4b64
commit 0561dd5dd5
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
6 changed files with 45 additions and 47 deletions

View File

@ -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,

View File

@ -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}`);
}

View File

@ -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);

View File

@ -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<Options>): Promise<{
client: SigningCosmWasmClient,
address: string,
const connect = async (
mnemonic: string,
opts: Partial<Options>,
): 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<void> => {
const r = await axios.post(defaultFaucetUrl, { ticker, address });
console.log(r.status);
console.log(r.data);
}
};
const randomAddress = async (prefix: string): Promise<string> => {
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
return mnemonicToAddress(prefix, mnemonic);
}
};
const mnemonicToAddress = async (prefix: string, mnemonic: string): Promise<string> => {
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<Uint8Array> => {
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;

View File

@ -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);

View File

@ -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);