From 2e35424925147ba0482b58df8cb17f5a96ff5feb Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 30 Jan 2024 03:14:44 +0300 Subject: [PATCH] [e2e test] add manzano & habanero relayer test (#333) * add relayer test * chore: fix test * feat: add auto relayer url init --- .../0_manual-tests/test-relayer-mint.mjs | 118 ++++++++++++++++++ .../src/lib/lit-auth-client.ts | 41 ++++++ 2 files changed, 159 insertions(+) create mode 100644 e2e-nodejs/0_manual-tests/test-relayer-mint.mjs diff --git a/e2e-nodejs/0_manual-tests/test-relayer-mint.mjs b/e2e-nodejs/0_manual-tests/test-relayer-mint.mjs new file mode 100644 index 0000000000..abb34520f9 --- /dev/null +++ b/e2e-nodejs/0_manual-tests/test-relayer-mint.mjs @@ -0,0 +1,118 @@ +import path from 'path'; +import { success, fail, testThis } from '../../tools/scripts/utils.mjs'; +import LITCONFIG from '../../lit.config.json' assert { type: 'json' }; +import { LitNodeClient } from '@lit-protocol/lit-node-client'; +import { LitAuthClient } from '@lit-protocol/lit-auth-client'; +import { LocalStorage } from 'node-localstorage'; +import { ethers } from 'ethers'; +import * as siwe from 'siwe'; +import { AuthMethodScope } from '@lit-protocol/constants'; + +async function getAuthSig(wallet, litNodeClient) { + const domain = 'localhost'; + const origin = 'https://localhost/login'; + const statement = + 'This is a test statement. You can put anything you want here.'; + + const address = await wallet.getAddress(); + + const nonce = await litNodeClient.getLatestBlockhash(); + + const siweMessage = new siwe.SiweMessage({ + domain, + address, + statement, + uri: origin, + version: '1', + chainId: 1, + nonce, + expirationTime: new Date(Date.now() + 60_000 * 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, + }; + + return authSig; +} + +export async function main() { + // ==================== Setup ==================== + + if (!process.env.NETWORK) { + return fail(`NETWORK env var not set.`); + } + + if (!['cayenne', 'habanero', 'manzano'].includes(process.env.NETWORK)) { + return fail( + `Invalid NETWORK env var set. It has to be either 'manzano', 'habanero', or 'cayenne'` + ); + } + + // -- setting up lit node client + const litNodeClient = new LitNodeClient({ + litNetwork: process.env.NETWORK, + debug: true, + checkNodeAttestation: true, + storageProvider: { + provider: new LocalStorage('./storage.test.db'), + }, + }); + + await litNodeClient.connect(); + + // -- controller wallet + const wallet = new ethers.Wallet( + LITCONFIG.CONTROLLER_PRIVATE_KEY, + new ethers.providers.JsonRpcProvider(LITCONFIG.CHRONICLE_RPC) + ); + + // -- setting up lit auth client, which uses + // the lit node client so that it knows which network to use + const litAuthClient = new LitAuthClient({ + litNodeClient, + litRelayConfig: { + relayApiKey: '...', + }, + debug: true, + }); + + console.log( + 'litAuthClient.litNodeClient.config:', + litAuthClient.litNodeClient.config + ); + + // -- setting up auth provider + const authSig = await getAuthSig(wallet, litNodeClient); + + const authMethod = { + authMethodType: 1, + accessToken: JSON.stringify(authSig), + }; + + console.log('authMethod', authMethod); + + // ==================== Test Logic ==================== + let res = await litAuthClient.mintPKPWithAuthMethods([authMethod], { + pkpPermissionScopes: [[AuthMethodScope.SignAnything]], + sendPkpToitself: true, + addPkpEthAddressAsPermittedAddress: true, + }); + + if (!res) { + return fail(`Res is empty, expected a response from the relayer`); + } + + // ==================== Post-Validation ==================== + return success(`Minted PKP via the relayer with auth methods`); +} + +await testThis({ name: path.basename(import.meta.url), fn: main }); diff --git a/packages/lit-auth-client/src/lib/lit-auth-client.ts b/packages/lit-auth-client/src/lib/lit-auth-client.ts index 36e7f9d126..6d32eefaec 100644 --- a/packages/lit-auth-client/src/lib/lit-auth-client.ts +++ b/packages/lit-auth-client/src/lib/lit-auth-client.ts @@ -62,6 +62,7 @@ export class LitAuthClient { this.providers = new Map(); bootstrapLogManager('auth-client'); this.debug = options?.debug ?? false; + // Check if custom relay object is provided if (options?.customRelay) { this.relay = options?.customRelay; @@ -86,6 +87,46 @@ export class LitAuthClient { }); } + // -- choose the correct relayer based on litNodeClient + // and if litRelayConfig.relayUrl is not set + if (!options?.litRelayConfig?.relayUrl) { + if (!options?.litRelayConfig?.relayApiKey) { + throw new Error( + '2 An API key is required to use the default Lit Relay server. Please provide either an API key or a custom relay server.' + ); + } + + const supportedNetworks = ['cayenne', 'habanero', 'manzano']; + + if (!supportedNetworks.includes(this.litNodeClient.config.litNetwork)) { + throw new Error( + `Unsupported litNetwork: ${ + this.litNodeClient.config.litNetwork + }. Supported networks are: ${supportedNetworks.join(', ')}` + ); + } + + let url; + + switch (this.litNodeClient.config.litNetwork) { + case 'cayenne': + url = 'https://relayer-server-staging-cayenne.getlit.dev'; + break; + case 'habanero': + url = 'https://habanero-relayer.getlit.dev'; + break; + case 'manzano': + url = 'https://manzano-relayer.getlit.dev'; + break; + } + + if (this.litNodeClient.config.litNetwork) + this.relay = new LitRelay({ + relayUrl: url, + relayApiKey: options?.litRelayConfig?.relayApiKey, + }); + } + // Set RPC URL this.rpcUrl = options?.rpcUrl || 'https://chain-rpc.litprotocol.com/http'; log('rpc url: ', this.rpcUrl);