encrypt static method

String encrypt(
  1. List<int> privKey,
  2. String passphrase,
  3. PubKeyModes pubKeyMode
)

Encrypt a Bitcoin private key without using ECDSA.

  • privKey: The Bitcoin private key to be encrypted.
  • passphrase: The passphrase for encryption.
  • pubKeyMode: The selected public key mode (compressed or uncompressed).

Implementation

static String encrypt(
  List<int> privKey,
  String passphrase,
  PubKeyModes pubKeyMode,
) {
  /// Compute the address hash from the private key and public key mode.
  final addressHash = Bip38NoEcUtils.addressHash(privKey, pubKeyMode);

  /// Derive key halves using the passphrase and address hash.
  final derivedHalves = Bip38NoEcUtils.deriveKeyHalves(
    passphrase,
    addressHash,
  );

  /// Extract the derived key halves.
  final derivedHalf1 = derivedHalves.$1;
  final derivedHalf2 = derivedHalves.$2;

  /// Encrypt the private key using the derived halves.
  final encryptedHalves = _encryptPrivateKey(
    privKey,
    derivedHalf1,
    derivedHalf2,
  );

  /// Extract the encrypted halves.
  final encryptedHalf1 = encryptedHalves.$1;
  final encryptedHalf2 = encryptedHalves.$2;

  /// Determine the flagbyte based on the public key mode.
  final flagbyte =
      pubKeyMode == PubKeyModes.compressed
          ? Bip38NoEcConst.flagbyteCompressed
          : Bip38NoEcConst.flagbyteUncompressed;

  /// Create the BIP38-encrypted private key as bytes.
  final encKeyBytes =
      Bip38NoEcConst.encKeyPrefix +
      flagbyte +
      addressHash +
      encryptedHalf1 +
      encryptedHalf2;

  /// Encode the encrypted private key as a Base58 string.
  return Base58Encoder.checkEncode(encKeyBytes);
}