Let Account use type types.PubKey

This commit is contained in:
Simon Warta 2020-03-02 17:34:22 +01:00
parent e510a83d47
commit 27f27d31e3
6 changed files with 13 additions and 38 deletions

View File

@ -1,22 +1,11 @@
import { Algorithm, PubkeyBytes } from "@iov/bcp";
import { Encoding } from "@iov/encoding";
import { decodeCosmosPubkey, pubkeyToAddress } from "./address";
import { pubkeyToAddress } from "./address";
const { fromBase64, fromHex } = Encoding;
describe("address", () => {
describe("decodeCosmosPubkey", () => {
it("works", () => {
expect(
decodeCosmosPubkey("cosmospub1addwnpepqd8sgxq7aw348ydctp3n5ajufgxp395hksxjzc6565yfp56scupfqhlgyg5"),
).toEqual({
data: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
algo: Algorithm.Secp256k1,
});
});
});
describe("pubkeyToAddress", () => {
it("works for Secp256k1 compressed", () => {
const prefix = "cosmos";

View File

@ -1,21 +1,9 @@
import { decodeBech32Pubkey, pubkeyToAddress as sdkPubkeyToAddress, types } from "@cosmwasm/sdk";
import { Address, Algorithm, PubkeyBundle, PubkeyBytes } from "@iov/bcp";
import { pubkeyToAddress as sdkPubkeyToAddress, types } from "@cosmwasm/sdk";
import { Address, Algorithm, PubkeyBundle } from "@iov/bcp";
import { Secp256k1 } from "@iov/crypto";
import { Encoding } from "@iov/encoding";
const { fromBase64, toBase64 } = Encoding;
export function decodeCosmosPubkey(encodedPubkey: string): PubkeyBundle {
const sdkPubKey = decodeBech32Pubkey(encodedPubkey);
switch (sdkPubKey.type) {
case types.pubkeyType.secp256k1:
return { algo: Algorithm.Secp256k1, data: fromBase64(sdkPubKey.value) as PubkeyBytes };
case types.pubkeyType.ed25519:
return { algo: Algorithm.Ed25519, data: fromBase64(sdkPubKey.value) as PubkeyBytes };
default:
throw new Error("Unsupported Pubkey type: " + sdkPubKey.type);
}
}
const { toBase64 } = Encoding;
// See https://github.com/tendermint/tendermint/blob/f2ada0a604b4c0763bda2f64fac53d506d3beca7/docs/spec/blockchain/encoding.md#public-key-cryptography
export function pubkeyToAddress(pubkey: PubkeyBundle, prefix: string): Address {

View File

@ -36,10 +36,10 @@ import equal from "fast-deep-equal";
import { ReadonlyDate } from "readonly-date";
import { Producer, Stream } from "xstream";
import { decodeCosmosPubkey, pubkeyToAddress } from "./address";
import { pubkeyToAddress } from "./address";
import { Caip5 } from "./caip5";
import { CosmWasmCodec } from "./cosmwasmcodec";
import { decodeAmount, parseTxsResponseSigned, parseTxsResponseUnsigned } from "./decode";
import { decodeAmount, decodePubkey, parseTxsResponseSigned, parseTxsResponseUnsigned } from "./decode";
import { buildSignedTx } from "./encode";
import { accountToNonce, BankToken, Erc20Token } from "./types";
@ -191,7 +191,7 @@ export class CosmWasmConnection implements BlockchainConnection {
...supportedBankCoins.map(coin => decodeAmount(this.bankTokens, coin)),
...nonZeroErc20Amounts,
].sort((a, b) => a.tokenTicker.localeCompare(b.tokenTicker));
const pubkey = bankAccount?.pubkey ? decodeCosmosPubkey(bankAccount.pubkey) : undefined;
const pubkey = bankAccount?.pubkey ? decodePubkey(bankAccount.pubkey) : undefined;
return {
address: address,
balance: balance,

View File

@ -1,3 +1,2 @@
import { Address, PubkeyBundle } from "@iov/bcp";
export declare function decodeCosmosPubkey(encodedPubkey: string): PubkeyBundle;
export declare function pubkeyToAddress(pubkey: PubkeyBundle, prefix: string): Address;

View File

@ -2,8 +2,9 @@ import { Sha256 } from "@iov/crypto";
import { Encoding } from "@iov/encoding";
import { Log, parseLogs } from "./logs";
import { decodeBech32Pubkey } from "./pubkey";
import { BroadcastMode, RestClient } from "./restclient";
import { Coin, CosmosSdkTx, StdTx } from "./types";
import { Coin, CosmosSdkTx, PubKey, StdTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
@ -14,8 +15,7 @@ export interface Account {
/** Bech32 account address */
readonly address: string;
readonly balance: ReadonlyArray<Coin>;
/** Bech32 encoded pubkey */
readonly pubkey: string | undefined;
readonly pubkey: PubKey | undefined;
readonly accountNumber: number;
readonly sequence: number;
}
@ -185,7 +185,7 @@ export class CosmWasmClient {
: {
address: value.address,
balance: value.coins,
pubkey: value.public_key || undefined,
pubkey: value.public_key ? decodeBech32Pubkey(value.public_key) : undefined,
accountNumber: value.account_number,
sequence: value.sequence,
};

View File

@ -1,6 +1,6 @@
import { Log } from "./logs";
import { BroadcastMode, RestClient } from "./restclient";
import { Coin, CosmosSdkTx, StdTx } from "./types";
import { Coin, CosmosSdkTx, PubKey, StdTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
readonly sequence: number;
@ -9,8 +9,7 @@ export interface Account {
/** Bech32 account address */
readonly address: string;
readonly balance: ReadonlyArray<Coin>;
/** Bech32 encoded pubkey */
readonly pubkey: string | undefined;
readonly pubkey: PubKey | undefined;
readonly accountNumber: number;
readonly sequence: number;
}