deriveChildNode function

NodeWithAddress deriveChildNode({
  1. required BipNode masterNode,
  2. required int chainIndex,
  3. required int index,
  4. required UTXONetworkType networkType,
  5. required Iterable<AddressType> addressTypes,
  6. required HDWalletPurpose? walletPurpose,
})

Implementation

NodeWithAddress deriveChildNode({
  required BipNode masterNode,
  required int chainIndex,
  required int index,
  required UTXONetworkType networkType,
  required Iterable<AddressType> addressTypes,
  required HDWalletPurpose? walletPurpose,
}) {
  if (index < 0) {
    throw UnsupportedError("index must not be negative");
  }
  if (chainIndex != EXTERNAL_CHAIN_INDEX &&
      chainIndex != INTERNAL_CHAIN_INDEX) {
    throw UnsupportedError("unexpected chainIndex");
  }

  final childDerivationPath = "$chainIndex/$index";

  final node = masterNode.derivePath(childDerivationPath);

  final publicKey = node.publicKey;

  final addressMap = {
    for (final addressType in addressTypes)
      addressType: pubKeyToAddress(publicKey, addressType, networkType),
  };

  final mainAddress = addressMap[addressTypes.first]!;

  return NodeWithAddress.fromChainIndex(
    node: node,
    address: mainAddress,
    chainIndex: chainIndex,
    derivationPath: childDerivationPath,
    addresses: addressMap,
    walletPurpose: walletPurpose,
  );
}