@wireapp/cryptobox
Version:
High-level API with persistent storage for Proteus.
87 lines (76 loc) • 3.11 kB
HTML
<!-- Wire, Copyright (C) 2018 Wire Swiss GmbH -->
<html>
<head>
<meta charset="UTF-8" />
<title>Demo</title>
<link rel="shortcut icon" href="data:image/x-icon;" type="image/x-icon" />
</head>
<body>
<script>
async function createStore(storeName) {
const engine = new StoreEngine.StoreEngine.IndexedDBEngine();
await engine.init(storeName);
engine.db.version(1).stores({
keys: '',
prekeys: '',
sessions: '',
});
return new cryptobox.store.CryptoboxCRUDStore(engine);
}
async function startDemo(sodium) {
window.localStorage.debug = '*';
const logger = window.logdown('Demo');
logger.log(`Testing Cryptobox v${cryptobox.Cryptobox.prototype.VERSION}`);
const alice = {
desktop: new cryptobox.Cryptobox(await createStore('alice_desktop'), 3)
};
const bob = {
desktop: new cryptobox.Cryptobox(await createStore('bob_desktop'), 1),
mobile: new cryptobox.Cryptobox(await createStore('bob_mobile'), 1)
};
alice.desktop.on(cryptobox.Cryptobox.TOPIC.NEW_PREKEYS, preKeys => {
logger.log(`New PreKeys from "alice_desktop"`, preKeys);
});
bob.desktop.on(cryptobox.Cryptobox.TOPIC.NEW_PREKEYS, preKeys => {
logger.log(`New PreKeys from "bob_desktop"`, preKeys);
});
bob.mobile.on(cryptobox.Cryptobox.TOPIC.NEW_PREKEYS, preKeys => {
logger.log(`New PreKeys from "bob_mobile"`, preKeys);
});
const messageFromBob = 'Hello Alice!';
Promise
.all([
alice.desktop.store.delete_all(),
bob.desktop.store.delete_all(),
bob.mobile.store.delete_all()
])
.then(() => Promise.all([
alice.desktop.create(),
bob.desktop.create(),
bob.mobile.create()
]))
.then(() => alice.desktop.store.load_prekey(0))
.then(prekey => {
const publicPreKeyBundle = Proteus.keys.PreKeyBundle.new(alice.desktop.identity.public_key, prekey);
return bob.desktop.encrypt('to_alice_desktop', messageFromBob, publicPreKeyBundle.serialise());
})
.then(ciphertext => alice.desktop.decrypt('to_bob_desktop', ciphertext))
.then(plaintext => {
document.write(sodium.to_string(plaintext));
return alice.desktop.store.load_prekey(2);
});
};
window.sodium = {
onload: sodium => startDemo(sodium)
};
</script>
<script src="../../dist/lib/dynamic/libsodium.js/sodium.js"></script>
<script src="../../dist/lib/dynamic/logdown/logdown.js"></script>
<script src="../../dist/lib/dynamic/dexie/dexie.js"></script>
<script src="../../../store-engine/dist/store-engine.bundle.js"></script>
<script src="../../../cbor/dist/cbor.bundle.js"></script>
<script src="../../../proteus/dist/proteus.bundle.js"></script>
<script src="../../dist/cryptobox.bundle.js"></script>
</body>
</html>