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 2961 auth unification breaking remove existing authsigs #444

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5b7e311
fix(sessionKey): signed session key mismatch with the one being signe…
Ansonhkg Apr 24, 2024
4344d6a
chore: pretty pretty lint
Ansonhkg Apr 24, 2024
30840f1
Merge branch 'feature/lit-2960-auth-unification-lit-endpoints-map' in…
Ansonhkg Apr 24, 2024
b954e37
Merge branch 'feature/lit-2962-auth-unification-better-siwe-authsig-c…
Ansonhkg Apr 24, 2024
72bcd8d
feat(accs): added compose lit action resource string helper function
Ansonhkg Apr 24, 2024
fe71ddf
feat(types): better interfaces
Ansonhkg Apr 24, 2024
430abb7
feat(test): add new e2e tests
Ansonhkg Apr 24, 2024
757d588
fix: unable to polyfill fetch when using cross-fetch, replaced with n…
Ansonhkg Apr 24, 2024
84f7095
chore: pretty pretty pretty lint
Ansonhkg Apr 24, 2024
35ede36
feat(migration): update pkpSign function
Ansonhkg Apr 25, 2024
45f9eed
feat(remove-authSig):
Ansonhkg Apr 26, 2024
63e3b54
fix(tinny): set default MAX_ATTEMPT = 1
Ansonhkg Apr 26, 2024
e4c3e79
fix(executeJs): jsParam is optional
Ansonhkg Apr 26, 2024
e01daaa
fix: node promises. now `pkpSign` & `executeJs` work!
Ansonhkg Apr 27, 2024
e2adf7d
feat: add pkp sign response parser as its own helper function and uni…
Ansonhkg Apr 27, 2024
1b9dd2c
fix: https://github.com/LIT-Protocol/js-sdk/pull/444#discussion_r1581…
Ansonhkg Apr 27, 2024
500967c
chore: remove unused function
Ansonhkg Apr 29, 2024
e30165e
fix: https://github.com/LIT-Protocol/js-sdk/pull/444#discussion_r1583…
Ansonhkg Apr 30, 2024
3b66113
fix: https://github.com/LIT-Protocol/js-sdk/pull/444#discussion_r1583…
Ansonhkg Apr 30, 2024
65e5f21
fix: https://github.com/LIT-Protocol/js-sdk/pull/444#discussion_r1583…
Ansonhkg Apr 30, 2024
ffa6555
fix: https://github.com/LIT-Protocol/js-sdk/pull/444#discussion_r1584…
Ansonhkg Apr 30, 2024
dce9581
fix: misleading function name
Ansonhkg Apr 30, 2024
bc3adbd
Merge branch 'feature/lit-2958-auth-unification-ts-tests' of https://…
Ansonhkg May 1, 2024
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
2 changes: 1 addition & 1 deletion local-tests/setup/tinny-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class TinnyEnvironment {
* Environment variables used in the process.
*/
public processEnvs: ProcessEnvs = {
MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 3,
MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 1,
NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN,
DEBUG: Boolean(process.env['DEBUG']) || false,
REQUEST_PER_KILOSECOND:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const testUseEoaSessionSigsToEncryptDecryptFile = async (
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const testUseEoaSessionSigsToEncryptDecryptString = async (
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const testUseEoaSessionSigsToEncryptDecryptZip = async (
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const testUsePkpSessionSigsToEncryptDecryptFile = async (
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const testUsePkpSessionSigsToEncryptDecryptString = async (
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const testUsePkpSessionSigsToEncryptDecryptZip = async (
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptFile =
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptString
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptZip =
}

const accsResourceString =
await LitAccessControlConditionResource.composeLitActionResourceString(
await LitAccessControlConditionResource.generateLitActionResourceString(
accs,
encryptRes.dataToEncryptHash
);
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-helpers/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class LitAccessControlConditionResource
* @param {string} dataToEncryptHash - The hash of the data to encrypt.
* @returns {Promise<string>} The composed resource string in the format 'hashedAccs/dataToEncryptHash'.
*/
public static async composeLitActionResourceString(
public static async generateLitActionResourceString(
accs: AccessControlConditions,
dataToEncryptHash: string
): Promise<string> {
Expand Down
8 changes: 4 additions & 4 deletions packages/constants/src/lib/constants/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ export const LIT_ENDPOINT = {
},
SIGN_SESSION_KEY: {
path: '/web/sign_session_key',
// version: LIT_ENDPOINT_VERSION.V1,
version: LIT_ENDPOINT_VERSION.V1,

// FIXME: Change this to V1 once the new version is deployed to all public networks
version: LIT_ENDPOINT_VERSION.V0,
// version: LIT_ENDPOINT_VERSION.V0,
Ansonhkg marked this conversation as resolved.
Show resolved Hide resolved
envName: 'SIGN_SESSION_KEY',
},
EXECUTE_JS: {
path: '/web/execute',
// FIXME: Change this to V1 once the new version is deployed to all public networks
version: LIT_ENDPOINT_VERSION.V0,
version: LIT_ENDPOINT_VERSION.V1,
envName: 'EXECUTE_JS',
},
PKP_SIGN: {
path: '/web/pkp/sign',
// version: LIT_ENDPOINT_VERSION.V1,
version: LIT_ENDPOINT_VERSION.V0,
version: LIT_ENDPOINT_VERSION.V1,
envName: 'PKP_SIGN',
},
PKP_CLAIM: {
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts-sdk/src/lib/auth-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SessionSig, StytchToken } from '@lit-protocol/types';
import { StytchToken } from '@lit-protocol/types';
import { ethers } from 'ethers';
import * as jose from 'jose';
/**
Expand Down
55 changes: 23 additions & 32 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import type {
NodeErrorV3,
RejectedNodePromises,
SendNodeCommand,
SessionSig,
SessionSigsMap,
SuccessNodePromises,
SupportedJsonRequests,
Expand Down Expand Up @@ -1018,48 +1017,40 @@ export class LitCore {
};

/**
* Retrieves the session signature for a given URL from the sessionSigs map.
* Throws an error if sessionSigs is not provided or if the session signature for the URL is not found.
*
* Get either auth sig or session auth sig
*
* @param sessionSigs - The session signatures map.
* @param url - The URL for which to retrieve the session signature.
* @returns The session signature for the given URL.
* @throws An error if sessionSigs is not provided or if the session signature for the URL is not found.
*/
getSessionOrAuthSig = ({
authSig,
getSessionSigByUrl = ({
sessionSigs,
url,
mustHave = true,
}: {
authSig?: AuthSig;
sessionSigs?: SessionSigsMap;
sessionSigs: SessionSigsMap;
url: string;
mustHave?: boolean;
}): AuthSig | SessionSig => {
if (!authSig && !sessionSigs) {
if (mustHave) {
throwError({
message: `You must pass either authSig, or sessionSigs`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
} else {
log(`authSig or sessionSigs not found. This may be using authMethod`);
}
}): AuthSig => {
if (!sessionSigs) {
return throwError({
message: `You must pass in sessionSigs`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
}

if (sessionSigs) {
const sigToPassToNode = sessionSigs[url];
const sigToPassToNode = sessionSigs[url];

if (!sigToPassToNode) {
throwError({
message: `You passed sessionSigs but we could not find session sig for node ${url}`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
}

return sigToPassToNode;
if (!sessionSigs[url]) {
throwError({
message: `You passed sessionSigs but we could not find session sig for node ${url}`,
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
}

return authSig!;
return sigToPassToNode;
};

validateAccessControlConditionsSchema = async (
Expand Down
43 changes: 7 additions & 36 deletions packages/encryption/src/lib/params-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import {
EncryptToJsonProps,
EncryptZipRequest,
EvmContractConditions,
ExecuteJsProps,
GetSignedTokenRequest,
JsonExecutionSdkParams,
SessionSigs,
SessionSigsMap,
SolRpcConditions,
UnifiedAccessControlConditions,
} from '@lit-protocol/types';
Expand Down Expand Up @@ -71,7 +72,7 @@ export const paramsValidators: Record<
string,
(params: any) => ParamsValidator[]
> = {
executeJs: (params: ExecuteJsProps) => [
executeJs: (params: JsonExecutionSdkParams) => [
new AuthMaterialValidator('executeJs', params),
new ExecuteJsValidator('executeJs', params),
new AuthMethodValidator('executeJs', params.authMethods),
Expand Down Expand Up @@ -379,8 +380,7 @@ class FileValidator implements ParamsValidator {
}

export interface AuthMaterialValidatorProps {
authSig?: AuthSig;
sessionSigs?: SessionSigs;
sessionSigs?: SessionSigsMap;
Ansonhkg marked this conversation as resolved.
Show resolved Hide resolved
chain?: string;
}

Expand All @@ -400,14 +400,7 @@ class AuthMaterialValidator implements ParamsValidator {
}

validate(): IEither<void> {
const { authSig, sessionSigs } = this.authMaterial;

if (authSig && !is(authSig, 'Object', 'authSig', this.fnName))
return ELeft({
message: 'authSig is not an object',
errorKind: LIT_ERROR.INVALID_PARAM_TYPE.kind,
errorCode: LIT_ERROR.INVALID_PARAM_TYPE.name,
});
const { sessionSigs } = this.authMaterial;

if (this.checkIfAuthSigRequiresChainParam) {
if (!this.authMaterial.chain)
Expand All @@ -416,20 +409,6 @@ class AuthMaterialValidator implements ParamsValidator {
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});

if (
authSig &&
!checkIfAuthSigRequiresChainParam(
authSig,
this.authMaterial.chain,
this.fnName
)
)
return ELeft({
message: 'authSig is not valid',
errorKind: LIT_ERROR.INVALID_PARAM_TYPE.kind,
errorCode: LIT_ERROR.INVALID_PARAM_TYPE.name,
});
}

if (sessionSigs && !is(sessionSigs, 'Object', 'sessionSigs', this.fnName))
Expand All @@ -439,17 +418,9 @@ class AuthMaterialValidator implements ParamsValidator {
errorCode: LIT_ERROR.INVALID_PARAM_TYPE.name,
});

if (!sessionSigs && !authSig)
return ELeft({
message: 'You must pass either authSig or sessionSigs',
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});

// -- validate: if sessionSig and authSig exists
if (sessionSigs && authSig)
if (!sessionSigs)
return ELeft({
message: 'You cannot have both authSig and sessionSigs',
message: 'You must pass in sessionSigs',
errorKind: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.kind,
errorCode: LIT_ERROR.INVALID_ARGUMENT_EXCEPTION.name,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { encodeCode } from './encode-code';

describe('encodeCode', () => {
it('should encode a string to base64', () => {
const code = 'console.log("Hello, World!")';
const encodedCode = encodeCode(code);

expect(encodedCode).toEqual('Y29uc29sZS5sb2coIkhlbGxvLCBXb3JsZCEiKQ==');
});

it('should handle empty string', () => {
const code = '';
const encodedCode = encodeCode(code);

expect(encodedCode).toEqual('');
});

it('should handle special characters', () => {
const code = 'const x = 10 + 5 - 3 * 2 / 1;';
const encodedCode = encodeCode(code);

expect(encodedCode).toEqual('Y29uc3QgeCA9IDEwICsgNSAtIDMgKiAyIC8gMTs=');
});

it('should handle non-ASCII characters', () => {
const code = 'const name = "Jérémy";';
const encodedCode = encodeCode(code);

expect(encodedCode).toEqual('Y29uc3QgbmFtZSA9ICJKw6lyw6lteSI7');
});
});
17 changes: 17 additions & 0 deletions packages/lit-node-client-nodejs/src/lib/helpers/encode-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
uint8arrayFromString,
uint8arrayToString,
} from '@lit-protocol/uint8arrays';

/**
* Encodes the given code string into base64 format.
*
* @param code - The code string to be encoded.
* @returns The encoded code string in base64 format.
*/
export const encodeCode = (code: string) => {
const _uint8Array = uint8arrayFromString(code, 'utf8');
const encodedJs = uint8arrayToString(_uint8Array, 'base64');

return encodedJs;
};
Loading
Loading