From 87df5679a340f78d8765a1e7bdba6953800dde12 Mon Sep 17 00:00:00 2001 From: James Riehl <33920192+jrriehl@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:03:42 +0100 Subject: [PATCH] feat: working agent broadcast (#237) Co-authored-by: jaswinder.khartri@fetch.ai --- packages/extension/src/config.ui.var.ts | 1 + .../agent-chat-section/chats-view-section.tsx | 20 ++++- .../extension/src/utils/sign-transaction.ts | 82 +++++++++---------- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/packages/extension/src/config.ui.var.ts b/packages/extension/src/config.ui.var.ts index 80da4ac658..c94986373d 100644 --- a/packages/extension/src/config.ui.var.ts +++ b/packages/extension/src/config.ui.var.ts @@ -99,4 +99,5 @@ export const AGENT_COMMANDS = [ export const TRANSACTION_APPROVED = "Transaction approved"; export const TRANSACTION_SENT = "Transaction sent"; +export const TRANSACTION_SIGNED = "Transaction signed"; export const TRANSACTION_FAILED = "Transaction failed"; diff --git a/packages/extension/src/pages/agent-chat-section/chats-view-section.tsx b/packages/extension/src/pages/agent-chat-section/chats-view-section.tsx index 238542f64d..8ab9240604 100644 --- a/packages/extension/src/pages/agent-chat-section/chats-view-section.tsx +++ b/packages/extension/src/pages/agent-chat-section/chats-view-section.tsx @@ -22,11 +22,12 @@ import { CHAT_PAGE_COUNT, TRANSACTION_FAILED, AGENT_ADDRESS, + TRANSACTION_SIGNED, } from "../../config.ui.var"; import { useStore } from "../../stores"; import style from "./style.module.scss"; import { AgentDisclaimer } from "@components/agents/agents-disclaimer"; -import { executeTxn } from "@utils/sign-transaction"; +import { signTransaction } from "@utils/sign-transaction"; import { useNotification } from "@components/notification"; import { InactiveAgentMessage, @@ -239,7 +240,22 @@ export const ChatsViewSection = ({ targetAddress, }; try { - await executeTxn(accountInfo, notification, payload, messagePayload); + const signResult = await signTransaction( + data, + messagePayload.chainId, + accountInfo.bech32Address + ); + deliverMessages( + messagePayload.accessToken, + messagePayload.chainId, + { + message: TRANSACTION_SIGNED, + signedTx: Buffer.from(signResult.signedTx).toString("base64"), + signature: signResult.signature.signature, + }, + accountInfo.bech32Address, + messagePayload.targetAddress + ); history.goBack(); } catch (e) { console.log(e); diff --git a/packages/extension/src/utils/sign-transaction.ts b/packages/extension/src/utils/sign-transaction.ts index 9c0e1db499..5f8830fd33 100644 --- a/packages/extension/src/utils/sign-transaction.ts +++ b/packages/extension/src/utils/sign-transaction.ts @@ -1,74 +1,68 @@ +/* eslint-disable import/no-extraneous-dependencies */ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ + import { ContextProps } from "@components/notification"; import { deliverMessages } from "@graphQL/messages-api"; import { cosmos } from "@keplr-wallet/cosmos"; import { AccountWithAll, getKeplrFromWindow } from "@keplr-wallet/stores"; +import Long from "long"; import { TRANSACTION_APPROVED } from "../config.ui.var"; -import ICoin = cosmos.base.v1beta1.ICoin; -//currently not in use export const signTransaction = async ( data: string, chainId: string, - address: string + signer: string ) => { const payload = JSON.parse(data); - const msg = { - chain_id: chainId, - account_number: payload.account_number, - msgs: payload.body.messages, - sequence: payload.sequence, - fee: { - gas: "96000", - amount: [ - { - denom: "atestfet", - amount: "480000000000000", - }, - ], - }, - memo: "", - }; - //sendTx - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const keplr = (await getKeplrFromWindow())!; - const signResponse = await keplr.signAmino(chainId, address, msg, { - preferNoSetFee: false, - preferNoSetMemo: true, - disableBalanceCheck: true, - }); - const signedTx = cosmos.tx.v1beta1.TxRaw.encode({ - bodyBytes: cosmos.tx.v1beta1.TxBody.encode({ - messages: payload.body.messages, - memo: signResponse.signed.memo, - }).finish(), + const pubKey = (await keplr.getKey(payload.chainId)).pubKey; + const unsignedTx = cosmos.tx.v1beta1.TxRaw.create({ + bodyBytes: payload.bodyBytes, authInfoBytes: cosmos.tx.v1beta1.AuthInfo.encode({ signerInfos: [ { publicKey: { type_url: "/cosmos.crypto.secp256k1.PubKey", value: cosmos.crypto.secp256k1.PubKey.encode({ - key: Buffer.from(signResponse.signature.pub_key.value, "base64"), + key: pubKey, }).finish(), }, - modeInfo: payload.authInfo.signerInfos[0].modeInfo, + modeInfo: { + single: { + mode: cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT, + }, + }, sequence: payload.sequence, }, ], fee: { - amount: signResponse.signed.fee.amount as ICoin[], - gasLimit: payload.sequence, + amount: [ + { + denom: "atestfet", + amount: "480000000000000", + }, + ], + gasLimit: Long.fromString(payload.gasLimit), }, }).finish(), + }); + const signDoc = { + bodyBytes: unsignedTx.bodyBytes, + authInfoBytes: unsignedTx.authInfoBytes, + chainId: payload.chainId, + accountNumber: payload.accountNumber, + }; + const signResponse = await keplr.signDirect(chainId, signer, signDoc, { + preferNoSetFee: false, + preferNoSetMemo: true, + disableBalanceCheck: true, + }); + const signedTx = cosmos.tx.v1beta1.TxRaw.encode({ + bodyBytes: signResponse.signed.bodyBytes, + authInfoBytes: signResponse.signed.authInfoBytes, signatures: [Buffer.from(signResponse.signature.signature, "base64")], }).finish(); - // console.log("signedTx", signedTx); - // const txHash = await keplr.sendTx( - // current.chainId, - // signedTx, - // "async" as BroadcastMode - // ); - // console.log("txHash", txHash); - return { ...signResponse, signedTx, @@ -76,7 +70,7 @@ export const signTransaction = async ( }; }; -//currently in use +//currently not in use export const executeTxn = async ( accountInfo: AccountWithAll, notification: ContextProps,