import CryptoJS from "crypto-js" ; import _ from "lodash" ; import { AddressType , NetworkId } from "../model/address" ; const localstorageKeyOfPlatform = "platformKeys" ; const localstorageKeyOfAsset = "assetKeys" ; const networkIdKey = "netowrkId" ; const passphraseKey = "authKey" ; export interface StoredKey { pathIndex : number ; type : AddressType ; key : string ; } function getStorageKeyOfPlatform ( networkId ) { return `${ localstorageKeyOfPlatform }_${ networkId }` ; } function getStorageKeyOfAsset ( networkId ) { return `${ localstorageKeyOfAsset }_${ networkId }` ; } export function getPlatformKeys ( networkId ) { const platformKeysString = localStorage . getItem ( getStorageKeyOfPlatform ( networkId ) ) ; if ( platformKeysString ) { try { return JSON . parse ( platformKeysString ) ; } catch ( e ) { console . log ( e ) ; } } return null ; } export function getAssetKeys ( networkId ) { const assetKeysString = localStorage . getItem ( getStorageKeyOfAsset ( networkId ) ) ; if ( assetKeysString ) { try { return JSON . parse ( assetKeysString ) ; } catch ( e ) { console . log ( e ) ; } } return null ; } export function clearWalletKeys ( ) { _ . each ( [ "cc" , "tc" , "wc" , "sc" ] , network => { localStorage . removeItem ( getStorageKeyOfAsset ( network as NetworkId ) ) ; localStorage . removeItem ( getStorageKeyOfPlatform ( network as NetworkId ) ) ; } ) ; } export function clearPassphrase ( ) { sessionStorage . removeItem ( passphraseKey ) ; } export function saveNetworkId ( netowrkId ) { sessionStorage . setItem ( networkIdKey , netowrkId ) ; } export function getNetworkId ( ) { return sessionStorage . getItem ( networkIdKey ) as NetworkId ; } export function savePlatformKeys ( platformKeys , networkId ) { localStorage . setItem ( getStorageKeyOfPlatform ( networkId ) , JSON . stringify ( platformKeys ) ) ; } export function saveAssetKeys ( assetKeys , netowrkId ) { localStorage . setItem ( getStorageKeyOfAsset ( netowrkId ) , JSON . stringify ( assetKeys ) ) ; } const cryptoKey = "secret key" ; export function savePassphrase ( passphrase ) { const encryptedKey = CryptoJS . AES . encrypt ( passphrase , cryptoKey ) . toString ( ) ; sessionStorage . setItem ( passphraseKey , encryptedKey ) ; } export function getPassphrase ( ) { const encryptedKey = sessionStorage . getItem ( passphraseKey ) ; if ( encryptedKey ) { return CryptoJS . AES . decrypt ( encryptedKey , cryptoKey ) . toString ( CryptoJS . enc . Utf8 ) ; } return null ; }