Skip to content

Commit

Permalink
feat: add metadata hash + raw data to request pay service (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi authored Dec 21, 2022
1 parent 811b595 commit 0af8243
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/request-pay-service-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
checkedToSats,
getJson,
isUrl,
sha256,
} from './utils'

const TAG_PAY_REQUEST = 'payRequest'
Expand Down Expand Up @@ -45,10 +46,13 @@ const parseLnUrlPayServiceResponse = (data: {
if (!(min && max) || min > max) return null

let metadata: Array<Array<string>>
let metadataHash: string
try {
metadata = JSON.parse(data.metadata + '')
metadataHash = sha256(data.metadata + '', 'utf8')
} catch {
metadata = []
metadataHash = sha256('[]', 'utf8')
}

let image = ''
Expand Down Expand Up @@ -77,10 +81,12 @@ const parseLnUrlPayServiceResponse = (data: {
min,
max,
domain: (domainMatch && domainMatch[0]) || undefined,
metadata: metadata,
metadata,
metadataHash,
identifier,
description,
image,
commentAllowed: Number(data.commentAllowed) || 0,
rawData: data,
}
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export type LnUrlPayServiceResponse = {
max: Satoshis
domain?: string
metadata: Array<Array<string>>
metadataHash: string
identifier: string
description: string
image: string
commentAllowed: number
rawData: { [key: string]: string | number }
}

export type LnUrlRequestInvoiceBaseArgs = {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export const getJson = async ({
})
}

export const sha256 = (data: string) => {
export const sha256 = (data: string, encoding: BufferEncoding = 'hex') => {
const sha256 = new Sha256()
sha256.update(Buffer.from(data, 'hex'))
sha256.update(Buffer.from(data, encoding))
return Buffer.from(sha256.digestSync()).toString('hex')
}

Expand Down
6 changes: 5 additions & 1 deletion test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const validPayServiceParams = [
['text/plain', 'Payment to user0'],
['text/identifier', '[email protected]'],
],
metadataHash:
'ad2c140d48fb958bc57d6d92536eedf2b21f6e3128c858cc27117b48a6d33abe',
min: 1,
},
},
Expand Down Expand Up @@ -60,12 +62,14 @@ export const validPayServiceParams = [
fixed: true,
identifier: '[email protected]',
image: '',
min: 10,
max: 10,
metadata: [
['text/plain', 'Payment to user11'],
['text/identifier', '[email protected]'],
],
min: 10,
metadataHash:
'382190a9edf28041fa43931f9f047c04252be80d6ae6483407fa3711df6d4152',
},
},
]

0 comments on commit 0af8243

Please sign in to comment.