on a3fd1ac help sign in
bridge/src/lib/flowCommand.js
71 lines 1.8 kB history raw
1import { useMemo } from 'react';
2import { isValidAddress } from './wallet';
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//
16const COMMANDS = {
17 TAKE_LOCKUP: 'takeLockup',
18 BITCOIN: 'btc',
19};
21const useFlowCommand = () => {
22 const flow = useMemo(() => {
23 let flow = {};
25 window.location.search
26 .substr(1)
27 .split('&')
28 .forEach(arg => {
29 if ('' === arg) return;
30 const pam = arg.split('=');
31 flow[pam[0]] = pam.length <= 1 ? true : decodeURIComponent(pam[1]);
32 });
34 if (typeof flow === 'object') {
35 switch (flow.kind) {
36 case COMMANDS.TAKE_LOCKUP:
37 flow.lock = flow.lock || 'linear';
38 if (
39 (flow.lock !== 'linear' && flow.lock !== 'conditional') ||
40 !isValidAddress(flow.from)
41 ) {
42 flow = null;
43 }
44 break;
45 //
46 case COMMANDS.BITCOIN:
47 try {
48 atob(flow.utx);
49 } catch (e) {
50 flow = null;
51 }
52 break;
53 //
54 default:
55 console.log('unrecognized kind of flow:', flow.kind);
56 // eslint-disable-next-line no-fallthrough
57 case undefined:
58 flow = null;
59 break;
60 }
61 } else {
62 flow = null;
63 }
65 return flow;
66 }, []);
68 return flow;
69};
71export { COMMANDS, useFlowCommand };