Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

bitcoin - How would I get a DER encoded non-deterministic canonical ecdsa signature with node.js?

I have tried the following:

var hash=sha256(sha256(msg)); // hash of the transaction
var priv_key= "...."; // the private key, 64 char length, ([a-z0-9])


//non-deterministic but not canonical
//********************************************
var crypto = require('crypto');
var KeyEncoder = require('key-encoder').default,
keyEncoder = new KeyEncoder("secp256k1");

var pemPrivateKey = keyEncoder.encodePrivate(priv_key, 'raw', 'pem')
const csign = crypto.createSign('SHA256');
csign.update(hash);    
console.log(csign.sign(pemPrivateKey, 'hex'));

//canonical but deterministic
//**********************************
var ec = new EC("secp256k1");
var mySign = ec.sign(Buffer.from(hash,"hex"), Buffer.from(priv_key,"hex"),{canonical:true});
var sig=mySign.toDER();

var DER_sig="";
for(var si=0;si<sig.length;si++){
    var si_int=sig[si]
    DER_sig+=norm_l(si_int.toString(16));
}   
console.log(DER_sig)

function norm_l(z){
if(z.length<2){
    z="0"+z;
}
return z;
}

The problem with this is, that I was never able to make the first version canonical or the second version non deterministic.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...