previousSignaturePayload method

Uint8List previousSignaturePayload()

Generate the payload for the previous signature by encoding address, type and data

Implementation

Uint8List previousSignaturePayload() {
  final Uint8List bufCodeSize = toByteArray(data!.code!.length, length: 4);
  // ignore: prefer_final_locals
  int contentSize = utf8.encode(data!.content!).length;
  final Uint8List bufContentSize = toByteArray(contentSize, length: 4);

  Uint8List ownershipsBuffers = Uint8List(0);
  for (Ownership ownership in data!.ownerships!) {
    final Uint8List bufAuthKeyLength = Uint8List.fromList(
        toByteArray(ownership.authorizedPublicKeys!.length));

    final List<Uint8List> authorizedKeysBuffer = <Uint8List>[
      Uint8List.fromList(<int>[bufAuthKeyLength.length]),
      bufAuthKeyLength
    ];

    ownership.authorizedPublicKeys!.sort((AuthorizedKey a, AuthorizedKey b) =>
        a.publicKey!.compareTo(b.publicKey!));

    for (AuthorizedKey authorizedKey in ownership.authorizedPublicKeys!) {
      authorizedKeysBuffer
          .add(Uint8List.fromList(hexToUint8List(authorizedKey.publicKey!)));
      authorizedKeysBuffer.add(Uint8List.fromList(
          hexToUint8List(authorizedKey.encryptedSecretKey!)));
    }

    ownershipsBuffers = concatUint8List(<Uint8List>[
      ownershipsBuffers,
      toByteArray(
          Uint8List.fromList(hexToUint8List(ownership.secret!)).lengthInBytes,
          length: 4),
      Uint8List.fromList(hexToUint8List(ownership.secret!)),
      concatUint8List(authorizedKeysBuffer)
    ]);
  }

  Uint8List ucoTransfersBuffers = Uint8List(0);
  if (data!.ledger!.uco!.transfers!.isNotEmpty) {
    for (UCOTransfer ucoTransfer in data!.ledger!.uco!.transfers!) {
      ucoTransfersBuffers = concatUint8List(<Uint8List>[
        ucoTransfersBuffers,
        Uint8List.fromList(hexToUint8List(ucoTransfer.to!)),
        toByteArray(ucoTransfer.amount!, length: 8)
      ]);
    }
  }

  Uint8List tokenTransfersBuffers = Uint8List(0);
  if (data!.ledger!.token!.transfers!.isNotEmpty) {
    for (TokenTransfer tokenTransfer in data!.ledger!.token!.transfers!) {
      final Uint8List bufTokenId =
          Uint8List.fromList(toByteArray(tokenTransfer.tokenId!));

      tokenTransfersBuffers = concatUint8List(<Uint8List>[
        tokenTransfersBuffers,
        Uint8List.fromList(hexToUint8List(tokenTransfer.tokenAddress!)),
        Uint8List.fromList(hexToUint8List(tokenTransfer.to!)),
        toByteArray(tokenTransfer.amount!, length: 8),
        Uint8List.fromList(<int>[bufTokenId.length]),
        bufTokenId
      ]);
    }
  }

  Uint8List recipients = Uint8List(0);
  for (String recipient in data!.recipients!) {
    recipients = concatUint8List(<Uint8List>[
      recipients,
      Uint8List.fromList(hexToUint8List(recipient))
    ]);
  }

  final Uint8List bufOwnershipLength =
      Uint8List.fromList(toByteArray(data!.ownerships!.length));
  final Uint8List bufUCOTransferLength =
      Uint8List.fromList(toByteArray(data!.ledger!.uco!.transfers!.length));
  final Uint8List bufTokenTransferLength =
      Uint8List.fromList(toByteArray(data!.ledger!.token!.transfers!.length));
  final Uint8List bufRecipientLength =
      Uint8List.fromList(toByteArray(data!.recipients!.length));

  return concatUint8List(<Uint8List>[
    toByteArray(version!, length: 4),
    Uint8List.fromList(hexToUint8List(address!)),
    Uint8List.fromList(<int>[txTypes[type]!]),
    bufCodeSize,
    Uint8List.fromList(utf8.encode(data!.code!)),
    bufContentSize,
    Uint8List.fromList(utf8.encode(data!.content!)),
    Uint8List.fromList(<int>[bufOwnershipLength.length]),
    bufOwnershipLength,
    ownershipsBuffers,
    Uint8List.fromList(<int>[bufUCOTransferLength.length]),
    bufUCOTransferLength,
    ucoTransfersBuffers,
    Uint8List.fromList(<int>[bufTokenTransferLength.length]),
    bufTokenTransferLength,
    tokenTransfersBuffers,
    Uint8List.fromList(<int>[bufRecipientLength.length]),
    bufRecipientLength,
    recipients
  ]);
}