Deduplicate makeSignBytes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/camelcase */
|
||||
import { marshalTx, sortJson, unmarshalTx } from "@cosmwasm/sdk";
|
||||
import { makeSignBytes, marshalTx, types, unmarshalTx } from "@cosmwasm/sdk";
|
||||
import {
|
||||
Address,
|
||||
ChainId,
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
TxCodec,
|
||||
UnsignedTransaction,
|
||||
} from "@iov/bcp";
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
import { CosmosBech32Prefix, isValidAddress, pubkeyToAddress } from "./address";
|
||||
import { Caip5 } from "./caip5";
|
||||
@@ -22,8 +21,6 @@ import { parseTx } from "./decode";
|
||||
import { buildSignedTx, buildUnsignedTx } from "./encode";
|
||||
import { nonceToAccountNumber, nonceToSequence, TokenInfos } from "./types";
|
||||
|
||||
const { toUtf8 } = Encoding;
|
||||
|
||||
export class CosmWasmCodec implements TxCodec {
|
||||
private readonly prefix: CosmosBech32Prefix;
|
||||
private readonly tokens: TokenInfos;
|
||||
@@ -34,18 +31,19 @@ export class CosmWasmCodec implements TxCodec {
|
||||
}
|
||||
|
||||
public bytesToSign(unsigned: UnsignedTransaction, nonce: Nonce): SigningJob {
|
||||
const memo = (unsigned as any).memo;
|
||||
const built = buildUnsignedTx(unsigned, this.tokens);
|
||||
|
||||
const signMsg = sortJson({
|
||||
account_number: nonceToAccountNumber(nonce).toString(),
|
||||
chain_id: Caip5.decode(unsigned.chainId),
|
||||
fee: (built.value as any).fee,
|
||||
memo: memo,
|
||||
msgs: (built.value as any).msg,
|
||||
sequence: nonceToSequence(nonce).toString(),
|
||||
});
|
||||
const signBytes = toUtf8(JSON.stringify(signMsg));
|
||||
const nonceInfo: types.NonceInfo = {
|
||||
account_number: nonceToAccountNumber(nonce),
|
||||
sequence: nonceToSequence(nonce),
|
||||
};
|
||||
const signBytes = makeSignBytes(
|
||||
built.value.msg[0],
|
||||
built.value.fee,
|
||||
Caip5.decode(unsigned.chainId),
|
||||
built.value.memo || "",
|
||||
nonceInfo,
|
||||
);
|
||||
|
||||
return {
|
||||
bytes: signBytes as SignableBytes,
|
||||
|
||||
@@ -23,13 +23,9 @@ const maxAcct = 1 << 23;
|
||||
// tslint:disable-next-line:no-bitwise
|
||||
const maxSeq = 1 << 20;
|
||||
|
||||
// NonceInfo is the data we need from account to create a nonce
|
||||
// Use this so no confusion about order of arguments
|
||||
export type NonceInfo = Pick<types.BaseAccount, "account_number" | "sequence">;
|
||||
|
||||
// this (lossily) encodes the two pieces of info (uint64) needed to sign into
|
||||
// one (53-bit) number. Cross your fingers.
|
||||
export function accountToNonce({ account_number: account, sequence }: NonceInfo): Nonce {
|
||||
export function accountToNonce({ account_number: account, sequence }: types.NonceInfo): Nonce {
|
||||
// we allow 23 bits (8 million) for accounts, and 20 bits (1 million) for tx/account
|
||||
// let's fix this soon
|
||||
if (account > maxAcct) {
|
||||
|
||||
Vendored
+1
-2
@@ -15,7 +15,6 @@ export interface TokenInfo {
|
||||
readonly fractionalDigits: number;
|
||||
}
|
||||
export declare type TokenInfos = ReadonlyArray<TokenInfo>;
|
||||
export declare type NonceInfo = Pick<types.BaseAccount, "account_number" | "sequence">;
|
||||
export declare function accountToNonce({ account_number: account, sequence }: NonceInfo): Nonce;
|
||||
export declare function accountToNonce({ account_number: account, sequence }: types.NonceInfo): Nonce;
|
||||
export declare function nonceToAccountNumber(nonce: Nonce): number;
|
||||
export declare function nonceToSequence(nonce: Nonce): number;
|
||||
|
||||
Reference in New Issue
Block a user