All files / src/auth sessionData.js

58.82% Statements 10/17
0% Branches 0/2
20% Functions 1/5
58.82% Lines 10/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86        1x                                                                             27x 27x 27x 27x 27x 27x 27x 27x       27x                                                              
/* @flow */
import type { GaiaHubConfig } from '../storage/hub'
import { InvalidStateError } from '../errors'
 
const SESSION_VERSION = '1.0.0'
 
export type SessionOptions = {
  appPrivateKey?: string,
  username?: string,
  identityAddress?: string,
  coreNode?: string,
  hubUrl?: string,
  storeOptions?: {},
  userData?: any,
  transitKey?: string,
  localStorageKey?: string,
  storeOptions?: {
    localStorageKey?: string
  }
}
 
export class SessionData {
  version: string
 
  appPrivateKey: ?string // required after sign in
 
  identityAddress: ?string // required after sign in
 
  username: ?string
 
  coreNode: ?string
 
  hubUrl: ?string // required after sign in
 
  transitKey: ?string
 
  // using this in place of
  // window.localStorage.setItem(BLOCKSTACK_STORAGE_LABEL, JSON.stringify(userData))
  userData: ?any
 
  gaiaHubConfig: ?GaiaHubConfig
 
  constructor(options: SessionOptions) {
    this.version = SESSION_VERSION
    this.appPrivateKey = options.appPrivateKey
    this.identityAddress = options.identityAddress
    this.username = options.username
    this.coreNode = options.coreNode
    this.hubUrl = options.hubUrl
    this.userData = options.userData
    this.transitKey = options.transitKey
 
    // initializing Gaia connection requires a network request
    // so we'll defer it until the first time it's needed
    this.gaiaHubConfig = null
  }
 
  getGaiaHubConfig() : ?GaiaHubConfig {
    return this.gaiaHubConfig
  }
 
  setGaiaHubConfig(config: GaiaHubConfig) : void {
    this.gaiaHubConfig = config
  }
 
  static fromJSON(json: Object) : SessionData {
    if (json.version !== SESSION_VERSION) {
      throw new InvalidStateError(`JSON data version ${json.version} not supported by SessionData`)
    }
    const options: SessionOptions = {
      appPrivateKey: json.appPrivateKey,
      identityAddress: json.identityAddress,
      username: json.username,
      coreNode: json.coreNode,
      hubUrl: json.hubUrl,
      userData: json.userData,
      transitKey: json.transitKey
    }
    return new SessionData(options)
  }
 
  toString(): string {
    return JSON.stringify(this)
  }
}