generateNonce function
String
generateNonce(
- PublicKey publicKey,
- int maxEpoch,
- dynamic randomness
)
Implementation
String generateNonce(PublicKey publicKey, int maxEpoch, dynamic randomness) {
BigInt publicKeyBytes = toBigIntBE(publicKey.toSuiBytes());
BigInt ephPublicKey0 = publicKeyBytes ~/ BigInt.two.pow(128);
BigInt ephPublicKey1 = publicKeyBytes % BigInt.two.pow(128);
BigInt bigNum;
if (randomness is String) {
bigNum = poseidonHash([
ephPublicKey0,
ephPublicKey1,
BigInt.from(maxEpoch),
BigInt.parse(randomness)
]);
} else if (randomness is BigInt) {
bigNum = poseidonHash(
[ephPublicKey0, ephPublicKey1, BigInt.from(maxEpoch), randomness]);
} else {
throw ArgumentError(
'Invalid type for randomness. It should be either BigInt or String.');
}
Uint8List z = toBigEndianBytes(bigNum, 20);
String nonce = base64UrlEncode(z)
.replaceAll('=', '')
.replaceAll('+', '-')
.replaceAll('/', '_');
if (nonce.length != NONCE_LENGTH) {
throw Exception(
'Length of nonce $nonce (${nonce.length}) is not equal to $NONCE_LENGTH');
}
return nonce;
}