From 27f27d31e36453bdf0fe88fa52a4e98fa134ccba Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 2 Mar 2020 17:34:22 +0100 Subject: [PATCH] Let Account use type types.PubKey --- packages/bcp/src/address.spec.ts | 13 +------------ packages/bcp/src/address.ts | 18 +++--------------- packages/bcp/src/cosmwasmconnection.ts | 6 +++--- packages/bcp/types/address.d.ts | 1 - packages/sdk/src/cosmwasmclient.ts | 8 ++++---- packages/sdk/types/cosmwasmclient.d.ts | 5 ++--- 6 files changed, 13 insertions(+), 38 deletions(-) diff --git a/packages/bcp/src/address.spec.ts b/packages/bcp/src/address.spec.ts index 0b52e387..459007dc 100644 --- a/packages/bcp/src/address.spec.ts +++ b/packages/bcp/src/address.spec.ts @@ -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"; diff --git a/packages/bcp/src/address.ts b/packages/bcp/src/address.ts index 11afa609..e26cf556 100644 --- a/packages/bcp/src/address.ts +++ b/packages/bcp/src/address.ts @@ -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 { diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index c57510b2..275bd81b 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -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, diff --git a/packages/bcp/types/address.d.ts b/packages/bcp/types/address.d.ts index aa49f713..98fa0ca9 100644 --- a/packages/bcp/types/address.d.ts +++ b/packages/bcp/types/address.d.ts @@ -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; diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index cb87bd5d..0f7a51b2 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -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; - /** 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, }; diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index 89f5f45e..e7ffa3d4 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -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; - /** Bech32 encoded pubkey */ - readonly pubkey: string | undefined; + readonly pubkey: PubKey | undefined; readonly accountNumber: number; readonly sequence: number; }