Remove all use of amino, identifier is disabled

This commit is contained in:
Ethan Frey 2020-02-03 19:59:20 +01:00
parent e50733eb71
commit 7e7f18a1b3
15 changed files with 55 additions and 27 deletions

View File

@ -43,7 +43,6 @@
"@iov/crypto": "^2.0.0-alpha.7",
"@iov/encoding": "^2.0.0-alpha.7",
"@iov/stream": "^2.0.0-alpha.7",
"@tendermint/amino-js": "^0.7.0-alpha.1",
"fast-deep-equal": "^3.1.1",
"readonly-date": "^1.0.0",
"xstream": "^11.11.0"

View File

@ -19,7 +19,7 @@ describe("cosmWasmCodec", () => {
expect(bytesToSign).toEqual(expected);
});
it("properly encodes transactions", () => {
xit("properly encodes transactions", () => {
const encoded = cosmWasmCodec.bytesToPost(signedTxJson);
expect(encoded).toEqual(signedTxBin);
});
@ -30,12 +30,12 @@ describe("cosmWasmCodec", () => {
);
});
it("properly decodes transactions", () => {
xit("properly decodes transactions", () => {
const decoded = cosmWasmCodec.parseBytes(signedTxBin as PostableBytes, chainId, nonce);
expect(decoded).toEqual(signedTxJson);
});
it("generates transaction id", () => {
xit("generates transaction id", () => {
const id = cosmWasmCodec.identifier(signedTxJson);
expect(id).toMatch(/^[0-9A-F]{64}$/);
expect(id).toEqual(txId);

View File

@ -15,7 +15,7 @@ import {
} from "@iov/bcp";
import { Sha256 } from "@iov/crypto";
import { Encoding } from "@iov/encoding";
import { marshalTx, unmarshalTx } from "@tendermint/amino-js";
import { unmarshalTx, marshalTx } from "@cosmwasm/sdk";
import { CosmosBech32Prefix, isValidAddress, pubkeyToAddress } from "./address";
import { Caip5 } from "./caip5";
@ -72,16 +72,20 @@ export class CosmWasmCodec implements TxCodec {
};
}
// PostableBytes are JSON-encoded StdTx
public bytesToPost(signed: SignedTransaction): PostableBytes {
// TODO: change this as well (return StdTx, not AminoTx)?
const built = buildSignedTx(signed, this.tokens);
const bytes = marshalTx(built, true);
return bytes as PostableBytes;
return marshalTx(built.value) as PostableBytes;
}
public identifier(signed: SignedTransaction): TransactionId {
const bytes = this.bytesToPost(signed);
const hash = new Sha256(bytes).digest();
return toHex(hash).toUpperCase() as TransactionId;
// TODO: this needs some marshalling going on...
// Do we need to support this??
public identifier(_signed: SignedTransaction): TransactionId {
throw new Error("Not yet implemented, requires amino encoding- talk to Ethan");
// const bytes = this.bytesToPost(signed);
// const hash = new Sha256(bytes).digest();
// return toHex(hash).toUpperCase() as TransactionId;
}
public parseBytes(bytes: PostableBytes, chainId: ChainId, nonce?: Nonce): SignedTransaction {

View File

@ -157,7 +157,7 @@ describe("decode", () => {
describe("parseTx", () => {
it("works", () => {
expect(parseTx(data.tx, chainId, nonce, defaultTokens)).toEqual(signedTxJson);
expect(parseTx(data.tx.value, chainId, nonce, defaultTokens)).toEqual(signedTxJson);
});
});

View File

@ -102,8 +102,7 @@ export function parseFee(fee: types.StdFee, tokens: TokenInfos): Fee {
};
}
export function parseTx(tx: types.Tx, chainId: ChainId, nonce: Nonce, tokens: TokenInfos): SignedTransaction {
const txValue = tx.value;
export function parseTx(txValue: types.StdTx, chainId: ChainId, nonce: Nonce, tokens: TokenInfos): SignedTransaction {
if (!types.isAminoStdTx(txValue)) {
throw new Error("Only Amino StdTx is supported");
}
@ -137,7 +136,7 @@ export function parseTxsResponse(
): ConfirmedAndSignedTransaction<UnsignedTransaction> {
const height = parseInt(response.height, 10);
return {
...parseTx(response.tx, chainId, nonce, tokens),
...parseTx(response.tx.value, chainId, nonce, tokens),
height: height,
confirmations: currentHeight - height + 1,
transactionId: response.txhash as TransactionId,

View File

@ -18,7 +18,7 @@ export declare class CosmWasmCodec implements TxCodec {
constructor(prefix: CosmosBech32Prefix, tokens: TokenInfos);
bytesToSign(unsigned: UnsignedTransaction, nonce: Nonce): SigningJob;
bytesToPost(signed: SignedTransaction): PostableBytes;
identifier(signed: SignedTransaction): TransactionId;
identifier(_signed: SignedTransaction): TransactionId;
parseBytes(bytes: PostableBytes, chainId: ChainId, nonce?: Nonce): SignedTransaction;
identityToAddress(identity: Identity): Address;
isValidAddress(address: string): boolean;

View File

@ -22,7 +22,7 @@ export declare function decodeAmount(tokens: TokenInfos, coin: types.Coin): Amou
export declare function parseMsg(msg: types.Msg, chainId: ChainId, tokens: TokenInfos): SendTransaction;
export declare function parseFee(fee: types.StdFee, tokens: TokenInfos): Fee;
export declare function parseTx(
tx: types.Tx,
txValue: types.StdTx,
chainId: ChainId,
nonce: Nonce,
tokens: TokenInfos,

View File

@ -39,7 +39,6 @@
},
"dependencies": {
"@iov/encoding": "^2.0.0-alpha.7",
"@tendermint/amino-js": "^0.7.0-alpha.1",
"axios": "^0.19.0"
},
"devDependencies": {

View File

@ -0,0 +1,11 @@
import { Encoding } from "@iov/encoding";
import { isAminoStdTx, StdTx } from "./types";
export function unmarshalTx(data: Uint8Array): StdTx {
const decoded = JSON.parse(Encoding.fromUtf8(data));
if (!isAminoStdTx(decoded)) {
throw new Error("Must be json encoded StdTx");
}
return decoded;
}

View File

@ -0,0 +1,8 @@
import { Encoding } from "@iov/encoding";
import { StdTx } from "./types";
export function marshalTx(tx: StdTx): Uint8Array {
const json = JSON.stringify(tx);
return Encoding.toUtf8(json);
}

View File

@ -1,4 +1,6 @@
import * as types from "./types";
export { unmarshalTx } from "./decoding";
export { marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient";
export { types };

View File

@ -13,6 +13,15 @@ export interface StdTx {
readonly memo: string | undefined;
}
export type AminoTx = Tx & { readonly value: StdTx };
export function isAminoStdTx(txValue: unknown): txValue is StdTx {
const { memo, msg, fee, signatures } = txValue as StdTx;
return (
typeof memo === "string" && Array.isArray(msg) && typeof fee === "object" && Array.isArray(signatures)
);
}
export interface Msg {
readonly type: string;
// TODO: make better union type
@ -58,12 +67,3 @@ export interface BaseAccount {
readonly account_number: string;
readonly sequence: string;
}
export type AminoTx = Tx & { readonly value: StdTx };
export function isAminoStdTx(txValue: unknown): txValue is StdTx {
const { memo, msg, fee, signatures } = txValue as StdTx;
return (
typeof memo === "string" && Array.isArray(msg) && typeof fee === "object" && Array.isArray(signatures)
);
}

View File

@ -0,0 +1,2 @@
import { StdTx } from "./types";
export declare function unmarshalTx(data: Uint8Array): StdTx;

View File

@ -0,0 +1,2 @@
import { StdTx } from "./types";
export declare function marshalTx(tx: StdTx): Uint8Array;

View File

@ -1,3 +1,5 @@
import * as types from "./types";
export { unmarshalTx } from "./decoding";
export { marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient";
export { types };