deriveKeyPair function
Implementation
KeyPair deriveKeyPair(String seed, int index, {String curve = "ed25519"}) {
if (!(seed is String)) {
throw "'seed' must be a string";
}
if (!(index is int) || index < 0) {
throw "index' must be a positive number";
}
Uint8List pvBuf = derivePrivateKey(seed, index);
switch (curve) {
case "ed25519":
var curveIdBuf = Uint8List.fromList([0]);
var signingKey = ed25519.SigningKey(seed: pvBuf);
Uint8List pubBuf = signingKey.publicKey.toUint8List();
return KeyPair(
privateKey: concatUint8List([curveIdBuf, pvBuf]),
publicKey: concatUint8List([curveIdBuf, pubBuf]));
case "P256":
var curveIdBuf = Uint8List.fromList([1]);
final p256 = ECCurve_prime256v1();
final point = p256.G;
final bigInt = BigInt.parse(hex.encode(pvBuf), radix: 16);
var curvePoint = point * bigInt;
Uint8List pubBuf = curvePoint!.getEncoded(false);
return KeyPair(
privateKey: concatUint8List([curveIdBuf, pvBuf]),
publicKey: concatUint8List([curveIdBuf, pubBuf]));
case "secp256k1":
var curveIdBuf = Uint8List.fromList([2]);
final secp256k1 = ECCurve_secp256k1();
final point = secp256k1.G;
final bigInt = BigInt.parse(hex.encode(pvBuf), radix: 16);
var curvePoint = point * bigInt;
Uint8List pubBuf = curvePoint!.getEncoded(false);
return KeyPair(
privateKey: concatUint8List([curveIdBuf, pvBuf]),
publicKey: concatUint8List([curveIdBuf, pubBuf]));
default:
throw "Curve not supported";
}
}