-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DeeplinkManager.js
156 lines (139 loc) · 3.93 KB
/
DeeplinkManager.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
import URL from 'url-parse';
import qs from 'qs';
import { InteractionManager, Alert } from 'react-native';
import { parse } from 'eth-url-parser';
import WalletConnect from '../core/WalletConnect';
import PaymentChannelsClient from '../core/PaymentChannelsClient';
import AppConstants from './AppConstants';
class DeeplinkManager {
constructor(_navigation) {
this.navigation = _navigation;
}
handleEthereumUrl(url) {
let action;
const ethUrl = parse(url);
action = 'send-eth';
if (ethUrl.function_name === 'transfer') {
// Send erc20 token
action = 'send-token';
} else if (ethUrl.function_name) {
// Other smart contract interaction
// That involves txs
action = 'smart-contract-interaction';
}
this.navigation.navigate('SendView', { txMeta: { ...ethUrl, action, source: url } });
}
handleBrowserUrl(url, callback) {
this.navigation.navigate('BrowserTabHome');
InteractionManager.runAfterInteractions(() => {
if (callback) {
callback(url);
} else {
this.navigation.navigate('BrowserView', {
newTabUrl: url
});
}
});
}
handlePaymentChannelsUrl(to, params) {
setTimeout(() => {
PaymentChannelsClient.hub.emit('payment::request', {
to,
...params
});
}, 1000);
}
parse(url, browserCallBack = null) {
const urlObj = new URL(url);
let params;
if (urlObj.query.length) {
params = qs.parse(urlObj.query.substring(1));
}
const { MM_UNIVERSAL_LINK_HOST } = AppConstants;
switch (urlObj.protocol.replace(':', '')) {
case 'http':
case 'https':
// Universal links
if (urlObj.hostname === MM_UNIVERSAL_LINK_HOST) {
// action is the first parth of the pathname
const action = urlObj.pathname.split('/')[1];
switch (action) {
case 'wc':
if (params && params.uri && params.redirectUrl) {
setTimeout(() => {
WalletConnect.newSession(params.uri, params.redirectUrl, false);
}, 1500);
}
break;
case 'dapp':
this.handleBrowserUrl(
urlObj.href.replace(`https://${MM_UNIVERSAL_LINK_HOST}/dapp/`, 'https://'),
browserCallBack
);
break;
case 'send':
this.handleEthereumUrl(
urlObj.href.replace(`https://${MM_UNIVERSAL_LINK_HOST}/send/`, 'ethereum:')
);
break;
case 'payment':
this.handlePaymentChannelsUrl(urlObj.pathname.replace('/payment/', ''), params);
break;
case 'focus':
case '':
break;
default:
Alert.alert('Error', 'invalid deeplink');
}
} else {
// Normal links (same as dapp)
urlObj.set('protocol', 'https:');
this.handleBrowserUrl(urlObj.href, browserCallBack);
}
break;
// walletconnect related deeplinks
// address, transactions, etc
case 'wc':
// eslint-disable-next-line no-case-declarations
const redirect = params && params.redirect;
// eslint-disable-next-line no-case-declarations
const autosign = params && params.autosign;
WalletConnect.newSession(url, redirect, autosign);
break;
case 'ethereum':
this.handleEthereumUrl(url);
break;
// Specific to the browser screen
// For ex. navigate to a specific dapp
case 'dapp':
// Enforce https
urlObj.set('protocol', 'https:');
this.handleBrowserUrl(urlObj.href, browserCallBack);
break;
// Specific to the MetaMask app
// For ex. go to settings
case 'metamask':
if (urlObj.hostname === 'payment') {
this.handlePaymentChannelsUrl(urlObj.pathname.replace('/', ''), params);
}
break;
}
}
}
let instance = null;
let pendingDeeplink = null;
const SharedDeeplinkManager = {
init: navigation => {
instance = new DeeplinkManager(navigation);
},
parse: (url, browserCallback) => instance.parse(url, browserCallback),
setDeeplink: url => {
pendingDeeplink = url;
},
getPendingDeeplink: () => pendingDeeplink,
expireDeeplink: () => {
pendingDeeplink = null;
}
};
export default SharedDeeplinkManager;