Skip to content

Commit

Permalink
Introduce data encryption and decryption to the beekeeper typescript …
Browse files Browse the repository at this point in the history
…interface
  • Loading branch information
mtyszczak committed Mar 8, 2024
1 parent 9996da7 commit a2b57c5
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,44 @@ test.describe('WASM beekeeper_api tests', () => {
}, WALLET_OPTIONS);
});

test('Check `encrypt_data`, `decrypt data` endpoints', async () => {
await page.evaluate(async (args) => {
/** @type {BeekeeperInstanceHelper} */
const api = new beekeper(args);

const walletNo = 9;
const from_key_number = 8;
const to_key_number = 9;
const content = 'peach-pear-plum';

{
(api.setAcceptError as unknown as boolean) = true;

const error_message = api.encryptData(api.implicitSessionToken, keys[from_key_number][1], keys[to_key_number][1], walletNames[walletNo], content);
assert.equal(error_message.includes("Wallet not found: w9"), true);
}

(api.setAcceptError as unknown as boolean) = false;

api.unlock(api.implicitSessionToken, walletNames[walletNo]);

{
(api.setAcceptError as unknown as boolean) = true;

const error_message = api.encryptData(api.implicitSessionToken, keys[from_key_number][1], keys[to_key_number][1], walletNames[walletNo], content);
assert.equal(error_message.includes("Public key 6a34GANY5LD8deYvvfySSWGd7sPahgVNYoFPapngMUD27pWb45 not found in w9 wallet"), true);
}

(api.setAcceptError as unknown as boolean) = false;

api.importKey(api.implicitSessionToken, walletNames[walletNo], keys[from_key_number][0]);

const encrypted_content = api.encryptData(api.implicitSessionToken, keys[from_key_number][1], keys[to_key_number][1], walletNames[walletNo], content);
const decrypted_content = api.decryptData(api.implicitSessionToken, keys[from_key_number][1], keys[to_key_number][1], walletNames[walletNo], encrypted_content);
assert.equal(decrypted_content, content);
}, WALLET_OPTIONS);
});

