Skip to content

Commit

Permalink
fix: test case & add extra info on logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed Feb 21, 2024
1 parent 8e1a07b commit 6f0756d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ 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';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };

// 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() {
// ==================== Setup ====================
const sessionKeyPair = client.getSessionKey();
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
authMethods: [
{
authMethodType: 1,
Expand All @@ -39,68 +37,41 @@ 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,
});

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

const pkpSignRes = await client?.pkpSign({
toSign: TO_SIGN,
pubKey: globalThis.LitCI.AUTH_METHOD_PKP_INFO.publicKey,
sessionSigs: sessionSigs,
});

const pkpWallet = new PKPEthersWallet({
pkpPubKey: globalThis.LitCI.AUTH_METHOD_PKP_INFO.publicKey,
controllerSessionSigs: sessionSigs,
controllerAuthMethods: [],
rpc: LITCONFIG.CHRONICLE_RPC,
litNetwork: globalThis.LitCI.network,
authContext: {
client: client,
getSessionSigsProps: {
chain: 'ethereum',
resourceAbilityRequests: resourceAbilities,
authNeededCallback,
},
},
});

await pkpWallet.init();

const signature = await pkpWallet.signMessage(TO_SIGN);

// ==================== Post-Validation ====================
// check if the given signature is a valid signature using ethers
const isValid = ethers.utils.verifyMessage(
TO_SIGN,
signature,
pkpWallet.address
);

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) {
if (!isValid) {
return fail(
'Failed to sign data with sessionSigs generated by eth wallet auth method'
`it should use sessionSigs generated by eth wallet auth method to sign data. Signature is ${signature} and pkpWallet.address is ${pkpWallet.address}`
);
}

// ==================== Success ====================
return success(
`it should use sessionSigs generated by eth wallet auth method to sign data. Signature is ${signature} and pkpSignRes is ${JSON.stringify(
pkpSignRes
)}`
`it should use sessionSigs generated by eth wallet auth method to sign data. Signature is ${signature} and pkpWallet.address is ${pkpWallet.address} and isValid is ${isValid}`
);
}
await testThis({ name: path.basename(import.meta.url), fn: main });
14 changes: 14 additions & 0 deletions packages/pkp-base/src/lib/pkp-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ export class PKPBase<T = PKPBaseDefaultParams> {
].filter(Boolean).length;

if (providedAuthentications !== 1) {

// log which authentications has the user provided
if (this.controllerAuthSig) {
logError('controllerAuthSig is provided');
}

if (this.controllerSessionSigs) {
logError('controllerSessionSigs is provided');
}

if (this.authContext) {
logError('authContext is provided');
}

this.throwError(
'Multiple authentications are defined, can only use one at a time'
);
Expand Down

0 comments on commit 6f0756d

Please sign in to comment.