Skip to content

Commit

Permalink
fix and clean test using new session cache interface to sign using PK…
Browse files Browse the repository at this point in the history
…PEthersWallet
  • Loading branch information
FedericoAmura committed Feb 16, 2024
1 parent 5a95641 commit fa0d17a
Showing 1 changed file with 23 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ import { client } from '../00-setup.mjs';
import { LitAbility, LitActionResource } from '@lit-protocol/auth-helpers';
import { ethers } from 'ethers';
import { PKPEthersWallet } from '@lit-protocol/pkp-ethers';
import { uint8arrayFromString } from '@lit-protocol/uint8arrays';

// 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]));
// PKPEthersWallet::signMessage does this for you before passing it to the lit client.
const message = [1, 2, 3, 4, 5];
// const TO_SIGN = ethers.utils.arrayify(ethers.utils.keccak256(message));

export async function main() {
// ==================== Setup ====================
const sessionKeyPair = client.getSessionKey();
const resourceAbilities = [];
const resourceAbilities = [
{
resource: new LitActionResource('*'),
ability: LitAbility.LitActionExecution,
// ability: LitAbility.PKPSigning,
},
];

const authNeededCallback = async (params) => {
const response = await client.signSessionKey({
statement: params.statement,
// authSig: globalThis.LitCI.CONTROLLER_AUTHSIG, // When this is empty or undefined, it will fail
// authSig: globalThis.LitCI.CONTROLLER_AUTHSIG,
authMethods: [
{
authMethodType: 1,
Expand All @@ -38,31 +45,32 @@ export async function main() {

// ==================== Test Logic ====================

const sessionSigs = await client.getSessionSigs({
chain: 'ethereum',
expiration: new Date(Date.now() + 60_000 * 60).toISOString(),
resourceAbilityRequests: resourceAbilities,
sessionKey: sessionKeyPair,
authNeededCallback,
});
// const sessionSigs = await client.getSessionSigs({
// chain: 'ethereum',
// expiration: new Date(Date.now() + 60_000 * 60).toISOString(),
// resourceAbilityRequests: resourceAbilities,
// sessionKey: sessionKeyPair,
// authNeededCallback,
// });

console.log('sessionSigs:', sessionSigs);
// console.log('sessionSigs:', sessionSigs);

// const pkpSignRes = await client?.pkpSign({
// toSign: TO_SIGN,
// pubKey: globalThis.LitCI.AUTH_METHOD_PKP_INFO.publicKey,
// sessionSigs: sessionSigs,
// });
// console.log(`====================== ${JSON.stringify(pkpSignRes)}`);

const pkpWallet = new PKPEthersWallet({
pkpPubKey: globalThis.LitCI.PKP_INFO.publicKey,
pkpPubKey: globalThis.LitCI.AUTH_METHOD_PKP_INFO.publicKey,
rpc: LITCONFIG.CHRONICLE_RPC,
litNetwork: globalThis.LitCI.network,
authContext: {
client,
getSessionSigsProps: {
chain: 'ethereum',
// expiration: new Date(Date.now() + 60_000 * 60).toISOString(),
expiration: new Date(Date.now() + 60_000 * 60).toISOString(),
resourceAbilityRequests: resourceAbilities,
sessionKey: sessionKeyPair,
authNeededCallback,
Expand All @@ -71,32 +79,10 @@ export async function main() {
});
await pkpWallet.init();

const signature = await pkpWallet.signMessage(TO_SIGN);
const signature = await pkpWallet.signMessage(message);

// ==================== Post-Validation ====================

// if (!pkpSignRes) {
// return fail(
// 'Failed to sign data with sessionSigs generated by eth wallet auth method'
// );
// }

// let missingKeys = [];

// if (pkpSignRes) {
// ['r', 's', 'recid', 'signature', 'publicKey', 'dataSigned'].forEach(
// (key) => {
// if (pkpSignRes[key] === undefined) {
// missingKeys.push(key);
// }
// }
// );
// }

// if (missingKeys.length > 0) {
// return fail(`Missing keys: ${missingKeys.join(', ')}`);
// }

if (!signature) {
return fail('Failed to sign data with sessionSigs generated previously');
}
Expand Down

0 comments on commit fa0d17a

Please sign in to comment.