generateKeyPair method

KeyPair generateKeyPair()

Generates a random key pair.

Implementation

KeyPair generateKeyPair() {
  final secureRandom = FortunaRandom()
    ..seed(KeyParameter(Uint8List.fromList(
        List.generate(32, (_) => Random.secure().nextInt(256)))));

  final curve = ECCurve_secp256k1();
  final domainParams = ECKeyGeneratorParameters(curve);

  final keyGenerator = ECKeyGenerator()
    ..init(ParametersWithRandom(domainParams, secureRandom));

  final keyPair = keyGenerator.generateKeyPair();

  final privateKey = keyPair.privateKey as ECPrivateKey;
  final publicKey = keyPair.publicKey as ECPublicKey;

  final privateKeyBytes =
      privateKey.d!.toRadixString(16).toUpperCase().padLeft(64, '0');
  final publicKeyBytes = publicKey.Q!.getEncoded(false);

  return KeyPair(
      privateKey: base64.encode(hex.decode(privateKeyBytes)),
      publicKey: base64.encode(publicKeyBytes));
}