bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.7k stars 2.11k forks source link

Signature results are different #1681

Closed caster1213 closed 3 years ago

caster1213 commented 3 years ago

Java and JS execution results are different

let hash = bitcoin.crypto.sha256(new Buffer('123','utf-8'))
let keyPair = new bitcoin.ECPair(bigInteger.fromBuffer(Buffer.from(priKey, 'hex')))
let sign = keyPair.sign(hash).toDER()
console.log(sign.toString('hex'))
byte[] sha256 = TransactionUtil.Sha256("123".getBytes(StandardCharsets.UTF_8));
Sha256Hash sha256Hash = Sha256Hash.wrap(sha256);
ECKey ecKey = ECKey.fromPrivate(priKey);
ECKey.ECDSASignature ecdsas = ecKey.sign(sha256Hash);
byte[] bytes = ecdsas.encodeToDER();

bitcoinj-core:0.14.7 bitcoinjs-lib:3.1.1

Java code cannot be modified how to make the result of the front-end signature the same as in Java?

junderw commented 3 years ago

bitcoinjs-lib v3.1.1 uses RFC6979 nonce generation. bitcoinj-core:0.14.7 does not.

This is not something that can be changed.

With a given private key and a given message, there are almost infinite number of valid signatures.

If you use the same key, message, AND nonce. Then the same signature will always be output.