dart-bitcoin / bip32-dart

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

Error when using ecc.sign(hash,x) #5

Closed peipeitu closed 3 years ago

peipeitu commented 3 years ago

I‘ve got an error when using function sign(hash, x) which is import 'package:bip32/src/utils/ecurve.dart' as ecc;

var key = '8c39fb3d889b6be22850254dc7ce3247c559d9f968785d88251ee4457633e335';
var msg = '6ad99bc30926fd79cf5a977044088fd298a59728c5b0c1040713d3a8e1c1e69b';

var sig = sign(Uint8List.fromList(HEX.decode(msg)), Uint8List.fromList(HEX.decode(key)));

The expected sig is 00349f1a2df670db4ec676674489f12684afb0ca8225e02a1299c01b8c9c99804837d43fce2267e2200526f0ad304360a5371f859698c2a1c042004c85f1c985

But it throw an error:

Unhandled exception:
Bad state: Too few elements
#0      _TypedIntListMixin.setRange (dart:typed_data-patch/typed_data_patch.dart:408:7)
#1      sign (package:bip32/src/utils/ecurve.dart:134:10)
#2      sign (file:///D:/intchains/TEST/wallet/sign.dart:104:10)
#3      main (file:///D:/intchains/TEST/wallet/sign.dart:94:13)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

I just found it’s because the length of encodeBigInt(sig.r) is 31, but buffer.setRange(0, 32, list) need 32. If the result encodeBigInt(sig.r) can be length 32 with 0 fixed in the front of the list, it will be right.

I hope this problem can be fixed.

pedr0-fr commented 3 years ago

I was about to post this, I noticed this happens about 1% of the times when signing an input of a bitcoin transaction.

p3root commented 3 years ago

happens here to. but only several times - any ideas?

gitcoinbot commented 3 years ago

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


This issue now has a funding of 0.0036 RBTC (201.57 USD @ $55993.0/RBTC) attached to it.

gitcoinbot commented 3 years ago

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


Work for 0.0036 RBTC has been submitted by:


p3root commented 3 years ago

PR created for the issue: https://github.com/dart-bitcoin/bip32-dart/pull/10

brunocalmels commented 3 years ago

I don't think it's it solved for every case. Please check https://github.com/ivaneidel/bip32-dart/blob/16025317311d0a8a3c41829af66d35d155d42166/test/ecc_test.dart for a test that was added, which is still facing the same issue.

p3root commented 3 years ago

I will check that and come back to you!

p3root commented 3 years ago

12