Type encrypted data

This commit is contained in:
RMidhunSuresh 2021-12-02 11:57:20 +05:30
parent b2efcb9515
commit d2c09933c7

View file

@ -17,6 +17,12 @@ import type {Key} from "./common";
import type {Platform} from "../../platform/web/Platform.js"; import type {Platform} from "../../platform/web/Platform.js";
import type {Transaction} from "../storage/idb/Transaction"; import type {Transaction} from "../storage/idb/Transaction";
type EncryptedData = {
iv: string;
ciphertext: string;
mac: string;
}
export class SecretStorage { export class SecretStorage {
private readonly _key: Key; private readonly _key: Key;
private readonly _platform: Platform; private readonly _platform: Platform;
@ -31,7 +37,7 @@ export class SecretStorage {
if (!accountData) { if (!accountData) {
return; return;
} }
const encryptedData = accountData?.content?.encrypted?.[this._key.id]; const encryptedData = accountData?.content?.encrypted?.[this._key.id] as EncryptedData;
if (!encryptedData) { if (!encryptedData) {
throw new Error(`Secret ${accountData.type} is not encrypted for key ${this._key.id}`); throw new Error(`Secret ${accountData.type} is not encrypted for key ${this._key.id}`);
} }
@ -43,7 +49,7 @@ export class SecretStorage {
} }
} }
async _decryptAESSecret(type: string, encryptedData: any): Promise<string> { async _decryptAESSecret(type: string, encryptedData: EncryptedData): Promise<string> {
const {base64, utf8} = this._platform.encoding; const {base64, utf8} = this._platform.encoding;
// now derive the aes and mac key from the 4s key // now derive the aes and mac key from the 4s key
const hkdfKey = await this._platform.crypto.derive.hkdf( const hkdfKey = await this._platform.crypto.derive.hkdf(