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

replace secp256k1": "^4.0.2 with tiny-secp256k1": "^2.2.0 #54

Closed
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
95 changes: 14 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bolt11",
"version": "1.3.4",
"version": "1.3.5",
"description": "A library for encoding and decoding lightning network payment requests as defined in [BOLT #11](https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md).",
"main": "payreq.js",
"types": "payreq.d.ts",
Expand Down Expand Up @@ -48,6 +48,6 @@
"create-hash": "^1.2.0",
"lodash": "^4.17.11",
"safe-buffer": "^5.1.1",
"secp256k1": "^4.0.2"
"tiny-secp256k1": "^2.2.0"
}
}
18 changes: 9 additions & 9 deletions payreq.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const createHash = require('create-hash')
const bech32 = require('bech32')
const secp256k1 = require('secp256k1')
const secp256k1 = require('tiny-secp256k1')
const Buffer = require('safe-buffer').Buffer
const BN = require('bn.js')
const bitcoinjsAddress = require('bitcoinjs-lib').address
Expand Down Expand Up @@ -490,7 +490,7 @@ function sign (inputPayReqObj, inputPrivateKey) {
if (payReqObj.complete && payReqObj.paymentRequest) return payReqObj

if (privateKey === undefined || privateKey.length !== 32 ||
!secp256k1.privateKeyVerify(privateKey)) {
!secp256k1.isPrivate(privateKey)) {
throw new Error('privateKey must be a 32 byte Buffer and valid private key')
}

Expand All @@ -511,7 +511,7 @@ function sign (inputPayReqObj, inputPrivateKey) {
// make sure if either exist they are in nodePublicKey
nodePublicKey = tagNodePublicKey || nodePublicKey

const publicKey = Buffer.from(secp256k1.publicKeyCreate(privateKey))
const publicKey = Buffer.from(secp256k1.pointFromScalar(privateKey))

// Check if pubkey matches for private key
if (nodePublicKey && !publicKey.equals(nodePublicKey)) {
Expand All @@ -530,14 +530,14 @@ function sign (inputPayReqObj, inputPrivateKey) {
// signature is 64 bytes (32 byte r value and 32 byte s value concatenated)
// PLUS one extra byte appended to the right with the recoveryID in [0,1,2,3]
// Then convert to 5 bit words with right padding 0 bits.
const sigObj = secp256k1.ecdsaSign(payReqHash, privateKey)
const sigObj = secp256k1.signRecoverable(payReqHash, privateKey)
sigObj.signature = Buffer.from(sigObj.signature)
const sigWords = hexToWord(sigObj.signature.toString('hex') + '0' + sigObj.recid)
const sigWords = hexToWord(sigObj.signature.toString('hex') + '0' + sigObj.recoveryId)

// append signature words to the words, mark as complete, and add the payreq
payReqObj.payeeNodeKey = publicKey.toString('hex')
payReqObj.signature = sigObj.signature.toString('hex')
payReqObj.recoveryFlag = sigObj.recid
payReqObj.recoveryFlag = sigObj.recoveryId
payReqObj.wordsTemp = bech32.encode('temp', words.concat(sigWords), Number.MAX_SAFE_INTEGER)
payReqObj.complete = true
payReqObj.paymentRequest = bech32.encode(payReqObj.prefix, words.concat(sigWords), Number.MAX_SAFE_INTEGER)
Expand Down Expand Up @@ -711,7 +711,7 @@ function encode (inputData, addDefaults) {
route.cltv_expiry_delta === undefined) {
throw new Error('Routing info is incomplete')
}
if (!secp256k1.publicKeyVerify(hexToBuffer(route.pubkey))) {
if (!secp256k1.isPoint(hexToBuffer(route.pubkey))) {
throw new Error('Routing info pubkey is not a valid pubkey')
}
const shortId = hexToBuffer(route.short_channel_id)
Expand Down Expand Up @@ -814,7 +814,7 @@ function encode (inputData, addDefaults) {
Earlier we check if the private key matches the payee node key IF they
gave one. */
if (nodePublicKey) {
const recoveredPubkey = Buffer.from(secp256k1.ecdsaRecover(Buffer.from(data.signature, 'hex'), data.recoveryFlag, payReqHash, true))
const recoveredPubkey = Buffer.from(secp256k1.recover(payReqHash, Buffer.from(data.signature, 'hex'), data.recoveryFlag, true))
if (nodePublicKey && !nodePublicKey.equals(recoveredPubkey)) {
throw new Error('Signature, message, and recoveryID did not produce the same pubkey as payeeNodeKey')
}
Expand Down Expand Up @@ -962,7 +962,7 @@ function decode (paymentRequest, network) {

const toSign = Buffer.concat([Buffer.from(prefix, 'utf8'), Buffer.from(convert(wordsNoSig, 5, 8))])
const payReqHash = sha256(toSign)
const sigPubkey = Buffer.from(secp256k1.ecdsaRecover(sigBuffer, recoveryFlag, payReqHash, true))
const sigPubkey = Buffer.from(secp256k1.recover(payReqHash, sigBuffer, recoveryFlag, true))
if (tagsContainItem(tags, TAGNAMES['19']) && tagsItems(tags, TAGNAMES['19']) !== sigPubkey.toString('hex')) {
throw new Error('Lightning Payment Request signature pubkey does not match payee pubkey')
}
Expand Down