Fix getAccount

This commit is contained in:
Ethan Frey 2020-01-22 23:16:37 +01:00
parent 69d9ef5ce8
commit 344d980b5f
2 changed files with 22 additions and 19 deletions

View File

@ -150,9 +150,10 @@ describe("CosmosConnection", () => {
throw new Error("Expected account not to be undefined"); throw new Error("Expected account not to be undefined");
} }
expect(account.address).toEqual(defaultAddress); expect(account.address).toEqual(defaultAddress);
expect(account.pubkey).toEqual(defaultPubkey); // Undefined until we sign a transaction
// Unsupported coins are filtered out expect(account.pubkey).toEqual(undefined);
expect(account.balance.length).toEqual(1); // Starts with two tokens
expect(account.balance.length).toEqual(2);
connection.disconnect(); connection.disconnect();
}); });
@ -164,12 +165,10 @@ describe("CosmosConnection", () => {
throw new Error("Expected account not to be undefined"); throw new Error("Expected account not to be undefined");
} }
expect(account.address).toEqual(defaultAddress); expect(account.address).toEqual(defaultAddress);
expect(account.pubkey).toEqual({ // Undefined until we sign a transaction
algo: Algorithm.Secp256k1, expect(account.pubkey).toEqual(undefined);
data: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"), // Starts with two tokens
}); expect(account.balance.length).toEqual(2);
// Unsupported coins are filtered out
expect(account.balance.length).toEqual(1);
connection.disconnect(); connection.disconnect();
}); });
}); });

View File

@ -145,18 +145,22 @@ export class CosmosConnection implements BlockchainConnection {
const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.prefix) : query.address; const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.prefix) : query.address;
const { result } = await this.restClient.authAccounts(address); const { result } = await this.restClient.authAccounts(address);
const account = result.value; const account = result.value;
if (!account.address) {
return undefined;
}
const supportedCoins = account.coins.filter(({ denom }) => const supportedCoins = account.coins.filter(({ denom }) =>
this.tokenInfo.find(token => token.denom === denom), this.tokenInfo.find(token => token.denom === denom),
); );
return account.public_key === null const pubkey = !account.public_key
? undefined ? undefined
: { : {
address: address,
balance: supportedCoins.map(decodeAmount(this.tokenInfo)),
pubkey: {
algo: Algorithm.Secp256k1, algo: Algorithm.Secp256k1,
data: fromBase64(account.public_key.value) as PubkeyBytes, data: fromBase64(account.public_key.value) as PubkeyBytes,
}, };
return {
address: address,
balance: supportedCoins.map(decodeAmount(this.tokenInfo)),
pubkey: pubkey,
}; };
} }