forked from adhax/react-native-uport-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (50 loc) · 1.4 KB
/
index.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
import './globals'
import { Linking } from 'react-native'
var uportConnect = require('uport-connect/dist/uport-connect')
const { Connect, SimpleSigner } = uportConnect
const uuidv1 = require('uuid/v1')
const qs = require('qs')
const configureUportConnect = (config) => {
const uriHandler = (url) => {
Linking.openURL(url)
}
const uport = new Connect(config.appName, {
clientId: config.appAddress,
signer: SimpleSigner(config.privateKey),
mobileUrlHandler: uriHandler,
uriHandler: uriHandler,
})
uport.topicFactory = (name) => {
const id = uuidv1()
const url = `${config.urlScheme}:${id}`
let handler
let cancel
const topic = new Promise((resolve, reject) => {
handler = (uri) => {
if (uri && uri.url.startsWith(url)) {
const decoded = uri.url.replace('%23', '#')
const params = qs.parse(decoded.slice(decoded.search(/\#/)+1))
if (params && params[name]) {
Linking.removeEventListener('url', handler)
resolve(params[name])
} else {
reject()
}
}
}
Linking.addEventListener('url', handler)
cancel = () => {
Linking.removeEventListener('url', handler)
resolve()
}
})
topic.url = url
topic.cancel = cancel
return topic
}
return {
...uportConnect,
uport,
}
}
export default configureUportConnect