You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there an easier way to persist the cipher object (DKeyRatchet.AsymetricRatchet) ?
I tried serializing with toJson() and hydrating with fromJson(), but there seems to be a bug in the implementation for cipher generated from PreKeyBundleProtocol as there are some fields that is not properly hydrated.
import * as DKeyRatchet from "..";
import {Convert} from "pvtsutils";
import {Crypto} from "@peculiar/webcrypto";
import * as _ from 'lodash';
function getObjectDiff(obj1: any, obj2:any) {
const diff = Object.keys(obj1).reduce((result, key) => {
if (!obj2.hasOwnProperty(key)) {
result.push(key);
} else if (_.isEqual(obj1[key], obj2[key])) {
const resultKeyIndex = result.indexOf(key);
result.splice(resultKeyIndex, 1);
}
return result;
}, Object.keys(obj2));
return diff;
}
async function compareCiphers(cipher: DKeyRatchet.AsymmetricRatchet) {
const cipherJson = await cipher.toJSON();
const identity = cipher.identity;
const remoteIdentity = cipher.remoteIdentity;
const restoredCipher = await DKeyRatchet.AsymmetricRatchet.fromJSON(identity, remoteIdentity, cipherJson);
console.log(_.isEqual(cipher, restoredCipher));
console.log(getObjectDiff(cipher, restoredCipher));
console.log('cipher: ', cipher.currentStep);
console.log('restoredCipher: ', restoredCipher.currentStep);
}
async function main() {
const crypto = new Crypto();
DKeyRatchet.setEngine("@peculiar/webcrypto", crypto);
const time = new Date().getTime();
const aliceID = await DKeyRatchet.Identity.create(time, 1);
const alicePreKeyBundle = new DKeyRatchet.PreKeyBundleProtocol();
await alicePreKeyBundle.identity.fill(aliceID);
alicePreKeyBundle.registrationId = aliceID.id;
const preKey = aliceID.signedPreKeys[0];
alicePreKeyBundle.preKeySigned.id = 0;
alicePreKeyBundle.preKeySigned.key = preKey.publicKey;
await alicePreKeyBundle.preKeySigned.sign(aliceID.signingKey.privateKey);
// Bob sending encrypted message to Alice
const bobID = await DKeyRatchet.Identity.create(2000, 1);
const aliceBundle = await DKeyRatchet.PreKeyBundleProtocol.importProto(alicePreKeyBundle);
const bobCipher = await DKeyRatchet.AsymmetricRatchet.create(bobID, aliceBundle);
const bobEncryptedMessage = await bobCipher.encrypt(Convert.FromUtf8String('Hello Alice from Bob'));
console.log('bobCipher');
await compareCiphers(bobCipher);
}
main().catch((e) => console.error(e));
Is there an easier way to persist the cipher object (DKeyRatchet.AsymetricRatchet) ?
I tried serializing with toJson() and hydrating with fromJson(), but there seems to be a bug in the implementation for cipher generated from PreKeyBundleProtocol as there are some fields that is not properly hydrated.
The result:
The text was updated successfully, but these errors were encountered: