Skip to content

Commit

Permalink
Merge branch 'feature/lit-2958-auth-unification-ts-tests' of https://…
Browse files Browse the repository at this point in the history
…github.com/LIT-Protocol/js-sdk into feature/lit-2961-auth-unification-breaking-remove-existing-authsigs
  • Loading branch information
Ansonhkg committed May 1, 2024
2 parents dce9581 + 8164bdc commit bc3adbd
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 298 deletions.
6 changes: 3 additions & 3 deletions packages/auth-helpers/src/lib/siwe/create-siwe-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

/**
* Creates a SIWE
* @param params - The parameters for creating the SIWE message.
* @param { BaseSiweMessage } params - The parameters for creating the SIWE message.
* @returns A promise that resolves to the created SIWE message as a string.
* @throws An error if the walletAddress parameter is missing.
*/
Expand Down Expand Up @@ -80,7 +80,7 @@ export const createSiweMessage = async <T extends BaseSiweMessage>(
/**
* Creates a SIWE message with recaps added to it.
*
* @param params - The parameters for creating the SIWE message with recaps.
* @param { WithRecap } params - The parameters for creating the SIWE message with recaps.
* @returns A Promise that resolves to a string representing the SIWE message.
*/
export const createSiweMessageWithRecaps = async (
Expand All @@ -93,7 +93,7 @@ export const createSiweMessageWithRecaps = async (

/**
* Creates a SIWE message with capacity delegation.
* @param params - The parameters for creating the SIWE message.
* @param { WithCapacityDelegation } params - The parameters for creating the SIWE message.
* @returns A Promise that resolves to the created SIWE message.
* @throws An error if litNodeClient is not provided.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { handleBlsResponseData } from './handle-bls-response';

describe('handleBlsResponseData', () => {
it('should return an array of signed data', () => {
const responseData = [
{
result: 'success',
signatureShare: {
ProofOfPossession:
'01b191b1d281857a95d2fd189683db366ab1088723338c1805daa4650459e9fcaebaa57b58108c284d233404dd5f2e58f208aafb87d981098aba3fe850980184a4b29643a21107b03f1d928646245b57af3745a81418989e0b6aad9bd1f192723c',
},
shareIndex: 0,
curveType: 'BLS',
siweMessage:
"litprotocol.com wants you to sign in with your Ethereum account:\n0x7f2e96c99F9551915DA9e9F828F512330f130acB\n\nLit Protocol PKP session signature I further authorize the stated URI to perform the following actions on my behalf: I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Execution' for 'lit-litaction://*'. (2) 'Threshold': 'Signing' for 'lit-pkp://*'. I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Execution' for 'lit-litaction://*'. (2) 'Threshold': 'Signing' for 'lit-pkp://*'. (3) 'Auth': 'Auth' for 'lit-resolvedauthcontext://*'.\n\nURI: lit:session:73e09d1ad1faa329bef12ebaf9b982d2925746e3677cabd4b6b7196096a6ee02\nVersion: 1\nChain ID: 1\nNonce: 0xa5f18dbc0fa2080649042ab8cb6cef3b246c20c15b62482ba43fb4ca2a4642cb\nIssued At: 2024-04-25T02:09:35Z\nExpiration Time: 2024-04-26T02:09:50.822Z\nResources:\n- urn:recap:eyJhdHQiOnsibGl0LWxpdGFjdGlvbjovLyoiOnsiVGhyZXNob2xkL0V4ZWN1dGlvbiI6W3t9XX0sImxpdC1wa3A6Ly8qIjp7IlRocmVzaG9sZC9TaWduaW5nIjpbe31dfSwibGl0LXJlc29sdmVkYXV0aGNvbnRleHQ6Ly8qIjp7IkF1dGgvQXV0aCI6W3siYXV0aF9jb250ZXh0Ijp7ImFjdGlvbklwZnNJZHMiOlsiUW1ZM3F1bjlxWDNmVUJIVmZyQTlmM3Y5UnB5eVBvOFJIRXVFTjFYWVBxMVByQSJdLCJhdXRoTWV0aG9kQ29udGV4dHMiOlt7ImFwcElkIjoibGl0IiwiYXV0aE1ldGhvZFR5cGUiOjEsImV4cGlyYXRpb24iOjE3MTQwOTczODYsInVzZWRGb3JTaWduU2Vzc2lvbktleVJlcXVlc3QiOnRydWUsInVzZXJJZCI6IjB4NzA5OTc5NzBDNTE4MTJkYzNBMDEwQzdkMDFiNTBlMGQxN2RjNzlDOCJ9XSwiYXV0aFNpZ0FkZHJlc3MiOm51bGwsInJlc291cmNlcyI6W119fV19fSwicHJmIjpbXX0",
dataSigned:
'b2efe867176b9212fd6acd39a33004a17e03d5a931250c700e31af95e2e7e4d5',
blsRootPubkey:
'a6f7c284ac766db1b43f8c65d8ff15c7271a05b0863b5205d96459fd32aa353e9390ce0626560fb76720c1a5c8ca6902',
},
{
result: 'success',
signatureShare: {
ProofOfPossession:
'038178034edcd5b48da4e2af6eb0891ece41389aa6119c80546d3fa00b5d2ba87eaec327b18d8013714b486246807498c8198e70cf8e917b1a5f1d8d0846787172521d41994de95bd641bdc1d9ccee9b459ceeb03f156cf357a4ff8faf5d2e167d',
},
shareIndex: 2,
curveType: 'BLS',
siweMessage:
"litprotocol.com wants you to sign in with your Ethereum account:\n0x7f2e96c99F9551915DA9e9F828F512330f130acB\n\nLit Protocol PKP session signature I further authorize the stated URI to perform the following actions on my behalf: I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Execution' for 'lit-litaction://*'. (2) 'Threshold': 'Signing' for 'lit-pkp://*'. I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Execution' for 'lit-litaction://*'. (2) 'Threshold': 'Signing' for 'lit-pkp://*'. (3) 'Auth': 'Auth' for 'lit-resolvedauthcontext://*'.\n\nURI: lit:session:73e09d1ad1faa329bef12ebaf9b982d2925746e3677cabd4b6b7196096a6ee02\nVersion: 1\nChain ID: 1\nNonce: 0xa5f18dbc0fa2080649042ab8cb6cef3b246c20c15b62482ba43fb4ca2a4642cb\nIssued At: 2024-04-25T02:09:35Z\nExpiration Time: 2024-04-26T02:09:50.822Z\nResources:\n- urn:recap:eyJhdHQiOnsibGl0LWxpdGFjdGlvbjovLyoiOnsiVGhyZXNob2xkL0V4ZWN1dGlvbiI6W3t9XX0sImxpdC1wa3A6Ly8qIjp7IlRocmVzaG9sZC9TaWduaW5nIjpbe31dfSwibGl0LXJlc29sdmVkYXV0aGNvbnRleHQ6Ly8qIjp7IkF1dGgvQXV0aCI6W3siYXV0aF9jb250ZXh0Ijp7ImFjdGlvbklwZnNJZHMiOlsiUW1ZM3F1bjlxWDNmVUJIVmZyQTlmM3Y5UnB5eVBvOFJIRXVFTjFYWVBxMVByQSJdLCJhdXRoTWV0aG9kQ29udGV4dHMiOlt7ImFwcElkIjoibGl0IiwiYXV0aE1ldGhvZFR5cGUiOjEsImV4cGlyYXRpb24iOjE3MTQwOTczODYsInVzZWRGb3JTaWduU2Vzc2lvbktleVJlcXVlc3QiOnRydWUsInVzZXJJZCI6IjB4NzA5OTc5NzBDNTE4MTJkYzNBMDEwQzdkMDFiNTBlMGQxN2RjNzlDOCJ9XSwiYXV0aFNpZ0FkZHJlc3MiOm51bGwsInJlc291cmNlcyI6W119fV19fSwicHJmIjpbXX0",
dataSigned:
'b2efe867176b9212fd6acd39a33004a17e03d5a931250c700e31af95e2e7e4d5',
blsRootPubkey:
'a6f7c284ac766db1b43f8c65d8ff15c7271a05b0863b5205d96459fd32aa353e9390ce0626560fb76720c1a5c8ca6902',
},
{
result: 'success',
signatureShare: {
ProofOfPossession:
'0292a026325a166398b85b53f3a7a34d147c5337e189d75c33c0f227f7926c839b408dfcc5d242a8685a81c68e0ccedc080c051219161dbc37f06627259b19d15120ab2f710075a44b1dcef18d511bb99b6625c8f575d2688c6b5b01ba6bf448c9',
},
shareIndex: 1,
curveType: 'BLS',
siweMessage:
"litprotocol.com wants you to sign in with your Ethereum account:\n0x7f2e96c99F9551915DA9e9F828F512330f130acB\n\nLit Protocol PKP session signature I further authorize the stated URI to perform the following actions on my behalf: I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Execution' for 'lit-litaction://*'. (2) 'Threshold': 'Signing' for 'lit-pkp://*'. I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Execution' for 'lit-litaction://*'. (2) 'Threshold': 'Signing' for 'lit-pkp://*'. (3) 'Auth': 'Auth' for 'lit-resolvedauthcontext://*'.\n\nURI: lit:session:73e09d1ad1faa329bef12ebaf9b982d2925746e3677cabd4b6b7196096a6ee02\nVersion: 1\nChain ID: 1\nNonce: 0xa5f18dbc0fa2080649042ab8cb6cef3b246c20c15b62482ba43fb4ca2a4642cb\nIssued At: 2024-04-25T02:09:35Z\nExpiration Time: 2024-04-26T02:09:50.822Z\nResources:\n- urn:recap:eyJhdHQiOnsibGl0LWxpdGFjdGlvbjovLyoiOnsiVGhyZXNob2xkL0V4ZWN1dGlvbiI6W3t9XX0sImxpdC1wa3A6Ly8qIjp7IlRocmVzaG9sZC9TaWduaW5nIjpbe31dfSwibGl0LXJlc29sdmVkYXV0aGNvbnRleHQ6Ly8qIjp7IkF1dGgvQXV0aCI6W3siYXV0aF9jb250ZXh0Ijp7ImFjdGlvbklwZnNJZHMiOlsiUW1ZM3F1bjlxWDNmVUJIVmZyQTlmM3Y5UnB5eVBvOFJIRXVFTjFYWVBxMVByQSJdLCJhdXRoTWV0aG9kQ29udGV4dHMiOlt7ImFwcElkIjoibGl0IiwiYXV0aE1ldGhvZFR5cGUiOjEsImV4cGlyYXRpb24iOjE3MTQwOTczODYsInVzZWRGb3JTaWduU2Vzc2lvbktleVJlcXVlc3QiOnRydWUsInVzZXJJZCI6IjB4NzA5OTc5NzBDNTE4MTJkYzNBMDEwQzdkMDFiNTBlMGQxN2RjNzlDOCJ9XSwiYXV0aFNpZ0FkZHJlc3MiOm51bGwsInJlc291cmNlcyI6W119fV19fSwicHJmIjpbXX0",
dataSigned:
'b2efe867176b9212fd6acd39a33004a17e03d5a931250c700e31af95e2e7e4d5',
blsRootPubkey:
'a6f7c284ac766db1b43f8c65d8ff15c7271a05b0863b5205d96459fd32aa353e9390ce0626560fb76720c1a5c8ca6902',
},
] as any;

const result = handleBlsResponseData(responseData);

expect(result).toEqual([
'b2efe867176b9212fd6acd39a33004a17e03d5a931250c700e31af95e2e7e4d5',
'b2efe867176b9212fd6acd39a33004a17e03d5a931250c700e31af95e2e7e4d5',
'b2efe867176b9212fd6acd39a33004a17e03d5a931250c700e31af95e2e7e4d5',
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { log } from '@lit-protocol/misc';
import { BlsResponseData } from '@lit-protocol/types';

/**
* Handles the response data from BLS signature scheme.
* @param responseData - The response data from BLS signature scheme.
* @returns An array of signed data.
* @throws Error if no data is provided.
*/
export function handleBlsResponseData(
responseData: BlsResponseData[]
): string[] {
if (!responseData) {
throw new Error('[handleBlsResponseData] No data provided');
}

const signatureShares = responseData.map((s) => ({
ProofOfPossession: s.signatureShare.ProofOfPossession,
}));
log(`[handleBlsResponseData] signatureShares:`, signatureShares);

if (!signatureShares || signatureShares.length <= 0) {
throw new Error('[handleBlsResponseData] No signature shares provided');
}

const signedDataList = responseData.map((s) => s.dataSigned);
log(`[handleBlsResponseData] signedDataList:`, signedDataList);

return signedDataList;
}
72 changes: 16 additions & 56 deletions packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ import type {
CapacityCreditsRes,
JsonSignSessionKeyRequestV1,
BlsResponseData,
SessionKeyCache,
JsonPkpSignSdkParams,
JsonExecutionSdkParams,
ExecuteJsNoSigningResponse,
Expand All @@ -119,20 +118,16 @@ import type {
SigResponse,
} from '@lit-protocol/types';
import * as blsSdk from '@lit-protocol/bls-sdk';
import { normalizeArray } from './helpers/normalize-array';
import { normalizeJsParams } from './helpers/normalize-params';
import { encodeCode } from './helpers/encode-code';
import { getFlattenShare, getSignatures } from './helpers/get-signatures';
import { removeDoubleQuotes } from './helpers/remove-double-quotes';
import { parseAsJsonOrString } from './helpers/parse-as-json-or-string';
import { getFlattenShare, getSignatures } from './helpers/get-signatures';
import { getClaimsList } from './helpers/get-claims-list';
import { getClaims } from './helpers/get-claims';
import { normalizeArray } from './helpers/normalize-array';
import { parsePkpSignResponse } from './helpers/parse-pkp-sign-response';

const TEMP_CACHE_PERIOD = 30000; // 30 seconds

// Global cache variable
let sessionKeyCache: SessionKeyCache | null = null;
import { handleBlsResponseData } from './helpers/handle-bls-response';

export class LitNodeClientNodeJs
extends LitCore
Expand All @@ -155,10 +150,15 @@ export class LitNodeClientNodeJs
createCapacityDelegationAuthSig = async (
params: CapacityCreditsReq
): Promise<CapacityCreditsRes> => {
// -- validate
if (!params.dAppOwnerWallet) {
throw new Error('dAppOwnerWallet must exist');
}

// Useful log for debugging
if (!params.delegateeAddresses || params.delegateeAddresses.length === 0) {
log(
`[createCapacityDelegationAuthSig] No delegatee addresses provided. It means that the capability will not restrict access based on delegatee list, but it may still enforce other restrictions such as usage limits (uses) and specific NFT IDs (nft_id).`
`[createCapacityDelegationAuthSig] 'delegateeAddresses' is an empty array. It means that no body can use it. However, if the 'delegateeAddresses' field is omitted, It means that the capability will not restrict access based on delegatee list, but it may still enforce other restrictions such as usage limits (uses) and specific NFT IDs (nft_id).`
);
}

Expand All @@ -173,29 +173,19 @@ export class LitNodeClientNodeJs
await this.connect();
}

// -- validate
if (!params.dAppOwnerWallet) {
throw new Error('dAppOwnerWallet must exist');
}

const nonce = await this.getLatestBlockhash();

const siweMessage = await createSiweMessageWithCapacityDelegation({
uri: 'lit:capability:delegation',
litNodeClient: this,
walletAddress: dAppOwnerWalletAddress,
nonce: nonce,

// -- default configuration for recap object capability
expiration:
params.expiration ?? new Date(Date.now() + 1000 * 60 * 7).toISOString(),
domain: params.domain ?? 'example.com',
statement:
params.statement ??
'This is a test statement. You can put anything you want here.',
expiration: params.expiration,
domain: params.domain,
statement: params.statement,

// -- capacity delegation specific configuration
uses: params.uses ?? '1',
uses: params.uses,
delegateeAddresses: params.delegateeAddresses,
capacityTokenId: params.capacityTokenId,
});
Expand Down Expand Up @@ -242,15 +232,6 @@ export class LitNodeClientNodeJs
`Storage key "${storageKey}" is missing. Not a problem. Contiune...`
);

// Check if a valid session key exists in cache
if (
sessionKeyCache &&
Date.now() - sessionKeyCache.timestamp < TEMP_CACHE_PERIOD
) {
log(`[getSessionKey] Returning session key from cache.`);
return sessionKeyCache.value;
}

// Generate new one
const newSessionKey = generateSessionKeyPair();

Expand All @@ -261,14 +242,6 @@ export class LitNodeClientNodeJs
log(
`[getSessionKey] Localstorage not available.Not a problem.Contiune...`
);

// Store in cache
sessionKeyCache = {
value: newSessionKey,
timestamp: Date.now(),
};

log(`[getSessionKey] newSessionKey set to cache: `, sessionKeyCache);
}

return newSessionKey;
Expand Down Expand Up @@ -2154,19 +2127,7 @@ export class LitNodeClientNodeJs
let signedDataList: any[] = [];

if (curveType === LIT_CURVE.BLS) {
let _responseData: BlsResponseData[] = responseData;

const signatureShares = _responseData.map((s) => ({
ProofOfPossession: s.signatureShare.ProofOfPossession,
}));

log(`[signSessionKey] signatureShares:`, signatureShares);

signedDataList = _responseData.map((s) => {
return s.dataSigned;
});

signedDataList = _responseData;
signedDataList = handleBlsResponseData(responseData);
} else {
signedDataList = responseData.map(
(r: any) => (r as SignedData).signedData
Expand Down Expand Up @@ -2272,9 +2233,7 @@ export class LitNodeClientNodeJs
);
log(`[signSessionKey] sigType:`, sigType);

const signatureShares = blsSignedData.map((s) => ({
ProofOfPossession: s.signatureShare.ProofOfPossession,
}));
const signatureShares = handleBlsResponseData(blsSignedData);

log(`[signSessionKey] signatureShares:`, signatureShares);

Expand Down Expand Up @@ -2548,6 +2507,7 @@ export class LitNodeClientNodeJs
};

/**
<<<<<<<<< Temporary merge branch 1
* Retrieves the PKP sessionSigs.
*
* @param params - The parameters for retrieving the PKP sessionSigs.
Expand Down
Loading

0 comments on commit bc3adbd

Please sign in to comment.