@@ -10,7 +10,8 @@ import { AppHomeWrapper, ShellParent } from '@blockstack/ui'
1010import {
1111 selectAccountCreated ,
1212 selectEncryptedBackupPhrase ,
13- selectIdentityAddresses
13+ selectIdentityAddresses ,
14+ selectIdentityKeypairs
1415} from '@common/store/selectors/account'
1516import {
1617 selectDefaultIdentity ,
@@ -28,7 +29,8 @@ import {
2829 hasLegacyCoreStateVersion ,
2930 migrateLegacyCoreEndpoints
3031} from '@utils/api-utils'
31- import { decrypt } from '@utils'
32+ import { uploadProfile } from '../account/utils'
33+ import { decrypt , signProfileForUpload } from '@utils'
3234const VIEWS = {
3335 INITIAL : 0 ,
3436 SUCCESS : 1 ,
@@ -43,7 +45,8 @@ const mapStateToProps = state => ({
4345 localIdentities : selectLocalIdentities ( state ) ,
4446 defaultIdentityIndex : selectDefaultIdentity ( state ) ,
4547 accountCreated : selectAccountCreated ( state ) ,
46- identityAddresses : selectIdentityAddresses ( state )
48+ identityAddresses : selectIdentityAddresses ( state ) ,
49+ identityKeypairs : selectIdentityKeypairs ( state )
4750} )
4851
4952const mapDispatchToProps = dispatch =>
@@ -137,51 +140,84 @@ class UpdatePage extends React.Component {
137140 const dataBuffer = new Buffer ( encryptedBackupPhrase , 'hex' )
138141 const { password } = this . state
139142
140- return decrypt ( dataBuffer , password )
141- . then ( backupPhraseBuffer => {
142- this . setState (
143- {
144- upgradeInProgress : true
145- } ,
146- ( ) =>
147- setTimeout ( ( ) => {
148- console . debug ( 'decryptKeyAndResetState: correct password!' )
149- const backupPhrase = backupPhraseBuffer . toString ( )
150- const numberOfIdentities =
151- localIdentities . length >= 1 ? localIdentities . length : 1
152- this . setState ( {
153- encryptedBackupPhrase,
154- backupPhrase,
155- defaultIdentityIndex,
156- numberOfIdentities
157- } )
158- if ( hasLegacyCoreStateVersion ( ) ) {
159- const migratedApi = migrateLegacyCoreEndpoints ( api )
160- this . props . migrateAPIEndpoints ( migratedApi )
161- }
162- // clear our state
163- this . props . updateState ( )
143+ const updateProfileUrls = localIdentities . map ( ( identity , index ) =>
144+ new Promise ( async ( resolve , reject ) => {
145+ try {
146+ const signedProfileTokenData = signProfileForUpload (
147+ identity . profile ,
148+ this . props . identityKeypairs [ index ] ,
149+ this . props . api
150+ )
151+ uploadProfile (
152+ this . props . api ,
153+ identity ,
154+ this . props . identityKeypairs [ index ] ,
155+ signedProfileTokenData
156+ ) . then ( resolve ) . catch ( reject )
157+ } catch ( error ) {
158+ reject ( error )
159+ }
160+
161+ } ) )
164162
165- // generate new account and IDs
166- this . createAccount ( ) . then ( ( ) => this . createNewIds ( ) )
167- . then ( ( ) => this . props . refreshIdentities (
168- this . props . api ,
169- this . props . identityAddresses
170- ) )
171- } , 150 )
172- )
173- } )
174- . catch ( error => {
175- console . error ( 'decryptKeyAndResetState: invalid password' , error )
176- this . setState ( {
177- loading : false ,
178- password : null ,
179- errors : {
180- password : 'Incorrect Password'
181- } ,
182- status : 'error'
163+ return Promise . all ( updateProfileUrls ) . then ( ( ) => {
164+ console . log ( 'updated profile URLs' )
165+ return decrypt ( dataBuffer , password )
166+ . then ( backupPhraseBuffer => {
167+ this . setState (
168+ {
169+ upgradeInProgress : true
170+ } ,
171+ ( ) =>
172+ setTimeout ( ( ) => {
173+ console . debug ( 'decryptKeyAndResetState: correct password!' )
174+ const backupPhrase = backupPhraseBuffer . toString ( )
175+ const numberOfIdentities =
176+ localIdentities . length >= 1 ? localIdentities . length : 1
177+ this . setState ( {
178+ encryptedBackupPhrase,
179+ backupPhrase,
180+ defaultIdentityIndex,
181+ numberOfIdentities
182+ } )
183+ if ( hasLegacyCoreStateVersion ( ) ) {
184+ const migratedApi = migrateLegacyCoreEndpoints ( api )
185+ this . props . migrateAPIEndpoints ( migratedApi )
186+ }
187+ // clear our state
188+ this . props . updateState ( )
189+
190+ // generate new account and IDs
191+ this . createAccount ( ) . then ( ( ) => this . createNewIds ( ) )
192+ . then ( ( ) => this . props . refreshIdentities (
193+ this . props . api ,
194+ this . props . identityAddresses
195+ ) )
196+ } , 150 )
197+ )
183198 } )
199+ . catch ( error => {
200+ console . error ( 'decryptKeyAndResetState: invalid password' , error )
201+ this . setState ( {
202+ loading : false ,
203+ password : null ,
204+ errors : {
205+ password : 'Incorrect Password'
206+ } ,
207+ status : 'error'
208+ } )
209+ } )
210+ } ) . catch ( error => {
211+ console . error ( 'upgradeBlockstackState: error updating profile' , error )
212+ this . setState ( {
213+ loading : false ,
214+ password : null ,
215+ errors : {
216+ password : 'Unable to update profile'
217+ } ,
218+ status : 'error'
184219 } )
220+ } )
185221 }
186222
187223 /**
0 commit comments