Skip to content

Commit

Permalink
test(encryption): LIT-2841 - Implement e2e tests for encrypting and d…
Browse files Browse the repository at this point in the history
…ecrypting from JSON strings including metadata
  • Loading branch information
MaximusHaximus committed Apr 18, 2024
1 parent 0c3bac2 commit 93cdeb3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from 'path';
import * as LitJsSdk from '@lit-protocol/lit-node-client';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import { client } from '../00-setup.mjs';

export async function main() {
// ==================== Setup ====================
const chain = 'ethereum';
const accessControlConditions = [
{
contractAddress: '',
standardContractType: '',
chain,
method: 'eth_getBalance',
parameters: [':userAddress', 'latest'],
returnValueTest: {
comparator: '>=',
value: '0',
},
},
];
const message = 'Hello world';
const blob = new Blob([message], { type: 'text/plain' });
const blobArray = new Uint8Array(await blob.arrayBuffer());

// ==================== Test Logic ====================
const encryptedJsonStr = await LitJsSdk.encryptToJson({
accessControlConditions,
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
chain,
file: blob,
litNodeClient: client,
});

const decryptedFile = await LitJsSdk.decryptFromJson({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
parsedJsonData: JSON.parse(encryptedJsonStr),
litNodeClient: client,
});

// ==================== Post-Validation ====================
if (blobArray.length !== decryptedFile.length) {
return fail(
`decrypted file should match the original file but received ${decryptedFile}`
);
}
for (let i = 0; i < blobArray.length; i++) {
if (blobArray[i] !== decryptedFile[i]) {
return fail(`decrypted file should match the original file`);
}
}

// ==================== Success ====================
return success('File was encrypted and then decrypted successfully');
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import path from 'path';
import * as LitJsSdk from '@lit-protocol/lit-node-client';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import { client } from '../00-setup.mjs';

export async function main() {
// ==================== Setup ====================
const chain = 'ethereum';
const accessControlConditions = [
{
contractAddress: '',
standardContractType: '',
chain,
method: 'eth_getBalance',
parameters: [':userAddress', 'latest'],
returnValueTest: {
comparator: '>=',
value: '0',
},
},
];
const message = 'Hello world';

// ==================== Test Logic ====================
const encryptedJsonStr = await LitJsSdk.encryptToJson({
accessControlConditions,
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
chain,
string: message,
litNodeClient: client,
});

const decryptedMessage = await LitJsSdk.decryptFromJson({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
parsedJsonData: JSON.parse(encryptedJsonStr),
litNodeClient: client,
});

// ==================== Post-Validation ====================
if (message !== decryptedMessage) {
return fail(
`decryptedMessage should be ${message} but received ${decryptedMessage}`
);
}

// ==================== Success ====================
return success('Message was encrypted and then decrypted successfully');
}

await testThis({ name: path.basename(import.meta.url), fn: main });

0 comments on commit 93cdeb3

Please sign in to comment.