on e586a74 help sign in
bridge/src/lib/flowCommand.ts
85 lines 2.1 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};
24export type FlowType = { [k: string]: string | boolean } | null;
26const useFlowCommand = () => {
27 const flow = useMemo(() => {
28 let flow: FlowType = {};
30 window.location.search
31 .substr(1)
32 .split('&')
33 .forEach(arg => {
34 if ('' === arg) return;
35 const pam = arg.split('=');
36 if (flow) {
37 flow[pam[0]] = pam.length <= 1 ? true : decodeURIComponent(pam[1]);
38 }
39 });
41 if (typeof flow === 'object') {
42 switch (flow.kind) {
43 case COMMANDS.TAKE_LOCKUP:
44 flow.lock = flow.lock || 'linear';
45 if (
46 (flow.lock !== 'linear' && flow.lock !== 'conditional') ||
47 !isValidAddress(flow.from)
48 ) {
49 flow = null;
50 }
51 break;
52 //
53 case COMMANDS.BITCOIN:
54 try {
55 if (flow.utx && typeof flow.utx === 'string') {
56 atob(flow.utx);
57 } else {
58 throw Error();
59 }
60 } catch (e) {
61 flow = null;
62 }
63 break;
64 //
65 case COMMANDS.XPUB:
66 break;
67 //
68 case undefined:
69 flow = null;
70 break;
71 default:
72 console.log('unrecognized kind of flow:', flow.kind);
73 flow = null;
74 }
75 } else {
76 flow = null;
77 }
79 return flow;
80 }, []);
82 return flow;
83};
85export { COMMANDS, useFlowCommand };