Skip to content

Commit

Permalink
feat: Decrypter and Encrypter
Browse files Browse the repository at this point in the history
  • Loading branch information
jimcase committed Dec 3, 2024
1 parent e357c4f commit 591d33e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/keri/app/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class Controller {
const signers = [];
for (const prx of prxs) {
const cipher = new Cipher({ qb64: prx });
const dsigner = decrypter.decrypt(null, cipher, true);
const dsigner = decrypter.decrypt(null, cipher, null,true);
signers.push(dsigner);
nprxs.push(encrypter.encrypt(b(dsigner.qb64)).qb64);
}
Expand Down
53 changes: 38 additions & 15 deletions src/keri/core/decrypter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import libsodium from 'libsodium-wrappers-sumo';

import { Matter, MatterArgs, MtrDex } from './matter';
import {ciXAllQB64Dex, ciXVarQB2Dex, ciXVarStrmDex, Matter, MatterArgs, MtrDex} from './matter';
import { Signer } from './signer';
import { Cipher } from './cipher';
import { EmptyMaterialError } from './kering';
import { Salter } from './salter';
import {Streamer} from "./streamer";

export class Decrypter extends Matter {
private readonly _decrypt: any;
Expand Down Expand Up @@ -47,34 +48,56 @@ export class Decrypter extends Matter {
}

decrypt(
ser: Uint8Array | null = null,
ser: Uint8Array | null = null, // qb64b
cipher: Cipher | null = null,
transferable: boolean = false
klas = null,
transferable: boolean = false,
bare: boolean = false
) {
if (ser == null && cipher == null) {
throw new EmptyMaterialError('Neither ser or cipher were provided');
}

if (ser != null) {
cipher = new Cipher({ qb64b: ser });
if (!cipher){
if (ser != null) {
cipher = new Cipher({ qb64b: ser });
} else {
throw new Error(`Need one of cipher or qb64`);
}
}

return this._decrypt(cipher, this.raw, transferable);
return this._decrypt(cipher, this.raw, klas, transferable, bare);
}

_x25519(cipher: Cipher, prikey: Uint8Array, transferable: boolean = false) {
_x25519(cipher: Cipher, prikey: Uint8Array, Klas?: typeof Matter | typeof Streamer, transferable: boolean = false, bare: boolean = false) {
const pubkey = libsodium.crypto_scalarmult_base(prikey);
const plain = libsodium.crypto_box_seal_open(
cipher.raw,
pubkey,
prikey
);
if (cipher.code == MtrDex.X25519_Cipher_Salt) {
return new Salter({ qb64b: plain });
} else if (cipher.code == MtrDex.X25519_Cipher_Seed) {
return new Signer({ qb64b: plain, transferable: transferable });

if (bare) {
return plain
} else {
throw new Error(`Unsupported cipher text code == ${cipher.code}`);
if (!Klas) {
if (cipher.code === MtrDex.X25519_Cipher_Salt){
Klas = Salter;
} else if (cipher.code === MtrDex.X25519_Cipher_Seed) {
Klas = Signer;
} else if (ciXVarStrmDex.includes(cipher.code)){
Klas = Streamer;
} else {
throw new Error(`Unsupported cipher code = ${cipher.code} when klas missing.`);
}
}

if (ciXAllQB64Dex.includes(cipher.code)) {
// @ts-ignore
return new Klas({qb64b: plain, transferable});
} else if (ciXVarStrmDex.includes(cipher.code)){
// @ts-ignore
return new Klas(plain)
} else {
throw new Error(`Unsupported cipher code = ${cipher.code}.`);
}
}
}
}
13 changes: 8 additions & 5 deletions src/keri/core/keeping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,9 @@ export class RandyKeeper implements Keeper {

this.signers = this.prxs.map((prx) =>
this.decrypter.decrypt(
new Cipher({ qb64: prx }).qb64b,
undefined,
null,
new Cipher({ qb64: prx }),
null,
this.transferable
)
);
Expand Down Expand Up @@ -567,8 +568,9 @@ export class RandyKeeper implements Keeper {

const signers = this.nxts!.map((nxt) =>
this.decrypter.decrypt(
undefined,
null,
new Cipher({ qb64: nxt }),
null,
this.transferable
)
);
Expand Down Expand Up @@ -600,8 +602,9 @@ export class RandyKeeper implements Keeper {
): Promise<SignResult> {
const signers = this.prxs!.map((prx) =>
this.decrypter.decrypt(
new Cipher({ qb64: prx }).qb64b,
undefined,
null,
new Cipher({ qb64: prx }),
null,
this.transferable
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/keri/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ class Keeper implements KeyStore {
const out = new Array<[string, Signer]>();
this._pris.forEach(function (val, pubKey) {
const verfer = new Verfer({ qb64: pubKey });
const signer = decrypter.decrypt(val, null, verfer.transferable);
const signer = decrypter.decrypt(val, null, null, verfer.transferable);
out.push([pubKey, signer]);
});
return out;
Expand All @@ -1104,7 +1104,7 @@ class Keeper implements KeyStore {
}
const verfer = new Verfer({ qb64: pubKey });

return decrypter.decrypt(val, null, verfer.transferable);
return decrypter.decrypt(val, null, null, verfer.transferable);
}

pinPths(pubKey: string, val: PubPath): boolean {
Expand Down
3 changes: 3 additions & 0 deletions test/core/decrypter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('Decrypter', () => {
let designer = decrypter.decrypt(
seedcipher.qb64b,
null,
null,
signer.verfer.transferable
);
assert.deepStrictEqual(designer.qb64b, seedqb64b);
Expand All @@ -109,6 +110,7 @@ describe('Decrypter', () => {
designer = decrypter.decrypt(
null,
seedcipher,
null,
signer.verfer.transferable
);
assert.deepStrictEqual(designer.qb64b, seedqb64b);
Expand Down Expand Up @@ -138,6 +140,7 @@ describe('Decrypter', () => {
designer = decrypter.decrypt(
b(cipherseed),
null,
null,
signer.verfer.transferable
);
assert.deepStrictEqual(designer.qb64b, seedqb64b);
Expand Down

0 comments on commit 591d33e

Please sign in to comment.