Skip to content

Commit

Permalink
feat: remove storage functions from user session
Browse files Browse the repository at this point in the history
  • Loading branch information
yknl committed Aug 11, 2020
1 parent 03c7c92 commit c27d948
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 124 deletions.
2 changes: 1 addition & 1 deletion packages/auth/src/auth.ts
Expand Up @@ -18,7 +18,7 @@ import { extractProfile } from './legacy/profiles/profileTokens'
import { UserSession } from './userSession'
import { config } from './legacy/config'
import { GaiaHubConfig } from './legacy/storage/hub'
import { hexStringToECPair } from './legacy/keys'
import { hexStringToECPair } from '@stacks/encryption'


const DEFAULT_PROFILE = {
Expand Down
5 changes: 1 addition & 4 deletions packages/auth/src/constants.ts
Expand Up @@ -43,10 +43,7 @@ export const DEFAULT_SCOPE = [AuthScope.store_write]
* @ignore
*/
export const BLOCKSTACK_APP_PRIVATE_KEY_LABEL = 'blockstack-transit-private-key'
/**
* @ignore
*/
export const BLOCKSTACK_DEFAULT_GAIA_HUB_URL = 'https://hub.blockstack.org'

/**
* @ignore
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/auth/src/index.ts
Expand Up @@ -16,3 +16,8 @@ export {
export {
handlePendingSignIn, signUserOut
} from './auth'
export {
UserSession
} from './userSession'
export * from './constants'
export * from './profile'
38 changes: 38 additions & 0 deletions packages/auth/src/profile.ts
@@ -0,0 +1,38 @@

import { resolveZoneFileToProfile } from './legacy/profiles/profileZoneFiles'
import { fetchPrivate } from '@stacks/common'
import { config } from './legacy/config'

/**
* Look up a user profile by blockstack ID
*
* @param {string} username - The Blockstack ID of the profile to look up
* @param {string} [zoneFileLookupURL=null] - The URL
* to use for zonefile lookup. If falsey, lookupProfile will use the
* blockstack.js [[getNameInfo]] function.
* @returns {Promise} that resolves to a profile object
*/
export function lookupProfile(username: string, zoneFileLookupURL?: string):
Promise<Record<string, any>> {
if (!username) {
return Promise.reject()
}
let lookupPromise
if (zoneFileLookupURL) {
const url = `${zoneFileLookupURL.replace(/\/$/, '')}/${username}`
lookupPromise = fetchPrivate(url)
.then(response => response.json())
} else {
lookupPromise = config.network.getNameInfo(username)
}
return lookupPromise
.then((responseJSON) => {
if (responseJSON.hasOwnProperty('zonefile')
&& responseJSON.hasOwnProperty('address')) {
return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address)
} else {
throw new Error('Invalid zonefile lookup response: did not contain `address`'
+ ' or `zonefile` field')
}
})
}
128 changes: 9 additions & 119 deletions packages/auth/src/userSession.ts
Expand Up @@ -7,17 +7,17 @@ import {
} from './sessionStore'

import * as authMessages from './messages'
import * as storage from './legacy/storage'
// import * as storage from './legacy/storage'

import {
nextHour,
MissingParameterError,
InvalidStateError,
Logger
} from '@stacks/common'
import { GaiaHubConfig, connectToGaiaHub } from './legacy/storage/hub'
import { BLOCKSTACK_DEFAULT_GAIA_HUB_URL, AuthScope } from './constants'
import { AuthScope } from './constants'
import { handlePendingSignIn, signUserOut, getAuthResponseToken } from './auth'
import { encryptContent, decryptContent, EncryptContentOptions } from '@stacks/encryption';


/**
Expand Down Expand Up @@ -202,9 +202,12 @@ export class UserSession {
*/
encryptContent(
content: string | Buffer,
options?: import('./legacy/storage').EncryptContentOptions
options?: EncryptContentOptions
): Promise<string> {
return storage.encryptContent(this, content, options)
if (!options.privateKey) {
options.privateKey = this.loadUserData().appPrivateKey;
}
return encryptContent(content, options)
}

