Comparing version 3.1.0 to 3.2.0
@@ -17,2 +17,3 @@ # API Reference (v3.x) | ||
- [`.signatureImport(Buffer signature)`](#signatureimportbuffer-signature---buffer) | ||
- [`.signatureImportLax(Buffer signature)`](#signatureimportlaxbuffer-signature---buffer) | ||
- [`.sign(Buffer message, Buffer privateKey [, Object options])`](#signbuffer-message-buffer-privatekey--object-options---signature-buffer-recovery-number) | ||
@@ -106,6 +107,12 @@ - [Option: `Function noncefn`](#option-function-noncefn) | ||
Parse a DER ECDSA *signature*. | ||
Parse a DER ECDSA *signature* (follow by [BIP66](https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki)). | ||
<hr> | ||
#####`.signatureImportLax(Buffer signature)` -> `Buffer` | ||
Same as [signatureImport](#signatureimportbuffer-signature---buffer) but not follow by [BIP66](https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki). | ||
<hr> | ||
#####`.sign(Buffer message, Buffer privateKey [, Object options])` -> `{signature: Buffer, recovery: number}` | ||
@@ -112,0 +119,0 @@ |
@@ -5,3 +5,4 @@ 'use strict' | ||
} catch (err) { | ||
console.log('Secp256k1 bindings are not compiled. Pure JS implementation will be used.') | ||
module.exports = require('./elliptic') | ||
} |
117
lib/index.js
'use strict' | ||
var bip66 = require('bip66') | ||
var assert = require('./assert') | ||
var der = require('./der') | ||
var messages = require('./messages.json') | ||
var EC_PRIVKEY_EXPORT_DER_COMPRESSED_BEGIN = new Buffer( | ||
'3081d30201010420', 'hex') | ||
var EC_PRIVKEY_EXPORT_DER_COMPRESSED_MIDDLE = new Buffer( | ||
'a08185308182020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a124032200', 'hex') | ||
var EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED_BEGIN = new Buffer( | ||
'308201130201010420', 'hex') | ||
var EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED_MIDDLE = new Buffer( | ||
'a081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a144034200', 'hex') | ||
var ZERO_BUFFER_32 = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex') | ||
function initCompressedValue (value, defaultValue) { | ||
@@ -37,32 +25,5 @@ if (value === undefined) return defaultValue | ||
compressed = initCompressedValue(compressed, true) | ||
var publicKey = secp256k1.privateKeyExport(privateKey, compressed) | ||
var result = new Buffer(compressed ? 214 : 279) | ||
var targetStart = 0 | ||
if (compressed) { | ||
EC_PRIVKEY_EXPORT_DER_COMPRESSED_BEGIN.copy(result, targetStart) | ||
targetStart += EC_PRIVKEY_EXPORT_DER_COMPRESSED_BEGIN.length | ||
privateKey.copy(result, targetStart) | ||
targetStart += privateKey.length | ||
EC_PRIVKEY_EXPORT_DER_COMPRESSED_MIDDLE.copy(result, targetStart) | ||
targetStart += EC_PRIVKEY_EXPORT_DER_COMPRESSED_MIDDLE.length | ||
publicKey.copy(result, targetStart) | ||
} else { | ||
EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED_BEGIN.copy(result, targetStart) | ||
targetStart += EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED_BEGIN.length | ||
privateKey.copy(result, targetStart) | ||
targetStart += privateKey.length | ||
EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED_MIDDLE.copy(result, targetStart) | ||
targetStart += EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED_MIDDLE.length | ||
publicKey.copy(result, targetStart) | ||
} | ||
return result | ||
return der.privateKeyExport(privateKey, publicKey, compressed) | ||
}, | ||
@@ -73,44 +34,5 @@ | ||
do { | ||
var length = privateKey.length | ||
privateKey = der.privateKeyImport(privateKey) | ||
if (privateKey && privateKey.length === 32 && secp256k1.privateKeyVerify(privateKey)) return privateKey | ||
// sequence header | ||
var index = 0 | ||
if (length < index + 1 || privateKey[index] !== 0x30) break | ||
index += 1 | ||
// sequence length constructor | ||
if (length < index + 1 || !(privateKey[index] & 0x80)) break | ||
var lenb = privateKey[index] & 0x7f | ||
index += 1 | ||
if (lenb < 1 || lenb > 2) break | ||
if (length < index + lenb) break | ||
// sequence length | ||
var len = privateKey[index + lenb - 1] | (lenb > 1 ? privateKey[index + lenb - 2] << 8 : 0) | ||
index += lenb | ||
if (length < index + len) break | ||
// sequence element 0: version number (=1) | ||
if (length < index + 3 || | ||
privateKey[index] !== 0x02 || | ||
privateKey[index + 1] !== 0x01 || | ||
privateKey[index + 2] !== 0x01) { | ||
break | ||
} | ||
index += 3 | ||
// sequence element 1: octet string, up to 32 bytes | ||
if (length < index + 2 || | ||
privateKey[index] !== 0x04 || | ||
privateKey[index + 1] > 0x20 || | ||
length < index + 2 + privateKey[index + 1]) { | ||
break | ||
} | ||
privateKey = privateKey.slice(index + 2, index + 2 + privateKey[index + 1]) | ||
if (privateKey.length === 32 && secp256k1.privateKeyVerify(privateKey)) return privateKey | ||
} while (false) | ||
throw new Error(messages.EC_PRIVATE_KEY_IMPORT_DER_FAIL) | ||
@@ -211,30 +133,23 @@ }, | ||
var sigObj = secp256k1.signatureExport(signature) | ||
return der.signatureExport(sigObj) | ||
}, | ||
var r = Buffer.concat([new Buffer([0]), sigObj.r]) | ||
for (var lenR = 33, posR = 0; lenR > 1 && r[posR] === 0x00 && !(r[posR + 1] & 0x80); --lenR, ++posR); | ||
signatureImport: function (sig) { | ||
assert.isBuffer(sig, messages.ECDSA_SIGNATURE_TYPE_INVALID) | ||
assert.isLengthGTZero(sig, messages.ECDSA_SIGNATURE_LENGTH_INVALID) | ||
var s = Buffer.concat([new Buffer([0]), sigObj.s]) | ||
for (var lenS = 33, posS = 0; lenS > 1 && s[posS] === 0x00 && !(s[posS + 1] & 0x80); --lenS, ++posS); | ||
var sigObj = der.signatureImport(sig) | ||
if (sigObj) return secp256k1.signatureImport(sigObj) | ||
return bip66.encode(r.slice(posR), s.slice(posS)) | ||
throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL) | ||
}, | ||
signatureImport: function (sig) { | ||
signatureImportLax: function (sig) { | ||
assert.isBuffer(sig, messages.ECDSA_SIGNATURE_TYPE_INVALID) | ||
assert.isLengthGTZero(sig, messages.ECDSA_SIGNATURE_LENGTH_INVALID) | ||
try { | ||
var sigObj = bip66.decode(sig) | ||
if (sigObj.r.length === 33 && sigObj.r[0] === 0x00) sigObj.r = sigObj.r.slice(1) | ||
if (sigObj.r.length > 32) throw new Error('R length is too long') | ||
if (sigObj.s.length === 33 && sigObj.s[0] === 0x00) sigObj.s = sigObj.s.slice(1) | ||
if (sigObj.s.length > 32) throw new Error('S length is too long') | ||
} catch (err) { | ||
throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL) | ||
} | ||
var sigObj = der.signatureImportLax(sig) | ||
if (sigObj) return secp256k1.signatureImport(sigObj) | ||
return secp256k1.signatureImport({ | ||
r: Buffer.concat([ZERO_BUFFER_32, sigObj.r]).slice(-32), | ||
s: Buffer.concat([ZERO_BUFFER_32, sigObj.s]).slice(-32) | ||
}) | ||
throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL) | ||
}, | ||
@@ -241,0 +156,0 @@ |
{ | ||
"name": "secp256k1", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "This module provides native bindings to ecdsa secp256k1 functions", | ||
@@ -43,7 +43,6 @@ "keywords": [ | ||
"coverage-lcov": "npm run coverage && nyc report -r lcov", | ||
"install": "npm run rebuild", | ||
"install": "npm run rebuild || echo \"Secp256k1 bindings compilation fail. Pure JS implementation will be used.\"", | ||
"lint": "standard", | ||
"rebuild": "node-gyp rebuild", | ||
"test": "npm run unit", | ||
"unit": "npm run unit:node && npm run unit:browser", | ||
"test": "npm run lint && npm run unit:node", | ||
"unit:browser": "karma start karma.conf.js", | ||
@@ -50,0 +49,0 @@ "unit:node": "tape test/index.js |faucet" |
libsecp256k1 | ||
============ | ||
[![Build Status](https://travis-ci.org/bitcoin/secp256k1.svg?branch=master)](https://travis-ci.org/bitcoin/secp256k1) | ||
[![Build Status](https://travis-ci.org/bitcoin-core/secp256k1.svg?branch=master)](https://travis-ci.org/bitcoin-core/secp256k1) | ||
@@ -6,0 +6,0 @@ Optimized C library for EC operations on curve secp256k1. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
924333
124
2260
128