signEVMTransationWithMPC method

Future<String> signEVMTransationWithMPC({
  1. required String accountId,
  2. required String publicKey,
  3. required String privateKey,
  4. required MpcTransactionInfo mpcTransactionInfo,
  5. required String senderAdress,
  6. String path = "flutterchain",
  7. String mpcContract = 'v2.multichain-mpc.testnet',
})

Signs an EVM transaction with the Near MPC.

Parameters:

  • accountId: The account ID of the Near sender.
  • publicKey: The public key of the Near sender.
  • privateKey: The private key of the Near sender.
  • mpcTransactionInfo: The transaction information obtained from the MPC.
  • senderAdress: The address of the sender provided by the MPC.
  • path: The path used for the MPC signing.
  • mpcContract: The contract address of the MPC.

Returns the signed transaction as a string.

Implementation

Future<String> signEVMTransationWithMPC({
  required String accountId,
  required String publicKey,
  required String privateKey,
  required MpcTransactionInfo mpcTransactionInfo,
  required String senderAdress,
  String path = "flutterchain",
  String mpcContract = 'v2.multichain-mpc.testnet',
}) async {
  final unsignedTransaction = mpcTransactionInfo.transactionInfo;
  final payload = Uint8List.fromList(
      List<int>.from(unsignedTransaction["payload"].values));
  final reversedPayload = payload.reversed.toList();
  final nearSignRequest = await callSmartContractFunction(
    NearTransferRequest(
      fromAddress: accountId,
      publicKey: publicKey,
      toAddress: mpcContract,
      privateKey: privateKey,
      gas: "300000000000000",
      arguments: NearBlockChainSmartContractArguments(
        method: "sign",
        args: {
          "payload": reversedPayload,
          "path": path,
          "key_version": 0,
        },
        transferAmount: '0',
      ),
    ),
  );

  if (nearSignRequest.data["error"] != null) {
    throw Exception(nearSignRequest.data["error"]);
  }

  final signatureValList =
      List<String>.from(jsonDecode(nearSignRequest.data["success"]));

  final signatureData = jsonEncode({
    "big_r": signatureValList[0],
    "big_s": signatureValList[1],
  });

  final serializedUnsignedTransaction = Uint8List.fromList(
      List<int>.from(unsignedTransaction["transaction"].values));

  final signedTransaction = await jsVMService.callJS(
    "window.EVMUtils.signTransactionWithMPCSignature('$signatureData', '${jsonEncode(serializedUnsignedTransaction)}', '$senderAdress', '${unsignedTransaction["typeOfTransaction"]}', '${jsonEncode(unsignedTransaction['chainInfo'])}')",
  );

  return signedTransaction;
}