-
-
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.
- Loading branch information
1 parent
e6e82b0
commit 15d17e0
Showing
23 changed files
with
727 additions
and
54 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
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 |
---|---|---|
|
@@ -168,6 +168,9 @@ export const WALLET_FIELDS = gql` | |
apiKeyRecv | ||
currencyRecv | ||
} | ||
... on WalletBolt12 { | ||
offer | ||
} | ||
} | ||
} | ||
` | ||
|
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,46 @@ | ||
const ALPHABET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l' | ||
|
||
export function decode (str) { | ||
const b5s = [] | ||
for (const char of str) { | ||
const i = ALPHABET.indexOf(char) | ||
if (i === -1) throw new Error('Invalid bech32 character') | ||
b5s.push(i) | ||
} | ||
const b8s = Buffer.from(converBits(b5s, 5, 8, false)) | ||
return b8s | ||
} | ||
|
||
export function encode (b8s) { | ||
const b5s = converBits(b8s, 8, 5, true) | ||
const str = [] | ||
for (const b5 of b5s) str.push(ALPHABET[b5]) | ||
return str.join('') | ||
} | ||
|
||
function converBits (data, frombits, tobits, pad) { | ||
let acc = 0 | ||
let bits = 0 | ||
const ret = [] | ||
const maxv = (1 << tobits) - 1 | ||
for (let p = 0; p < data.length; ++p) { | ||
const value = data[p] | ||
if (value < 0 || (value >> frombits) !== 0) { | ||
throw new RangeError('input value is outside of range') | ||
} | ||
acc = (acc << frombits) | value | ||
bits += frombits | ||
while (bits >= tobits) { | ||
bits -= tobits | ||
ret.push((acc >> bits) & maxv) | ||
} | ||
} | ||
if (pad) { | ||
if (bits > 0) { | ||
ret.push((acc << (tobits - bits)) & maxv) | ||
} | ||
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { | ||
throw new RangeError('could not convert bits') | ||
} | ||
return ret | ||
} |
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,78 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
import { payViaPaymentRequest, parsePaymentRequest } from 'ln-service' | ||
import { estimateRouteFee } from '@/api/lnd' | ||
|
||
import { payViaBolt12PaymentRequest, parseBolt12Request, estimateBolt12RouteFee } from '@/lib/lndk' | ||
|
||
export function isBolt11 (request) { | ||
return request.startsWith('lnbc') || request.startsWith('lntb') || request.startsWith('lntbs') || request.startsWith('lnbcrt') | ||
} | ||
|
||
export function parseBolt11 ({ request }) { | ||
if (!isBolt11(request)) { | ||
throw new Error('Not a bolt11 invoice') | ||
} | ||
return parsePaymentRequest({ request }) | ||
} | ||
|
||
export function payBolt11 ({ lnd, request, max_fee, ...args }) { | ||
if (!isBolt11(request)) { | ||
throw new Error('Not a bolt11 invoice') | ||
} | ||
return payViaPaymentRequest({ | ||
lnd, | ||
request, | ||
max_fee, | ||
...args | ||
}) | ||
} | ||
|
||
export function isBolt12Offer (invoice) { | ||
return invoice.startsWith('lno1') | ||
} | ||
|
||
export function isBolt12Invoice (invoice) { | ||
console.log('isBolt12Invoice', invoice) | ||
console.trace() | ||
return invoice.startsWith('lni1') | ||
} | ||
|
||
export async function payBolt12 ({ lnd, request: invoice, max_fee }) { | ||
if (!isBolt12Invoice(invoice)) { | ||
throw new Error('Not a bolt12 invoice') | ||
} | ||
if (!invoice) throw new Error('No invoice in bolt12, please use prefetchBolt12Invoice') | ||
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 }) | ||
} | ||
|
||
export async function payInvoice ({ lnd, request: invoice, max_fee, ...args }) { | ||
if (isBolt12Invoice(invoice)) { | ||
return await payBolt12({ lnd, request: invoice, max_fee, ...args }) | ||
} else { | ||
return await payBolt11({ lnd, request: invoice, max_fee, ...args }) | ||
} | ||
} | ||
|
||
export async function parseInvoice ({ lnd, request }) { | ||
if (isBolt12Invoice(request)) { | ||
return await parseBolt12({ lnd, request }) | ||
} else { | ||
return await parseBolt11({ request }) | ||
} | ||
} | ||
|
||
export async function estimateFees ({ lnd, destination, tokens, mtokens, request, timeout }) { | ||
if (isBolt12Invoice(request)) { | ||
return await estimateBolt12RouteFee({ lnd, destination, tokens, mtokens, request, timeout }) | ||
} else { | ||
return await estimateRouteFee({ lnd, destination, tokens, request, mtokens, timeout }) | ||
} | ||
} |
Oops, something went wrong.