generateCesiumDewif method

Future<NewWallet> generateCesiumDewif(
  1. Uint8List seed,
  2. String password, {
  3. int dewifCurrencyCode = DEWIF_CURRENCY_CODE_G1,
  4. int dewifVersion = DEWIF_VERSION,
  5. bool test = false,
})

Return the base64 DEWIF string generated from given seed and password.

Implementation

Future<NewWallet> generateCesiumDewif(Uint8List seed, String password,
    {int dewifCurrencyCode = DEWIF_CURRENCY_CODE_G1,
    int dewifVersion = DEWIF_VERSION,
    bool test = false}) async {
  if (test) print('Seed: $seed');

  const int log_n = 14;

  var nonce = randomByte(12);

  // To test
  if (test) nonce = 'c54299ae71fe2a4ecdc7d58a';

  // Header is data version + currency code on 4 bytes
  final header = (dewifVersion.toRadixString(16).padLeft(8, '0') +
      dewifCurrencyCode.toRadixString(16).padLeft(8, '0'));

  // Start data with header + log_n + used algorithm code + nonce
  var data = header +
      log_n.toRadixString(16).padLeft(2, '0') +
      DEWIF_ALGORITHM_CODE_BIP32_ED25519.toRadixString(16).padLeft(2, '0') +
      nonce;

  // Salt is sha256("dewif" || nonce || passphrase)
  final salt =
      sha256("dewif".codeUnits + HEX.decode(nonce) + password.codeUnits);

  final scrypt = KeyDerivator('scrypt');
  scrypt.init(
    ScryptParameters(
      pow(2, log_n).toInt(), //16384
      16,
      1,
      42,
      salt.toUint8List(),
    ),
  );
  final scryptedPassword = scrypt.process(password.codeUnits.toUint8List());

  var seedPadding = randomByte(40 - seed.length);

  // To test
  if (test) seedPadding = 'b34b1ac4c297c238';

  final checksum =
      sha256(HEX.decode(nonce) + [seed.length] + seed).sublist(0, 8);

  final dataToEncrypt = seed.length.toRadixString(16) +
      HEX.encode(seed) +
      seedPadding +
      HEX.encode(checksum);

  final encryptedData = byteXor(
      Uint8List.fromList(HEX.decode(dataToEncrypt)), scryptedPassword);

  data = data + HEX.encode(encryptedData);
  final base64Data = base64.encode(HEX.decode(data));

  //To test
  if (test &&
      base64Data ==
          'AAAAARAAAAEOAcVCma5x/ipOzcfViv8J5cdgASn7zedlNz6M/19wbE79aNvILr1CEyM664m8BjgLKeWiz1GYJSR14+ZY61U=') {
    print('Data is conform to test !');
  }

  // Show me everything
  // print('\nVersion: ' + dewifVersion.toRadixString(16));
  // print('log N: ' + log_n.toRadixString(16));
  // print('Algorithm: ' + DEWIF_ALGORITHM_CODE_BIP32_ED25519.toRadixString(16));
  // print('Nonce: ' + nonce);
  // print('Seed length: ' + seed.length.toRadixString(16));
  // print('Seed: ' + HEX.encode(seed));
  // print('Seed padding: ' + seedPadding);
  // print('Checksum: ' + HEX.encode(checksum));
  // print(data);

  return NewWallet._(base64Data, password);
}