encryptHdWallet method

EncryptedWallet encryptHdWallet(
  1. HdWallet wallet,
  2. String password
)

Encrypts an hd wallet

EncryptedWallet encryptedHdWallet = new WalletFactory().encryptWallet(wallet, password);

Implementation

EncryptedWallet encryptHdWallet(HdWallet wallet, String password) {
  String walletJson = json.encode(wallet);

  var key = _toSha256I(password);
  var iv = _toSha256I(walletJson).sublist(0, 16);
  CipherParameters params = new PaddedBlockCipherParameters(
      new ParametersWithIV(new KeyParameter(key as Uint8List), iv as Uint8List), null);

  BlockCipher encryptionCipher = new PaddedBlockCipher("AES/CBC/PKCS7");
  encryptionCipher.init(true, params);
  Uint8List encrypted = encryptionCipher.process(utf8.encode(walletJson) as Uint8List);
  String cipherText = hex.encode(encrypted);

  return EncryptedWallet(Source("flutter"), CipherText(cipherText),
      wallet.address, Salt(hex.encode(iv)));
}