signBTCTransactionWithMPC method
Signs a Bitcoin 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.transactionInfo
: The transaction information obtained from the MPC.mpcSenderPublicKey
: The public key 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> signBTCTransactionWithMPC({
required String accountId,
required String publicKey,
required String privateKey,
required MpcTransactionInfo transactionInfo,
required String mpcSenderPublicKey,
String path = "flutterchain",
String mpcContract = 'v2.multichain-mpc.testnet',
}) async {
final unsignedTransaction = transactionInfo.transactionInfo;
//Get payloads of all utxos
final payloadsListEncoded = await jsVMService.callJS(
"window.BitcoinUtils.getReversedPayloadsToSignForMPC('${unsignedTransaction['psbt']}', '${jsonEncode(unsignedTransaction['utxos'])}', '$mpcSenderPublicKey')");
final payloadsList = jsonDecode(payloadsListEncoded) as List<dynamic>;
final List<Map<String, dynamic>> signatures = [];
//Get signatures for each payload
for (var i = 0; i < payloadsList.length; i++) {
final payload = Uint8List.fromList(List<int>.from(payloadsList[i]));
final nearSignRequest = await callSmartContractFunction(
NearTransferRequest(
fromAddress: accountId,
publicKey: publicKey,
toAddress: mpcContract,
privateKey: privateKey,
gas: "300000000000000",
arguments: NearBlockChainSmartContractArguments(
method: "sign",
args: {
"payload": payload,
"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 = {
"big_r": signatureValList[0],
"big_s": signatureValList[1],
};
signatures.add(signatureData);
}
//Sign transaction
final signedTransactionEncoded = await jsVMService.callJS(
"window.BitcoinUtils.signTransactionWithMPCSignature('${unsignedTransaction['psbt']}', '${jsonEncode(signatures)}', '$mpcSenderPublicKey')");
final signedTransaction = jsonDecode(signedTransactionEncoded) as String;
return signedTransaction;
}