forked from LaconicNetwork/icns-frontend
Add initial sendMsgs function
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -10,7 +10,12 @@
|
||||
"lint": "next lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"@keplr-wallet/common": "^0.11.23",
|
||||
"@keplr-wallet/cosmos": "^0.11.23",
|
||||
"@keplr-wallet/proto-types": "^0.11.23",
|
||||
"@keplr-wallet/types": "^0.11.23",
|
||||
"axios": "^0.27.2",
|
||||
"buffer": "^6.0.3",
|
||||
"crypto": "^1.0.1",
|
||||
"iron-session": "^6.3.1",
|
||||
"next": "13.0.5",
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
import { Wallet } from "./types";
|
||||
import { BaseAccount } from "@keplr-wallet/cosmos";
|
||||
import Axios from "axios";
|
||||
import { StdSignDoc } from "@keplr-wallet/types";
|
||||
import { sortObjectByKey } from "@keplr-wallet/common";
|
||||
import { Buffer } from "buffer/";
|
||||
import {
|
||||
AuthInfo,
|
||||
Fee,
|
||||
TxBody,
|
||||
TxRaw,
|
||||
} from "@keplr-wallet/proto-types/cosmos/tx/v1beta1/tx";
|
||||
import { Any } from "@keplr-wallet/proto-types/google/protobuf/any";
|
||||
import { PubKey } from "@keplr-wallet/proto-types/cosmos/crypto/secp256k1/keys";
|
||||
import { SignMode } from "@keplr-wallet/proto-types/cosmos/tx/signing/v1beta1/signing";
|
||||
import { MsgExecuteContract } from "@keplr-wallet/proto-types/cosmwasm/wasm/v1/tx";
|
||||
|
||||
export async function sendMsgs(
|
||||
wallet: Wallet,
|
||||
chainInfo: {
|
||||
readonly chainId: string;
|
||||
readonly rpc: string;
|
||||
readonly rest: string;
|
||||
},
|
||||
sender: string,
|
||||
msgs: {
|
||||
amino: {
|
||||
readonly type: string;
|
||||
readonly value: any;
|
||||
}[];
|
||||
proto: Any[];
|
||||
},
|
||||
fee: {
|
||||
readonly amount: readonly {
|
||||
readonly amount: string;
|
||||
readonly denom: string;
|
||||
}[];
|
||||
readonly gas: string;
|
||||
},
|
||||
memo: string = "",
|
||||
): Promise<Uint8Array> {
|
||||
const account = await BaseAccount.fetchFromRest(
|
||||
Axios.create({
|
||||
baseURL: chainInfo.rest,
|
||||
}),
|
||||
sender,
|
||||
true,
|
||||
);
|
||||
|
||||
const signDocRaw: StdSignDoc = {
|
||||
chain_id: chainInfo.chainId,
|
||||
account_number: account.getAccountNumber().toString(),
|
||||
sequence: account.getSequence().toString(),
|
||||
fee,
|
||||
msgs: msgs.amino,
|
||||
memo: memo,
|
||||
};
|
||||
|
||||
const signDoc = sortObjectByKey(signDocRaw);
|
||||
|
||||
const signResponse = await wallet.signAmino(
|
||||
chainInfo.chainId,
|
||||
sender,
|
||||
signDoc,
|
||||
);
|
||||
|
||||
const signedTx = TxRaw.encode({
|
||||
bodyBytes: TxBody.encode(
|
||||
TxBody.fromPartial({
|
||||
// XXX: I don't know why typing error occurs.
|
||||
// TODO: Solve typing problem.
|
||||
messages: msgs.proto as any,
|
||||
memo: signResponse.signed.memo,
|
||||
}),
|
||||
).finish(),
|
||||
authInfoBytes: AuthInfo.encode({
|
||||
signerInfos: [
|
||||
{
|
||||
publicKey: {
|
||||
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
|
||||
value: PubKey.encode({
|
||||
key: Buffer.from(signResponse.signature.pub_key.value, "base64"),
|
||||
}).finish(),
|
||||
},
|
||||
modeInfo: {
|
||||
single: {
|
||||
mode: SignMode.SIGN_MODE_LEGACY_AMINO_JSON,
|
||||
},
|
||||
multi: undefined,
|
||||
},
|
||||
sequence: signResponse.signed.sequence,
|
||||
},
|
||||
],
|
||||
fee: Fee.fromPartial({
|
||||
// XXX: I don't know why typing error occurs.
|
||||
// TODO: Solve typing problem.
|
||||
// amount: signResponse.signed.fee.amount as {
|
||||
// amount: string;
|
||||
// denom: string;
|
||||
// }[],
|
||||
amount: signResponse.signed.fee.amount as any,
|
||||
gasLimit: signResponse.signed.fee.gas,
|
||||
}),
|
||||
}).finish(),
|
||||
signatures: [Buffer.from(signResponse.signature.signature, "base64")],
|
||||
}).finish();
|
||||
|
||||
return wallet.broadcastTxSync(chainInfo.chainId, signedTx);
|
||||
}
|
||||
|
||||
export function makeCosmwasmExecMsg(
|
||||
sender: string,
|
||||
contractAddress: string,
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
obj: object,
|
||||
funds: { readonly amount: string; readonly denom: string }[],
|
||||
): {
|
||||
amino: {
|
||||
readonly type: string;
|
||||
readonly value: any;
|
||||
};
|
||||
proto: Any;
|
||||
} {
|
||||
const amino = {
|
||||
type: "wasm/MsgExecuteContract",
|
||||
value: {
|
||||
sender,
|
||||
contract: contractAddress,
|
||||
msg: obj,
|
||||
funds,
|
||||
},
|
||||
};
|
||||
|
||||
const proto = {
|
||||
typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract",
|
||||
value: MsgExecuteContract.encode({
|
||||
sender: amino.value.sender,
|
||||
contract: amino.value.contract,
|
||||
msg: Buffer.from(JSON.stringify(amino.value.msg)),
|
||||
funds: amino.value.funds,
|
||||
}).finish(),
|
||||
};
|
||||
|
||||
return {
|
||||
amino,
|
||||
proto,
|
||||
};
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./types";
|
||||
export * from "./keplr";
|
||||
export * from "./cosmos";
|
||||
|
||||
Reference in New Issue
Block a user