test.afterAll(async () => {
await browser.close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,23 @@ test.describe('Beekeeper factory tests for Node.js', () => {

expect(info).toStrictEqual(['w0','w1','w2']);
});

test('Shold be able to encrypt and decrypt messages', async () => {
const input = "Big Brother is Watching You";

const beekeeper = await beekeeperFactory({ storageRoot: STORAGE_ROOT_NODE });

const session = beekeeper.createSession("my.salt");

const { wallet } = await session.createWallet('w0', 'mypassword');

const inputKey = await wallet.importKey('5KLytoW1AiGSoHHBA73x1AmgZnN16QDgU1SPpG9Vd2dpdiBgSYw');
const outputKey = await wallet.importKey('5KXNQP5feaaXpp28yRrGaFeNYZT7Vrb1PqLEyo7E3pJiG1veLKG');

const encrypted = wallet.encryptData(input, inputKey, outputKey);

const retVal = wallet.decryptData(encrypted, inputKey, outputKey);

expect(retVal).toBe(input);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ test.describe('Beekeeper factory tests', () => {
expect(info).toStrictEqual(['w0','w1','w2']);
});

test('Shold be able to encrypt and decrypt messages', async ({ page }) => {
const input = "Big Brother is Watching You";

const retVal = await page.evaluate(async (input) => {
const beekeeper = await factory();

const session = beekeeper.createSession("my.salt");

const { wallet } = await session.createWallet('w0', 'mypassword');

const inputKey = await wallet.importKey('5KLytoW1AiGSoHHBA73x1AmgZnN16QDgU1SPpG9Vd2dpdiBgSYw');
const outputKey = await wallet.importKey('5KXNQP5feaaXpp28yRrGaFeNYZT7Vrb1PqLEyo7E3pJiG1veLKG');

const encrypted = wallet.encryptData(input, inputKey, outputKey);

return wallet.decryptData(encrypted, inputKey, outputKey);
}, input);

expect(retVal).toBe(input);
});

test.afterAll(async () => {
await browser.close();
});
Expand Down
86 changes: 73 additions & 13 deletions programs/beekeeper/beekeeper_wasm/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:235
src/interfaces.ts:261

___

Expand All @@ -156,7 +156,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:242
src/interfaces.ts:268


<a name="interfacesibeekeeperoptionsmd"></a>
Expand Down Expand Up @@ -240,7 +240,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:222
src/interfaces.ts:248

___

Expand Down Expand Up @@ -269,7 +269,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:193
src/interfaces.ts:219

___

Expand All @@ -291,7 +291,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:172
src/interfaces.ts:198

___

Expand All @@ -313,7 +313,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:181
src/interfaces.ts:207

___

Expand All @@ -335,7 +335,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:213
src/interfaces.ts:239

___

Expand Down Expand Up @@ -363,7 +363,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:204
src/interfaces.ts:230


<a name="interfacesibeekeeperunlockedwalletmd"></a>
Expand Down Expand Up @@ -420,6 +420,66 @@ src/interfaces.ts:15

___

### decryptData

**decryptData**(`content`, `key`, `anotherKey?`): `string`

Decrypts given data from a specific entity and returns the decrypted message

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `content` | `string` | Base58 content to be decrypted |
| `key` | `string` | public key to find the private key in the wallet and decrypt the data |
| `anotherKey?` | `string` | other public key to find the private key in the wallet and decrypt the data (optional - use if the message was encrypted for somebody else) |

#### Returns

`string`

decrypted buffer

**`Throws`**

on any beekeeper API-related error (error parsing response, invalid input, timeout error, fs sync error etc.)

#### Defined in

src/interfaces.ts:141

___

### encryptData

**encryptData**(`content`, `key`, `anotherKey?`): `string`

Encrypts given data for a specific entity and returns the encrypted message

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `content` | `string` | Content to be encrypted |
| `key` | `string` | public key to find the private key in the wallet and encrypt the data |
| `anotherKey?` | `string` | other public key to find the private key in the wallet and encrypt the data (optional - use if the message is to encrypt for somebody else) |

#### Returns

`string`

base58 encrypted buffer

**`Throws`**

on any beekeeper API-related error (error parsing response, invalid input, timeout error, fs sync error etc.)

#### Defined in

src/interfaces.ts:128

___

### getPublicKeys

**getPublicKeys**(): `string`[]
Expand All @@ -438,7 +498,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:124
src/interfaces.ts:150

___

Expand Down Expand Up @@ -583,7 +643,7 @@ Indicates if the wallet is unlocked. If the wallet is locked, this property will

#### Defined in

src/interfaces.ts:145
src/interfaces.ts:171

## Methods

Expand Down Expand Up @@ -637,7 +697,7 @@ on any beekeeper API-related error (error parsing response, invalid input, timeo

#### Defined in

src/interfaces.ts:137
src/interfaces.ts:163


<a name="interfacesiwalletmd"></a>
Expand Down Expand Up @@ -701,7 +761,7 @@ Password used for unlocking your wallet

#### Defined in

src/interfaces.ts:161
src/interfaces.ts:187

___

Expand All @@ -713,4 +773,4 @@ Unlocked, ready to use wallet

#### Defined in

src/interfaces.ts:154
src/interfaces.ts:180
20 changes: 20 additions & 0 deletions programs/beekeeper/beekeeper_wasm/src/detailed/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ interface IBeekeeperKeys {
}>;
}

interface IEncryptData {
encrypted_content: string;
}

interface IDecryptData {
decrypted_content: string;
}

export class BeekeeperUnlockedWallet implements IBeekeeperUnlockedWallet {
public constructor(
private readonly api: BeekeeperApi,
Expand Down Expand Up @@ -60,6 +68,18 @@ export class BeekeeperUnlockedWallet implements IBeekeeperUnlockedWallet {
return result.keys.map(value => value.public_key);
}

public encryptData(content: string, key: TPublicKey, anotherKey: TPublicKey): string {
const result = this.api.extract(this.api.api.encrypt_data(this.session.token, key, anotherKey || key, this.locked.name, content)) as IEncryptData;

return result.encrypted_content;
}

public decryptData(content: string, key: TPublicKey, anotherKey?: TPublicKey): string {
const result = this.api.extract(this.api.api.decrypt_data(this.session.token, key, anotherKey || key, this.locked.name, content)) as IDecryptData;

return result.decrypted_content;
}

public close(): IBeekeeperSession {
return this.locked.close();
}
Expand Down
26 changes: 26 additions & 0 deletions programs/beekeeper/beekeeper_wasm/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ export interface IBeekeeperUnlockedWallet extends IWallet {
*/
signDigest(publicKey: TPublicKey, sigDigest: string): TSignature;

/**
* Encrypts given data for a specific entity and returns the encrypted message
*
* @param {string} content Content to be encrypted
* @param {TPublicKey} key public key to find the private key in the wallet and encrypt the data
* @param {?TPublicKey} anotherKey other public key to find the private key in the wallet and encrypt the data (optional - use if the message is to encrypt for somebody else)
*
* @returns {string} base58 encrypted buffer
*
* @throws {BeekeeperError} on any beekeeper API-related error (error parsing response, invalid input, timeout error, fs sync error etc.)
*/
encryptData(content: string, key: TPublicKey, anotherKey?: TPublicKey): string;

/**
* Decrypts given data from a specific entity and returns the decrypted message
*
* @param {string} content Base58 content to be decrypted
* @param {TPublicKey} key public key to find the private key in the wallet and decrypt the data
* @param {?TPublicKey} anotherKey other public key to find the private key in the wallet and decrypt the data (optional - use if the message was encrypted for somebody else)
*
* @returns {string} decrypted buffer
*
* @throws {BeekeeperError} on any beekeeper API-related error (error parsing response, invalid input, timeout error, fs sync error etc.)
*/
decryptData(content: string, key: TPublicKey, anotherKey?: TPublicKey): string;

/**
* Lists all of the public keys
*
Expand Down

0 comments on commit a2b57c5

Please sign in to comment.