Skip to content

Commit

Permalink
Merge pull request #69 from bitcoinjs/fix/timestamp
Browse files Browse the repository at this point in the history
Fix timestamp encoding
  • Loading branch information
junderw authored Mar 22, 2023
2 parents 3d9f67a + d2cf3ec commit d42c172
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bolt11",
"version": "1.4.0",
"version": "1.4.1",
"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
3 changes: 3 additions & 0 deletions payreq.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,9 @@ function encode (inputData, addDefaults) {

// timestamp converted to 5 bit number array (left padded with 0 bits, NOT right padded)
const timestampWords = intBEToWords(data.timestamp)
while (timestampWords.length < 7) {
timestampWords.unshift(0)
}

const tags = data.tags
let tagWords = []
Expand Down
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,31 @@ tape('can decode unknown network payment request', (t) => {
t.ok(decoded.complete === true)
t.end()
})

tape('can encode and decode small timestamp', (t) => {
const encoded = lnpayreq.encode({
satoshis: 12,
timestamp: 1,
network: {
bech32: 'tb',
pubKeyHash: 111,
scriptHash: 196,
validWitnessVersions: [0, 1]
},
tags: [
{
tagName: 'payment_hash',
data: '0001020304050607080900010203040506070809000102030405060708090102'
}
]
})

const signedData = lnpayreq.sign(encoded, fixtures.privateKey)

const decoded = lnpayreq.decode(signedData.paymentRequest)
delete decoded.paymentRequest
// This would fail because of corruption before fixing timestamp encoding
const reEncoded = lnpayreq.encode(decoded)
t.same(reEncoded.paymentRequest, signedData.paymentRequest)
t.end()
})

0 comments on commit d42c172

Please sign in to comment.