Skip to content

Commit 0a9c772

Browse files
committed
feat: add health check tests and update environment configuration
1 parent bd8971d commit 0a9c772

File tree

5 files changed

+1168
-11
lines changed

5 files changed

+1168
-11
lines changed

.env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ TESTNET_MANAGER_URL=http://0.0.0.0:8000
1919
USE_LIT_BINARIES=true
2020
LIT_NODE_BINARY_PATH=/path/to/lit_node/binary
2121
LIT_ACTION_BINARY_PATH=/path/to/lit_action_binary
22+
23+
# ---------- For Health check ----------
24+
LIT_STATUS_BACKEND_URL=CHANGE_THIS
25+
LIT_STATUS_WRITE_KEY=CHANGE_THIS
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { getEoaSessionSigs } from "local-tests/setup/session-sigs/get-eoa-session-sigs";
2+
import { getPkpSessionSigs } from "local-tests/setup/session-sigs/get-pkp-session-sigs";
3+
import { TinnyEnvironment } from "local-tests/setup/tinny-environment"
4+
import { TinnyPerson } from "local-tests/setup/tinny-person";
5+
import { LIT_ABILITY } from '@lit-protocol/constants';
6+
import { ILitNodeClient } from '@lit-protocol/types';
7+
import { AccessControlConditions } from 'local-tests/setup/accs/accs';
8+
import { LitAccessControlConditionResource } from '@lit-protocol/auth-helpers';
9+
import { encryptString, decryptToString } from '@lit-protocol/encryption';
10+
11+
export class DatilHealthManager {
12+
13+
env: TinnyEnvironment;
14+
alice: TinnyPerson;
15+
eoaSessionSigs: any;
16+
17+
constructor(){
18+
this.env = new TinnyEnvironment();
19+
}
20+
21+
async init(){
22+
await this.env.init();
23+
}
24+
25+
// ========== Person Creation ==========
26+
// create a person
27+
// this action contains chain & rpc interactions
28+
// best to cache it, but for the time being, we will create a new person for each test, since we are only running this test
29+
// once in every 30 minutes.
30+
async initPerson(){
31+
this.alice = await this.env.createNewPerson("Alice");
32+
this.eoaSessionSigs = await getEoaSessionSigs(this.env, this.alice);
33+
}
34+
35+
validatePrerequisites(){
36+
if(!this.alice){
37+
throw new Error("❌ Person not initialized");
38+
}
39+
if(!this.eoaSessionSigs){
40+
throw new Error("❌ EOA Session Sigs not initialized");
41+
}
42+
}
43+
44+
45+
// ========== Endpoint Tests ==========
46+
handshakeTest = async () => {
47+
try{
48+
await this.env.setupLitNodeClient();
49+
}catch(e){
50+
console.error("❌ Failed to setup Lit Node Client");
51+
throw e;
52+
}
53+
}
54+
55+
pkpSignTest = async () => {
56+
this.validatePrerequisites();
57+
try{
58+
await this.env.litNodeClient.pkpSign({
59+
toSign: this.alice.loveLetter,
60+
pubKey: this.alice.pkp.publicKey,
61+
sessionSigs: this.eoaSessionSigs,
62+
})
63+
}catch(e){
64+
console.error("❌ Failed to run pkpSign");
65+
throw e;
66+
}
67+
}
68+
69+
signSessionKeyTest = async () => {
70+
this.validatePrerequisites();
71+
try{
72+
await getPkpSessionSigs(this.env, this.alice);
73+
}catch(e){
74+
console.error("❌ Failed to run signSessionKey");
75+
throw e;
76+
}
77+
}
78+
79+
executeJsTest = async () => {
80+
this.validatePrerequisites();
81+
try{
82+
await this.env.litNodeClient.executeJs({
83+
sessionSigs: this.eoaSessionSigs,
84+
code: `(async () => {
85+
const sigShare = await LitActions.signEcdsa({
86+
toSign: dataToSign,
87+
publicKey,
88+
sigName: "sig",
89+
});
90+
})();`,
91+
jsParams: {
92+
dataToSign: this.alice.loveLetter,
93+
publicKey: this.alice.pkp.publicKey,
94+
}
95+
})
96+
}catch(e){
97+
console.error("❌ Failed to run executeJs");
98+
throw e;
99+
}
100+
}
101+
102+
decryptTest = async () => {
103+
this.validatePrerequisites();
104+
try{
105+
// Set access control conditions for encrypting and decrypting
106+
const accs = AccessControlConditions.getEmvBasicAccessControlConditions({
107+
userAddress: this.alice.wallet.address,
108+
});
109+
110+
// First encrypt some test data
111+
const encryptRes = await encryptString(
112+
{
113+
accessControlConditions: accs,
114+
dataToEncrypt: 'Hello world',
115+
},
116+
this.env.litNodeClient as unknown as ILitNodeClient
117+
);
118+
119+
if (!encryptRes.ciphertext) {
120+
throw new Error(`Expected "ciphertext" in encryptRes`);
121+
}
122+
123+
if (!encryptRes.dataToEncryptHash) {
124+
throw new Error(`Expected "dataToEncryptHash" in encryptRes`);
125+
}
126+
127+
// Generate resource string for the encrypted data
128+
const accsResourceString =
129+
await LitAccessControlConditionResource.generateResourceString(
130+
accs,
131+
encryptRes.dataToEncryptHash
132+
);
133+
134+
// Get session sigs with decryption capability
135+
const eoaSessionSigs = await getEoaSessionSigs(this.env, this.alice, [
136+
{
137+
resource: new LitAccessControlConditionResource(accsResourceString),
138+
ability: LIT_ABILITY.AccessControlConditionDecryption,
139+
},
140+
]);
141+
142+
// Decrypt the encrypted string
143+
const decryptRes = await decryptToString(
144+
{
145+
accessControlConditions: accs,
146+
ciphertext: encryptRes.ciphertext,
147+
dataToEncryptHash: encryptRes.dataToEncryptHash,
148+
sessionSigs: eoaSessionSigs,
149+
chain: 'ethereum',
150+
},
151+
this.env.litNodeClient as unknown as ILitNodeClient
152+
);
153+
154+
if (decryptRes !== 'Hello world') {
155+
throw new Error(
156+
`Expected decryptRes to be 'Hello world' but got ${decryptRes}`
157+
);
158+
}
159+
}catch(e){
160+
console.error("❌ Failed to run decrypt");
161+
throw e;
162+
}
163+
}
164+
}

