UNPKG

@cyberk/hyperion-sdk

Version:

Hyperion SDK from Cyberk

221 lines 6.65 kB
"use strict"; // import { StdSignDoc, makeSignDoc } from "@cosmjs/amino"; // import { fromBase64, toBase64 } from "@cosmjs/encoding"; // import { // TxBody, // AuthInfo, // Fee as ProtoFee, // TxRaw, // } from "cosmjs-types/cosmos/tx/v1beta1/tx"; // import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing"; // import { PubKey as ProtoSecp256k1PubKey } from "cosmjs-types/cosmos/crypto/secp256k1/keys"; // import { Any } from "cosmjs-types/google/protobuf/any"; // import { MessageComposer } from "@titanlabjs/titan-types/cosmwasm/wasm/v1/tx.registry"; // import { toUtf8 } from "@titanlabjs/encoding"; // interface SignContractExecuteParams { // provider: any; // WalletConnect UniversalProvider // signerAddress: string; // chainId: string; // contractAddress: string; // msg: any; // The contract message to execute // funds?: { denom: string; amount: string }[]; // memo?: string; // lcdUrl: string; // } // export async function getAccountDetails( // address: string, // lcdUrl: string // ): Promise<{ accountNumber: string; sequence: string }> { // try { // const response = await fetch( // `${lcdUrl}/cosmos/auth/v1beta1/accounts/${address}` // ); // const data = await response.json(); // const account = data.account; // if (!account) throw new Error("Account not found"); // const accountNumber = // account.account_number || // (account.base_account && account.base_account.account_number); // const sequence = // account.sequence || // (account.base_account && account.base_account.sequence); // if (accountNumber === undefined || sequence === undefined) { // throw new Error("Could not retrieve account number or sequence."); // } // return { // accountNumber: accountNumber.toString(), // sequence: sequence.toString(), // }; // } catch (error) { // console.error(`Failed to get account details for ${address}:`, error); // throw error; // } // } // export async function prepareContractExecuteSignDoc({ // signerAddress, // contractAddress, // msg, // funds = [], // memo = "", // lcdUrl, // chainId, // }: Omit<SignContractExecuteParams, "provider">): Promise<StdSignDoc> { // const { accountNumber, sequence } = await getAccountDetails( // signerAddress, // lcdUrl // ); // const messages = [ // { // type: "wasm/MsgExecuteContract", // value: { // sender: signerAddress, // contract: contractAddress, // msg: toUtf8(JSON.stringify(msg)), // funds: funds, // }, // }, // ]; // const fee = { // amount: [{ denom: "utitan", amount: "5000" }], // Example fee, adjust as needed // gas: "200000", // Example gas limit, adjust as needed // }; // return makeSignDoc(messages, fee, chainId, memo, accountNumber, sequence); // } // export async function signContractExecute({ // provider, // signerAddress, // chainId, // contractAddress, // msg, // funds, // memo, // lcdUrl, // }: SignContractExecuteParams): Promise<{ // signature: string; // signedData: StdSignDoc; // }> { // const signDoc = await prepareContractExecuteSignDoc({ // signerAddress, // contractAddress, // msg, // funds, // memo, // lcdUrl, // chainId, // }); // try { // const signResponse = await provider.request({ // method: "cosmos_signAmino", // params: { // signerAddress: signerAddress, // signDoc: signDoc, // }, // }); // return { // signature: signResponse.signature.signature, // signedData: signResponse.signed, // }; // } catch (error) { // console.error("Error signing contract execute message:", error); // throw error; // } // } // export async function constructTxRawFromAminoSignature( // signedAminoDoc: StdSignDoc, // wcSignature: { pub_key: { type: string; value: string }; signature: string } // ): Promise<Uint8Array> { // // 1. Encode Messages to Protobuf Any[] // const messagesProto: Any[] = signedAminoDoc.msgs.map((msg) => { // if (msg.type === "wasm/MsgExecuteContract") { // const aminoMsg = msg.value as any; // return { // typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", // value: MessageComposer.fromPartial.executeContract({ // sender: aminoMsg.sender, // contract: aminoMsg.contract, // msg: aminoMsg.msg, // funds: aminoMsg.funds, // }), // }; // } // throw new Error( // `Unsupported Amino message type for Protobuf conversion: ${msg.type}` // ); // }); // // 2. Encode TxBody // const txBodyBytes = TxBody.encode( // TxBody.fromPartial({ // messages: messagesProto, // memo: signedAminoDoc.memo, // }) // ).finish(); // // 3. Prepare Public Key for AuthInfo // if (!wcSignature.pub_key || !wcSignature.pub_key.value) { // throw new Error( // "Public key not found in WalletConnect signature response." // ); // } // const pubkeyBytes = fromBase64(wcSignature.pub_key.value); // const pubKeyProtoForAuthInfo: Any = { // typeUrl: "/cosmos.crypto.secp256k1.PubKey", // value: ProtoSecp256k1PubKey.encode({ key: pubkeyBytes }).finish(), // }; // // 4. Encode AuthInfo // const authInfoBytes = AuthInfo.encode({ // signerInfos: [ // { // publicKey: pubKeyProtoForAuthInfo, // modeInfo: { // single: { // mode: SignMode.SIGN_MODE_LEGACY_AMINO_JSON, // }, // }, // sequence: BigInt(signedAminoDoc.sequence), // }, // ], // fee: ProtoFee.fromPartial({ // amount: signedAminoDoc.fee.amount.map((coin) => ({ // denom: coin.denom, // amount: coin.amount, // })), // gasLimit: BigInt(signedAminoDoc.fee.gas), // }), // }).finish(); // // 5. Get Signature Bytes // const signatureBytes = fromBase64(wcSignature.signature); // // 6. Encode TxRaw // const txRawBytes = TxRaw.encode({ // bodyBytes: txBodyBytes, // authInfoBytes: authInfoBytes, // signatures: [signatureBytes], // }).finish(); // return txRawBytes; // } // export async function broadcastTx( // txRawBytes: Uint8Array, // lcdUrl: string, // mode: // | "BROADCAST_MODE_SYNC" // | "BROADCAST_MODE_ASYNC" // | "BROADCAST_MODE_BLOCK" = "BROADCAST_MODE_SYNC" // ): Promise<any> { // try { // const txString = toBase64(txRawBytes); // const broadcastRequest = { // tx_bytes: txString, // mode: mode, // }; // const response = await fetch(`${lcdUrl}/cosmos/tx/v1beta1/txs`, { // method: "POST", // headers: { // "Content-Type": "application/json", // }, // body: JSON.stringify(broadcastRequest), // }); // return await response.json(); // } catch (error) { // console.error("Transaction broadcast failed:", error); // throw error; // } // } //# sourceMappingURL=ab.js.map