Comparing version 0.0.2 to 0.1.0
@@ -34,9 +34,9 @@ /*! | ||
ec.generatePrivateKey = function generatePrivateKey() { | ||
let priv; | ||
let key; | ||
do { | ||
priv = random.randomBytes(32); | ||
} while (!secp256k1.privateKeyVerify(priv)); | ||
key = random.randomBytes(32); | ||
} while (!secp256k1.privateKeyVerify(key)); | ||
return priv; | ||
return key; | ||
}; | ||
@@ -46,3 +46,3 @@ | ||
* Create a public key from a private key. | ||
* @param {Buffer} priv | ||
* @param {Buffer} key | ||
* @param {Boolean?} compress | ||
@@ -52,5 +52,4 @@ * @returns {Buffer} | ||
ec.publicKeyCreate = function publicKeyCreate(priv, compress) { | ||
assert(Buffer.isBuffer(priv)); | ||
return secp256k1.publicKeyCreate(priv, compress); | ||
ec.publicKeyCreate = function publicKeyCreate(key, compress) { | ||
return secp256k1.publicKeyCreate(key, compress); | ||
}; | ||
@@ -70,9 +69,10 @@ | ||
* ((tweak + key) % n) | ||
* @param {Buffer} privateKey | ||
* @param {Buffer} key | ||
* @param {Buffer} tweak | ||
* @returns {Buffer} privateKey | ||
* @returns {Buffer} key | ||
*/ | ||
ec.privateKeyTweakAdd = function privateKeyTweakAdd(privateKey, tweak) { | ||
return secp256k1.privateKeyTweakAdd(privateKey, tweak); | ||
ec.privateKeyTweakAdd = function privateKeyTweakAdd(key, tweak) { | ||
assert(Buffer.isBuffer(key)); | ||
return secp256k1.privateKeyTweakAdd(key, tweak); | ||
}; | ||
@@ -82,9 +82,9 @@ | ||
* ((g * tweak) + key) | ||
* @param {Buffer} publicKey | ||
* @param {Buffer} key | ||
* @param {Buffer} tweak | ||
* @returns {Buffer} publicKey | ||
* @returns {Buffer} key | ||
*/ | ||
ec.publicKeyTweakAdd = function publicKeyTweakAdd(publicKey, tweak, compress) { | ||
return secp256k1.publicKeyTweakAdd(publicKey, tweak, compress); | ||
ec.publicKeyTweakAdd = function publicKeyTweakAdd(key, tweak, compress) { | ||
return secp256k1.publicKeyTweakAdd(key, tweak, compress); | ||
}; | ||
@@ -105,28 +105,55 @@ | ||
/** | ||
* Recover a public key. | ||
* Validate a public key. | ||
* @param {Buffer} key | ||
* @returns {Boolean} True if buffer is a valid public key. | ||
*/ | ||
ec.publicKeyVerify = function publicKeyVerify(key) { | ||
return secp256k1.publicKeyVerify(key); | ||
}; | ||
/** | ||
* Validate a private key. | ||
* @param {Buffer} key | ||
* @returns {Boolean} True if buffer is a valid private key. | ||
*/ | ||
ec.privateKeyVerify = function privateKeyVerify(key) { | ||
return secp256k1.privateKeyVerify(key); | ||
}; | ||
/** | ||
* Sign a message. | ||
* @param {Buffer} msg | ||
* @param {Buffer} sig | ||
* @param {Number?} j | ||
* @param {Boolean?} compress | ||
* @returns {Buffer[]|Buffer|null} | ||
* @param {Buffer} key - Private key. | ||
* @returns {Buffer} R/S-formatted signature. | ||
*/ | ||
ec.recover = function recover(msg, sig, j, compress) { | ||
if (!j) | ||
j = 0; | ||
ec.sign = function sign(msg, key) { | ||
assert(Buffer.isBuffer(msg)); | ||
assert(Buffer.isBuffer(key)); | ||
try { | ||
sig = secp256k1.signatureImport(sig); | ||
} catch (e) { | ||
return null; | ||
} | ||
// Sign message | ||
const {signature} = secp256k1.sign(msg, key); | ||
let key; | ||
try { | ||
key = secp256k1.recover(msg, sig, j, compress); | ||
} catch (e) { | ||
return null; | ||
} | ||
// Ensure low S value | ||
return secp256k1.signatureNormalize(signature); | ||
}; | ||
return key; | ||
/** | ||
* Sign a message. | ||
* @param {Buffer} msg | ||
* @param {Buffer} key - Private key. | ||
* @returns {Buffer} DER-formatted signature. | ||
*/ | ||
ec.signDER = function signDER(msg, key) { | ||
assert(Buffer.isBuffer(msg)); | ||
assert(Buffer.isBuffer(key)); | ||
// Sign message | ||
const sig = ec.sign(msg, key); | ||
// Convert to DER | ||
return secp256k1.signatureExport(sig); | ||
}; | ||
@@ -137,3 +164,3 @@ | ||
* @param {Buffer} msg | ||
* @param {Buffer} sig - DER formatted. | ||
* @param {Buffer} sig - R/S formatted. | ||
* @param {Buffer} key | ||
@@ -155,3 +182,2 @@ * @returns {Boolean} | ||
try { | ||
sig = secp256k1.signatureImportLax(sig); | ||
sig = secp256k1.signatureNormalize(sig); | ||
@@ -165,40 +191,65 @@ return secp256k1.verify(msg, sig, key); | ||
/** | ||
* Validate a public key. | ||
* Verify a signature. | ||
* @param {Buffer} msg | ||
* @param {Buffer} sig - DER formatted. | ||
* @param {Buffer} key | ||
* @returns {Boolean} True if buffer is a valid public key. | ||
* @returns {Boolean} | ||
*/ | ||
ec.publicKeyVerify = function publicKeyVerify(key) { | ||
return secp256k1.publicKeyVerify(key); | ||
ec.verifyDER = function verifyDER(msg, sig, key) { | ||
assert(Buffer.isBuffer(msg)); | ||
assert(Buffer.isBuffer(sig)); | ||
assert(Buffer.isBuffer(key)); | ||
if (sig.length === 0) | ||
return false; | ||
if (key.length === 0) | ||
return false; | ||
try { | ||
sig = secp256k1.signatureImportLax(sig); | ||
} catch (e) { | ||
return false; | ||
} | ||
return ec.verify(msg, sig, key); | ||
}; | ||
/** | ||
* Validate a private key. | ||
* @param {Buffer} key | ||
* @returns {Boolean} True if buffer is a valid private key. | ||
* Recover a public key. | ||
* @param {Buffer} msg | ||
* @param {Buffer} sig | ||
* @param {Number?} param | ||
* @param {Boolean?} compress | ||
* @returns {Buffer[]|Buffer|null} | ||
*/ | ||
ec.privateKeyVerify = function privateKeyVerify(key) { | ||
return secp256k1.privateKeyVerify(key); | ||
ec.recover = function recover(msg, sig, param, compress) { | ||
if (param == null) | ||
param = 0; | ||
try { | ||
return secp256k1.recover(msg, sig, param, compress); | ||
} catch (e) { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* Sign a message. | ||
* Recover a public key. | ||
* @param {Buffer} msg | ||
* @param {Buffer} key - Private key. | ||
* @returns {Buffer} DER-formatted signature. | ||
* @param {Buffer} sig | ||
* @param {Number?} param | ||
* @param {Boolean?} compress | ||
* @returns {Buffer[]|Buffer|null} | ||
*/ | ||
ec.sign = function sign(msg, key) { | ||
assert(Buffer.isBuffer(msg)); | ||
assert(Buffer.isBuffer(key)); | ||
// Sign message | ||
let sig = secp256k1.sign(msg, key); | ||
// Ensure low S value | ||
sig = secp256k1.signatureNormalize(sig.signature); | ||
// Convert to DER | ||
return secp256k1.signatureExport(sig); | ||
ec.recoverDER = function recoverDER(msg, sig, param, compress) { | ||
try { | ||
sig = secp256k1.signatureImport(sig); | ||
} catch (e) { | ||
return null; | ||
} | ||
return ec.recover(msg, sig, param, compress); | ||
}; | ||
@@ -235,17 +286,13 @@ | ||
ec.isLowS = function isLowS(sig) { | ||
let s; | ||
assert(Buffer.isBuffer(sig)); | ||
try { | ||
const rs = secp256k1.signatureImport(sig); | ||
s = rs.slice(32, 64); | ||
} catch (e) { | ||
if (sig.length !== 64) | ||
return false; | ||
} | ||
if (s.equals(ZERO_S)) | ||
sig = sig.slice(32, 64); | ||
if (sig.equals(ZERO_S)) | ||
return false; | ||
// If S is greater than half the order, | ||
// it's too high. | ||
if (s.compare(HALF_ORDER) > 0) | ||
if (sig.compare(HALF_ORDER) > 0) | ||
return false; | ||
@@ -255,1 +302,17 @@ | ||
}; | ||
/** | ||
* Test whether a signature has a low S value. | ||
* @param {Buffer} sig | ||
* @returns {Boolean} | ||
*/ | ||
ec.isLowDER = function isLowDER(sig) { | ||
assert(Buffer.isBuffer(sig)); | ||
try { | ||
sig = secp256k1.signatureImport(sig); | ||
} catch (e) { | ||
return false; | ||
} | ||
return ec.isLowS(sig); | ||
}; |
{ | ||
"name": "bcrypto", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Bcoin crypto module", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
407076
6607