aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/invoice.js2
-rw-r--r--src/lib/nostr.js14
-rw-r--r--src/lib/parse.js0
-rw-r--r--src/lib/samples.js23
-rw-r--r--src/lib/socket.js27
-rw-r--r--src/lib/store.js3
-rw-r--r--src/lib/translations/index.js5
-rw-r--r--src/lib/translations/lang.json3
-rw-r--r--src/lib/utils.js18
9 files changed, 62 insertions, 33 deletions
diff --git a/src/lib/invoice.js b/src/lib/invoice.js
index bb75d4cb..23ce1757 100644
--- a/src/lib/invoice.js
+++ b/src/lib/invoice.js
@@ -11,7 +11,7 @@ export default async ({ cookies, request, url }) => {
let invoice = {
amount,
- tip: parseInt(form.get('tip')),
+ tip: parseInt(form.get('tip')) || 0,
type: form.get('type'),
prompt: form.get('prompt') === 'true',
rate: parseFloat(form.get('rate')) || rates[form.get('currency')],
diff --git a/src/lib/nostr.js b/src/lib/nostr.js
index de19b477..d72f48ef 100644
--- a/src/lib/nostr.js
+++ b/src/lib/nostr.js
@@ -5,7 +5,7 @@ import { Buffer } from 'buffer';
import { bech32m } from 'bech32';
import { bip32, fail, wait, post, stretch } from '$lib/utils';
import { calculateId, signId } from 'nostr';
-import { password as pw, passwordPrompt, pin, loginRedirect } from '$lib/store';
+import { decrypted, password as pw, passwordPrompt, pin, loginRedirect } from '$lib/store';
import { goto, invalidate } from '$app/navigation';
const { encode, decode, fromWords, toWords } = bech32m;
@@ -58,8 +58,10 @@ export let encrypt = async ({ message, recipient, user }) => {
};
export let decrypt = async ({ event, user }) => {
+ let cache = get(decrypted);
try {
- let { content, pubkey } = event;
+ let { content, pubkey, id } = event;
+ if (cache[id]) return cache[id];
if (pubkey === user.pubkey) pubkey = event.tags[0][1];
pubkey = Uint8Array.from(Buffer.from('02' + pubkey, 'hex'));
@@ -78,9 +80,12 @@ export let decrypt = async ({ event, user }) => {
let message = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, key, cipher);
- return Buffer.from(message).toString('utf8');
+ cache[id] = Buffer.from(message).toString('utf8');
+ decrypted.set(cache);
+
+ return cache[id];
} catch (e) {
- console.log(e);
+ // console.log(e);
}
};
@@ -119,6 +124,7 @@ let getPrivateKey = async (user) => {
seed = mnemonicToSeedSync(mnemonic);
key = bip32.fromSeed(seed);
child = key.derivePath("m/44'/1237'/0'/0/0");
+
return child.privateKey;
};
diff --git a/src/lib/parse.js b/src/lib/parse.js
deleted file mode 100644
index e69de29b..00000000
--- a/src/lib/parse.js
+++ /dev/null
diff --git a/src/lib/samples.js b/src/lib/samples.js
new file mode 100644
index 00000000..93b9aa24
--- /dev/null
+++ b/src/lib/samples.js
@@ -0,0 +1,23 @@
+export default {
+ register: `curl "https://coinos.io/api/register" -H "application/json" -d '{
+ "username": "adam",
+ "password": "hunter2"
+}'`,
+ login: `curl "https://coinos.io/api/login" -H "application/json" -d '{
+ "username": "adam",
+ "password": "hunter2"
+}'`,
+ invoice: `curl "https://coinos.io/api/invoice"
+ -H "application/json"
+ -H "Authorization: Bearer $token"
+ -d '{
+ "invoice": {
+ "amount": 1000,
+ "type": "lightning",
+ "webhook": "https://example.com/payment/received",
+ "secret": "hunter2"
+ },
+}'`,
+ fetchInvoice: `curl "https://coinos.io/api/invoice/bc1qmhfk9stzffhd9umzmld92vff7zg3mdlh7rvvaj"
+ -H "application/json"'`
+};
diff --git a/src/lib/socket.js b/src/lib/socket.js
index ce3cbe4b..aef8a513 100644
--- a/src/lib/socket.js
+++ b/src/lib/socket.js
@@ -1,23 +1,11 @@
import { get } from 'svelte/store';
-import {
- events as $events,
- invoice,
- request,
- newPayment,
- last,
- rate,
- payments,
- user
-} from '$lib/store';
+import { event, invoice, request, newPayment, last, rate, payments, user } from '$lib/store';
import { success, sat } from '$lib/utils';
import { PUBLIC_SOCKET } from '$env/static/public';
import { invalidate } from '$app/navigation';
import { browser } from '$app/environment';
let socket, token;
-let le = browser && localStorage.getItem('events');
-let events = le && le !== 'undefined' ? JSON.parse(le) : {};
-let q = Object.keys(events);
export const auth = () => token && send('login', token) && send('heartbeat');
@@ -30,23 +18,12 @@ export const messages = (data) => ({
id() {
last.set(Date.now());
},
-
invoice() {
invoice.set(data);
},
-
event() {
- data.seen = Math.round(Date.now() / 1000);
- events[data.id] = data;
- q.push(data.id);
- $events.set(events);
- while (q.length > 10000) delete events[q.shift()];
- localStorage.setItem(
- 'events',
- JSON.stringify(q.slice(-100).reduce((a, b) => (a = { ...a, [b]: events[b] }), {}))
- );
+ event.set(data);
},
-
rate() {
rate.set(data);
},
diff --git a/src/lib/store.js b/src/lib/store.js
index e08d9f94..7f43e914 100644
--- a/src/lib/store.js
+++ b/src/lib/store.js
@@ -36,11 +36,12 @@ export const animatedRate = writable(0);
export const avatar = writable();
export const banner = writable();
export const colorTheme = writable('from-primary to-gradient');
+export const event = writable();
export const events = writable({});
export const invoice = writable();
export const last = writable();
export const loginRedirect = writable();
-export const messages = persistLocal('messages', {});
+export const decrypted = persistLocal('decrypted', {});
export const newPayment = persistLocal('newPayment');
export const password = persistLocal('password');
export const passwordPrompt = writable();
diff --git a/src/lib/translations/index.js b/src/lib/translations/index.js
index 9c798530..dfd7f47f 100644
--- a/src/lib/translations/index.js
+++ b/src/lib/translations/index.js
@@ -46,6 +46,11 @@ const config = {
locale: 'de',
key: '',
loader: async () => (await import('../../locales/de.json')).default
+ },
+ {
+ locale: 'zh',
+ key: '',
+ loader: async () => (await import('../../locales/zh.json')).default
}
]
};
diff --git a/src/lib/translations/lang.json b/src/lib/translations/lang.json
index c537d4cf..c607ebd0 100644
--- a/src/lib/translations/lang.json
+++ b/src/lib/translations/lang.json
@@ -3,5 +3,6 @@
"fr": "Français",
"es": "Español",
"de": "Deutsch",
- "pt": "Português"
+ "pt": "Português",
+ "zh": "中国人"
}
diff --git a/src/lib/utils.js b/src/lib/utils.js
index d510195b..6617f76e 100644
--- a/src/lib/utils.js
+++ b/src/lib/utils.js
@@ -147,6 +147,14 @@ export const login = async (user, cookies) => {
export const auth = (cookies) => ({ authorization: `Bearer ${cookies.get('token')}` });
+export const fiat = (amount, rate) => (amount * rate) / sats;
+export const fd = async (req) => {
+ let obj = Object.fromEntries(await req.formData());
+ for (let k in obj)
+ (obj[k] === 'undefined' && (obj[k] = undefined)) || (obj[k] === 'false' && (obj[k] = false));
+ return obj;
+};
+
export const f = (s, currency) =>
new Intl.NumberFormat('en-US', {
style: 'currency',
@@ -236,4 +244,12 @@ export const validateEmail = (email) => {
const re =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
-}; \ No newline at end of file
+};
+
+export const types = {
+ bitcoin: 'bitcoin',
+ lightning: 'lightning',
+ internal: 'internal',
+ pot: 'pot',
+ classic: 'classic'
+};