sdk38: Make use of OfflineWallet interface more strict

This commit is contained in:
willclarktech 2020-07-09 11:52:45 +02:00
parent 821b666f98
commit 717ff6caf7
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
6 changed files with 79 additions and 39 deletions

View File

@ -33,8 +33,11 @@ describe("CosmosClient.searchTx", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, async (signBytes) =>
wallet.sign(wallet.address, signBytes),
wallet.sign(walletAddress, signBytes),
);
{
@ -58,7 +61,7 @@ describe("CosmosClient.searchTx", () => {
const { accountNumber, sequence } = await client.getNonce();
const chainId = await client.getChainId();
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
const signature = await wallet.sign(wallet.address, signBytes);
const signature = await wallet.sign(walletAddress, signBytes);
const tx: CosmosSdkTx = {
type: "cosmos-sdk/StdTx",
value: {

View File

@ -194,6 +194,9 @@ describe("CosmosClient", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new CosmosClient(wasmd.endpoint);
const memo = "My first contract on chain";
@ -224,7 +227,7 @@ describe("CosmosClient", () => {
const chainId = await client.getChainId();
const { accountNumber, sequence } = await client.getNonce(faucet.address);
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
const signature = await wallet.sign(wallet.address, signBytes);
const signature = await wallet.sign(walletAddress, signBytes);
const signedTx = {
msg: [sendMsg],
fee: fee,

View File

@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/camelcase */
import { assert, sleep } from "@cosmjs/utils";
import { rawSecp256k1PubkeyToAddress } from "../address";
import { Coin } from "../coins";
import { isPostTxFailure } from "../cosmosclient";
import { makeSignBytes } from "../encoding";
@ -218,8 +217,11 @@ describe("LcdClient", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, async (signBytes) =>
wallet.sign(wallet.address, signBytes),
wallet.sign(walletAddress, signBytes),
);
{
@ -267,7 +269,7 @@ describe("LcdClient", () => {
const { accountNumber, sequence } = await client.getNonce();
const chainId = await client.getChainId();
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
const signature = await wallet.sign(wallet.address, signBytes);
const signature = await wallet.sign(walletAddress, signBytes);
const signedTx = {
msg: [sendMsg],
fee: fee,
@ -352,8 +354,11 @@ describe("LcdClient", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, async (signBytes) =>
wallet.sign(wallet.address, signBytes),
wallet.sign(walletAddress, signBytes),
);
const recipient = makeRandomAddress();
@ -535,6 +540,9 @@ describe("LcdClient", () => {
it("can send tokens", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const memo = "My first contract on chain";
const theMsg: MsgSend = {
@ -565,7 +573,7 @@ describe("LcdClient", () => {
const { account_number, sequence } = (await client.auth.account(faucet.address)).result.value;
const signBytes = makeSignBytes([theMsg], fee, wasmd.chainId, memo, account_number, sequence);
const signature = await wallet.sign(wallet.address, signBytes);
const signature = await wallet.sign(walletAddress, signBytes);
const signedTx = makeSignedTx(theMsg, fee, memo, signature);
const result = await client.postTx(signedTx);
expect(result.code).toBeUndefined();
@ -585,9 +593,12 @@ describe("LcdClient", () => {
const account1 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const account3 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(2));
const address1 = rawSecp256k1PubkeyToAddress(account1.pubkey, "cosmos");
const address2 = rawSecp256k1PubkeyToAddress(account2.pubkey, "cosmos");
const address3 = rawSecp256k1PubkeyToAddress(account3.pubkey, "cosmos");
const [address1, address2, address3] = await Promise.all(
[account1, account2, account3].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
const memo = "My first contract on chain";
const theMsg: MsgSend = {
@ -622,9 +633,9 @@ describe("LcdClient", () => {
const signBytes1 = makeSignBytes([theMsg], fee, wasmd.chainId, memo, an1, sequence1);
const signBytes2 = makeSignBytes([theMsg], fee, wasmd.chainId, memo, an2, sequence2);
const signBytes3 = makeSignBytes([theMsg], fee, wasmd.chainId, memo, an3, sequence3);
const signature1 = await account1.sign(account1.address, signBytes1);
const signature2 = await account2.sign(account2.address, signBytes2);
const signature3 = await account3.sign(account3.address, signBytes3);
const signature1 = await account1.sign(address1, signBytes1);
const signature2 = await account2.sign(address2, signBytes2);
const signature3 = await account3.sign(address3, signBytes3);
const signedTx = {
msg: [theMsg],
fee: fee,
@ -638,14 +649,16 @@ describe("LcdClient", () => {
it("can send multiple messages with one signature", async () => {
pendingWithoutWasmd();
const account1 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const address1 = rawSecp256k1PubkeyToAddress(account1.pubkey, "cosmos");
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const memo = "My first contract on chain";
const msg1: MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {
from_address: address1,
from_address: walletAddress,
to_address: defaultRecipientAddress,
amount: [
{
@ -658,7 +671,7 @@ describe("LcdClient", () => {
const msg2: MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {
from_address: address1,
from_address: walletAddress,
to_address: defaultRecipientAddress,
amount: [
{
@ -680,10 +693,10 @@ describe("LcdClient", () => {
};
const client = LcdClient.withExtensions({ apiUrl: wasmd.endpoint }, setupAuthExtension);
const { account_number, sequence } = (await client.auth.account(address1)).result.value;
const { account_number, sequence } = (await client.auth.account(walletAddress)).result.value;
const signBytes = makeSignBytes([msg1, msg2], fee, wasmd.chainId, memo, account_number, sequence);
const signature1 = await account1.sign(account1.address, signBytes);
const signature1 = await wallet.sign(walletAddress, signBytes);
const signedTx = {
msg: [msg1, msg2],
fee: fee,
@ -698,8 +711,12 @@ describe("LcdClient", () => {
pendingWithoutWasmd();
const account1 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const address1 = rawSecp256k1PubkeyToAddress(account1.pubkey, "cosmos");
const address2 = rawSecp256k1PubkeyToAddress(account2.pubkey, "cosmos");
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
const memo = "My first contract on chain";
const msg1: MsgSend = {
@ -745,8 +762,8 @@ describe("LcdClient", () => {
const signBytes1 = makeSignBytes([msg2, msg1], fee, wasmd.chainId, memo, an1, sequence1);
const signBytes2 = makeSignBytes([msg2, msg1], fee, wasmd.chainId, memo, an2, sequence2);
const signature1 = await account1.sign(account1.address, signBytes1);
const signature2 = await account2.sign(account2.address, signBytes2);
const signature1 = await account1.sign(address1, signBytes1);
const signature2 = await account2.sign(address2, signBytes2);
const signedTx = {
msg: [msg2, msg1],
fee: fee,
@ -766,8 +783,12 @@ describe("LcdClient", () => {
pendingWithoutWasmd();
const account1 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const address1 = rawSecp256k1PubkeyToAddress(account1.pubkey, "cosmos");
const address2 = rawSecp256k1PubkeyToAddress(account2.pubkey, "cosmos");
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
const memo = "My first contract on chain";
const msg1: MsgSend = {
@ -813,8 +834,8 @@ describe("LcdClient", () => {
const signBytes1 = makeSignBytes([msg1, msg2], fee, wasmd.chainId, memo, an1, sequence1);
const signBytes2 = makeSignBytes([msg1, msg2], fee, wasmd.chainId, memo, an2, sequence2);
const signature1 = await account1.sign(account1.address, signBytes1);
const signature2 = await account2.sign(account2.address, signBytes2);
const signature1 = await account1.sign(address1, signBytes1);
const signature2 = await account2.sign(address2, signBytes2);
const signedTx = {
msg: [msg1, msg2],
fee: fee,
@ -829,8 +850,12 @@ describe("LcdClient", () => {
pendingWithoutWasmd();
const account1 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const address1 = rawSecp256k1PubkeyToAddress(account1.pubkey, "cosmos");
const address2 = rawSecp256k1PubkeyToAddress(account2.pubkey, "cosmos");
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
const memo = "My first contract on chain";
const msg1: MsgSend = {
@ -876,8 +901,8 @@ describe("LcdClient", () => {
const signBytes1 = makeSignBytes([msg2, msg1], fee, wasmd.chainId, memo, an1, sequence1);
const signBytes2 = makeSignBytes([msg2, msg1], fee, wasmd.chainId, memo, an2, sequence2);
const signature1 = await account1.sign(account1.address, signBytes1);
const signature2 = await account2.sign(account2.address, signBytes2);
const signature1 = await account1.sign(address1, signBytes1);
const signature2 = await account2.sign(address2, signBytes2);
const signedTx = {
msg: [msg2, msg1],
fee: fee,

View File

@ -22,8 +22,11 @@ describe("SigningCosmosClient", () => {
describe("makeReadOnly", () => {
it("can be constructed", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address } = accounts[0];
const client = new SigningCosmosClient(httpUrl, faucet.address, async (signBytes) =>
wallet.sign(wallet.address, signBytes),
wallet.sign(address, signBytes),
);
expect(client).toBeTruthy();
});
@ -33,8 +36,11 @@ describe("SigningCosmosClient", () => {
it("always uses authAccount implementation", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address } = accounts[0];
const client = new SigningCosmosClient(httpUrl, faucet.address, async (signBytes) =>
wallet.sign(wallet.address, signBytes),
wallet.sign(address, signBytes),
);
const openedClient = (client as unknown) as PrivateCosmWasmClient;
@ -53,8 +59,11 @@ describe("SigningCosmosClient", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address } = accounts[0];
const client = new SigningCosmosClient(httpUrl, faucet.address, async (signBytes) =>
wallet.sign(wallet.address, signBytes),
wallet.sign(address, signBytes),
);
// instantiate

View File

@ -80,7 +80,7 @@ export class Secp256k1OfflineWallet implements OfflineWallet {
return new Secp256k1OfflineWallet(privkey, Secp256k1.compressPubkey(uncompressed), prefix);
}
public readonly pubkey: Uint8Array;
private readonly pubkey: Uint8Array;
private readonly privkey: Uint8Array;
private readonly prefix: string;
private readonly algo: Algo = "secp256k1";
@ -92,7 +92,7 @@ export class Secp256k1OfflineWallet implements OfflineWallet {
this.prefix = prefix;
}
public get address(): string {
private get address(): string {
return rawSecp256k1PubkeyToAddress(this.pubkey, this.prefix);
}

View File

@ -32,13 +32,13 @@ export declare class Secp256k1OfflineWallet implements OfflineWallet {
hdPath?: readonly Slip10RawIndex[],
prefix?: string,
): Promise<Secp256k1OfflineWallet>;
readonly pubkey: Uint8Array;
private readonly pubkey;
private readonly privkey;
private readonly prefix;
private readonly algo;
private enabled;
private constructor();
get address(): string;
private get address();
enable(): Promise<boolean>;
getAccounts(): Promise<readonly AccountData[]>;
sign(address: string, message: Uint8Array, prehashType?: PrehashType): Promise<StdSignature>;