/**
Expand All @@ -216,120 +219,7 @@ export class UserSession {
* @returns {String|Buffer} decrypted content.
*/
decryptContent(content: string, options?: {privateKey?: string}): Promise<Buffer | string> {
return storage.decryptContent(this, content, options)
}

/**
* Stores the data provided in the app's data store to to the file specified.
* @param {String} path - the path to store the data in
* @param {String|Buffer} content - the data to store in the file
* @param options a [[PutFileOptions]] object
*
* @returns {Promise} that resolves if the operation succeed and rejects
* if it failed
*/
putFile(
path: string,
content: string | Buffer | ArrayBufferView | Blob,
options?: import('./legacy/storage').PutFileOptions
) {
return storage.putFile(this, path, content, options)
}

/**
* Retrieves the specified file from the app's data store.
*
* @param {String} path - the path to the file to read
* @param {Object} options a [[GetFileOptions]] object
*
* @returns {Promise} that resolves to the raw data in the file
* or rejects with an error
*/
getFile(path: string, options?: import('./legacy/storage').GetFileOptions) {
return storage.getFile(this, path, options)
}

/**
* Get the URL for reading a file from an app's data store.
*
* @param {String} path - the path to the file to read
*
* @returns {Promise<string>} that resolves to the URL or rejects with an error
*/
getFileUrl(path: string, options?: import('./legacy/storage').GetFileUrlOptions): Promise<string> {
return storage.getFileUrl(this, path, options)
}

/**
* List the set of files in this application's Gaia storage bucket.
*
* @param {function} callback - a callback to invoke on each named file that
* returns `true` to continue the listing operation or `false` to end it
*
* @returns {Promise} that resolves to the number of files listed
*/
listFiles(callback: (name: string) => boolean): Promise<number> {
return storage.listFiles(this, callback)
return decryptContent(content, options)
}

/**
* Deletes the specified file from the app's data store.
* @param path - The path to the file to delete.
* @param options - Optional options object.
* @param options.wasSigned - Set to true if the file was originally signed
* in order for the corresponding signature file to also be deleted.
* @returns Resolves when the file has been removed or rejects with an error.
*/
public deleteFile(path: string, options?: { wasSigned?: boolean }) {
return storage.deleteFile(this, path, options)
}


/**
* @ignore
*/
getOrSetLocalGaiaHubConnection(): Promise<GaiaHubConfig> {
const sessionData = this.store.getSessionData()
const userData = sessionData.userData
if (!userData) {
throw new InvalidStateError('Missing userData')
}
const hubConfig = userData.gaiaHubConfig
if (hubConfig) {
return Promise.resolve(hubConfig)
}
return this.setLocalGaiaHubConnection()
}

/**
* These two functions are app-specific connections to gaia hub,
* they read the user data object for information on setting up
* a hub connection, and store the hub config to localstorage
* @private
* @returns {Promise} that resolves to the new gaia hub connection
*/
async setLocalGaiaHubConnection(): Promise<GaiaHubConfig> {
const userData = this.loadUserData()

if (!userData) {
throw new InvalidStateError('Missing userData')
}

if (!userData.hubUrl) {
userData.hubUrl = BLOCKSTACK_DEFAULT_GAIA_HUB_URL
}

const gaiaConfig = await connectToGaiaHub(
userData.hubUrl,
userData.appPrivateKey,
userData.gaiaAssociationToken)

userData.gaiaHubConfig = gaiaConfig

const sessionData = this.store.getSessionData()
sessionData.userData.gaiaHubConfig = gaiaConfig
this.store.setSessionData(sessionData)

return gaiaConfig
}
}

0 comments on commit c27d948

Please sign in to comment.