#24 - Generate ECKeyPair with Dart

Date: 2018-09-01 12:00 - dart

Generate a elliptic curve key pair in Dart using pointy castle package.

import 'dart:async';
import 'dart:math';
import 'dart:typed_data';

import "package:pointycastle/export.dart";

class WalletService {
  static Wallet generateWallet() {
    var curve = ECCurve_secp256k1();
    var params = ECKeyGeneratorParameters(curve);
    var keyGenerator = ECKeyGenerator();

    var secureRandom = FortunaRandom();
    var dartRandom = Random.secure();
    var seeds = List<int>();
    for (int i = 0; i < 32; i++) {
      seeds.add(dartRandom.nextInt(255));
    }
    secureRandom.seed(KeyParameter(Uint8List.fromList(seeds)));

    var randomParams = ParametersWithRandom(params, secureRandom);
    keyGenerator.init(randomParams);
    var keyPair = keyGenerator.generateKeyPair();
    return Wallet(keyPair.publicKey as ECPublicKey, keyPair.privateKey as ECPrivateKey);
  }
}

Previous snippet | Next snippet