derivePath method

Bip32 derivePath(
  1. String path
)

Implementation

Bip32 derivePath(String path) {
  final regex = RegExp(r"^(m\/)?(\d+'?\/)*\d+'?$");
  if (!regex.hasMatch(path)) throw ArgumentError('Expected BIP32 Path');
  List<String> splitPath = path.split('/');
  if (splitPath[0] == 'm') {
    if (parentFingerprint != 0) {
      throw ArgumentError('Expected master, got child');
    }
    splitPath = splitPath.sublist(1);
  }
  return splitPath.fold(this, (Bip32 prevHd, String indexStr) {
    int index;
    if (indexStr.substring(indexStr.length - 1) == "'") {
      index = int.parse(indexStr.substring(0, indexStr.length - 1));
      return prevHd.deriveHardened(index);
    } else {
      index = int.parse(indexStr);
      return prevHd.derive(index);
    }
  });
}