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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…