Initial release

This commit is contained in:
akovalenko committed 2021-07-17 13:44:07 +03:00
commit 31f0ea8e0d
27 files changed
+3086

No files matched your search

+233
View File
@@ -0,0 +1,233 @@
<script>
export let name;
import { Styles,
Container,
Row,
Col,
Card, CardBody, CardHeader, CardTitle,
Button,
Tooltip,
Icon,
InputGroup,
Nav, NavItem, NavLink,
Modal, ModalHeader, ModalBody, ModalFooter,
} from 'sveltestrap';
import SiteHead from './SiteHead.svelte';
import SiteCard from './SiteCard.svelte';
import SiteDeck from './SiteDeck.svelte';
import InputMask from './InputMask.svelte';
import payways from './payways.js';
import QR from './QR.svelte';
import CTC from './CTC.svelte';
import Tipped from './Tipped.svelte';
import { bech32 } from 'bech32'
import UTF8 from 'utf-8'
import PayFlow from './PayFlow.svelte';
let payway = payways[0];
setTimeout(()=>{
payway = payways[0];
accounts[inputId]=null;
},0);
let inputId;
let amountMask;
let realAmount;
$: inputId = payway.iid||payway.id;
$: amountMask = {
mask: Number, scale:2,
min: payway.min, max:payway.max,
radix:".",mapToRadix:[","],
padFractionalZeros: true,
normalizeZeros: true}
$: realAmount = amounts[payway.id] && (
Math.max(Math.min(amounts[payway.id], payway.max),payway.min))
function genAutoMemo(payway,account,amount) {
if (!account)
return "...automatic";
if (amount)
amount = parseFloat(amount)
let sum = amount?
amount.toFixed(amount===Math.round(amount)?0:2)
+ " " + payway.currency.toLowerCase() : "some sats";
return sum + " to " + payway.id + " " + account;
}
let autoMemo;
$: autoMemo = genAutoMemo(payway,
accountComplete && accounts[inputId],
realAmount)
function lnurlEncode(url) {
return bech32.encode("LNURL",
bech32.toWords(UTF8.setBytesFromString(url)),2048)
}
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
function genLNURL(payway,account,amount,memo) {
let params = new URLSearchParams();
params.set("mtg","pay");
params.set("p",payway.id);
params.set("acc",toHexString(UTF8.setBytesFromString(account)))
params.set("v","1")
if (amount) {
params.set(payway.currency.toLowerCase(),amount)
}
if (memo) {
params.set("m",toHexString(UTF8.setBytesFromString(memo)))
}
return lnurlEncode("https://lnurl-pay.me/pay?"+params.toString()).toUpperCase()
}
let accounts = {};
let amounts = {};
let memo;
let lnurl;
$: lnurl = accountComplete ?
genLNURL(payway, accounts[inputId], realAmount, memo) : "";
const curr = {
"UAH":"₴",
"RUB":"₽",
"USD":"$",
"KZT":"₸",
}
let accountComplete = false;
const canShare = !!navigator.share;
let getInvoiceFor;
</script>
<SiteHead/>
<SiteDeck>
<SiteCard>
<p class="form-text">Fill in everything to get your lnurl-pay link</p>
<label class="form-label mb-3">Payment direction
<select class="form-select" autofocus bind:value="{payway}">
{#each payways as payway}
<option value="{payway}">{payway.name}</option>
{/each}
</select>
</label>
{#key payway}
<label class="form-label mb-3">{payway.acc}
<InputMask
unmask
bind:value={accounts[inputId]}
bind:isComplete={accountComplete}
imask={payway.imask||{mask:/.*/}}
autocomplete={payway.autocomplete}
inputmode={payway.inputmode||"numeric"}
placeholder={payway.placeholder}
class="form-control"/>
</label>
<label class="form-label mb-3">
Amount: {payway.currency} {payway.min} – {payway.max}
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">{curr[payway.currency]}</span>
</div>
<InputMask bind:value={amounts[payway.id]}
unmask
imask={amountMask}
placeholder="determined by payer"
inputmode={"numeric"}
class="form-control"/>
<button class="btn btn-outline-secondary"
type="button"
on:click={()=> amounts[payway.id]=payway.min.toString()}>Min
</button>
<button class="btn btn-outline-secondary"
type="button"
on:click={()=> amounts[payway.id]=payway.max.toString()}>Max
</button>
</div>
</label>
{/key}
<label class="form-label mb-3">Lnurl MEMO
<input bind:value={memo} placeholder="{autoMemo}"
class="form-control"/>
</label>
</SiteCard>
<SiteCard>
{#if lnurl}
<CTC let:id let:action force text={lnurl}>
<div class="form-text user-select-all mb-2 mt-2"
style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis"
on:click={action} id={id}>{lnurl}
</div>
</CTC>
{:else}
<div class="form-text mb-2 mt-2">Expecting: {payway.acc.toLowerCase()}</div>
{/if}
<div class="d-flex justify-content-center align-items-stretch m-2"
class:invisible={!lnurl}>
<!-- {#key lnurl} -->
<a href="lightning:{lnurl}">
<QR value={lnurl} size="{230}" />
</a>
<!-- {/key} -->
</div>
<div class="d-flex justify-content-center align-items-stretch"
class:invisible={!lnurl}>
<p class="form-text">You might want to save image and share it
as you like. All form data are present in lnurl: don't share what you wanted to hide.</p>
</div>
<div class="btn-group flex-wrap mt-auto mb-2"
role="group" class:invisible={!lnurl}>
<CTC let:id let:action text={lnurl}>
<button type="button" class="btn btn-outline-secondary"
id={id} on:click={action}>Copy</button>
</CTC>
{#if canShare}
<button type="button" class="btn btn-outline-secondary"
on:click={()=>{ navigator.share({text:lnurl})}}>Share
</button>
{/if}
<Tipped let:id>
<a slot="thing" role="button" class="btn btn-outline-secondary"
href="lightning:{lnurl}" {id}>Pay</a>
<div slot="tip">open in your wallet</div>
</Tipped>
<Tipped let:id>
<button slot="thing" {id} type="button"
class="btn btn-outline-secondary"
on:click="{()=>getInvoiceFor=lnurl}">
Invoice</button>
<div slot="tip">get an invoice for wallets with no lnurl-pay</div>
</Tipped>
</div>
</SiteCard>
</SiteDeck>
<PayFlow bind:lnurl={getInvoiceFor}/>
<!-- <Styles/> -->
+29
View File
@@ -0,0 +1,29 @@
<script>
import { Tooltip } from 'sveltestrap';
const uuid=Math.random().toString(36).replace('0.','ctc');
const canCopy = navigator.clipboard && navigator.clipboard.writeText;
export let text;
export let force = false;
const click = ()=> {
navigator.clipboard.writeText(text)
.then(()=>{
justCopied = true;
})
}
let justCopied=false;
let isOpen;
$: if (justCopied && !isOpen) justCopied = false;
</script>
{#if canCopy||force}
<slot id="{uuid}" action="{click}"/>
{#if canCopy}
<Tooltip target={uuid} bind:isOpen>
{#if justCopied}
copied!
{:else}
click to copy
{/if}
</Tooltip>
{/if}
{/if}
+71
View File
@@ -0,0 +1,71 @@
<input type="text"
bind:this={input}
use:action={maskRef}
{...attrs}
on:accept={accept}
on:accept
on:complete
>
<script type="text/javascript">
import { onMount, onDestroy, tick } from 'svelte';
import IMaskSvelte from '@imask/svelte';
import IMask from 'imask';
const action = IMaskSvelte.imask; // REPL does not understand named import
let input;
export let value = null;
export let isComplete = false;
let maskRef, imask, unmask, attrs;
$: {
({ imask, unmask, ...attrs } = $$props);
if (maskRef) {
writeValue(value);
attrs.value = maskRef.value;
tick().then(() => value = getValue());
}
}
function getValue () {
if (unmask === 'typed') return maskRef.typedValue;
if (unmask) return maskRef.unmaskedValue;
return maskRef.value;
}
function setValue (v) {
v = v == null ? '' : v;
if (unmask === 'typed') maskRef.typedValue = v;
else if (unmask) maskRef.unmaskedValue = v;
else maskRef.value = v;
isComplete = maskRef.masked.isComplete;
}
function writeValue (v) {
if (getValue() !== v ||
// handle cases like Number('') === 0,
// for details see https://github.com/uNmAnNeR/imaskjs/issues/134
(typeof v !== 'string' && value === '') && !maskRef.el.isActive
) {
setValue(v);
}
}
onMount(() => {
maskRef = IMask(input, imask);
setValue(value);
});
onDestroy(() => {
if (maskRef) maskRef.destroy();
maskRef = undefined;
isComplete = false;
});
function accept ({ detail: mask }) {
value = getValue();
isComplete = mask.masked.isComplete;
}
</script>
+170
View File
@@ -0,0 +1,170 @@
<script>
import { Styles,
Container,
Row,
Col,
Card, CardBody, CardHeader, CardTitle,
Button,
Tooltip,
Icon,
InputGroup,
Input,
Nav, NavItem, NavLink,
Modal, ModalHeader, ModalBody, ModalFooter,
} from 'sveltestrap';
import InputMask from './InputMask.svelte';
import QR from './QR.svelte';
import * as pf from './payflow.js';
import CTC from './CTC.svelte';
import Tipped from './Tipped.svelte';
export let lnurl = undefined;
let isOpen;
let res1;
let res2;
let memo;
let err;
let bolt;
let confirmAmount;
let amount;
let minSendSat=0, maxSendSat=0;
$: minSendSat = res1 && (Math.round((res1.minSendable+999)/1000))
$: maxSendSat = res1 && (Math.round(res1.maxSendable/1000))
$: if (lnurl && !isOpen) {
res1 = res2 = err = memo = confirmAmount = undefined;
isOpen = true;
fetchInvoice()
}
let stage;
$: if (!res1) {
stage = err || "Connecting to server…";
} else if (confirmAmount) {
stage = "Amount confirmation";
} else if (!res2) {
stage = err || "Requesting invoice";
} else {
stage = err || "Invoice ready.";
}
async function fetchInvoice() {
try {
let url = pf.decodeLnurl(lnurl)
//await new Promise(r => setTimeout(r, 5000));
res1 = await pf.payStep1(url)
if (res1.status == "ERROR") {
throw new Error(res1.reason)
}
if (!isOpen) { return }
if (res1.minSendable != res1.maxSendable) {
amount = Math.round((res1.minSendable+999)/1000).toString();
await new Promise((resolve, reject)=>{confirmAmount = resolve})
confirmAmount = undefined;
} else {
amount = (res1.minSendable/1000).toString();
}
res2 = await pf.payStep2(url,res1,amount*1000)
if (!isOpen) { return }
if (res2.status == "ERROR") {
throw new Error(res2.reason)
}
} catch(e) {
err = e.toString()
}
}
function mdText() {
for (let md of res1.decodedMetadata) {
if (md[0]=="text/plain") {
return md[1]
}
}
}
const canShare = !!navigator.share;
</script>
<Modal {isOpen} size='lg'>
<ModalHeader>BOLT11 invoice: {stage}</ModalHeader>
<ModalBody>
{#if confirmAmount}
<div class="row">
<label class="form-label mb-3">
Amount: {minSendSat} – {maxSendSat} satoshi
<div class="input-group">
{#key minSendSat}
<InputMask bind:value={amount}
autofocus
unmask
imask={{
mask: Number, scale:0,
min: minSendSat , max: maxSendSat,
normalizeZeros: true}}
inputmode={"numeric"}
class="form-control"/>
{/key}
<button class="btn btn-outline-secondary"
type="button"
on:click={()=> amount=minSendSat.toString()}>Min
</button>
<button class="btn btn-outline-secondary"
type="button"
on:click={()=> amount=maxSendSat.toString()}>Max
</button>
<button class="btn btn-secondary"
type="button"
disabled={!(amount && amount <= maxSendSat
&& amount >= minSendSat)}
on:click={()=> confirmAmount()}>OK
</button>
</div>
</label>
</div>
{/if}
{#if (res2&&res2.pr)}
<div class="row">
<CTC let:id let:action force text={res2.pr}>
<div class="form-text user-select-all mb-2 mt-2"
style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis"
on:click={action} id={id}>{res2.pr.toUpperCase()}
</div>
</CTC>
<div class="d-flex justify-content-center">
<a href="lightning:{res2.pr}">
<QR value="{res2.pr}" size="230"/>
</a>
</div>
</div>
{/if}
</ModalBody>
<ModalFooter>
<div class="btn-group flex-wrap">
{#if res2 && res2.pr}
<CTC let:id let:action text={res2.pr}>
<button type="button" class="btn btn-outline-secondary"
id={id} on:click={action}>Copy</button>
</CTC>
{#if canShare}
<button type="button" class="btn btn-outline-secondary"
on:click={()=>{ navigator.share({text:res2.pr})}}>Share
</button>
{/if}
<Tipped let:id>
<a slot="thing" role="button" class="btn btn-outline-secondary"
href="lightning:{res2.pr}" {id}>Pay</a>
<div slot="tip">open invoice in your wallet</div>
</Tipped>
{/if}
<button
class="btn btn-outline-primary"
type="button" on:click="{()=>{isOpen=false; lnurl = null}}">Cancel</button>
</div>
</ModalFooter>
</Modal>
+42
View File
@@ -0,0 +1,42 @@
<svelte:options immutable={true} />
<script>
import {onMount, afterUpdate} from 'svelte'
import kjua from 'kjua'
export let value = ''
export let color = '#333'
export let size = 300
let id = parseInt(Math.random() * 100000)
let container
var qr
onMount(() => {
qr = makeQR()
container.appendChild(qr)
})
afterUpdate(() => {
container.removeChild(qr)
qr = makeQR()
container.appendChild(qr)
})
function makeQR() {
return kjua({
rounded: 100,
text: value.toUpperCase(),
fill: color,
back: 'white',
quiet: 2,
size
})
}
</script>
<style>
div { text-align: center; }
</style>
<div bind:this={container}></div>
+12
View File
@@ -0,0 +1,12 @@
<script>
import {
Card,
} from 'sveltestrap';
</script>
<div class="col-md-6 d-flex">
<Card body class="shadow-lg d-flex">
<slot/>
</Card>
</div>
+13
View File
@@ -0,0 +1,13 @@
<script>
import {
Container,
Row,
} from 'sveltestrap';
</script>
<Container class="container mt-1">
<Row class="gy-2">
<slot/>
</Row>
</Container>
+39
View File
@@ -0,0 +1,39 @@
<script>
import { Styles, Tooltip } from 'sveltestrap'
import Tipped from './Tipped.svelte'
</script>
<header>
<div class="text-center p-1" style="background-color: #e0e5ff">
<h1>lnurl-pay generator</h1>
<p>Create lnurl-pay code for sending fiat money to various destinations from Bitcoin Lightning Network.
</p>
</div>
<div class="container-fluid d-flex">
<div class="nav ms-auto">
<div class="nav-item">
<Tipped let:id>
<a slot="thing" {id} class="nav-link"
href="https://twitter.com/LNURL_pay_me">News</a>
<div slot="tip">project twitter</div>
</Tipped>
</div>
<div class="nav-item">
<Tipped let:id>
<a slot="thing" {id} class="nav-link"
href="https://t.me/lntorubbot_dev">Support</a>
<div slot="tip">telegram discussion group</div>
</Tipped>
</div>
<div class="nav-item">
<Tipped let:id>
<a slot="thing" {id}
class="nav-link" href="https://teletype.in/@lnurl-pay.me/SSUzJFa0njT">
About
</a>
<div slot="tip">short intro</div>
</Tipped>
</div>
</div>
</div>
</header>
+9
View File
@@ -0,0 +1,9 @@
<script>
import { Tooltip } from 'sveltestrap';
const uuid=Math.random().toString(36).replace('0.','tip');
</script>
<slot name="thing" id="{uuid}"/>
<Tooltip target={uuid}>
<slot name="tip"/>
</Tooltip>
+10
View File
@@ -0,0 +1,10 @@
import App from './App.svelte';
const app = new App({
target: document.body,
props: {
name: 'world'
}
});
export default app;
+51
View File
@@ -0,0 +1,51 @@
import { bech32 } from 'bech32'
import UTF8 from 'utf-8'
function getDomain(url) {
return url
.split('://')[1]
.split('/')[0]
.split('@')
.slice(-1)[0]
.split(':')[0]
}
export function decodeLnurl(lnurl) {
let obj = bech32.decode(lnurl,20000)
if (obj.prefix!="lnurl")
throw new Error("bad prefix")
return UTF8.getStringFromBytes(bech32.fromWords(obj.words))
}
export async function payStep1(url) {
const r = await fetch(url)
if (r.status >= 300) {
throw new Error(await r.text())
}
let res = await r.json()
if (res.status == "ERROR")
return res;
if (res.tag != "payRequest") {
throw new Error(`LNURL type ${r.tag}, expected: payRequest`)
}
try {
res.decodedMetadata = JSON.parse(res.metadata)
} catch (err) {
res.decodedMetadata = []
}
return res
}
export async function payStep2(url, res, msat, comment) {
let cb = res.callback;
let params = new URLSearchParams();
params.set("amount",msat);
cb = cb+(cb.includes("?")?"&":"?")+params.toString()
const r = await fetch(cb)
if (r.status >= 300) {
throw new Error(await r.text())
}
let res2 = await r.json()
return res2
}
+193
View File
@@ -0,0 +1,193 @@
const masks = {
cc: {
mask: "0000 0000 0000 0000"
},
telru: { mask: "{+}{7} 000 000-0000" },
telua: { mask: "{+}{3}{8} {\\0}00 000 0000"},
tel: {
mask: [
{
mask: '+00 {21} 0 000 0000',
startsWith: '30',
lazy: false,
country: 'Greece'
},
{
mask: '+0 000 000 0000',
// startsWith: '77',
lazy: false,
country: 'Kazakhstan'
},
{
mask: '+0 000 000-0000',
startsWith: '7',
// lazy: false,
country: 'Russia'
},
{
mask: "+00 000 000-0000",
startsWith: '380',
// lazy: false,
country: 'Ukraine'
},
{
mask: '+00-0000-000000',
startsWith: '91',
lazy: false,
country: 'India'
},
{
mask: '+0000000[000000000]',
startsWith: '',
country: 'unknown'
}
],
dispatch: function (appended, dynamicMasked) {
var number = (dynamicMasked.value + appended).replace(/\D/g,'');
return dynamicMasked.compiledMasks.find(function (m) {
return number.indexOf(m.startsWith) === 0;
});
}
}
}
const maskYoo = {
mask:[{mask: "00000000000000[0]",startsWith: '41001'},...masks.tel.mask],
dispatch: masks.tel.dispatch
}
export default [
{"name":"💳 🇷🇺 RUB ₽ Visa/MC/Mir",
"currency":"RUB",
"min":500,
"max":10000,
"acc":"Card number",
"imask":masks.cc,
"iid":"cc",
"autocomplete":"cc-number",
"id":"card",
"placeholder":"4111 1111 1111 1111",
},
{"name":"💳 🇺🇦 UAH ₴ Visa/MC",
"currency":"UAH",
"min":10,
"max":5000,
"imask":masks.cc,
"iid":"cc",
"acc":"Card number",
"autocomplete":"cc-number",
"id":"uacard",
"placeholder":"4111 1111 1111 1111",
},
{"name":"💳 🇰🇿 KZT ₸ Visa/MC",
"currency":"KZT",
"min":500,
"max":50000,
"imask":masks.cc,
"acc":"Card number",
"iid":"cc",
"autocomplete":"cc-number",
"id":"kzcard",
"placeholder":"4111 1111 1111 1111",
},
{"name":"📱 🇷🇺 RUB ₽ Mobile top-up",
"currency":"RUB",
"min":1,
"max":2000,
"imask":masks.telru,
"acc":"Phone number",
"autocomplete":"tel",
"inputmode":"tel",
"id":"mobile",
"placeholder":"+7 955 555-1234",
},
{"name":"📱 🇺🇦 UAH ₴ Mobile top-up",
"currency":"UAH",
"min":1,
"max":500,
"imask":masks.telua,
"acc":"Phone number",
"autocomplete":"tel",
"inputmode":"tel",
"id":"uamobile",
"placeholder":"+38 055 555 1234",
},
{"name":"👛 🇷🇺 RUB ₽ Qiwi",
"imask": masks.tel,
"currency":"RUB",
"min":1,
"max":10000,
"acc":"Wallet (Phone number)",
"autocomplete":"tel",
"inputmode":"tel",
"id":"qiwi",
"placeholder":"+7 955 555-1234",
},
{"name":"👛 🇷🇺 RUB ₽ YooMoney",
"currency":"RUB",
"min":0.01,
"max":10000,
"acc":"Wallet ID or mobile number",
"autocomplete":"tel",
"id":"yandex",
"imask":maskYoo,
"placeholder":"41001…",
},
{"name":"🚌 🇷🇺 RUB ₽ Strelka",
"currency":"RUB",
"min":10,
"max":2000,
"imask":{mask:"0000 0000 000"},
"acc":"Strelka card number",
"id":"strelka",
"placeholder":"0000 0000 000",
},
{"name":"🌐 🇷🇺 RUB ₽ Flex provider",
"currency":"RUB",
"min":20,
"max":2000,
"acc":"Flex account (NB! not user login)",
"id":"flex",
"imask":{mask:"0[0000000]"},
},
{"name":"☎️ 🇺🇸 USD $ Skype account",
"currency":"USD",
"min":0.05,
"max":30,
"acc":"Skype login name",
"id":"skype",
"inputmode":"text",
"imask":{mask:"a[xxxxxxxxxxxxxx]",
definitions:{'a':/[A-Za-z_]/,'x':/[A-Za-z0-9_,\.\-]/}},
"placeholder":"amazing1337_user",
},
{"name":"📞 🇷🇺 RUB ₽ SipNet",
"currency":"RUB",
"min":10,
"max":2000,
"acc":"SIP ID",
"id":"sipnet",
"imask":{mask:"000[000000]"}
},
{"name":"🌐 🇷🇺 RUB ₽ Spaceweb.ru",
"currency":"RUB",
"min":10,
"max":2000,
"acc":"Login on sweb.ru",
"id":"spaceweb",
"inputmode":"text",
"imask":{mask:"a[xxxxxxxxxxxxxx]",
definitions:{'a':/[A-Za-z_]/,'x':/[A-Za-z0-9_,\.\-]/}},
},
{"name":"🚕 🇷🇺 RUB ₽ Taxi Maxim",
"currency":"RUB",
"min":10,
"max":2000,
"imask":{mask:"000[000000]"},
"acc":"Account number ",
"id":"maxim",
},
]