2020-12-15 07:15:00 +01:00
|
|
|
import _ from 'lodash';
|
2021-01-21 03:46:01 +01:00
|
|
|
import { UserUtils } from '.';
|
2021-02-15 05:16:38 +01:00
|
|
|
import { getItemById } from '../../../ts/data/data';
|
2021-01-21 03:46:01 +01:00
|
|
|
import { KeyPair } from '../../../libtextsecure/libsignal-protocol';
|
|
|
|
import { PubKey } from '../types';
|
2021-07-27 08:41:15 +02:00
|
|
|
import { fromHexToArray, toHex } from './String';
|
2021-06-24 07:10:38 +02:00
|
|
|
import { getConversationController } from '../conversations';
|
2021-11-29 07:40:46 +01:00
|
|
|
import { LokiProfile } from '../../types/Message';
|
2021-01-21 03:46:01 +01:00
|
|
|
|
2021-01-29 01:29:24 +01:00
|
|
|
export type HexKeyPair = {
|
|
|
|
pubKey: string;
|
|
|
|
privKey: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this pubkey is us, using the cache.
|
2021-02-08 06:18:36 +01:00
|
|
|
* Throws an error if our pubkey is not set
|
2021-01-29 01:29:24 +01:00
|
|
|
*/
|
|
|
|
export function isUsFromCache(pubKey: string | PubKey | undefined): boolean {
|
2021-01-21 03:46:01 +01:00
|
|
|
if (!pubKey) {
|
|
|
|
throw new Error('pubKey is not set');
|
|
|
|
}
|
2021-01-29 01:29:24 +01:00
|
|
|
const ourNumber = UserUtils.getOurPubKeyStrFromCache();
|
2021-01-21 03:46:01 +01:00
|
|
|
const pubKeyStr = pubKey instanceof PubKey ? pubKey.key : pubKey;
|
|
|
|
return pubKeyStr === ourNumber;
|
|
|
|
}
|
2020-12-15 07:15:00 +01:00
|
|
|
|
2021-01-12 06:11:05 +01:00
|
|
|
/**
|
2021-01-29 01:29:24 +01:00
|
|
|
* Returns the public key of this current device as a STRING, or throws an error
|
2021-01-12 06:11:05 +01:00
|
|
|
*/
|
2021-01-29 01:29:24 +01:00
|
|
|
export function getOurPubKeyStrFromCache(): string {
|
|
|
|
const ourNumber = window.textsecure.storage.user.getNumber();
|
|
|
|
if (!ourNumber) {
|
|
|
|
throw new Error('ourNumber is not set');
|
|
|
|
}
|
|
|
|
return ourNumber;
|
2020-06-01 04:53:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 01:29:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the public key of this current device as a PubKey, or throws an error
|
|
|
|
*/
|
|
|
|
export function getOurPubKeyFromCache(): PubKey {
|
|
|
|
const ourNumber = UserUtils.getOurPubKeyStrFromCache();
|
2021-01-21 03:46:01 +01:00
|
|
|
if (!ourNumber) {
|
2021-01-20 06:58:59 +01:00
|
|
|
throw new Error('ourNumber is not set');
|
|
|
|
}
|
|
|
|
return PubKey.cast(ourNumber);
|
|
|
|
}
|
|
|
|
|
2021-07-13 06:18:33 +02:00
|
|
|
let cachedIdentityKeyPair: KeyPair | undefined;
|
|
|
|
|
2020-12-15 07:15:00 +01:00
|
|
|
/**
|
2021-01-05 04:48:46 +01:00
|
|
|
* This return the stored x25519 identity keypair for the current logged in user
|
2020-12-15 07:15:00 +01:00
|
|
|
*/
|
2020-06-01 04:53:51 +02:00
|
|
|
export async function getIdentityKeyPair(): Promise<KeyPair | undefined> {
|
2021-07-13 06:18:33 +02:00
|
|
|
if (cachedIdentityKeyPair) {
|
|
|
|
return cachedIdentityKeyPair;
|
|
|
|
}
|
2020-06-01 04:53:51 +02:00
|
|
|
const item = await getItemById('identityKey');
|
|
|
|
|
2021-07-13 06:18:33 +02:00
|
|
|
cachedIdentityKeyPair = item?.value;
|
|
|
|
return cachedIdentityKeyPair;
|
2020-06-01 04:53:51 +02:00
|
|
|
}
|
2020-12-15 07:15:00 +01:00
|
|
|
|
|
|
|
export async function getUserED25519KeyPair(): Promise<HexKeyPair | undefined> {
|
|
|
|
// 'identityKey' keeps the ed25519KeyPair under a ed25519KeyPair field.
|
|
|
|
// it is only set if the user migrated to the ed25519 way of generating a key
|
|
|
|
const item = await getItemById('identityKey');
|
|
|
|
const ed25519KeyPair = item?.value?.ed25519KeyPair;
|
|
|
|
if (ed25519KeyPair?.publicKey && ed25519KeyPair?.privateKey) {
|
|
|
|
const pubKeyAsArray = _.map(ed25519KeyPair.publicKey, a => a);
|
|
|
|
const privKeyAsArray = _.map(ed25519KeyPair.privateKey, a => a);
|
|
|
|
return {
|
2021-01-21 03:46:01 +01:00
|
|
|
pubKey: toHex(new Uint8Array(pubKeyAsArray)),
|
|
|
|
privKey: toHex(new Uint8Array(privKeyAsArray)),
|
2020-12-15 07:15:00 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-02-23 04:22:01 +01:00
|
|
|
|
2021-03-01 04:21:29 +01:00
|
|
|
export function isSignInByLinking(): boolean {
|
|
|
|
return window.textsecure.storage.user.isSignInByLinking();
|
2021-02-23 04:22:01 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 04:21:29 +01:00
|
|
|
export function setSignInByLinking(isLinking: boolean) {
|
|
|
|
window.textsecure.storage.user.setSignInByLinking(isLinking);
|
2021-02-23 04:22:01 +01:00
|
|
|
}
|
2021-02-26 01:48:08 +01:00
|
|
|
|
2021-11-02 23:38:12 +01:00
|
|
|
export function isSignWithRecoveryPhrase(): boolean {
|
|
|
|
return window.textsecure.storage.user.isSignWithRecoveryPhrase();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setSignWithRecoveryPhrase(isLinking: boolean) {
|
|
|
|
window.textsecure.storage.user.setSignWithRecoveryPhrase(isLinking);
|
|
|
|
}
|
|
|
|
|
2021-11-29 07:40:46 +01:00
|
|
|
export function getOurProfile(): LokiProfile | undefined {
|
2021-02-26 01:48:08 +01:00
|
|
|
try {
|
|
|
|
// Secondary devices have their profile stored
|
|
|
|
// in their primary device's conversation
|
|
|
|
const ourNumber = window.storage.get('primaryDevicePubKey');
|
2021-06-24 07:10:38 +02:00
|
|
|
const ourConversation = getConversationController().get(ourNumber);
|
2021-07-27 08:41:15 +02:00
|
|
|
const ourProfileKeyHex = ourConversation.get('profileKey');
|
|
|
|
const profileKeyAsBytes = ourProfileKeyHex ? fromHexToArray(ourProfileKeyHex) : null;
|
2021-02-26 06:07:13 +01:00
|
|
|
|
2021-02-26 01:48:08 +01:00
|
|
|
const avatarPointer = ourConversation.get('avatarPointer');
|
|
|
|
const { displayName } = ourConversation.getLokiProfile();
|
2021-02-26 06:07:13 +01:00
|
|
|
return {
|
|
|
|
displayName,
|
|
|
|
avatarPointer,
|
2021-07-27 08:41:15 +02:00
|
|
|
profileKey: profileKeyAsBytes?.length ? profileKeyAsBytes : null,
|
2021-02-26 06:07:13 +01:00
|
|
|
};
|
2021-02-26 01:48:08 +01:00
|
|
|
} catch (e) {
|
2021-05-19 07:54:31 +02:00
|
|
|
window?.log?.error(`Failed to get our profile: ${e}`);
|
2021-02-26 01:48:08 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 06:14:13 +01:00
|
|
|
|
|
|
|
export function getLastProfileUpdateTimestamp(): number | undefined {
|
|
|
|
return window.textsecure.storage.user.getLastProfileUpdateTimestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setLastProfileUpdateTimestamp(lastUpdateTimestamp: number) {
|
2021-04-22 10:03:58 +02:00
|
|
|
return window.textsecure.storage.user.setLastProfileUpdateTimestamp(lastUpdateTimestamp);
|
2021-02-26 06:14:13 +01:00
|
|
|
}
|
2021-03-01 00:16:15 +01:00
|
|
|
|
|
|
|
export function getCurrentRecoveryPhrase() {
|
|
|
|
return window.textsecure.storage.get('mnemonic');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveRecoveryPhrase(mnemonic: string) {
|
|
|
|
return window.textsecure.storage.put('mnemonic', mnemonic);
|
|
|
|
}
|