derive method

BIP32 derive(
  1. int index
)

Implementation

BIP32 derive(int index) {
  if (index > _constUnit32Max || index < 0) throw  ArgumentError("Expected UInt32");
  final isHardened = index >= _constHighestBit;
  Uint8List data =  Uint8List(37);
  if (isHardened) {
    if (isNeutered()) {
      throw  ArgumentError("Missing private key for hardened child key");
    }
    data[0] = 0x00;
    data.setRange(1, 33, privateKey!);
    data.buffer.asByteData().setUint32(33, index);
  } else {
    data.setRange(0, 33, publicKey);
    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);
  }
  BIP32 hd;
  if (!isNeutered()) {
    final ki = ecc.privateAdd(privateKey!, il);
    if (ki == null) return derive(index + 1);
    hd = BIP32.fromPrivateKey(ki, ir, network);
  } else {
    final ki = ecc.pointAddScalar(publicKey, il, true);
    if (ki == null) return derive(index + 1);
    hd = BIP32.fromPublicKey(ki, ir, network);
  }
  hd.depth = depth + 1;
  hd.index = index;
  hd.parentFingerprint = fingerprint.buffer.asByteData().getUint32(0);
  return hd;
}