Skip to content

Instantly share code, notes, and snippets.

@xlewkanx
Last active March 17, 2020 09:34
Show Gist options
  • Select an option

  • Save xlewkanx/8d223c9b3003029df1a19d0f76812558 to your computer and use it in GitHub Desktop.

Select an option

Save xlewkanx/8d223c9b3003029df1a19d0f76812558 to your computer and use it in GitHub Desktop.
Migrate from v4 to e3kit-js v0.4.1
// to load private keys created with sdk v4, you can use the `KeyStorage` class from sdk v5 - https://github.com/VirgilSecurity/virgil-sdk-javascript/blob/master/src/Storage/KeyStorage.ts
// (ignore the deprecation warning)
// note, if you used the default key storage name with sdk v4 (i.e. didn't provide `keyStorageName` option to the `API` function),
// then you will have to provide a `name` config parameter equal to "VirgilSecurityKeys" into the `KeyStorage` constructor - `new KeyStorage({ name: "VirgilSecurityKeys" })`
// otherwise, provide the same value as you did for the `keyStorageName` option
// One caveat is that the `KeyStorage` from v5 sdk only uses IndexedDB as a storage backend, whereas in v4 we used the localforage (https://github.com/localForage/localForage)
// library, which aslo uses IndexedDB, but falls back to using WebSQL or localStorage if IndexedDB is not available.
// So if you can verify that all of your users have devices that support IndexedDB (https://caniuse.com/#search=IndexedDB), then you can safely use
// `KeyStorage` from v5 as is, if not, you will need to implement a custom adapter for it using localforage to mimic the functionality of v4 sdk
import { EThree } from '@virgilsecurity/e3kit';
import { KeyStorage } from 'virgil-sdk';
(async function () {
const oldKeyStorage = new KeyStorage({ name: "VirgilSecurityKeys" });
// function to get Virgil JWT from your backend
const getToken = () => fetch('/virgil-jwt').then(res => res.json());
// the useSha256Identifiers is important - the users won't be able to decrypt the previously encrypted data without it
const e3kit = await EThree.initialize(getToken, { useSha256Identifiers: true });
const privateKeyData = await oldKeyStorage.load('old_user_key');
// you need to "import" the loaded private key bytes with `e3kit.virgilCrypto.importPrivateKey` to make it usable with e3kit. This will get you back
// a PrivateKey object, which you can use to extractPublicKey and save private key to e3kit.keyEntryStorage.
const privateKey = e3kit.virgilCrypto.importPrivateKey(privateKeyData);
const publicKey = e3kit.virgilCrypto.extractPublicKey(privateKey);
await e3kit.keyEntryStorage.save({ name: e3kit.identity, value: privateKey });
// E3kit publish card under the hood of the register method.
// Also, e3kit allows only one card per one identity. Thats how we define a user.
// There is no restriction from backend side to upload multiple cards for one indentity, but we have client side checks
// which are not allow to manipulate with identities with multiple cards.
// So be sure you publish one card for one user, it is imposible to do with documented e3kit methods.
await e3kit.cardManager.publishCard({
privateKey, publicKey
});
// After card is published and old private key is moved to new storage, you can safely delete private key from old storage
await oldKeyStorage.remove('old_user_key');
// check if user registered and avalible for search thru e3kit
const publicKeys = await e3kit.lookupPublicKeys(e3kit.identity);
console.log(publicKeys)
// old v4 encrypt
/* const encryptedData = aliceKey
.signThenEncrypt(message, bobCards) */
// more documentation https://github.com/VirgilSecurity/virgil-sdk-javascript/tree/v4
// new v5 e3kit encrypt
// https://developer.virgilsecurity.com/docs/e3kit/end-to-end-encryption/default/
const encryptedText = await eThree.encrypt('this text will be encrypted', publicKeys);
// You can find more information about e3kit usage here: https://developer.virgilsecurity.com/docs/use-cases/v5/encrypted-communication
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment