Skip to content

Commit

Permalink
feat: working agent broadcast (#237)
Browse files Browse the repository at this point in the history
Co-authored-by: jaswinder.khartri@fetch.ai <jaswinder.khartri@fetch.ai>
  • Loading branch information
jrriehl and jaswinder.khartri@fetch.ai authored Apr 12, 2023
1 parent 10bbd12 commit 87df567
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
1 change: 1 addition & 0 deletions packages/extension/src/config.ui.var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
82 changes: 38 additions & 44 deletions packages/extension/src/utils/sign-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,76 @@
/* 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,
message: "Transaction Signed",
};
};

//currently in use
//currently not in use
export const executeTxn = async (
accountInfo: AccountWithAll,
notification: ContextProps,
Expand Down

0 comments on commit 87df567

Please sign in to comment.