Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lit 1799 fix req data mapping in js sdk #244

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e-nodejs/group-lit-actions/test-lit-action-1-sig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function main() {
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
toSign: TO_SIGN,
publicKey: LITCONFIG.PKP_PUBKEY,
Expand Down
1 change: 1 addition & 0 deletions e2e-nodejs/group-lit-actions/test-lit-action-2-sigs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function main() {
const sigShares = await signMultipleSigs(numberOfSigs, toSign, publicKey);

})();`,
authMethods: [],
jsParams: {
numberOfSigs: 2,
toSign: TO_SIGN,
Expand Down
1 change: 1 addition & 0 deletions e2e-nodejs/group-lit-actions/test-lit-action-claim-key.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function main() {
code: `(async () => {
Lit.Actions.claimKey({keyId: "foo"});
})();`,
authMethods: [],
jsParams: {
// Nada!
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 { LitAbility, LitActionResource } from '@lit-protocol/auth-helpers';
import { LitAuthClient } from '@lit-protocol/lit-auth-client';
import { AuthMethodType, ProviderType } from '@lit-protocol/constants';
import { ethers } from 'ethers';
import { PKPEthersWallet } from '@lit-protocol/pkp-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 = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode("meow")));

export async function main() {
// ==================== Setup ====================

const pkpSignRes = await client?.executeJs({
authSig: LITCONFIG.CONTROLLER_AUTHSIG,
authMethods: [],
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign,
publicKey,
sigName: "sig",
});
LitActions.setResponse({response: JSON.stringify(Lit.Auth)});
})();`,
jsParams: {
toSign: TO_SIGN,
publicKey: LITCONFIG.PKP_PUBKEY,
},
});

// ==================== 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.signatures['sig'][key] === undefined) {
missingKeys.push(key);
}
}
);
}

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

if (!pkpSignRes.response.authSigAddress == LITCONFIG.CONTROLLER_AUTHSIG_2.address) {
return fail(`Missing or non matching auth sig address in Lit.Auth context`);
}

if(pkpSignRes.response.authMethodContexts.length > 0) {
return fail('should not have auth methods only authSigAddress');
}

// ==================== Success ====================
return success(
`it should use sessionSigs generated by eth wallet auth method to sign data. Signature is ${pkpSignRes.signatures} and pkpSignRes is ${JSON.stringify(
pkpSignRes
)}`
);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,6 @@ const TO_SIGN = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEnc

export async function main() {
// ==================== Setup ====================

const litAuthClient = new LitAuthClient({
litRelayConfig: {
relayApiKey: '67e55044-10b1-426f-9247-bb680e5fe0c8_relayer',
},
version: 'V3',
litNodeClient: client,
});

// -- eth wallet
const authProvider = litAuthClient.initProvider(ProviderType.EthWallet);
const authMethod = {
authMethodType: AuthMethodType.EthWallet,
accessToken: JSON.stringify(LITCONFIG.CONTROLLER_AUTHSIG),
};

let pkps = await authProvider.fetchPKPsThroughRelayer(authMethod);

if (pkps.length <= 0) {
try {
await authProvider.mintPKPThroughRelayer(authMethod);
} catch (e) {
return fail('Failed to mint PKP');
}
pkps = await authProvider.fetchPKPsThroughRelayer(authMethod);
}

const pkp = pkps[pkps.length - 1];

// convert BigNumber to string
pkp.tokenId = ethers.BigNumber.from(pkp.tokenId).toString();

const pkpPubKey = pkp.publicKey;

const pkpSignRes = await client?.executeJs({
authSig: LITCONFIG.CONTROLLER_AUTHSIG_2,
authMethods: [authMethod],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function main() {
const authProvider = litAuthClient.initProvider(ProviderType.EthWallet);
const authMethod = {
authMethodType: AuthMethodType.EthWallet,
accessToken: JSON.stringify(LITCONFIG.CONTROLLER_AUTHSIG),
accessToken: JSON.stringify(LITCONFIG.CONTROLLER_AUTHSIG_2),
};

let pkps = await authProvider.fetchPKPsThroughRelayer(authMethod);
Expand Down Expand Up @@ -91,6 +91,7 @@ export async function main() {
const pkpWallet = new PKPEthersWallet({
pkpPubKey: pkpPubKey,
controllerSessionSigs: sessionSigs,
controllerAuthMethods: []
});

await pkpWallet.init();
Expand Down
25 changes: 2 additions & 23 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,17 @@ export class LitCore {
* Get either auth sig or session auth sig
*
*/
getAuthMaterial = ({
authMethods,
getSessionOrAuthSig = ({
authSig,
sessionSigs,
url,
mustHave = true,
}: {
authMethods?: Array<Object>,
authSig?: AuthSig;
sessionSigs?: SessionSigsMap;
url: string;
mustHave?: boolean;
}): AuthSig | SessionSig | Object[] => {
}): AuthSig | SessionSig => {

if (!authSig && !sessionSigs) {
if (mustHave) {
Expand Down Expand Up @@ -393,28 +391,9 @@ export class LitCore {
return sigToPassToNode;
}

if (authMethods) {
return authMethods;
}

return authSig!;
};

/*
Here we do a check on the 'length' property of the object
returned to see if it is an array type
we cast to the array type to check as the union of types does not overlap
*/
setAuthMaterial<T extends JsonExecutionRequest | JsonPkpSignRequest>(reqBody: T, authMaterial: AuthSig | SessionSig | Object[]): T {
if (!(authMaterial as Object[]).length){
reqBody.authSig = (authMaterial as AuthSig);
} else {
reqBody.authMethods = (authMaterial as Object[]);
}

return reqBody;
}

/**
*
* Get hash of access control conditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,13 @@ export class LitNodeClientNodeJs extends LitCore {
this.getLitActionRequestBody(params);

// -- choose the right signature
const sigToPassToNode = this.getAuthMaterial({
authMethods,
const sigToPassToNode = this.getSessionOrAuthSig({
authSig,
sessionSigs,
url,
});

this.setAuthMaterial(reqBody, sigToPassToNode);
reqBody.authSig = sigToPassToNode;

// this return { url: string, data: JsonRequest }
const singleNodePromise = this.getJsExecutionShares(
Expand Down Expand Up @@ -1338,14 +1337,13 @@ export class LitNodeClientNodeJs extends LitCore {
const requestId = this.getRequestId();
const nodePromises = this.getNodePromises((url: string) => {
// -- choose the right signature
let sigToPassToNode = this.getAuthMaterial({
authMethods,
let sigToPassToNode = this.getSessionOrAuthSig({
authSig,
sessionSigs,
url,
});

this.setAuthMaterial(reqBody, sigToPassToNode);
reqBody.authSig = sigToPassToNode;

return this.getJsExecutionShares(url, reqBody, requestId);
});
Expand Down Expand Up @@ -1505,7 +1503,7 @@ export class LitNodeClientNodeJs extends LitCore {
const requestId = this.getRequestId();
const nodePromises = this.getNodePromises((url: string) => {
// -- choose the right signature
let sigToPassToNode = this.getAuthMaterial({
let sigToPassToNode = this.getSessionOrAuthSig({
authSig,
sessionSigs,
url,
Expand Down