sendCoeusTx method

Future<String> sendCoeusTx(
  1. String fromAddress,
  2. List<UserOperation> userOperations,
  3. HydraPrivate hydraPrivate, {
  4. int? layer1SenderNonce,
  5. int? layer2PublicKeyNonce,
})

Implementation

Future<String> sendCoeusTx(
  String fromAddress,
  List<UserOperation> userOperations,
  HydraPrivate hydraPrivate, {
  int? layer1SenderNonce,
  int? layer2PublicKeyNonce,
}) async {
  // TODO: we have to get the nonce from somewhere else rather using layer2api
  final layer2Api = Layer2Api.createCoeusApi(_networkConfig);
  final secpPubKey =
      hydraPrivate.public.keyByAddress(fromAddress).publicKey();
  final secpPrivKey = hydraPrivate.keyByPublicKey(secpPubKey).privateKey();
  final noncedBuilder = NoncedBundleBuilder.create();
  userOperations.forEach((element) => noncedBuilder.add(element));

  layer2PublicKeyNonce ??=
      (await layer2Api.getLastNonce(PublicKey.fromSecp(secpPubKey))) + 1;
  final noncedBundle = noncedBuilder.build(layer2PublicKeyNonce);
  final signedBundle = noncedBundle.sign(PrivateKey.fromSecp(secpPrivKey));

  layer1SenderNonce ??= (await getWalletNonce(fromAddress)) + 1;
  final tx = CoeusTxBuilder.create(_networkConfig.network).build(
    signedBundle,
    secpPubKey,
    layer1SenderNonce,
  );

  final signedTx = secpPrivKey.signHydraTransaction(tx);
  return await sendTx(signedTx.toString());
}