2019-02-25 20:10:13 +01:00
|
|
|
const { Component } = require('react')
|
|
|
|
const { connect } = require('react-redux')
|
|
|
|
const PropTypes = require('prop-types')
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const classnames = require('classnames')
|
|
|
|
const PubNub = require('pubnub')
|
|
|
|
|
2019-03-22 00:03:30 +01:00
|
|
|
const { requestRevealSeedWords, fetchInfoToSync } = require('../../store/actions')
|
|
|
|
const { DEFAULT_ROUTE } = require('../../helpers/constants/routes')
|
|
|
|
const actions = require('../../store/actions')
|
2019-02-25 20:10:13 +01:00
|
|
|
|
|
|
|
const qrCode = require('qrcode-generator')
|
|
|
|
|
2019-03-22 00:03:30 +01:00
|
|
|
import Button from '../../components/ui/button'
|
|
|
|
import LoadingScreen from '../../components/ui/loading-screen'
|
2019-02-25 20:10:13 +01:00
|
|
|
|
|
|
|
const PASSWORD_PROMPT_SCREEN = 'PASSWORD_PROMPT_SCREEN'
|
|
|
|
const REVEAL_SEED_SCREEN = 'REVEAL_SEED_SCREEN'
|
2019-06-12 16:54:45 +02:00
|
|
|
const KEYS_GENERATION_TIME = 30000
|
2019-02-25 20:10:13 +01:00
|
|
|
|
|
|
|
class MobileSyncPage extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
history: PropTypes.object,
|
|
|
|
selectedAddress: PropTypes.string,
|
|
|
|
displayWarning: PropTypes.func,
|
|
|
|
fetchInfoToSync: PropTypes.func,
|
|
|
|
requestRevealSeedWords: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
screen: PASSWORD_PROMPT_SCREEN,
|
|
|
|
password: '',
|
|
|
|
seedWords: null,
|
|
|
|
error: null,
|
|
|
|
syncing: false,
|
|
|
|
completed: false,
|
2019-06-12 16:54:45 +02:00
|
|
|
channelName: undefined,
|
|
|
|
cipherKey: undefined,
|
2019-02-25 20:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.syncing = false
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
const passwordBox = document.getElementById('password-box')
|
|
|
|
if (passwordBox) {
|
|
|
|
passwordBox.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
this.setState({ seedWords: null, error: null })
|
|
|
|
this.props.requestRevealSeedWords(this.state.password)
|
|
|
|
.then(seedWords => {
|
2019-06-12 16:54:45 +02:00
|
|
|
this.startKeysGeneration()
|
2019-02-25 20:10:13 +01:00
|
|
|
this.setState({ seedWords, screen: REVEAL_SEED_SCREEN })
|
|
|
|
})
|
|
|
|
.catch(error => this.setState({ error: error.message }))
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:54:45 +02:00
|
|
|
startKeysGeneration () {
|
|
|
|
this.handle && clearTimeout(this.handle)
|
|
|
|
this.disconnectWebsockets()
|
|
|
|
this.generateCipherKeyAndChannelName()
|
|
|
|
this.initWebsockets()
|
|
|
|
this.handle = setTimeout(() => {
|
|
|
|
this.startKeysGeneration()
|
|
|
|
}, KEYS_GENERATION_TIME)
|
|
|
|
}
|
|
|
|
|
2019-02-25 20:10:13 +01:00
|
|
|
generateCipherKeyAndChannelName () {
|
|
|
|
this.cipherKey = `${this.props.selectedAddress.substr(-4)}-${PubNub.generateUUID()}`
|
|
|
|
this.channelName = `mm-${PubNub.generateUUID()}`
|
2019-06-12 16:54:45 +02:00
|
|
|
this.setState({cipherKey: this.cipherKey, channelName: this.channelName})
|
|
|
|
}
|
|
|
|
|
|
|
|
initWithCipherKeyAndChannelName (cipherKey, channelName) {
|
|
|
|
this.cipherKey = cipherKey
|
|
|
|
this.channelName = channelName
|
2019-02-25 20:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
initWebsockets () {
|
2019-08-06 21:33:41 +02:00
|
|
|
// Make sure there are no existing listeners
|
|
|
|
this.disconnectWebsockets()
|
|
|
|
|
2019-02-25 20:10:13 +01:00
|
|
|
this.pubnub = new PubNub({
|
|
|
|
subscribeKey: process.env.PUBNUB_SUB_KEY,
|
|
|
|
publishKey: process.env.PUBNUB_PUB_KEY,
|
|
|
|
cipherKey: this.cipherKey,
|
|
|
|
ssl: true,
|
|
|
|
})
|
|
|
|
|
2019-08-06 21:33:41 +02:00
|
|
|
this.pubnubListener = {
|
2019-02-25 20:10:13 +01:00
|
|
|
message: (data) => {
|
|
|
|
const {channel, message} = data
|
|
|
|
// handle message
|
|
|
|
if (channel !== this.channelName || !message) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.event === 'start-sync') {
|
2019-07-31 22:17:11 +02:00
|
|
|
this.startSyncing()
|
2019-06-12 16:54:45 +02:00
|
|
|
} else if (message.event === 'connection-info') {
|
2019-07-31 22:17:11 +02:00
|
|
|
this.handle && clearTimeout(this.handle)
|
|
|
|
this.disconnectWebsockets()
|
|
|
|
this.initWithCipherKeyAndChannelName(message.cipher, message.channel)
|
|
|
|
this.initWebsockets()
|
2019-02-25 20:10:13 +01:00
|
|
|
} else if (message.event === 'end-sync') {
|
2019-07-31 22:17:11 +02:00
|
|
|
this.disconnectWebsockets()
|
|
|
|
this.setState({syncing: false, completed: true})
|
2019-02-25 20:10:13 +01:00
|
|
|
}
|
|
|
|
},
|
2019-08-06 21:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.pubnub.addListener(this.pubnubListener)
|
2019-02-25 20:10:13 +01:00
|
|
|
|
|
|
|
this.pubnub.subscribe({
|
|
|
|
channels: [this.channelName],
|
|
|
|
withPresence: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectWebsockets () {
|
|
|
|
if (this.pubnub && this.pubnubListener) {
|
2019-08-06 21:33:41 +02:00
|
|
|
this.pubnub.removeListener(this.pubnubListener)
|
2019-02-25 20:10:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
// Calculating a PubNub Message Payload Size.
|
2019-02-25 20:10:13 +01:00
|
|
|
calculatePayloadSize (channel, message) {
|
|
|
|
return encodeURIComponent(
|
2019-07-31 22:17:11 +02:00
|
|
|
channel + JSON.stringify(message)
|
2019-02-25 20:10:13 +01:00
|
|
|
).length + 100
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkString (str, size) {
|
|
|
|
const numChunks = Math.ceil(str.length / size)
|
|
|
|
const chunks = new Array(numChunks)
|
|
|
|
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
|
|
|
|
chunks[i] = str.substr(o, size)
|
|
|
|
}
|
|
|
|
return chunks
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyError (errorMsg) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.pubnub.publish(
|
|
|
|
{
|
|
|
|
message: {
|
|
|
|
event: 'error-sync',
|
|
|
|
data: errorMsg,
|
|
|
|
},
|
|
|
|
channel: this.channelName,
|
|
|
|
sendByPost: false, // true to send via post
|
|
|
|
storeInHistory: false,
|
2019-07-31 22:17:11 +02:00
|
|
|
},
|
|
|
|
(status, response) => {
|
|
|
|
if (!status.error) {
|
|
|
|
resolve()
|
|
|
|
} else {
|
|
|
|
reject(response)
|
|
|
|
}
|
|
|
|
})
|
2019-02-25 20:10:13 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async startSyncing () {
|
|
|
|
if (this.syncing) return false
|
|
|
|
this.syncing = true
|
|
|
|
this.setState({syncing: true})
|
|
|
|
|
|
|
|
const { accounts, network, preferences, transactions } = await this.props.fetchInfoToSync()
|
|
|
|
|
|
|
|
const allDataStr = JSON.stringify({
|
|
|
|
accounts,
|
|
|
|
network,
|
|
|
|
preferences,
|
|
|
|
transactions,
|
|
|
|
udata: {
|
|
|
|
pwd: this.state.password,
|
|
|
|
seed: this.state.seedWords,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const chunks = this.chunkString(allDataStr, 17000)
|
|
|
|
const totalChunks = chunks.length
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < totalChunks; i++) {
|
|
|
|
await this.sendMessage(chunks[i], i + 1, totalChunks)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.props.displayWarning('Sync failed :(')
|
|
|
|
this.setState({syncing: false})
|
|
|
|
this.syncing = false
|
|
|
|
this.notifyError(e.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage (data, pkg, count) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.pubnub.publish(
|
2019-07-31 22:17:11 +02:00
|
|
|
{
|
|
|
|
message: {
|
|
|
|
event: 'syncing-data',
|
|
|
|
data,
|
|
|
|
totalPkg: count,
|
|
|
|
currentPkg: pkg,
|
|
|
|
},
|
|
|
|
channel: this.channelName,
|
|
|
|
sendByPost: false, // true to send via post
|
|
|
|
storeInHistory: false,
|
2019-02-25 20:10:13 +01:00
|
|
|
},
|
|
|
|
(status, response) => {
|
|
|
|
if (!status.error) {
|
|
|
|
resolve()
|
|
|
|
} else {
|
|
|
|
reject(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.disconnectWebsockets()
|
|
|
|
}
|
|
|
|
|
|
|
|
renderWarning (text) {
|
|
|
|
return (
|
|
|
|
h('.page-container__warning-container', [
|
2019-07-31 22:17:11 +02:00
|
|
|
h('.page-container__warning-message', [
|
2019-02-25 20:10:13 +01:00
|
|
|
h('div', [text]),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent () {
|
|
|
|
const { t } = this.context
|
|
|
|
|
|
|
|
if (this.state.syncing) {
|
|
|
|
return h(LoadingScreen, {loadingMessage: 'Sync in progress'})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.completed) {
|
|
|
|
return h('div.reveal-seed__content', {},
|
2019-07-31 22:17:11 +02:00
|
|
|
h('label.reveal-seed__label', {
|
|
|
|
style: {
|
|
|
|
width: '100%',
|
|
|
|
textAlign: 'center',
|
|
|
|
},
|
|
|
|
}, t('syncWithMobileComplete')),
|
2019-02-25 20:10:13 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.state.screen === PASSWORD_PROMPT_SCREEN
|
|
|
|
? h('div', {}, [
|
|
|
|
this.renderWarning(this.context.t('mobileSyncText')),
|
|
|
|
h('.reveal-seed__content', [
|
|
|
|
this.renderPasswordPromptContent(),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
: h('div', {}, [
|
|
|
|
this.renderWarning(this.context.t('syncWithMobileBeCareful')),
|
|
|
|
h('.reveal-seed__content', [ this.renderRevealSeedContent() ]),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPasswordPromptContent () {
|
|
|
|
const { t } = this.context
|
|
|
|
|
|
|
|
return (
|
|
|
|
h('form', {
|
|
|
|
onSubmit: event => this.handleSubmit(event),
|
|
|
|
}, [
|
|
|
|
h('label.input-label', {
|
|
|
|
htmlFor: 'password-box',
|
|
|
|
}, t('enterPasswordContinue')),
|
|
|
|
h('.input-group', [
|
|
|
|
h('input.form-control', {
|
|
|
|
type: 'password',
|
|
|
|
placeholder: t('password'),
|
|
|
|
id: 'password-box',
|
|
|
|
value: this.state.password,
|
|
|
|
onChange: event => this.setState({ password: event.target.value }),
|
|
|
|
className: classnames({ 'form-control--error': this.state.error }),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
this.state.error && h('.reveal-seed__error', this.state.error),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRevealSeedContent () {
|
|
|
|
|
|
|
|
const qrImage = qrCode(0, 'M')
|
2019-06-12 16:54:45 +02:00
|
|
|
qrImage.addData(`metamask-sync:${this.state.channelName}|@|${this.state.cipherKey}`)
|
2019-02-25 20:10:13 +01:00
|
|
|
qrImage.make()
|
|
|
|
|
|
|
|
const { t } = this.context
|
|
|
|
return (
|
|
|
|
h('div', [
|
|
|
|
h('label.reveal-seed__label', {
|
|
|
|
style: {
|
2019-07-31 22:17:11 +02:00
|
|
|
width: '100%',
|
|
|
|
textAlign: 'center',
|
2019-02-25 20:10:13 +01:00
|
|
|
},
|
|
|
|
}, t('syncWithMobileScanThisCode')),
|
|
|
|
h('.div.qr-wrapper', {
|
|
|
|
style: {
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
dangerouslySetInnerHTML: {
|
|
|
|
__html: qrImage.createTableTag(4),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderFooter () {
|
|
|
|
return this.state.screen === PASSWORD_PROMPT_SCREEN
|
|
|
|
? this.renderPasswordPromptFooter()
|
|
|
|
: this.renderRevealSeedFooter()
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPasswordPromptFooter () {
|
|
|
|
return (
|
|
|
|
h('div.new-account-import-form__buttons', {style: {padding: 30}}, [
|
|
|
|
|
|
|
|
h(Button, {
|
|
|
|
type: 'default',
|
|
|
|
large: true,
|
|
|
|
className: 'new-account-create-form__button',
|
|
|
|
onClick: () => this.props.history.push(DEFAULT_ROUTE),
|
|
|
|
}, this.context.t('cancel')),
|
|
|
|
|
|
|
|
h(Button, {
|
2019-04-16 21:35:22 +02:00
|
|
|
type: 'secondary',
|
2019-02-25 20:10:13 +01:00
|
|
|
large: true,
|
|
|
|
className: 'new-account-create-form__button',
|
|
|
|
onClick: event => this.handleSubmit(event),
|
|
|
|
disabled: this.state.password === '',
|
|
|
|
}, this.context.t('next')),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRevealSeedFooter () {
|
|
|
|
return (
|
|
|
|
h('.page-container__footer', {style: {padding: 30}}, [
|
|
|
|
h(Button, {
|
|
|
|
type: 'default',
|
|
|
|
large: true,
|
|
|
|
className: 'page-container__footer-button',
|
|
|
|
onClick: () => this.props.history.push(DEFAULT_ROUTE),
|
|
|
|
}, this.context.t('close')),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
h('.page-container', [
|
|
|
|
h('.page-container__header', [
|
|
|
|
h('.page-container__title', this.context.t('syncWithMobileTitle')),
|
|
|
|
this.state.screen === PASSWORD_PROMPT_SCREEN ? h('.page-container__subtitle', this.context.t('syncWithMobileDesc')) : null,
|
|
|
|
this.state.screen === PASSWORD_PROMPT_SCREEN ? h('.page-container__subtitle', this.context.t('syncWithMobileDescNewUsers')) : null,
|
|
|
|
]),
|
|
|
|
h('.page-container__content', [
|
2019-07-31 22:17:11 +02:00
|
|
|
this.renderContent(),
|
2019-02-25 20:10:13 +01:00
|
|
|
]),
|
|
|
|
this.renderFooter(),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MobileSyncPage.propTypes = {
|
|
|
|
requestRevealSeedWords: PropTypes.func,
|
|
|
|
fetchInfoToSync: PropTypes.func,
|
|
|
|
history: PropTypes.object,
|
|
|
|
}
|
|
|
|
|
|
|
|
MobileSyncPage.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
return {
|
|
|
|
requestRevealSeedWords: password => dispatch(requestRevealSeedWords(password)),
|
|
|
|
fetchInfoToSync: () => dispatch(fetchInfoToSync()),
|
|
|
|
displayWarning: (message) => dispatch(actions.displayWarning(message || null)),
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const {
|
|
|
|
metamask: { selectedAddress },
|
|
|
|
} = state
|
|
|
|
|
|
|
|
return {
|
|
|
|
selectedAddress,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(MobileSyncPage)
|