aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-28 20:21:58 -0700
committerAdam Soltys <[email protected]>2022-09-28 20:21:58 -0700
commit9e07792fcdd0ba0b7c07a20fe83db54597388e26 (patch)
treea79d54ea6b864abc1953a3aafcc3ff630c1b06e5 /src/lib
parent732f77dcabc45a3df578a1a03c457b9c94866619 (diff)
downloadcoinos-ui-9e07792fcdd0ba0b7c07a20fe83db54597388e26.tar.gz
coinos-ui-9e07792fcdd0ba0b7c07a20fe83db54597388e26.zip
withdraw page and other fixups
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/parse.js271
-rw-r--r--src/lib/utils.js5
2 files changed, 276 insertions, 0 deletions
diff --git a/src/lib/parse.js b/src/lib/parse.js
new file mode 100644
index 00000000..2a827956
--- /dev/null
+++ b/src/lib/parse.js
@@ -0,0 +1,271 @@
+export default (text) => {
+ if (!text) return;
+
+ if (text.includes('@') && text.includes('.')) {
+ let [name, domain] = text.split('@');
+ try {
+ clearTimeout(debounce);
+ await new Promise((r) => (debounce = setTimeout(r, 1500)));
+ ({ data: text } = await Vue.axios.get(
+ `/encode?domain=${domain}&name=${name}`
+ ));
+ } catch (e) {}
+ }
+
+ try {
+ if (text.toLowerCase().slice(0, 10) === 'lightning:')
+ text = text.slice(10);
+ let payreq = text.toLowerCase();
+ payment.payreq = payreq;
+ payment.payobj = bolt11.decode(payment.payreq);
+ let {
+ network: { bech32 },
+ } = payment.payobj;
+
+ if (!networks.includes('lightning'))
+ return commit('error', 'Lightning not supported in this account');
+
+ if (bech32 !== process.env.VUE_APP_NETWORK_BECH32) {
+ dispatch('clearPayment');
+ commit(
+ 'error',
+ `Wrong network prefix '${bech32}' instead of '${process.env.VUE_APP_NETWORK_BECH32}'`
+ );
+ throw new Error('Wrong network');
+ }
+
+ if (user.account.ticker !== 'BTC')
+ await dispatch(
+ 'shiftAccount',
+ user.accounts.find((a) => a.asset === BTC).id
+ );
+
+ let { tags, satoshis, millisatoshis } = payment.payobj;
+ let description = tags.find((t) => t.tagName === 'description');
+ if (description) payment.memo = description.data;
+ payment.amount = millisatoshis
+ ? Math.round(millisatoshis / 1000)
+ : satoshis;
+ payment.fiatAmount = ((payment.amount * getters.rate) / SATS).toFixed(
+ 2
+ );
+ payment.network = 'lightning';
+
+ await Vue.axios.post('/lightning/query', { payreq });
+ if (!getters.payment.recipient)
+ go({ name: 'send', params: { keep: true } });
+
+ return;
+ } catch (e) {
+ if (e.response) commit('error', e.response.data);
+ }
+ //
+ // let url;
+ // try {
+ // if (nodes.includes('bitcoin')) {
+ // url = bip21.decode(text);
+ // if (!networks.includes('bitcoin'))
+ // return commit('error', 'Bitcoin not supported in this account');
+ // payment.network = 'bitcoin';
+ // url.options.asset = BTC;
+ // }
+ // } catch (e) {
+ // if (nodes.includes('liquid')) {
+ // try {
+ // url = bip21.decode(text, 'liquidnetwork');
+ // if (!networks.includes('liquid'))
+ // return commit('error', 'Liquid not supported in this account');
+ // payment.network = 'liquid';
+ // } catch (e) {
+ // /**/
+ // }
+ // }
+ // }
+ //
+ // if (url) {
+ // let { amount, asset, assetid, message } = url.options;
+ // if (assetid) asset = assetid;
+ // if (!asset) asset = BTC;
+ // let account = user.accounts.find((a) => a.asset === asset);
+ // if (account) {
+ // if (account.asset !== user.account.asset)
+ // await dispatch('shiftAccount', account.id);
+ // } else return commit('error', 'Unrecognized asset');
+ //
+ // payment.address = url.address;
+ //
+ // if (message) payment.memo = message;
+ //
+ // if (amount) {
+ // payment.amount = parseInt((amount * SATS).toFixed(0));
+ // payment.fiatAmount = ((payment.amount * getters.rate) / SATS).toFixed(
+ // 2
+ // );
+ // }
+ //
+ // await dispatch('isInternal');
+ // await dispatch('estimateFee');
+ // go({ name: 'send', params: { keep: true } });
+ // return;
+ // }
+ //
+ // if (nodes.includes('bitcoin') && validate(text)) {
+ // if (!networks.includes('bitcoin'))
+ // return commit('error', 'Bitcoin not supported in this account');
+ // payment.address = text;
+ // payment.network = 'bitcoin';
+ // await dispatch('isInternal');
+ // go({ name: 'send', params: { keep: true } });
+ // return;
+ // }
+ //
+ // // Liquid
+ // if (nodes.includes('liquid') && isLiquid(text)) {
+ // if (!networks.includes('liquid'))
+ // return commit('error', 'Liquid not supported in this account');
+ // payment.address = text;
+ // payment.network = 'liquid';
+ // await dispatch('isInternal');
+ // go({ name: 'send', params: { keep: true } });
+ // return;
+ // }
+ //
+ // if (text.startsWith('6P')) {
+ // commit('text', text);
+ // return go('/decrypt');
+ // }
+ //
+ // try {
+ // let ecpair = ECPair.fromWIF(text, {
+ // ...this._vm.$network,
+ // confidentialPrefix: 1,
+ // assetHash: '',
+ // });
+ // commit('ecpair', ecpair);
+ // go('/sweep');
+ // } catch (e) {
+ // /* */
+ // }
+ //
+ // if (text.toLowerCase().startsWith('lnurl:')) {
+ // try {
+ // const { data: lnurl } = await Vue.axios.get(`/url?code=${text}`);
+ // await dispatch('handleScan', lnurl);
+ // return;
+ // } catch (e) {
+ // commit('error', e.response ? e.response.data : e.message);
+ // }
+ // }
+ //
+ // if (text.toLowerCase().includes('lnurl')) {
+ // commit('loading', true);
+ // let txt = text.toLowerCase().match(/lnurl[a-z0-9]+/);
+ //
+ // try {
+ // const { data: params } = await Vue.axios.get(`/decode?text=${txt}`);
+ // if (params.status === 'ERROR') {
+ // let { reason } = params;
+ // try {
+ // ({ reason } = JSON.parse(reason.replace(/.*{/, '{')));
+ // } catch {
+ // /**/
+ // }
+ //
+ // commit('loading', false);
+ // return commit('error', reason);
+ // }
+ //
+ // let { seed } = getters;
+ //
+ // switch (params.tag) {
+ // case 'channelRequest':
+ // await dispatch('openChannel', params);
+ // break;
+ // case 'login':
+ // if (!seed) ({ seed } = await dispatch('passwordPrompt'));
+ // try {
+ // const key = linkingKey(params.domain, seed);
+ // const signedMessage = secp256k1.ecdsaSign(
+ // hexToUint8Array(params.k1),
+ // key.privateKey
+ // );
+ // const signedMessageDER = secp256k1.signatureExport(
+ // signedMessage.signature
+ // );
+ // const linkingKeyPub = secp256k1.publicKeyCreate(
+ // key.privateKey,
+ // true
+ // );
+ // const sig = bytesToHexString(signedMessageDER);
+ //
+ // const response = await Vue.axios.post('/login', {
+ // params,
+ // sig,
+ // key: bytesToHexString(linkingKeyPub),
+ // });
+ //
+ // if (response.data.status === 'OK') {
+ // commit('snack', 'Login success');
+ // } else {
+ // commit('error', response.status);
+ // }
+ //
+ // go('/home');
+ // } catch (e) {
+ // commit('error', e.response ? e.response.data : e.message);
+ // }
+ // break;
+ //
+ // case 'withdrawRequest':
+ // commit('lnurl', params);
+ // go('/withdraw');
+ // break;
+ // case 'payRequest':
+ // commit('lnurl', params);
+ // go('/pay');
+ // break;
+ // }
+ // } catch (e) {
+ // commit('error', e.response ? e.response.data : e.message);
+ // }
+ //
+ // commit('loading', false);
+ // }
+ //
+ // if (isUuid(text)) {
+ // try {
+ // const { data: payment } = await Vue.axios.post('/checkRedeemCode', {
+ // redeemcode: text,
+ // });
+ //
+ // if (payment) {
+ // window.location.href = `${window.location.protocol}//${window.location.host}/redeem/${text}`;
+ // return;
+ // }
+ // } catch (e) {
+ // commit('error', e.response ? e.response.data : e.message);
+ // }
+ //
+ // try {
+ // const { data: invoice } = await Vue.axios.get(
+ // `/invoice?uuid=${text}`
+ // );
+ // await dispatch('handleScan', invoice.text);
+ // return;
+ // } catch (e) {
+ // commit('error', e.response ? e.response.data : e.message);
+ // }
+ // }
+ //
+ // if (
+ // text.startsWith(
+ // `${window.location.protocol}//${window.location.host}/redeem`
+ // )
+ // ) {
+ // window.location.href = text;
+ // }
+ //
+ // commit('stopScanning', true);
+ //
+ // return 'no';
+};
diff --git a/src/lib/utils.js b/src/lib/utils.js
index df621c4b..61e2ddac 100644
--- a/src/lib/utils.js
+++ b/src/lib/utils.js
@@ -1,6 +1,7 @@
import cookie from 'cookie';
import { browser } from '$app/environment';
import { toast } from '@zerodevx/svelte-toast';
+import { goto } from '$app/navigation';
export function scroll(section) {
section.scrollIntoView({ behavior: 'smooth' });
@@ -144,3 +145,7 @@ export const toggleFullscreen = (el) =>
: el.requestFullscreen().catch(console.log);
export const sats = 100000000;
+
+export const goBack = () => {
+ browser && history.go(-1);
+};