Fix medium group sending for single device users; protocol changes
parent
9be0dcabd9
commit
237bd84b35
@ -0,0 +1,85 @@
|
||||
import { PubKey } from '../types';
|
||||
import { onGroupReceived } from '../../receiver/receiver';
|
||||
import { StringUtils } from '../utils';
|
||||
import * as Data from '../../../js/modules/data';
|
||||
|
||||
import {
|
||||
createSenderKeyForGroup,
|
||||
RatchetState,
|
||||
saveSenderKeys,
|
||||
saveSenderKeysInner,
|
||||
} from './senderKeys';
|
||||
import { getChainKey } from './ratchet';
|
||||
|
||||
export {
|
||||
createSenderKeyForGroup,
|
||||
saveSenderKeys,
|
||||
saveSenderKeysInner,
|
||||
getChainKey,
|
||||
};
|
||||
|
||||
async function createSenderKeysForMembers(
|
||||
groupId: string,
|
||||
members: Array<string>
|
||||
): Promise<Array<RatchetState>> {
|
||||
// TODO: generate for secondary devices too
|
||||
return Promise.all(
|
||||
members.map(async pk => {
|
||||
return createSenderKeyForGroup(groupId, PubKey.cast(pk));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function createMediumSizeGroup(
|
||||
groupName: string,
|
||||
members: Array<string>
|
||||
) {
|
||||
const { ConversationController, libsignal } = window;
|
||||
|
||||
// Create Group Identity
|
||||
const identityKeys = await libsignal.KeyHelper.generateIdentityKeyPair();
|
||||
const groupId = StringUtils.decode(identityKeys.pubKey, 'hex');
|
||||
|
||||
const groupSecretKeyHex = StringUtils.decode(identityKeys.privKey, 'hex');
|
||||
|
||||
const primary = window.storage.get('primaryDevicePubKey');
|
||||
|
||||
const allMembers = [primary, ...members];
|
||||
|
||||
const senderKeys = await createSenderKeysForMembers(groupId, allMembers);
|
||||
|
||||
// TODO: make this strongly typed!
|
||||
await Data.createOrUpdateIdentityKey({
|
||||
id: groupId,
|
||||
secretKey: groupSecretKeyHex,
|
||||
});
|
||||
|
||||
const groupDetails = {
|
||||
id: groupId,
|
||||
name: groupName,
|
||||
members: allMembers,
|
||||
recipients: allMembers,
|
||||
active: true,
|
||||
expireTimer: 0,
|
||||
avatar: '',
|
||||
secretKey: new Uint8Array(identityKeys.privKey),
|
||||
senderKeys,
|
||||
is_medium_group: true,
|
||||
};
|
||||
|
||||
await onGroupReceived(groupDetails);
|
||||
|
||||
const convo = await ConversationController.getOrCreateAndWait(
|
||||
groupId,
|
||||
'group'
|
||||
);
|
||||
|
||||
convo.updateGroupAdmins([primary]);
|
||||
|
||||
convo.updateGroup(groupDetails);
|
||||
|
||||
window.owsDesktopApp.appView.openConversation(groupId, {});
|
||||
|
||||
// Subscribe to this group id
|
||||
window.SwarmPolling.addGroupId(new PubKey(groupId));
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
import { PubKey } from '../types';
|
||||
import { StringUtils } from '../utils';
|
||||
import * as Data from '../../../js/modules/data';
|
||||
|
||||
const toHex = (buffer: ArrayBuffer) => StringUtils.decode(buffer, 'hex');
|
||||
const fromHex = (hex: string) => StringUtils.encode(hex, 'hex');
|
||||
|
||||
export interface RatchetState {
|
||||
chainKey: Uint8Array;
|
||||
keyIdx: number;
|
||||
pubKey: Uint8Array;
|
||||
}
|
||||
|
||||
// TODO: make this private when no longer needed by JS
|
||||
export async function saveSenderKeysInner(
|
||||
groupId: string,
|
||||
senderIdentity: PubKey,
|
||||
chainKeyHex: string,
|
||||
keyIdx: number,
|
||||
messageKeys: any
|
||||
) {
|
||||
const { log } = window;
|
||||
|
||||
const ratchet = {
|
||||
chainKeyHex,
|
||||
messageKeys,
|
||||
idx: keyIdx,
|
||||
};
|
||||
|
||||
log.debug(
|
||||
'saving ratchet keys for group ',
|
||||
groupId,
|
||||
'sender',
|
||||
senderIdentity.key
|
||||
);
|
||||
|
||||
await Data.createOrUpdateSenderKeys({
|
||||
groupId,
|
||||
senderIdentity: senderIdentity.key,
|
||||
ratchet,
|
||||
});
|
||||
|
||||
log.debug(
|
||||
`Saving sender keys for groupId ${groupId}, sender ${senderIdentity.key}`
|
||||
);
|
||||
}
|
||||
|
||||
export async function createSenderKeyForGroup(
|
||||
groupId: string,
|
||||
senderIdentity: PubKey
|
||||
): Promise<RatchetState> {
|
||||
// Generate Chain Key (32 random bytes)
|
||||
const rootChainKey = await window.libsignal.crypto.getRandomBytes(32);
|
||||
const rootChainKeyHex = toHex(rootChainKey);
|
||||
|
||||
const keyIdx = 0;
|
||||
const messageKeys = {};
|
||||
|
||||
await saveSenderKeysInner(
|
||||
groupId,
|
||||
senderIdentity,
|
||||
rootChainKeyHex,
|
||||
keyIdx,
|
||||
messageKeys
|
||||
);
|
||||
|
||||
const pubKey = new Uint8Array(fromHex(senderIdentity.key));
|
||||
const chainKey = new Uint8Array(rootChainKey);
|
||||
|
||||
return { pubKey, chainKey, keyIdx: 0 };
|
||||
}
|
||||
|
||||
// Save somebody else's key
|
||||
export async function saveSenderKeys(
|
||||
groupId: string,
|
||||
senderIdentity: PubKey,
|
||||
chainKeyHex: string,
|
||||
keyIdx: number
|
||||
) {
|
||||
const messageKeys = {};
|
||||
await saveSenderKeysInner(
|
||||
groupId,
|
||||
senderIdentity,
|
||||
chainKeyHex,
|
||||
keyIdx,
|
||||
messageKeys
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue