-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(encryption): LIT-2841 - Implement e2e tests for encrypting and d…
…ecrypting from JSON strings including metadata
- Loading branch information
1 parent
0c3bac2
commit 93cdeb3
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
e2e-nodejs/group-pkp-encryption-decryption/test-pkp-encryption-decryption-json-file.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
50 changes: 50 additions & 0 deletions
50
e2e-nodejs/group-pkp-encryption-decryption/test-pkp-encryption-decryption-json-string.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |