sdk38: Remove explicit enable method from offline signer

This commit is contained in:
willclarktech 2020-07-09 12:38:13 +02:00
parent 6a02704805
commit 6dcbf77d56
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
7 changed files with 12 additions and 34 deletions

View File

@ -33,7 +33,6 @@ 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, wallet);

View File

@ -194,7 +194,6 @@ 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);

View File

@ -217,7 +217,6 @@ 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, wallet);
@ -533,7 +532,6 @@ 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];
@ -588,7 +586,6 @@ describe("LcdClient", () => {
const account3 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(2));
const [address1, address2, address3] = await Promise.all(
[account1, account2, account3].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
@ -643,7 +640,6 @@ describe("LcdClient", () => {
it("can send multiple messages with one signature", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
@ -706,7 +702,6 @@ describe("LcdClient", () => {
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
@ -778,7 +773,6 @@ describe("LcdClient", () => {
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);
@ -845,7 +839,6 @@ describe("LcdClient", () => {
const account2 = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
await wallet.enable();
return (await wallet.getAccounts())[0].address;
}),
);

View File

@ -78,7 +78,6 @@ export class SigningCosmosClient extends CosmosClient {
transferAmount: readonly Coin[],
memo = "",
): Promise<PostTxResult> {
await this.signer.enable();
const sendMsg: MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {

View File

@ -15,23 +15,15 @@ describe("Secp256k1OfflineWallet", () => {
expect(wallet).toBeTruthy();
});
describe("enable", () => {
it("resolves to true", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(defaultMnemonic);
const enabled = await wallet.enable();
expect(enabled).toEqual(true);
});
});
describe("getAccounts", () => {
it("rejects if not enabled", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(defaultMnemonic);
spyOn(wallet as any, "enable").and.resolveTo(false);
await expectAsync(wallet.getAccounts()).toBeRejectedWithError(/wallet not enabled/i);
});
it("resolves to a list of accounts if enabled", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(defaultMnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
expect(accounts.length).toEqual(1);
expect(accounts[0]).toEqual({
@ -45,7 +37,6 @@ describe("Secp256k1OfflineWallet", () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(
"oyster design unusual machine spread century engine gravity focus cave carry slot",
);
await wallet.enable();
const { address } = (await wallet.getAccounts())[0];
expect(address).toEqual("cosmos1cjsxept9rkggzxztslae9ndgpdyt2408lk850u");
});
@ -54,13 +45,13 @@ describe("Secp256k1OfflineWallet", () => {
describe("sign", () => {
it("rejects if not enabled", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(defaultMnemonic);
spyOn(wallet as any, "enable").and.resolveTo(false);
const message = toAscii("foo bar");
await expectAsync(wallet.sign(defaultAddress, message)).toBeRejectedWithError(/wallet not enabled/i);
});
it("resolves to valid signature if enabled", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(defaultMnemonic);
await wallet.enable();
const message = toAscii("foo bar");
const signature = await wallet.sign(defaultAddress, message);
const valid = await Secp256k1.verifySignature(

View File

@ -25,11 +25,6 @@ export interface AccountData {
}
export interface OfflineSigner {
/**
* Request access to the user's accounts. Wallet should ask the user to approve or deny access. Returns true if granted access or false if denied.
*/
readonly enable: () => Promise<boolean>;
/**
* Get AccountData array from wallet. Rejects if not enabled.
*/
@ -96,13 +91,16 @@ export class Secp256k1OfflineWallet implements OfflineSigner {
return rawSecp256k1PubkeyToAddress(this.pubkey, this.prefix);
}
public async enable(): Promise<boolean> {
/**
* Request access to the user's accounts. Some wallets will ask the user to approve or deny access. Returns true if granted access or false if denied.
*/
private async enable(): Promise<boolean> {
this.enabled = true;
return this.enabled;
}
public async getAccounts(): Promise<readonly AccountData[]> {
if (!this.enabled) {
if (!this.enabled && !(await this.enable())) {
throw new Error("Wallet not enabled");
}
return [
@ -119,7 +117,7 @@ export class Secp256k1OfflineWallet implements OfflineSigner {
message: Uint8Array,
prehashType: PrehashType = "sha256",
): Promise<StdSignature> {
if (!this.enabled) {
if (!this.enabled && !(await this.enable())) {
throw new Error("Wallet not enabled");
}
if (address !== this.address) {

View File

@ -8,10 +8,6 @@ export interface AccountData {
readonly pubkey: Uint8Array;
}
export interface OfflineSigner {
/**
* Request access to the user's accounts. Wallet should ask the user to approve or deny access. Returns true if granted access or false if denied.
*/
readonly enable: () => Promise<boolean>;
/**
* Get AccountData array from wallet. Rejects if not enabled.
*/
@ -39,7 +35,10 @@ export declare class Secp256k1OfflineWallet implements OfflineSigner {
private enabled;
private constructor();
private get address();
enable(): Promise<boolean>;
/**
* Request access to the user's accounts. Some wallets will ask the user to approve or deny access. Returns true if granted access or false if denied.
*/
private enable;
getAccounts(): Promise<readonly AccountData[]>;
sign(address: string, message: Uint8Array, prehashType?: PrehashType): Promise<StdSignature>;
}