Skip to content

Commit

Permalink
Add/dynamic contract resolving jit test pkps (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bean authored Dec 2, 2023
1 parent 7263c7d commit f439914
Show file tree
Hide file tree
Showing 34 changed files with 785 additions and 257 deletions.
79 changes: 71 additions & 8 deletions e2e-nodejs/00-setup.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { LitNodeClient } from '@lit-protocol/lit-node-client';
import { LitNodeClient, uint8arrayFromString } from '@lit-protocol/lit-node-client';
import LITCONFIG from '../lit.config.json' assert { type: 'json' };
import { fail } from '../tools/scripts/utils.mjs';
import {LitContracts} from "@lit-protocol/contracts-sdk";
import {ethers} from "ethers";
import * as siwe from 'siwe';

// ==================== ENV Loading ====================
const network = process.env.NETWORK ?? LITCONFIG.TEST_ENV.litNetwork;
const debug = process.env.DEBUG === 'true' ?? LITCONFIG.TEST_ENV.debug;
const minNodeCount = LITCONFIG.TEST_ENV.minNodeCount;
const checkSevAttestation = process.env.CHECK_SEV ?? false;

const client = new LitNodeClient({
litNetwork: LITCONFIG.TEST_ENV.litNetwork,
debug: process.env.DEBUG === 'true' ?? LITCONFIG.TEST_ENV.debug,
minNodeCount: LITCONFIG.TEST_ENV.minNodeCount,
checkNodeAttestation: false,
litNetwork: network,
debug: debug,
minNodeCount: minNodeCount,
checkNodeAttestation: checkSevAttestation
});
await client.connect();

Expand All @@ -19,9 +28,63 @@ if (LITCONFIG.CONTROLLER_AUTHSIG === undefined) {
fail('Controller authSig cannot be empty');
}

if (LITCONFIG.PKP_PUBKEY === undefined) {
fail('PKP pubkey cannot be empty');
}
// ==================== SIWE Gen ====================
const provider = new ethers.providers.JsonRpcProvider(
LITCONFIG.CHRONICLE_RPC
);

const wallet = new ethers.Wallet(LITCONFIG.CONTROLLER_PRIVATE_KEY, provider);
const address = ethers.utils.getAddress(await wallet.getAddress());

// Craft the SIWE message
const domain = 'localhost';
const origin = 'https://localhost/login';
const statement =
'This is a test statement. You can put anything you want here.';
const siweMessage = new siwe.SiweMessage({
domain,
address: address,
statement,
uri: origin,
version: '1',
chainId: 1,
expirationTime: new Date(Date.now() + 1000 * 60).toISOString()
});
const messageToSign = siweMessage.prepareMessage();

// Sign the message and format the authSig
const signature = await wallet.signMessage(messageToSign);

const authSig = {
sig: signature,
derivedVia: 'web3.eth.personal.sign',
signedMessage: messageToSign,
address: address,
};
console.log("generated siwe for test run: ", authSig);

// ==================== Global Vars ====================
globalThis.LitCI = {};
globalThis.LitCI.network = network;
globalThis.LitCI.debug = debug;
globalThis.LitCI.sevAttestation = checkSevAttestation;
globalThis.LitCI.CONTROLLER_AUTHSIG = authSig;
globalThis.LitCI.CONTROLLER_AUTHSIG_2 = LITCONFIG.CONTROLLER_AUTHSIG_2;


globalThis.LitCI.PKP_INFO = {};
globalThis.LitCI.PKP_INFO.publicKey = LITCONFIG.PKP_PUBKEY;

let contractClient = new LitContracts({
signer: wallet,
debug: process.env.DEBUG === 'true' ?? LITCONFIG.TEST_ENV.debug,
network: process.env.NETWORK ?? LITCONFIG.TEST_ENV.litNetwork,
});
await contractClient.connect();

let res = await contractClient.pkpNftContractUtils.write.mint();
globalThis.LitCI.PKP_INFO = res.pkp;


// ==================== Success ====================
export { client };
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { client } from '../00-setup.mjs';
import { ethers } from 'ethers';

// NOTE: you need to hash data before you send it in.
// If you send something that isn't 32 bytes, the nodes will return an error.
const TO_SIGN = ethers.utils.arrayify(ethers.utils.keccak256([1, 2, 3, 4, 5]));

export async function main() {
// ==================== Test Logic ====================
let results = await Promise.all([
(async () => {
console.time("request 1");
let res = await client.executeJs({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
dataToSign: TO_SIGN,
publicKey: globalThis.LitCI.PKP_INFO.publicKey,
},
});
console.timeEnd("request 1");
return res;
})(),
(async () => {
console.time("request 2");
let res = await client.executeJs({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
dataToSign: TO_SIGN,
publicKey: globalThis.LitCI.PKP_INFO.publicKey,
},
});
console.timeEnd("request 2");
return res;
})(),
(async () => {
console.time("request 3");
let res = await client.executeJs({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
dataToSign: TO_SIGN,
publicKey: globalThis.LitCI.PKP_INFO.publicKey,
},
});
console.timeEnd("request 3");
return res;
})(),
(async () => {
console.time("request 4");
let res = await client.executeJs({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
dataToSign: TO_SIGN,
publicKey: globalThis.LitCI.PKP_INFO.publicKey,
},
});
console.timeEnd("request 4");
return res;
})(),
(async () => {
console.time("request 5");
let res = await client.executeJs({
authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
dataToSign: TO_SIGN,
publicKey: globalThis.LitCI.PKP_INFO.publicKey,
},
});
console.timeEnd("request 5");
return res;
})()
]);
// ==================== Post-Validation ====================
for (const res of results) {
if (Object.keys(res.signatures).length <= 0) {
return fail(
`should have at least 1 signature but received ${
Object.keys(res.signatures).length
}`
);
}

['sig', 'r', 's', 'recid', 'signature', 'publicKey', 'dataSigned'].forEach(
(key) => {
if (!res.signatures.sig[key]) {
return fail(`sig.${key} is undefined, empty, or null`);
}
}
);
}

// ==================== Success ====================
return success('Lit Action should log sign x1 sig in parallel');
}

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

0 comments on commit f439914

Please sign in to comment.