Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uport Connect - React Native example
{
"name": "exampleapp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node_modules/react-native/packager/packager.sh --nonPersistent",
"test": "jest",
"build-uport-connect": "node_modules/.bin/derequire node_modules/uport-connect/dist/uport-connect.js >src/vendor/uport-connect.js"
},
"dependencies": {
"react": "16.0.0-alpha.6",
"react-native": "^0.43.2",
"react-redux": "^5.0.3",
"redux": "^3.6.0",
"redux-logic": "^0.11.9",
"reselect": "^3.0.0",
"uport-connect": "^0.5.5"
},
"react-native": {
"stream": false,
"crypto": false
},
"devDependencies": {
"babel-jest": "^18.0.0",
"babel-plugin-transform-imports": "^1.1.0",
"babel-preset-react-native": "^1.9.1",
"derequire": "^2.0.6",
"jest": "^19.0.2",
"jest-react-native": "^18.0.0",
"react-test-renderer": "^15.4.2",
"standard": "^8.6.0"
},
"jest": {
"preset": "react-native"
}
}
const UportConnect = require('../vendor/uport-connect')
const Connect = UportConnect.ConnectCore
const SimpleSigner = UportConnect.SimpleSigner
import randomString from '../tools/randomString' // just a function to create a random number
// import Web3 from 'web3'
import { Linking } from 'react-native'
const rpcUrl = 'https://ropsten.infura.io/<INSERT YOUR INFURAKEY HERE>'
import URL from 'url-parse'
import qs from 'qs'
const uriHandler = (url) => {
console.log(url)
Linking.openURL(url)
}
export const uport = new Connect('APPNAME', {
mobileUrlHandler: uriHandler,
uriHandler: uriHandler,
rpcUrl
})
uport.topicFactory = (name) => {
const id = randomString(10)
const path = `/uport/${id}`
const url = `APPURLSCHEMA:${path}` // You need to set this up in xcode. See https://facebook.github.io/react-native/docs/linking.html
let handler
let cancel
const topic = new Promise((resolve, reject) => {
handler = (event) => {
if (event.url) {
const url = URL(event.url, true)
if (url.pathname === path) {
if (url.hash) {
const params = qs.parse(url.hash.slice(1))
Linking.removeEventListener('url', handler)
resolve(params[name])
} else {
console.log('no hash')
reject()
}
} else {
console.log('ignoring request')
}
}
}
Linking.addEventListener('url', handler)
cancel = () => {
Linking.removeEventListener('url', handler)
resolve()
}
})
topic.url = url
topic.cancel = cancel
return topic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.