diff --git a/programs/beekeeper/beekeeper_wasm/__tests__/detailed/beekeeper_api.spec.ts b/programs/beekeeper/beekeeper_wasm/__tests__/detailed/beekeeper_api.spec.ts index eaadb05bb9..afea93de94 100644 --- a/programs/beekeeper/beekeeper_wasm/__tests__/detailed/beekeeper_api.spec.ts +++ b/programs/beekeeper/beekeeper_wasm/__tests__/detailed/beekeeper_api.spec.ts @@ -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(); }); diff --git a/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.node.spec.ts b/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.node.spec.ts index 8049dcc6ea..7f3aba7de1 100644 --- a/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.node.spec.ts +++ b/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.node.spec.ts @@ -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); + }); }); diff --git a/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.spec.ts b/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.spec.ts index 478c3809c3..c3550363f0 100644 --- a/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.spec.ts +++ b/programs/beekeeper/beekeeper_wasm/__tests__/detailed/factory.spec.ts @@ -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(); }); diff --git a/programs/beekeeper/beekeeper_wasm/api.md b/programs/beekeeper/beekeeper_wasm/api.md index b0f04d119d..4b5eabbd93 100644 --- a/programs/beekeeper/beekeeper_wasm/api.md +++ b/programs/beekeeper/beekeeper_wasm/api.md @@ -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 ___ @@ -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 @@ -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 ___ @@ -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 ___ @@ -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 ___ @@ -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 ___ @@ -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 ___ @@ -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 @@ -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`[] @@ -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 ___ @@ -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 @@ -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 @@ -701,7 +761,7 @@ Password used for unlocking your wallet #### Defined in -src/interfaces.ts:161 +src/interfaces.ts:187 ___ @@ -713,4 +773,4 @@ Unlocked, ready to use wallet #### Defined in -src/interfaces.ts:154 +src/interfaces.ts:180 diff --git a/programs/beekeeper/beekeeper_wasm/src/detailed/wallet.ts b/programs/beekeeper/beekeeper_wasm/src/detailed/wallet.ts index 5d40a6dfae..60e0ab38a1 100644 --- a/programs/beekeeper/beekeeper_wasm/src/detailed/wallet.ts +++ b/programs/beekeeper/beekeeper_wasm/src/detailed/wallet.ts @@ -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, @@ -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(); } diff --git a/programs/beekeeper/beekeeper_wasm/src/interfaces.ts b/programs/beekeeper/beekeeper_wasm/src/interfaces.ts index d8c097ab84..adc6647636 100644 --- a/programs/beekeeper/beekeeper_wasm/src/interfaces.ts +++ b/programs/beekeeper/beekeeper_wasm/src/interfaces.ts @@ -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 *