Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1776 from blockstack/hotfix/gaia-config-profile
Browse files Browse the repository at this point in the history
Hotfix to correct gaiaHubConfig info in profile.json
  • Loading branch information
yknl authored Jan 8, 2019
2 parents 08916c6 + da7630e commit edb2877
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 54 deletions.
6 changes: 4 additions & 2 deletions app/js/account/utils/blockstack-inc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import log4js from 'log4js'
import { connectToGaiaHub, GaiaHubConfig } from 'blockstack'
import { connectToGaiaHub as bsConnectToGaiaHub, GaiaHubConfig, uploadToGaiaHub } from 'blockstack'

const logger = log4js.getLogger(__filename)

Expand All @@ -11,4 +11,6 @@ export function redirectToConnectToGaiaHub() {
window.top.location.href = `http://${host}:${port}/account/storage#gaiahub`
}

export { connectToGaiaHub, GaiaHubConfig }
const connectToGaiaHub = (hubUrl: string, key: string) => bsConnectToGaiaHub(hubUrl, key)

export { connectToGaiaHub, GaiaHubConfig, uploadToGaiaHub }
8 changes: 3 additions & 5 deletions app/js/account/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @flow
import { parseZoneFile } from 'zone-file'

import type { GaiaHubConfig } from 'blockstack'
import { connectToGaiaHub, uploadToGaiaHub } from 'blockstack'
import type { GaiaHubConfig } from './blockstack-inc'
import { connectToGaiaHub, uploadToGaiaHub } from './blockstack-inc'

import { getTokenFileUrlFromZoneFile } from '@utils/zone-utils'

Expand Down Expand Up @@ -88,8 +88,6 @@ export function uploadProfile(
signedProfileTokenData: string
) {
return connectToGaiaHub(api.gaiaHubUrl, identityKeyPair.key).then(identityHubConfig => {
const globalHubConfig = api.gaiaHubConfig

const urlToWrite = getProfileUploadLocation(identity, identityHubConfig)

let uploadAttempt = tryUpload(
Expand All @@ -102,7 +100,7 @@ export function uploadProfile(
uploadAttempt = tryUpload(
urlToWrite,
signedProfileTokenData,
globalHubConfig,
identityHubConfig,
'application/json'
)
}
Expand Down
2 changes: 1 addition & 1 deletion app/js/store/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function initializeStateVersion() {
* and other state is regenerated.
* @type {number}
*/
export const CURRENT_VERSION: number = 16
export const CURRENT_VERSION: number = 17

const AppReducer = combineReducers({
account: AccountReducer,
Expand Down
126 changes: 81 additions & 45 deletions app/js/update/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { AppHomeWrapper, ShellParent } from '@blockstack/ui'
import {
selectAccountCreated,
selectEncryptedBackupPhrase,
selectIdentityAddresses
selectIdentityAddresses,
selectIdentityKeypairs
} from '@common/store/selectors/account'
import {
selectDefaultIdentity,
Expand All @@ -28,7 +29,8 @@ import {
hasLegacyCoreStateVersion,
migrateLegacyCoreEndpoints
} from '@utils/api-utils'
import { decrypt } from '@utils'
import { uploadProfile } from '../account/utils'
import { decrypt, signProfileForUpload } from '@utils'
const VIEWS = {
INITIAL: 0,
SUCCESS: 1,
Expand All @@ -43,7 +45,8 @@ const mapStateToProps = state => ({
localIdentities: selectLocalIdentities(state),
defaultIdentityIndex: selectDefaultIdentity(state),
accountCreated: selectAccountCreated(state),
identityAddresses: selectIdentityAddresses(state)
identityAddresses: selectIdentityAddresses(state),
identityKeypairs: selectIdentityKeypairs(state)
})

const mapDispatchToProps = dispatch =>
Expand Down Expand Up @@ -137,51 +140,84 @@ class UpdatePage extends React.Component {
const dataBuffer = new Buffer(encryptedBackupPhrase, 'hex')
const { password } = this.state

return decrypt(dataBuffer, password)
.then(backupPhraseBuffer => {
this.setState(
{
upgradeInProgress: true
},
() =>
setTimeout(() => {
console.debug('decryptKeyAndResetState: correct password!')
const backupPhrase = backupPhraseBuffer.toString()
const numberOfIdentities =
localIdentities.length >= 1 ? localIdentities.length : 1
this.setState({
encryptedBackupPhrase,
backupPhrase,
defaultIdentityIndex,
numberOfIdentities
})
if (hasLegacyCoreStateVersion()) {
const migratedApi = migrateLegacyCoreEndpoints(api)
this.props.migrateAPIEndpoints(migratedApi)
}
// clear our state
this.props.updateState()
const updateProfileUrls = localIdentities.map((identity, index) =>
new Promise(async (resolve, reject) => {
try {
const signedProfileTokenData = signProfileForUpload(
identity.profile,
this.props.identityKeypairs[index],
this.props.api
)
uploadProfile(
this.props.api,
identity,
this.props.identityKeypairs[index],
signedProfileTokenData
).then(resolve).catch(reject)
} catch (error) {
reject(error)
}

}))

// generate new account and IDs
this.createAccount().then(() => this.createNewIds())
.then(() => this.props.refreshIdentities(
this.props.api,
this.props.identityAddresses
))
}, 150)
)
})
.catch(error => {
console.error('decryptKeyAndResetState: invalid password', error)
this.setState({
loading: false,
password: null,
errors: {
password: 'Incorrect Password'
},
status: 'error'
return Promise.all(updateProfileUrls).then(() => {
console.log('updated profile URLs')
return decrypt(dataBuffer, password)
.then(backupPhraseBuffer => {
this.setState(
{
upgradeInProgress: true
},
() =>
setTimeout(() => {
console.debug('decryptKeyAndResetState: correct password!')
const backupPhrase = backupPhraseBuffer.toString()
const numberOfIdentities =
localIdentities.length >= 1 ? localIdentities.length : 1
this.setState({
encryptedBackupPhrase,
backupPhrase,
defaultIdentityIndex,
numberOfIdentities
})
if (hasLegacyCoreStateVersion()) {
const migratedApi = migrateLegacyCoreEndpoints(api)
this.props.migrateAPIEndpoints(migratedApi)
}
// clear our state
this.props.updateState()

// generate new account and IDs
this.createAccount().then(() => this.createNewIds())
.then(() => this.props.refreshIdentities(
this.props.api,
this.props.identityAddresses
))
}, 150)
)
})
.catch(error => {
console.error('decryptKeyAndResetState: invalid password', error)
this.setState({
loading: false,
password: null,
errors: {
password: 'Incorrect Password'
},
status: 'error'
})
})
}).catch(error => {
console.error('upgradeBlockstackState: error updating profile', error)
this.setState({
loading: false,
password: null,
errors: {
password: 'Unable to update profile'
},
status: 'error'
})
})
}

/**
Expand Down
10 changes: 9 additions & 1 deletion app/js/utils/profile-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,19 @@ export function signProfileForUpload(profile, keypair, api) {
const privateKey = keypair.key
const publicKey = keypair.keyID

if (profile.api && profile.api.gaiaHubConfig) {
profile.api.gaiaHubConfig = {
url_prefix: profile.api.gaiaHubConfig.url_prefix
}
}

if (api) {
profile = {
...profile,
api: {
gaiaHubConfig: api.gaiaHubConfig,
gaiaHubConfig: {
url_prefix: api.gaiaHubConfig.url_prefix
},
gaiaHubUrl: api.gaiaHubUrl
}
}
Expand Down

0 comments on commit edb2877

Please sign in to comment.