-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code cleanup, add bolt12info (bolt11 tags equivalent)
- Loading branch information
1 parent
332d1e1
commit f927fc5
Showing
11 changed files
with
131 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { decode } from 'bolt11' | ||
|
||
export function isBolt11 (request) { | ||
return request.startsWith('lnbc') || request.startsWith('lntb') || request.startsWith('lntbs') || request.startsWith('lnbcrt') | ||
} | ||
|
||
export function bolt11Info (bolt11) { | ||
if (!isBolt11(bolt11)) throw new Error('not a bolt11 invoice') | ||
return decode(bolt11).tagsObject | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
import { decode } from 'bolt11' | ||
/* eslint-disable camelcase */ | ||
import { payViaPaymentRequest, parsePaymentRequest } from 'ln-service' | ||
|
||
export function bolt11Tags (bolt11) { | ||
return decode(bolt11).tagsObject | ||
export function isBolt11 (request) { | ||
return request.startsWith('lnbc') || request.startsWith('lntb') || request.startsWith('lntbs') || request.startsWith('lnbcrt') | ||
} | ||
|
||
export async function parseBolt11 ({ request }) { | ||
if (!isBolt11(request)) throw new Error('not a bolt11 invoice') | ||
return parsePaymentRequest({ request }) | ||
} | ||
|
||
export async function payBolt11 ({ lnd, request, max_fee, ...args }) { | ||
if (!isBolt11(request)) throw new Error('not a bolt11 invoice') | ||
return payViaPaymentRequest({ | ||
lnd, | ||
request, | ||
max_fee, | ||
...args | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { deserializeTLVStream } from './tlv' | ||
import * as bech32b12 from '@/lib/bech32b12' | ||
|
||
export function isBolt12 (invoice) { | ||
return invoice.startsWith('lni1') || invoice.startsWith('lno1') | ||
} | ||
|
||
export function bolt12Info (bolt12) { | ||
if (!isBolt12(bolt12)) throw new Error('not a bolt12 invoice or offer') | ||
const buf = bech32b12.decode(bolt12.substring(4)/* remove lni1 or lno1 prefix */) | ||
const tlv = deserializeTLVStream(buf) | ||
const INFO_TYPES = { | ||
description: 10n, | ||
payment_hash: 168n | ||
} | ||
const info = { | ||
description: '', | ||
payment_hash: '' | ||
} | ||
for (const { type, value } of tlv) { | ||
if (type === INFO_TYPES.description) { | ||
info.description = value.toString() | ||
} else if (type === INFO_TYPES.payment_hash) { | ||
info.payment_hash = value.toString('hex') | ||
} | ||
} | ||
return info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
import { payViaBolt12PaymentRequest, parseBolt12Request } from '@/lib/lndk' | ||
|
||
export function isBolt12Offer (invoice) { | ||
return invoice.startsWith('lno1') | ||
} | ||
|
||
export function isBolt12Invoice (invoice) { | ||
return invoice.startsWith('lni1') | ||
} | ||
|
||
export function isBolt12 (invoice) { | ||
return isBolt12Offer(invoice) || isBolt12Invoice(invoice) | ||
} | ||
|
||
export async function payBolt12 ({ lnd, request: invoice, max_fee }) { | ||
if (!isBolt12Invoice(invoice)) throw new Error('not a bolt12 invoice') | ||
return await payViaBolt12PaymentRequest({ lnd, request: invoice, max_fee }) | ||
} | ||
|
||
export function parseBolt12 ({ lnd, request: invoice }) { | ||
if (!isBolt12Invoice(invoice)) throw new Error('not a bolt12 request') | ||
return parseBolt12Request({ lnd, request: invoice }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export function deserializeTLVStream (buff) { | ||
const tlvs = [] | ||
let bytePos = 0 | ||
while (bytePos < buff.length) { | ||
const [type, typeLength] = readBigSize(buff, bytePos) | ||
bytePos += typeLength | ||
|
||
let [length, lengthLength] = readBigSize(buff, bytePos) | ||
length = Number(length) | ||
bytePos += lengthLength | ||
|
||
if (bytePos + length > buff.length) { | ||
throw new Error('invalid tlv stream') | ||
} | ||
|
||
const value = buff.subarray(bytePos, bytePos + length) | ||
bytePos += length | ||
|
||
tlvs.push({ type, length, value }) | ||
} | ||
return tlvs | ||
} | ||
|
||
function readBigSize (buf, offset) { | ||
if (buf[offset] <= 252) { | ||
return [BigInt(buf[offset]), 1] | ||
} else if (buf[offset] === 253) { | ||
return [BigInt(buf.readUInt16BE(offset + 1)), 3] | ||
} else if (buf[offset] === 254) { | ||
return [BigInt(buf.readUInt32BE(offset + 1)), 5] | ||
} else if (buf[offset] === 255) { | ||
return [buf.readBigUInt64BE(offset + 1), 9] | ||
} else { | ||
throw new Error('Invalid bigsize') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters