Ed25519Keypair.fromSecretKey constructor

Ed25519Keypair.fromSecretKey(
  1. Uint8List secretKey, {
  2. bool skipValidation = true,
})

Create a Ed25519 keypair from a raw secret key byte array.

throws error if the provided secret key is invalid and validation is not skipped.

Implementation

factory Ed25519Keypair.fromSecretKey(
  Uint8List secretKey,
  { bool skipValidation = true }
) {
  if (secretKey.length == 32) {
    return Ed25519Keypair.fromSeed(secretKey);
  }
  if (secretKey.length != 64) throw ArgumentError("Wrong secretKey size. Expected 64 bytes, got ${secretKey.length}.");

  final privateKey = ed25519.PrivateKey(secretKey);
  final publicKey = ed25519.public(privateKey);

  if (!skipValidation) {
    final msg =  Uint8List.fromList(utf8.encode('sui validation'));
    final signature = ed25519.sign(privateKey,msg);
    if (!ed25519.verify(publicKey, msg, signature)) {
      throw ArgumentError('provided secretKey is invalid');
    }
  }
  return Ed25519Keypair(secretKey);
}