2019-12-17 20:25:57 +00:00
|
|
|
/* global window */
|
|
|
|
const { ipcRenderer: ipc, remote } = require('electron');
|
|
|
|
const sharp = require('sharp');
|
|
|
|
const pify = require('pify');
|
|
|
|
const { readFile } = require('fs');
|
|
|
|
const config = require('url').parse(window.location.toString(), true).query;
|
|
|
|
const { noop, uniqBy } = require('lodash');
|
2019-12-19 23:27:02 +00:00
|
|
|
const pMap = require('p-map');
|
2020-03-31 20:03:38 +00:00
|
|
|
const { deriveStickerPackKey } = require('../ts/Crypto');
|
2019-12-17 20:25:57 +00:00
|
|
|
const { makeGetter } = require('../preload_utils');
|
|
|
|
|
|
|
|
const { dialog } = remote;
|
2020-03-19 17:57:50 +00:00
|
|
|
const { nativeTheme } = remote.require('electron');
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
window.ROOT_PATH = window.location.href.startsWith('file') ? '../../' : '/';
|
|
|
|
window.PROTO_ROOT = '../../protos';
|
|
|
|
window.getEnvironment = () => config.environment;
|
|
|
|
window.getVersion = () => config.version;
|
|
|
|
window.getGuid = require('uuid/v4');
|
2020-02-07 19:37:04 +00:00
|
|
|
window.PQueue = require('p-queue').default;
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
window.localeMessages = ipc.sendSync('locale-data');
|
|
|
|
|
|
|
|
require('../js/logging');
|
2019-12-18 15:13:36 +00:00
|
|
|
|
|
|
|
window.log.info('sticker-creator starting up...');
|
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
const Signal = require('../js/modules/signal');
|
|
|
|
|
|
|
|
window.Signal = Signal.setup({});
|
2020-04-27 20:35:14 +00:00
|
|
|
window.textsecure = require('../ts/textsecure').default;
|
2019-12-17 20:25:57 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
const { initialize: initializeWebAPI } = require('../ts/textsecure/WebAPI');
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
const WebAPI = initializeWebAPI({
|
|
|
|
url: config.serverUrl,
|
2020-04-27 20:35:14 +00:00
|
|
|
cdnUrlObject: {
|
2020-04-17 22:51:39 +00:00
|
|
|
'0': config.cdnUrl0,
|
|
|
|
'2': config.cdnUrl2,
|
|
|
|
},
|
2019-12-17 20:25:57 +00:00
|
|
|
certificateAuthority: config.certificateAuthority,
|
|
|
|
contentProxyUrl: config.contentProxyUrl,
|
|
|
|
proxyUrl: config.proxyUrl,
|
2020-01-21 21:48:05 +00:00
|
|
|
version: config.version,
|
2019-12-17 20:25:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
window.convertToWebp = async (path, width = 512, height = 512) => {
|
2019-12-19 23:27:02 +00:00
|
|
|
const imgBuffer = await pify(readFile)(path);
|
|
|
|
const sharpImg = sharp(imgBuffer);
|
|
|
|
const meta = await sharpImg.metadata();
|
|
|
|
|
|
|
|
const buffer = await sharpImg
|
2019-12-17 20:25:57 +00:00
|
|
|
.resize({
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
fit: 'contain',
|
|
|
|
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
|
|
})
|
|
|
|
.webp()
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
return {
|
|
|
|
path,
|
|
|
|
buffer,
|
|
|
|
src: `data:image/webp;base64,${buffer.toString('base64')}`,
|
2019-12-19 23:27:02 +00:00
|
|
|
meta,
|
2019-12-17 20:25:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
window.encryptAndUpload = async (
|
|
|
|
manifest,
|
|
|
|
stickers,
|
|
|
|
cover,
|
|
|
|
onProgress = noop
|
|
|
|
) => {
|
2020-03-05 21:14:58 +00:00
|
|
|
const usernameItem = await window.Signal.Data.getItemById('uuid_id');
|
|
|
|
const oldUsernameItem = await window.Signal.Data.getItemById('number_id');
|
2019-12-17 20:25:57 +00:00
|
|
|
const passwordItem = await window.Signal.Data.getItemById('password');
|
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
if (!oldUsernameItem || !passwordItem) {
|
2019-12-17 20:25:57 +00:00
|
|
|
const { message } = window.localeMessages[
|
|
|
|
'StickerCreator--Authentication--error'
|
|
|
|
];
|
|
|
|
|
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'warning',
|
|
|
|
message,
|
|
|
|
});
|
|
|
|
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { value: username } = usernameItem;
|
2020-03-05 21:14:58 +00:00
|
|
|
const { value: oldUsername } = oldUsernameItem;
|
2019-12-17 20:25:57 +00:00
|
|
|
const { value: password } = passwordItem;
|
|
|
|
|
|
|
|
const packKey = window.libsignal.crypto.getRandomBytes(32);
|
|
|
|
const encryptionKey = await deriveStickerPackKey(packKey);
|
|
|
|
const iv = window.libsignal.crypto.getRandomBytes(16);
|
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
const server = WebAPI.connect({
|
|
|
|
username: username || oldUsername,
|
|
|
|
password,
|
|
|
|
});
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
const uniqueStickers = uniqBy([...stickers, { webp: cover }], 'webp');
|
|
|
|
|
|
|
|
const manifestProto = new window.textsecure.protobuf.StickerPack();
|
|
|
|
manifestProto.title = manifest.title;
|
|
|
|
manifestProto.author = manifest.author;
|
|
|
|
manifestProto.stickers = stickers.map(({ emoji }, id) => {
|
|
|
|
const s = new window.textsecure.protobuf.StickerPack.Sticker();
|
|
|
|
s.id = id;
|
|
|
|
s.emoji = emoji;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
const coverSticker = new window.textsecure.protobuf.StickerPack.Sticker();
|
|
|
|
coverSticker.id =
|
|
|
|
uniqueStickers.length === stickers.length ? 0 : uniqueStickers.length - 1;
|
|
|
|
coverSticker.emoji = '';
|
|
|
|
manifestProto.cover = coverSticker;
|
|
|
|
|
|
|
|
const encryptedManifest = await encrypt(
|
|
|
|
manifestProto.toArrayBuffer(),
|
|
|
|
encryptionKey,
|
|
|
|
iv
|
|
|
|
);
|
2019-12-19 23:27:02 +00:00
|
|
|
const encryptedStickers = await pMap(
|
|
|
|
uniqueStickers,
|
|
|
|
({ webp }) => encrypt(webp.buffer, encryptionKey, iv),
|
|
|
|
{ concurrency: 3 }
|
2019-12-17 20:25:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const packId = await server.putStickers(
|
|
|
|
encryptedManifest,
|
|
|
|
encryptedStickers,
|
|
|
|
onProgress
|
|
|
|
);
|
|
|
|
|
|
|
|
const hexKey = window.Signal.Crypto.hexFromBytes(packKey);
|
|
|
|
|
|
|
|
ipc.send('install-sticker-pack', packId, hexKey);
|
|
|
|
|
|
|
|
return { packId, key: hexKey };
|
|
|
|
};
|
|
|
|
|
|
|
|
async function encrypt(data, key, iv) {
|
|
|
|
const { ciphertext } = await window.textsecure.crypto.encryptAttachment(
|
2020-04-27 20:35:14 +00:00
|
|
|
data instanceof ArrayBuffer
|
|
|
|
? data
|
|
|
|
: window.Signal.Crypto.typedArrayToArrayBuffer(data),
|
2019-12-17 20:25:57 +00:00
|
|
|
key,
|
|
|
|
iv
|
|
|
|
);
|
|
|
|
|
|
|
|
return ciphertext;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getThemeSetting = makeGetter('theme-setting');
|
|
|
|
|
|
|
|
async function resolveTheme() {
|
2020-04-16 00:17:58 +00:00
|
|
|
const theme = (await getThemeSetting()) || 'system';
|
2019-12-17 20:25:57 +00:00
|
|
|
if (process.platform === 'darwin' && theme === 'system') {
|
2020-03-19 17:57:50 +00:00
|
|
|
return nativeTheme.shouldUseDarkColors ? 'dark' : 'light';
|
2019-12-17 20:25:57 +00:00
|
|
|
}
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function applyTheme() {
|
|
|
|
window.document.body.classList.remove('dark-theme');
|
|
|
|
window.document.body.classList.remove('light-theme');
|
|
|
|
window.document.body.classList.add(`${await resolveTheme()}-theme`);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('DOMContentLoaded', applyTheme);
|
|
|
|
|
2020-03-19 17:57:50 +00:00
|
|
|
nativeTheme.on('updated', () => {
|
|
|
|
applyTheme();
|
|
|
|
});
|
2019-12-18 15:13:36 +00:00
|
|
|
|
|
|
|
window.log.info('sticker-creator preload complete...');
|