deserializeKey static method

Bip32DeserializedKey deserializeKey(
  1. String serKeyStr, {
  2. Bip32KeyNetVersions? keyNetVer,
})

Deserialize a key.

Implementation

static Bip32DeserializedKey deserializeKey(String serKeyStr,
    {Bip32KeyNetVersions? keyNetVer}) {
  final serKeyBytes = Base58Decoder.checkDecode(serKeyStr);

  // Get if key is public/private depending on the net version
  final isPublic = _getIfPublic(
      serKeyBytes, keyNetVer ?? Bip32Const.mainNetKeyNetVersions);

  // Validate length
  if (isPublic &&
      serKeyBytes.length != Bip32KeySerConst.serializedPubKeyByteLen) {
    throw Bip32KeyError(
        'Invalid extended public key (wrong length: ${serKeyBytes.length})');
  }
  if (!isPublic &&
      !Bip32KeySerConst.serializedPrivKeyByteLen
          .contains(serKeyBytes.length)) {
    throw Bip32KeyError(
        'Invalid extended private key (wrong length: ${serKeyBytes.length})');
  }

  // Get parts back
  final keyParts = _getPartsFromBytes(serKeyBytes, isPublic);

  return Bip32DeserializedKey(keyParts.item1, keyParts.item2, isPublic);
}