parseExtendedKey function
Implementation
ParsedExtendedKey parseExtendedKey(String value) {
try {
final payload = Base58Decoder.checkDecode(value);
if (payload.length != 78) {
throw const WalletHDDerivationException(
'extended key payload must be 78 bytes');
}
final versionHex = BytesUtils.toHexString(payload.sublist(0, 4));
final isPrivate = payload[45] == 0;
final entry = _formats.entries
.where((entry) =>
versionHex ==
(isPrivate ? entry.value.private : entry.value.public))
.firstOrNull;
if (entry == null) {
throw WalletHDDerivationException(
'unknown extended-key version: $versionHex');
}
final depth = payload[4];
if (depth == 0 &&
(payload.sublist(5, 9).any((byte) => byte != 0) ||
payload.sublist(9, 13).any((byte) => byte != 0))) {
throw const WalletHDDerivationException(
'root extended key must have zero parent fingerprint and child number');
}
final node =
Bip32Slip10Secp256k1.fromExtendedKey(value, _versions(entry.value));
return ParsedExtendedKey._(
value: value,
versionHex: versionHex,
format: entry.key,
isPrivate: isPrivate,
depth: node.depth.toInt(),
childNumber: node.index.toInt(),
parentFingerprintHex:
BytesUtils.toHexString(node.parentFingerPrint.toBytes()),
chainCodeHex: BytesUtils.toHexString(node.chainCode.toBytes()),
publicKeyHex: BytesUtils.toHexString(node.publicKey.compressed),
node: node);
} catch (error) {
if (error is WalletHDDerivationException) rethrow;
throw const WalletHDDerivationException(
'invalid extended key checksum or material');
}
}