dart-bitcoin / bip32-dart

A BIP32 compatible library for Flutter writing by Dart.
MIT License
19 stars 33 forks source link

Update to pointycastle 2.0 broke things #8

Closed fusion44 closed 3 years ago

fusion44 commented 3 years ago

Hi,

I have the following code:

final mnemonic = bip39.generateMnemonic();
final seed = bip39.mnemonicToSeed(mnemonic);
final root = bip32.BIP32.fromSeed(seed); // error: Private key not in range [1, n] since pointycastle 2.0

Line three throws an error Private key not in range [1, n]

The same code seems to work well when overriding dependencies with an old pointycastle version:

dependency_overrides:
  pointycastle: ^1.0.2

Thank you.

willyfromtheblock commented 3 years ago

in my project downgrading crypto to 2.0.3 did the trick for some reason.

redDwarf03 commented 3 years ago

@fusion44 , have you find a solution please ? with flutter 2.0, we need to update all dependencies.

fusion44 commented 3 years ago

@redDwarf03 Unfortunately not. Currently working on another project but I eventually will get back to this one. So I'm still interested in a solution.

redDwarf03 commented 3 years ago

apparently, this is the method decodeBigInt the source of the issue new code: https://github.com/bcgit/pc-dart/blob/be4142082f01cd3971bec4890342e039799c1f0e/lib/src/utils.dart#L19

/// Decode a BigInt from bytes in big-endian encoding.
/// Twos compliment.
BigInt decodeBigInt(List<int> bytes) {
  var negative = bytes.isNotEmpty && bytes[0] & 0x80 == 0x80;

  BigInt result;

  if (bytes.length == 1) {
    result = BigInt.from(bytes[0]);
  } else {
    result = BigInt.zero;
    for (var i = 0; i < bytes.length; i++) {
      var item = bytes[bytes.length - i - 1];
      result |= (BigInt.from(item) << (8 * i));
    }
  }
  return result != BigInt.zero
      ? negative ? result.toSigned(result.bitLength) : result
      : BigInt.zero;
}

old code:

BigInt decodeBigInt(List<int> bytes) {
  BigInt result = new BigInt.from(0);
  for (int i = 0; i < bytes.length; i++) {
    result += new BigInt.from(bytes[bytes.length - i - 1]) << (8 * i);
  }
  return result;
}

to be confirmed

p3root commented 3 years ago

Should also be fixed with my PR! https://github.com/dart-bitcoin/bip32-dart/pull/10