combine function

Implementation

CombineSignedTransactionResult combine(
  rosetta.ConstructionCombineRequestPart req,
) {
  final signaturesBySigData = <String, rosetta.WalletSignature>{};
  for (final sig in req.signatures) {
    signaturesBySigData.putIfAbsent(sig.signingPayload.hexBytes, () => sig);
  }
  final unsignedTransaction = cborDecode<Map>(req.unsignedTransaction.toU8a());

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

  final envelopes = [];
  for (final updateItem in unsignedTransaction['updates']) {
    final reqType = updateItem[0];
    final update = updateItem[1];
    final requestEnvelopes = [];
    for (final ingressExpiry in unsignedTransaction['ingress_expiries']) {
      update['ingress_expiry'] = BigInt.from(ingressExpiry).toInt();

      final readState = makeReadStateFromUpdate(update);
      final transactionSignature = signaturesBySigData[
          blobToHex(makeSignatureData(httpCanisterUpdateId(update)))];
      final readStateSignature = signaturesBySigData[blobToHex(
        makeSignatureData(
          httpReadStateRepresentationIndependentHash(readState),
        ),
      )];

      final envelope = {
        'content': {'request_type': 'call', ...update},
        'sender_pubkey': Ed25519PublicKey.fromRaw(
          blobFromHex(transactionSignature!.publicKey.hexBytes),
        ).toDer(),
        'sender_sig': blobFromHex(transactionSignature.hexBytes),
        'sender_delegation': null,
      };
      final readStateEnvelope = {
        'content': {'request_type': 'read_state', ...readState},
        'sender_pubkey': Ed25519PublicKey.fromRaw(
          blobFromHex(readStateSignature!.publicKey.hexBytes),
        ).toDer(),
        'sender_sig': blobFromHex(readStateSignature.hexBytes),
        'sender_delegation': null,
      };
      requestEnvelopes.add(
        {'update': envelope, 'read_state': readStateEnvelope},
      );
    }
    envelopes.add([reqType, requestEnvelopes]);
  }

  final signedTransaction = blobToHex(
    cborEncode(envelopes, withSerializer: initCborSerializerNoHead()),
  );
  return CombineSignedTransactionResult(signedTransaction);
}