feat: add snjs package

This commit is contained in:
Karol Sójko
2022-07-06 14:04:18 +02:00
parent 321a055bae
commit 0e40469e2f
296 changed files with 46109 additions and 187 deletions

View File

@@ -0,0 +1,48 @@
import { isNullOrUndefined } from '@standardnotes/utils'
import { isEnvironmentMobile } from '@Lib/Application/Platforms'
import { PreviousSnjsVersion1_0_0 } from '../../../Version'
import { isMobileDevice, LegacyKeys1_0_0 } from '@standardnotes/services'
import { StorageReader } from '../Reader'
export class StorageReader1_0_0 extends StorageReader {
static override version() {
return PreviousSnjsVersion1_0_0
}
public async getAccountKeyParams() {
return this.deviceInterface.getJsonParsedRawStorageValue(LegacyKeys1_0_0.AllAccountKeyParamsKey)
}
/**
* In 1.0.0, web uses raw storage for unwrapped account key, and mobile uses
* the keychain
*/
public async hasNonWrappedAccountKeys() {
if (isMobileDevice(this.deviceInterface)) {
const value = await this.deviceInterface.getRawKeychainValue()
return !isNullOrUndefined(value)
} else {
const value = await this.deviceInterface.getRawStorageValue('mk')
return !isNullOrUndefined(value)
}
}
public async hasPasscode() {
if (isEnvironmentMobile(this.environment)) {
const rawPasscodeParams = await this.deviceInterface.getJsonParsedRawStorageValue(
LegacyKeys1_0_0.MobilePasscodeParamsKey,
)
return !isNullOrUndefined(rawPasscodeParams)
} else {
const encryptedStorage = await this.deviceInterface.getJsonParsedRawStorageValue(
LegacyKeys1_0_0.WebEncryptedStorageKey,
)
return !isNullOrUndefined(encryptedStorage)
}
}
/** Keychain was not used on desktop/web in 1.0.0 */
public usesKeychain() {
return isEnvironmentMobile(this.environment) ? true : false
}
}

View File

@@ -0,0 +1,46 @@
import { isNullOrUndefined } from '@standardnotes/utils'
import { RawStorageKey, StorageKey, namespacedKey, ValueModesKeys } from '@standardnotes/services'
import { StorageReader } from '../Reader'
import { PreviousSnjsVersion2_0_0 } from '@Lib/Version'
export class StorageReader2_0_0 extends StorageReader {
static override version() {
return PreviousSnjsVersion2_0_0
}
private async getStorage() {
const storageKey = namespacedKey(this.identifier, RawStorageKey.StorageObject)
const storage = await this.deviceInterface.getRawStorageValue(storageKey)
const values = storage ? JSON.parse(storage) : undefined
return values
}
private async getNonWrappedValue(key: string) {
const values = await this.getStorage()
if (!values) {
return undefined
}
return values[ValueModesKeys.Nonwrapped]?.[key]
}
/**
* In 2.0.0+, account key params are stored in NonWrapped storage
*/
public async getAccountKeyParams() {
return this.getNonWrappedValue(StorageKey.RootKeyParams)
}
public async hasNonWrappedAccountKeys() {
const value = await this.deviceInterface.getNamespacedKeychainValue(this.identifier)
return !isNullOrUndefined(value)
}
public async hasPasscode() {
const wrappedRootKey = await this.getNonWrappedValue(StorageKey.WrappedRootKey)
return !isNullOrUndefined(wrappedRootKey)
}
public usesKeychain() {
return true
}
}

View File

@@ -0,0 +1,2 @@
export { StorageReader2_0_0 } from './Reader2_0_0'
export { StorageReader1_0_0 } from './Reader1_0_0'