Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature replace secp256k1 with elliptic #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
var bs58check = require('bs58check')
var bufferEquals = require('buffer-equals')
var createHash = require('create-hash')
var secp256k1 = require('secp256k1')
var EC = require('elliptic').ec
var BN = require('bn.js')
var varuint = require('varuint-bitcoin')
var ec = null

function getEC () {
if (ec === null) {
ec = new EC('secp256k1')
}
return ec
}

function sha256 (b) {
return createHash('sha256').update(b).digest()
Expand Down Expand Up @@ -46,16 +55,36 @@ function magicHash (message, messagePrefix) {

function sign (message, privateKey, compressed, messagePrefix) {
var hash = magicHash(message, messagePrefix)
var sigObj = secp256k1.sign(hash, privateKey)
return encodeSignature(sigObj.signature, sigObj.recovery, compressed)
var eccrypto = getEC()
var sigObj = eccrypto.sign(hash, privateKey, {canonical: true})
var signature = Buffer.concat([sigObj.r.toArrayLike(Buffer, 'be', 32), sigObj.s.toArrayLike(Buffer, 'be', 32)])
return encodeSignature(signature, sigObj.recoveryParam, compressed)
}

function recover (message, signature, recovery, compressed) {
var sigObj = { r: signature.slice(0, 32), s: signature.slice(32, 64) }

var eccrypto = getEC()
var sigr = new BN(sigObj.r)
var sigs = new BN(sigObj.s)
if (sigr.cmp(eccrypto.curve.n) >= 0 || sigs.cmp(eccrypto.curve.n) >= 0) throw new Error("couldn't parse signature")

try {
if (sigr.isZero() || sigs.isZero()) throw new Error()

var point = eccrypto.recoverPubKey(message, sigObj, recovery)
return Buffer.from(point.encode(true, compressed))
} catch (err) {
throw new Error("couldn't recover public key from signature")
}
}

function verify (message, address, signature, messagePrefix) {
if (!Buffer.isBuffer(signature)) signature = Buffer.from(signature, 'base64')

var parsed = decodeSignature(signature)
var hash = magicHash(message, messagePrefix)
var publicKey = secp256k1.recover(hash, parsed.signature, parsed.recovery, parsed.compressed)
var publicKey = recover(hash, parsed.signature, parsed.recovery, parsed.compressed)

var actual = hash160(publicKey)
var expected = bs58check.decode(address).slice(1)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"bs58check": "^2.0.2",
"buffer-equals": "^1.0.3",
"create-hash": "^1.1.2",
"secp256k1": "^3.0.1",
"elliptic": "^6.4.0",
"varuint-bitcoin": "^1.0.1"
},
"devDependencies": {
Expand Down
Loading