Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...