aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/send/+page.server.js
blob: c8edb1cc482d001b7d410ced796a4b42eaf55591 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import validate from 'bitcoin-address-validation';
import bip21 from 'bip21';
import { get } from '$lib/utils';
import { invalid, redirect } from '@sveltejs/kit';

export const actions = {
	default: async ({ request }) => {
		const values = await request.formData();
		const t = values.get('text');
		if (!t) return;

		// lightning
		if (t.startsWith('ln')) {
			throw redirect(303, `/send/lightning/${t}`);
		}

		// bitcoin
		if (validate(t)) {
			let user;
			try {
				({ user } = await get(`/invoice/${t}`));
			} catch (e) {
				throw redirect(303, `/send/bitcoin/${t}`);
			}

			if (user) throw redirect(303, `/send/user/${user.username}`);
		}

		// ln address
		if (t.includes('@') && t.includes('.')) {
			let [name, domain] = t.split('@');
			try {
				let lnurl = await get(`/encode?domain=${domain}&name=${name}`);
			} catch (e) {}
		}

		// user
		let user;
		try {
			user = await get(`/users/${t}`);
		} catch (e) {}

		if (user) throw redirect(303, `/send/user/${t}`);

		return invalid(403, { error: 'Failed to recognize input' });
	}
};