encrypt static method

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

Encrypt a Bitcoin private key without using ECDSA.

This method encrypts a Bitcoin private key without using the Elliptic Curve Digital Signature Algorithm (ECDSA). It takes a private key, passphrase, and a public key mode (compressed or uncompressed) as inputs and returns the BIP38-encrypted private key as a string.

  • privKey: The Bitcoin private key to be encrypted.
  • passphrase: The passphrase for encryption.
  • pubKeyMode: The selected public key mode (compressed or uncompressed).
  • Returns: The BIP38-encrypted private key as a string.

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.item1;
  final derivedHalf2 = derivedHalves.item2;

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

  /// Extract the encrypted halves.
  final encryptedHalf1 = encryptedHalves.item1;
  final encryptedHalf2 = encryptedHalves.item2;

  /// 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(List<int>.from(encKeyBytes));
}