local-tests/health/index.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { createLitStatusClient } from '@lit-protocol/lit-status-sdk';
2+
import { DatilHealthManager } from './DatilHealthManager';
3+
4+
// Configuration
5+
const NETWORK = process.env.NETWORK!;
6+
const PRODUCT = 'js-sdk/datil';
7+
8+
async function runHealthCheck(){
9+
10+
if(!NETWORK){
11+
throw new Error("❌ NETWORK is not set");
12+
}
13+
14+
const statusClient = createLitStatusClient({
15+
url: process.env.LIT_STATUS_BACKEND_URL,
16+
apiKey: process.env.LIT_STATUS_WRITE_KEY
17+
});
18+
19+
const txs = await statusClient.getOrRegisterFunctions({
20+
network: NETWORK,
21+
product: PRODUCT,
22+
functions: [
23+
'handshake',
24+
'pkpSign',
25+
'signSessionKey',
26+
'executeJs',
27+
'decrypt'
28+
] as const
29+
});
30+
31+
const healthManager = new DatilHealthManager();
32+
await healthManager.init();
33+
34+
// (test) /web/handshake
35+
console.log("🔄 Running handshake test");
36+
await statusClient.executeAndLog(txs.handshake.id, healthManager.handshakeTest);
37+
38+
// after handshake, we can create a person to test
39+
await healthManager.initPerson();
40+
41+
// (test) /web/pkp/sign
42+
console.log("🔄 Running pkpSign test");
43+
await statusClient.executeAndLog(txs.pkpSign.id, healthManager.pkpSignTest);
44+
45+
// (test) /web/sign_session_key
46+
console.log("🔄 Running signSessionKey test");
47+
await statusClient.executeAndLog(txs.signSessionKey.id, healthManager.signSessionKeyTest);
48+
49+
// (test) /web/execute
50+
console.log("🔄 Running executeJs test");
51+
await statusClient.executeAndLog(txs.executeJs.id, healthManager.executeJsTest);
52+
53+
// (test) /web/encryption/sign
54+
console.log("🔄 Running decryptTest test");
55+
await statusClient.executeAndLog(txs.decrypt.id, healthManager.decryptTest);
56+
57+
58+
}
59+
60+
61+
(async () => {
62+
try{
63+
await runHealthCheck();
64+
} catch (error) {
65+
console.error(error);
66+
} finally {
67+
process.exit();
68+
}
69+
})();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"test:unit": "nx run-many --target=test",
2121
"test:unit:watch": "nx run-many --target=test --watch",
2222
"test:unit:bun": "bun ./tools/scripts/unit-test-with-bun.mjs",
23-
"test:health": "node ./local-tests/build.mjs && dotenvx run --env-file=.env -- node ./local-tests/build/health/index.mjs",
23+
"test:health": "DEBUG=false node ./local-tests/build.mjs && dotenvx run --env-file=.env -- node ./local-tests/build/health/index.mjs",
2424
"publish:packages": "yarn node ./tools/scripts/pub.mjs --prod",
2525
"publish:beta": "yarn node ./tools/scripts/pub.mjs --tag beta",
2626
"publish:staging": "yarn node ./tools/scripts/pub.mjs --tag staging",
@@ -43,7 +43,7 @@
4343
"@dotenvx/dotenvx": "^1.6.4",
4444
"@lit-protocol/accs-schemas": "^0.0.31",
4545
"@lit-protocol/contracts": "^0.0.74",
46-
"@lit-protocol/lit-status-sdk": "^0.1.0",
46+
"@lit-protocol/lit-status-sdk": "^0.1.2",
4747
"@metamask/eth-sig-util": "5.0.2",
4848
"@mysten/sui.js": "^0.37.1",
4949
"@openagenda/verror": "^3.1.4",

0 commit comments

Comments
 (0)