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

@ -95,9 +95,9 @@ describe("CosmosConnection", () => {
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens); const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const token = await connection.getToken("COSM" as TokenTicker); const token = await connection.getToken("COSM" as TokenTicker);
expect(token).toEqual({ expect(token).toEqual({
fractionalDigits: 6, fractionalDigits: 6,
tokenName: "Fee Token", tokenName: "Fee Token",
tokenTicker: "COSM" as TokenTicker, tokenTicker: "COSM" as TokenTicker,
}); });
connection.disconnect(); connection.disconnect();
}); });
@ -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,19 +145,23 @@ 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, algo: Algorithm.Secp256k1,
balance: supportedCoins.map(decodeAmount(this.tokenInfo)), data: fromBase64(account.public_key.value) as PubkeyBytes,
pubkey: {
algo: Algorithm.Secp256k1,
data: fromBase64(account.public_key.value) as PubkeyBytes,
},
}; };
return {
address: address,
balance: supportedCoins.map(decodeAmount(this.tokenInfo)),
pubkey: pubkey,
};
} }
public watchAccount(_account: AccountQuery): Stream<Account | undefined> { public watchAccount(_account: AccountQuery): Stream<Account | undefined> {