on 6c9171b help sign in
bridge/src/lib/flowCommand.ts
77 lines 1.9 kB history raw
1import { useMemo } from 'react';
2import { isValidAddress } from './utils/address';
4// looks into the GET parameters and attempts to construct a "flow" object,
5// which would indicate bridge is to be used for one specific flow.
6// expects to find any of the following sets of parameters, varying by kind.
7//
8// ? kind = takeLockup
9// & lock = linear | conditional // which lockup contract, defaults to linear
10// & from = 0x1234abcd // ethereum address to accept transfer from
11//
12// ? kind = btc
13// & utx = somebase64string
14//
15// ? kind = xpub
16//
18const COMMANDS = {
19 TAKE_LOCKUP: 'takeLockup',
20 BITCOIN: 'btc',
21 XPUB: 'xpub',
22};
24const useFlowCommand = () => {
25 const flow = useMemo(() => {
26 let flow = {};
28 window.location.search
29 .substr(1)
30 .split('&')
31 .forEach(arg => {
32 if ('' === arg) return;
33 const pam = arg.split('=');
34 flow[pam[0]] = pam.length <= 1 ? true : decodeURIComponent(pam[1]);
35 });
37 if (typeof flow === 'object') {
38 switch (flow.kind) {
39 case COMMANDS.TAKE_LOCKUP:
40 flow.lock = flow.lock || 'linear';
41 if (
42 (flow.lock !== 'linear' && flow.lock !== 'conditional') ||
43 !isValidAddress(flow.from)
44 ) {
45 flow = null;
46 }
47 break;
48 //
49 case COMMANDS.BITCOIN:
50 try {
51 atob(flow.utx);
52 } catch (e) {
53 flow = null;
54 }
55 break;
56 //
57 case COMMANDS.XPUB:
58 break;
59 //
60 default:
61 console.log('unrecognized kind of flow:', flow.kind);
62 // eslint-disable-next-line no-fallthrough
63 case undefined:
64 flow = null;
65 break;
66 }
67 } else {
68 flow = null;
69 }
71 return flow;
72 }, []);
74 return flow;
75};
77export { COMMANDS, useFlowCommand };