creating sealed subject in mobile app

This commit is contained in:
balzack 2022-12-12 22:15:56 -08:00
parent ee13914f6b
commit 5cf74e9793
7 changed files with 39 additions and 10 deletions

View File

@ -669,7 +669,7 @@ SPEC CHECKSUMS:
FirebaseInstallations: 99d24bac0243cf8b0e96cf5426340d211f0bcc80 FirebaseInstallations: 99d24bac0243cf8b0e96cf5426340d211f0bcc80
FirebaseMessaging: 4487bbff9b9b927ba1dd3ea40d1ceb58e4ee3cb5 FirebaseMessaging: 4487bbff9b9b927ba1dd3ea40d1ceb58e4ee3cb5
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9 fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 3d02b25ca00c2d456734d0bcff864cbc62f6ae1a glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
GoogleDataTransport: 1c8145da7117bd68bbbed00cf304edb6a24de00f GoogleDataTransport: 1c8145da7117bd68bbbed00cf304edb6a24de00f
GoogleUtilities: 1d20a6ad97ef46f67bbdec158ce00563a671ebb7 GoogleUtilities: 1d20a6ad97ef46f67bbdec158ce00563a671ebb7
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431 nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431

View File

@ -1,7 +1,6 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil'; import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function addChannel(server, token, type, subject, cards ) { export async function addChannel(server, token, type, data, cards ) {
let data = { subject };
let params = { dataType: type, data: JSON.stringify(data), groups: [], cards }; let params = { dataType: type, data: JSON.stringify(data), groups: [], cards };
let channel = await fetchWithTimeout(`https://${server}/content/channels?agent=${token}`, { method: 'POST', body: JSON.stringify(params)} ); let channel = await fetchWithTimeout(`https://${server}/content/channels?agent=${token}`, { method: 'POST', body: JSON.stringify(params)} );
checkResponse(channel); checkResponse(channel);

View File

@ -3,8 +3,9 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function getChannels(server, token, revision) { export async function getChannels(server, token, revision) {
let param = "?agent=" + token let param = "?agent=" + token
if (revision != null) { if (revision != null) {
param += '&channelRevision=' + revision param += `&channelRevision=${revision}`
} }
param += `&types=${encodeURIComponent(JSON.stringify(['superbasic']))}`;
let channels = await fetchWithTimeout(`https://${server}/content/channels${param}`, { method: 'GET' }); let channels = await fetchWithTimeout(`https://${server}/content/channels${param}`, { method: 'GET' });
checkResponse(channels) checkResponse(channels)
let ret = await channels.json() let ret = await channels.json()

View File

@ -3,11 +3,12 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function getContactChannels(server, token, viewRevision, channelRevision) { export async function getContactChannels(server, token, viewRevision, channelRevision) {
let param = "?contact=" + token let param = "?contact=" + token
if (viewRevision != null) { if (viewRevision != null) {
param += '&viewRevision=' + viewRevision param += `&viewRevision=${viewRevision}`;
} }
if (channelRevision != null) { if (channelRevision != null) {
param += '&channelRevision=' + channelRevision param += `&channelRevision=${channelRevision}`;
} }
param += `&types=${encodeURIComponent(JSON.stringify(['superbasic']))}`;
let channels = await fetchWithTimeout(`https://${server}/content/channels${param}`, { method: 'GET' }); let channels = await fetchWithTimeout(`https://${server}/content/channels${param}`, { method: 'GET' });
checkResponse(channels) checkResponse(channels)
return await channels.json() return await channels.json()

View File

@ -18,6 +18,8 @@ import { clearChannelCard } from 'api/clearChannelCard';
import { addFlag } from 'api/addFlag'; import { addFlag } from 'api/addFlag';
import { setChannelNotifications } from 'api/setChannelNotifications'; import { setChannelNotifications } from 'api/setChannelNotifications';
import { getChannelNotifications } from 'api/getChannelNotifications'; import { getChannelNotifications } from 'api/getChannelNotifications';
import { JSEncrypt } from 'jsencrypt'
import CryptoJS from "crypto-js";
export function useChannelContext() { export function useChannelContext() {
const [state, setState] = useState({ const [state, setState] = useState({
@ -277,11 +279,28 @@ export function useChannelContext() {
}, },
addBasic: async (subject, cards) => { addBasic: async (subject, cards) => {
const { server, appToken } = session.current; const { server, appToken } = session.current;
return await addChannel(server, appToken, 'superbasic', subject, cards); return await addChannel(server, appToken, 'superbasic', { subject }, cards);
}, },
addSealed: async (subject, cards) => { addSealed: async (subject, cards, keys) => {
const { server, appToken } = session.current; const { server, appToken } = session.current;
return await addChannel(server, appToken, 'sealed', subject, cards);
const key = CryptoJS.lib.WordArray.random(256 / 8);
const iv = CryptoJS.lib.WordArray.random(128 / 8);
const encrypted = CryptoJS.AES.encrypt(JSON.stringify({ subject }), key, { iv: iv });
const subjectEncrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64)
const subjectIv = iv.toString();
const keyHex = key.toString();
let seals = [];
let crypto = new JSEncrypt();
keys.forEach(publicKey => {
crypto.setPublicKey(publicKey);
const sealedKey = crypto.encrypt(keyHex);
seals.push({ publicKey, sealedKey });
});
const data = { subjectEncrypted, subjectIv, seals };
return await addChannel(server, appToken, 'sealed', data, cards);
}, },
removeTopic: async (channelId, topicId) => { removeTopic: async (channelId, topicId) => {
const { server, appToken } = session.current; const { server, appToken } = session.current;

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useRef, useContext } from 'react'; import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage"; import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'databag_v047.db'; const DATABAG_DB = 'databag_v048.db';
export function useStoreContext() { export function useStoreContext() {
const [state, setState] = useState({}); const [state, setState] = useState({});

View File

@ -242,6 +242,15 @@ export function useChannels() {
updateState({ adding: false }); updateState({ adding: false });
}, },
addTopic: async () => { addTopic: async () => {
if (state.sealed) {
let keys = [ account.state.status.seal.publicKey ];
state.contacts.forEach(contact => {
if(state.addMembers.includes(contact.cardId)) {
keys.push(contact.profile.seal);
}
});
return await channel.actions.addSealed(state.addSubject, state.addMembers, keys);
}
return await channel.actions.addBasic(state.addSubject, state.addMembers); return await channel.actions.addBasic(state.addSubject, state.addMembers);
} }
}; };