combine function

Implementation

CombineSignedTransactionResult combine(
    rosetta.ConstructionCombineRequestPart req) {
  Map<String, rosetta.Signature> signaturesBySigData =
      <String, rosetta.Signature>{};
  for (var sig in req.signatures) {
    signaturesBySigData.putIfAbsent(sig.signing_payload.hex_bytes, () => sig);
  }

  var unsignedTransaction = cborDecode<Map>(req.unsigned_transaction.toU8a());

  assert(req.signatures.length ==
      unsignedTransaction["ingress_expiries"]?.length * 2);
  assert((unsignedTransaction["updates"] as List).length == 1);

  var envelopes = [];
  for (var updateItem in unsignedTransaction["updates"]) {
    var reqType = updateItem[0];

    var update = updateItem[1];
    var requestEnvelopes = [];

    for (var ingressExpiry in unsignedTransaction["ingress_expiries"]) {
      update["ingress_expiry"] = BigInt.from(ingressExpiry).toInt();

      var readState = make_read_state_from_update(update);

      var transactionSignature = signaturesBySigData[
          blobToHex(make_sig_data(HttpCanisterUpdate_id(update)))];

      var readStateSignature = signaturesBySigData[blobToHex(make_sig_data(
          HttpReadState_representation_independent_hash(readState)))];

      var envelope = {
        "content": {
          "request_type": "call",
          ...update,
        },
        "sender_pubkey": Ed25519PublicKey.fromRaw(
                blobFromHex(transactionSignature!.public_key.hex_bytes))
            .toDer(),
        "sender_sig": blobFromHex(transactionSignature.hex_bytes),
        "sender_delegation": null,
      };

      // envelope.content.encodeCBOR = cbor.Encoder.encodeIndefinite;

      var readStateEnvelope = {
        "content": {
          "request_type": "read_state",
          ...readState,
        },
        "sender_pubkey": Ed25519PublicKey.fromRaw(
                blobFromHex(readStateSignature!.public_key.hex_bytes))
            .toDer(),
        "sender_sig": blobFromHex(readStateSignature.hex_bytes),
        "sender_delegation": null,
      };
      requestEnvelopes
          .add({"update": envelope, "read_state": readStateEnvelope});
    }
    envelopes.add([reqType, requestEnvelopes]);
  }

  var signedTransaction = blobToHex(
      cborEncode(envelopes, withSerializer: initCborSerializerNoHead()));

  return CombineSignedTransactionResult(signedTransaction);
}