All files / src/auth sessionData.ts

61.11% Statements 11/18
0% Branches 0/4
20% Functions 1/5
61.11% Lines 11/18

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    1x     1x                                     1x                                       39x 39x 39x 39x 39x 39x 39x 39x                                                              
 
import { GaiaHubConfig } from '../storage/hub'
import { InvalidStateError } from '../errors'
import { UserData } from './authApp'
 
const SESSION_VERSION = '1.0.0'
 
export interface SessionOptions {
  appPrivateKey?: string,
  username?: string,
  identityAddress?: string,
  coreNode?: string,
  hubUrl?: string,
  userData?: UserData,
  transitKey?: string,
  localStorageKey?: string,
  storeOptions?: {
    localStorageKey?: string
  }
}
 
/**
 * @ignore
 */
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?: UserData
 
  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
  }
 
  getGaiaHubConfig(): GaiaHubConfig {
    return this.userData && this.userData.gaiaHubConfig
  }
 
  setGaiaHubConfig(config: GaiaHubConfig): void {
    this.userData.gaiaHubConfig = config
  }
 
  static fromJSON(json: any): 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)
  }
}