Let Account use type types.PubKey
This commit is contained in:
parent
e510a83d47
commit
27f27d31e3
@ -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";
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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,
|
||||
|
||||
1
packages/bcp/types/address.d.ts
vendored
1
packages/bcp/types/address.d.ts
vendored
@ -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;
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
5
packages/sdk/types/cosmwasmclient.d.ts
vendored
5
packages/sdk/types/cosmwasmclient.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user