derive method
Derives a new HDNode from this node based on index
Implementation
HDNode derive(int index) {
if (index > UINT32_MAX || index < 0) throw ArgumentError("Expected UInt32");
final isHardened = index >= HIGHEST_BIT;
Uint8List data = new Uint8List(37);
if (isHardened) {
if (_isNeutered()) {
throw ArgumentError("Missing private key for hardened child key");
}
data[0] = 0x00;
data.setRange(1, 33, _keyPair.privateKey);
data.buffer.asByteData().setUint32(33, index);
} else {
data.setRange(0, 33, publicKeyList);
data.buffer.asByteData().setUint32(33, index);
}
final I = hmacSHA512(_chainCode, data);
final IL = I.sublist(0, 32);
final IR = I.sublist(32);
// if (!ecc.isPrivate(IL)) {
// return derive(index + 1);
// }
ECPair derivedKeyPair;
if (!_isNeutered()) {
final ki = ECurve.privateAdd(_keyPair.privateKey, IL);
if (ki == null) return derive(index + 1);
derivedKeyPair = ECPair(ki, null, network: this._keyPair.network);
} else {
final ki = ECurve.pointAddScalar(publicKeyList, IL, true);
if (ki == null) return derive(index + 1);
derivedKeyPair = ECPair(null, ki, network: this._keyPair.network);
}
final hd = HDNode(derivedKeyPair, IR);
hd.depth = depth + 1;
hd.index = index;
hd.parentFingerprint = fingerprint.buffer.asByteData().getUint32(0);
return hd;
}