sign method Null safety

void sign(
  1. KeyPair signer,
  2. Network network
)

Signs the authorization entry.

The signature will be added to the signatureArgs of the soroban credentials

Implementation

void sign(KeyPair signer, Network network) {
  XdrSorobanCredentials xdrCredentials = credentials.toXdr();
  if (credentials.addressCredentials == null ||
      xdrCredentials.type !=
          XdrSorobanCredentialsType.SOROBAN_CREDENTIALS_ADDRESS ||
      xdrCredentials.address == null) {
    throw Exception("no soroban address credentials found");
  }

  XdrHashIDPreimageSorobanAuthorization authPreimageXdr =
      XdrHashIDPreimageSorobanAuthorization(
          XdrHash(network.networkId!),
          xdrCredentials.address!.nonce,
          xdrCredentials.address!.signatureExpirationLedger,
          rootInvocation.toXdr());
  XdrHashIDPreimage rootInvocationPreimage =
      XdrHashIDPreimage(XdrEnvelopeType.ENVELOPE_TYPE_SOROBAN_AUTHORIZATION);
  rootInvocationPreimage.sorobanAuthorization = authPreimageXdr;
  XdrDataOutputStream xdrOutputStream = XdrDataOutputStream();
  XdrHashIDPreimage.encode(xdrOutputStream, rootInvocationPreimage);
  Uint8List payload = Util.hash(Uint8List.fromList(xdrOutputStream.bytes));
  Uint8List signatureBytes = signer.sign(payload);
  AccountEd25519Signature signature =
      AccountEd25519Signature(signer.xdrPublicKey, signatureBytes);
  credentials.addressCredentials!.signatureArgs.add(signature.toXdrSCVal());
}