derive method

HDWallet derive(
  1. String derivationPath
)

Derives a child wallet using the specified derivation path.

Path format: "m/44'/60'/0'/0/0" (BIP-44 Ethereum path) Use apostrophe (') to indicate hardened derivation.

Implementation

HDWallet derive(String derivationPath) {
  if (!derivationPath.startsWith('m/') && !derivationPath.startsWith('/')) {
    throw ArgumentError('Invalid derivation path format');
  }

  final pathParts = derivationPath.split('/');
  var current = this;

  for (var i = (pathParts[0] == 'm') ? 1 : 0; i < pathParts.length; i++) {
    final part = pathParts[i];
    if (part.isEmpty) continue;

    final isHardened = part.endsWith("'");
    final indexStr = isHardened ? part.substring(0, part.length - 1) : part;
    final index = int.parse(indexStr);

    if (index < 0 || index >= (1 << 31)) {
      throw ArgumentError('Invalid child index: $index');
    }

    final childIndex = isHardened ? index + (1 << 31) : index;
    current = current.deriveChild(childIndex);
  }

  return current;
}