getNonceForAddress method Null safety
Helper method to get the nonce for a given address (account or contract) for the contract specified by the contractId. Used for contract auth.
Implementation
Future<int> getNonceForAddress(Address address, String contractId) async {
XdrLedgerKey ledgerKey = XdrLedgerKey(XdrLedgerEntryType.CONTRACT_DATA);
ledgerKey.contractID = XdrHash(Util.hexToBytes(contractId));
XdrSCVal nonceKeyVal = XdrSCVal.forNonceKeyWithAddress(address);
ledgerKey.contractDataKey = nonceKeyVal;
GetLedgerEntryResponse response =
await getLedgerEntry(ledgerKey.toBase64EncodedXdrString());
if (!response.isErrorResponse &&
response.ledgerEntryDataXdr != null &&
response.ledgerEntryDataXdr!.contractData != null) {
XdrSCVal val = response.ledgerEntryDataXdr!.contractData!.val;
if (val.u64 != null) {
return val.u64!.uint64;
}
}
return 0;
}