import { playgroundHelper } from "../helpers/helpers"; import { MPCSignature } from "chainsig.js"; import { Schema, serialize } from "borsh"; import { PublicKey } from "@near-js/crypto"; import { sha256 } from "js-sha256"; 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); let nonceBuffer = Buffer.alloc(32); nonceBuffer = crypto.getRandomValues(nonceBuffer); const payload = { nonce: [...nonceBuffer], recipient: "intents.near", message: JSON.stringify({ deadline: "2025-09-05T08:25:45.461Z", intents: [ { intent: "token_diff", diff: { "nep141:usdt.tether-token.near": "-956982", "nep141:wrap.near": "391978960211266229069479", }, }, ], signer_id: addressInfo.address, }), }; const signatures = await playgroundHelper.account.callFunction({ contractId: playgroundHelper.vaultContractId, methodName: "propose_execution", args: { policy_id: "near_nep_413_chain_sig", function_args: JSON.stringify(payload), deadline_hours: 0, }, }); const firstSignResult = signatures[0]; if (!("signature" in firstSignResult)) { throw new Error("invalid signature"); } const signatureBuffer = Buffer.from(firstSignResult.signature); const MessageSchema: Schema = { struct: { tag: "u32", message: "string", nonce: { array: { type: "u8", len: 32, }, }, recipient: "string", callbackUrl: { option: "string", }, }, }; interface INep0413_PayloadToSign { message: string; // The same message passed in `SignMessageParams.message` nonce: Buffer; // The same nonce passed in `SignMessageParams.nonce` recipient: string; // The same receiver passed in `SignMessageParams.recipient` callbackUrl?: string; // The same callbackUrl passed in `SignMessageParams.callbackUrl` } class Message { tag: number; message: string; nonce: Buffer; recipient: string; callbackUrl?: string; constructor(data: INep0413_PayloadToSign) { this.tag = 2147484061; this.message = data.message; this.nonce = data.nonce; this.recipient = data.recipient; if (data.callbackUrl) { this.callbackUrl = data.callbackUrl; } } } const hashedPayload = serialize( MessageSchema, new Message({ ...payload, nonce: Buffer.from(payload.nonce), }) ); const pbKey = PublicKey.from(addressInfo.public_key); const isVerified = pbKey.verify( new Uint8Array(sha256.array(hashedPayload)), signatureBuffer ); console.log({ isVerified });