crypstie3-node/scripts/create-live-fixture.js

46 lines
1.4 KiB
JavaScript

'use strict';
const { Boss, SymmetricKey } = require('unicrypto');
const { initializeCrypto, openCrypstie } = require('../src');
async function main() {
await initializeCrypto();
const key = new SymmetricKey();
const text = `Crypstie Node compatibility fixture ${new Date().toISOString()}`;
const record = {
guid: '',
encryptedData: key.etaEncryptSync(new TextEncoder().encode(text)),
deleteAt: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000),
burnOnShow: false,
showWide: false,
syntaxHighlighting: null,
editable: false,
ownerHandle: null,
};
const form = new FormData();
form.append('data', new Blob([Boss.dump(record)]), 'crypstie.boss');
const response = await fetch('https://crypstie.com/api/crypstie', {
method: 'POST',
body: form,
});
if (!response.ok) throw new Error(`create failed: HTTP ${response.status}`);
const id = await response.text();
const encodedKey = Buffer.from(key.pack())
.toString('base64')
.replaceAll('+', '.')
.replaceAll('/', '_')
.replace(/=+$/, '');
const url = `https://crypstie.com/_${id}#${encodedKey}`;
const opened = await openCrypstie(url);
if (opened.text !== text) throw new Error('live round trip returned different plaintext');
console.log(url);
console.log(opened.text);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});