import { playgroundHelper } from "../helpers/helpers"; import { MPCSignature } from "chainsig.js"; import { actionCreators, createTransaction, Signature, SignedTransaction, } from "@near-js/transactions"; import { baseDecode } from "@near-js/utils"; import { Account } from "@near-js/accounts"; import { KeyType, PublicKey } from "@near-js/crypto"; const addressInfo = await playgroundHelper.provider.callFunction<{ public_key: string; address: string; }>( playgroundHelper.vaultContractId, "derive_address_and_public_key", { path: "1", chain: "NearWasm", near_network: "Testnet", }); if(!addressInfo){ throw new Error("Unable to find chain sig address info") } console.log(addressInfo); const block = await playgroundHelper.provider.block({ finality: "near-final", }); const derivedAccount = new Account( addressInfo.address, playgroundHelper.provider ); const allKeys = await derivedAccount.getAccessKeyList(); const matchingKey = allKeys.keys.find( (e) => e.public_key === addressInfo.public_key ); if (!matchingKey) { throw new Error("Key not found"); } const transaction = createTransaction( addressInfo.address, PublicKey.from(addressInfo.public_key), "guest-book.testnet", BigInt(matchingKey.access_key.nonce) + 1n, [ actionCreators.functionCall( "addMessage", { text: "coming from chain sig", }, 30000000000000n, 0n ), ], baseDecode(block.header.hash) ); const encodedTx = Buffer.from(transaction.encode()).toString("base64"); const signatures = await playgroundHelper.account.callFunction({ contractId: playgroundHelper.vaultContractId, methodName: "propose_execution", args: { policy_id: "guestbook_testnet_near_chainsig_add_msg", function_args: encodedTx, deadline_hours: 0, }, }); const firstSignResult = signatures[0]; if (!("signature" in firstSignResult)) { throw new Error("invalid signature"); } const signatureBuffer = Buffer.from(firstSignResult.signature); const signedTx = new SignedTransaction({ transaction: transaction, signature: new Signature({ keyType: KeyType.ED25519, data: signatureBuffer, }), }); const receipt = await playgroundHelper.provider.sendTransaction(signedTx); console.log(receipt.transaction.hash);