Skip to content

Commit

Permalink
[e2e test] add manzano & habanero relayer test (#333)
Browse files Browse the repository at this point in the history
* add relayer test

* chore: fix test

* feat: add auto relayer url init
  • Loading branch information
Ansonhkg authored Jan 30, 2024
1 parent dccdaf8 commit 2e35424
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
118 changes: 118 additions & 0 deletions e2e-nodejs/0_manual-tests/test-relayer-mint.mjs
Original file line number Diff line number Diff line change
@@ -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 });
41 changes: 41 additions & 0 deletions packages/lit-auth-client/src/lib/lit-auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 2e35424

Please sign in to comment.