pathToKey method

Bip32Key pathToKey(
  1. String path
)

Implementation

Bip32Key pathToKey(String path) {
  final kind = path.split('/').removeAt(0);

  if (![_privateKeyPrefix, _publicKeyPrefix].contains(kind)) {
    throw Exception("Path needs to start with 'm' or 'M'");
  }

  if (kind == _privateKeyPrefix && root is Bip32PublicKey) {
    throw Exception('Cannot derive private key from public master');
  }

  final wantsPrivate = kind == _privateKeyPrefix;
  final children = _parseChildren(path);

  if (children.isEmpty) {
    if (wantsPrivate) {
      return root;
    }
    return root.publicKey as Bip32Key;
  }

  return children.fold(root, (previousKey, childNumber) {
    return previousKey.derive(childNumber);
  });
}