on b78a856 help sign in
bridge/src/lib/flowCommand.ts
84 lines 2.0 kB history raw
1import { useMemo } from 'react';
2import { isValidAddress } from './utils/crypto';
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};
24type FlowType = {
25 kind?: 'takeLockup' | 'btc' | 'xpub';
26 lock?: 'linear' | 'conditional';
27 from?: string;
28 utx?: string;
29} | null;
31const useFlowCommand = () => {
32 const flow = useMemo(() => {
33 let flow: FlowType = {};
35 window.location.search
36 .substr(1)
37 .split('&')
38 .forEach(arg => {
39 if ('' === arg) return;
40 const pam = arg.split('=');
41 flow[pam[0]] = pam.length <= 1 ? true : decodeURIComponent(pam[1]);
42 });
44 if (typeof flow === 'object') {
45 switch (flow.kind) {
46 case COMMANDS.TAKE_LOCKUP:
47 flow.lock = flow.lock || 'linear';
48 if (
49 (flow.lock !== 'linear' && flow.lock !== 'conditional') ||
50 !isValidAddress(flow.from)
51 ) {
52 flow = null;
53 }
54 break;
55 //
56 case COMMANDS.BITCOIN:
57 try {
58 atob(flow.utx);
59 } catch (e) {
60 flow = null;
61 }
62 break;
63 //
64 case COMMANDS.XPUB:
65 break;
66 //
67 default:
68 console.log('unrecognized kind of flow:', flow.kind);
69 // eslint-disable-next-line no-fallthrough
70 case undefined:
71 flow = null;
72 break;
73 }
74 } else {
75 flow = null;
76 }
78 return flow;
79 }, []);
81 return flow;
82};
84export { COMMANDS, useFlowCommand };