Implementation
KeyPair deriveKeyPair(String seed, int index, {String curve = 'P256'}) {
if (!(seed is String)) {
throw "'seed' must be a string";
}
if (!(index is int) || index < 0) {
throw "index' must be a positive number";
}
final Uint8List pvBuf = derivePrivateKey(seed, index);
final Uint8List softwareIdBuf = Uint8List.fromList([0]);
switch (curve) {
case 'P256':
final Uint8List curveIdBuf = Uint8List.fromList([1]);
var ec = elliptic.getP256();
elliptic.PrivateKey privateKey = elliptic.PrivateKey.fromBytes(ec, pvBuf);
elliptic.PublicKey publicKey = ec.privateToPublicKey(privateKey);
return KeyPair(
privateKey: concatUint8List([curveIdBuf, softwareIdBuf, pvBuf]),
publicKey: concatUint8List(
[curveIdBuf, softwareIdBuf, hexToUint8List(publicKey.toHex())]));
case 'secp256k1':
final Uint8List curveIdBuf = Uint8List.fromList([2]);
var ec = elliptic.getSecp256k1();
elliptic.PrivateKey privateKey = elliptic.PrivateKey.fromBytes(ec, pvBuf);
elliptic.PublicKey publicKey = ec.privateToPublicKey(privateKey);
return KeyPair(
privateKey: concatUint8List([curveIdBuf, softwareIdBuf, pvBuf]),
publicKey: concatUint8List(
[curveIdBuf, softwareIdBuf, hexToUint8List(publicKey.toHex())]));
default:
throw 'Curve not supported';
}
}