Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var QP = master.keyPair.Q
var serQP = master.keyPair.getPublicKeyBuffer()
var d1 = child.keyPair.d
var d2
var data = new Buffer(37)
serQP.copy(data, 0)
// search index space until we find it
for (var i = 0; i < ark.HDNode.HIGHEST_BIT; ++i) {
data.writeUInt32BE(i, 33)
// calculate I
var I = crypto.createHmac('sha512', master.chainCode).update(data).digest()
var IL = I.slice(0, 32)
var pIL = bigi.fromBuffer(IL)
// See hdnode.js:273 to understand
d2 = d1.subtract(pIL).mod(curve.n)
var Qp = new ark.ECPair(d2).Q
if (Qp.equals(QP)) break
}
var node = new ark.HDNode(new ark.ECPair(d2), master.chainCode, master.network)
node.depth = master.depth
node.index = master.index
node.masterFingerprint = master.masterFingerprint
return node
}
Signature.signHash = function(dataSha256, privateKey, encoding = 'hex') {
if(typeof dataSha256 === 'string') {
dataSha256 = Buffer.from(dataSha256, encoding)
}
if( dataSha256.length !== 32 || ! Buffer.isBuffer(dataSha256) )
throw new Error("dataSha256: 32 byte buffer requred")
privateKey = PrivateKey(privateKey)
assert(privateKey, 'privateKey required')
var der, e, ecsignature, i, lenR, lenS, nonce;
i = null;
nonce = 0;
e = BigInteger.fromBuffer(dataSha256);
while (true) {
ecsignature = ecdsa.sign(curve, dataSha256, privateKey.d, nonce++);
der = ecsignature.toDER();
lenR = der[3];
lenS = der[5 + lenR];
if (lenR === 32 && lenS === 32) {
i = ecdsa.calcPubKeyRecoveryParam(curve, e, ecsignature, privateKey.toPublic().Q);
i += 4; // compressed
i += 27; // compact // 24 or 27 :( forcing odd-y 2nd key candidate)
break;
}
if (nonce % 10 === 0) {
console.log("WARN: " + nonce + " attempts to find canonical signature");
}
}
return Signature(ecsignature.r, ecsignature.s, i);
PrivateKey.fromBuffer = function(buf) {
if (!Buffer.isBuffer(buf)) {
throw new Error("Expecting parameter to be a Buffer type");
}
if(buf.length === 33 && buf[32] === 1) {
// remove compression flag
buf = buf.slice(0, -1)
}
if (32 !== buf.length) {
throw new Error(`Expecting 32 bytes, instead got ${buf.length}`);
}
return PrivateKey(BigInteger.fromBuffer(buf));
}
function verify(curve, hash, signature, Q) {
// 1.4.2 H = Hash(M), already done by the user
// 1.4.3 e = H
var e = BigInteger.fromBuffer(hash)
return verifyRaw(curve, e, signature, Q)
}
function verify(curve, hash, signature, Q) {
// 1.4.2 H = Hash(M), already done by the user
// 1.4.3 e = H
var e = BigInteger.fromBuffer(hash)
return verifyRaw(curve, e, signature, Q)
}
function verify(curve, hash, signature, Q) {
// 1.4.2 H = Hash(M), already done by the user
// 1.4.3 e = H
var e = BigInteger.fromBuffer(hash)
return verifyRaw(curve, e, signature, Q)
}
function verify(curve, hash, signature, Q) {
// 1.4.2 H = Hash(M), already done by the user
// 1.4.3 e = H
var e = BigInteger.fromBuffer(hash)
return verifyRaw(curve, e, signature, Q)
}
function recoverHash(dataSha256, encoding = 'hex') {
if(typeof dataSha256 === 'string') {
dataSha256 = Buffer.from(dataSha256, encoding)
}
if(dataSha256.length !== 32 || !Buffer.isBuffer(dataSha256)) {
throw new Error("dataSha256: 32 byte String or buffer requred")
}
const e = BigInteger.fromBuffer(dataSha256);
let i2 = i
i2 -= 27;
i2 = i2 & 3;
const Q = ecdsa.recoverPubKey(curve, e, {r, s, i}, i2);
return PublicKey.fromPoint(Q);
};
static fromBuffer(buf) {
var i, r, s;
assert.equal(buf.length, 65, 'Invalid signature length');
i = buf.readUInt8(0);
assert.equal(i - 27, i - 27 & 7, 'Invalid signature parameter');
r = BigInteger.fromBuffer(buf.slice(1, 33));
s = BigInteger.fromBuffer(buf.slice(33));
return new Signature(r, s, i);
};
static fromBuffer(buf) {
var i, r, s;
assert.equal(buf.length, 65, 'Invalid signature length');
i = buf.readUInt8(0);
assert.equal(i - 27, i - 27 & 7, 'Invalid signature parameter');
r = BigInteger.fromBuffer(buf.slice(1, 33));
s = BigInteger.fromBuffer(buf.slice(33));
return new Signature(r, s, i);
};