Skip to content

Commit

Permalink
add more logs to tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Long committed Nov 27, 2023
1 parent 7656f03 commit 3e1b20c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ export class LitCore {
*/
handleNodePromises = async <T>(
nodePromises: Array<Promise<T>>,
minNodeCount?: number
requestId?: string,
minNodeCount?: number,
): Promise<SuccessNodePromises<T> | RejectedNodePromises> => {
// -- prepare
const responses = await Promise.allSettled(nodePromises);
Expand Down Expand Up @@ -505,7 +506,7 @@ export class LitCore {
)
);

log(`most common error: ${JSON.stringify(mostCommonError)}`);
logErrorWithRequestId(requestId || "", `most common error: ${JSON.stringify(mostCommonError)}`);

const rejectedPromises: RejectedNodePromises = {
success: false,
Expand Down
79 changes: 41 additions & 38 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 @@ -24,6 +24,7 @@ import {
hexPrefixed,
log,
logError,
logErrorWithRequestId,
logWithRequestId,
mostCommonString,
throwError,
Expand Down Expand Up @@ -101,7 +102,6 @@ import {
} from '@lit-protocol/misc-browser';
import { nacl } from '@lit-protocol/nacl';
import { BigNumber, ethers, utils } from 'ethers';

/** ---------- Main Export Class ---------- */

export class LitNodeClientNodeJs extends LitCore {
Expand Down Expand Up @@ -579,7 +579,7 @@ export class LitNodeClientNodeJs extends LitCore {
params: any,
requestId: string
) => {
log('getPkpSigningShares');
logWithRequestId(requestId, 'getPkpSigningShares');
const urlWithPath = `${url}/web/pkp/sign`;
if (!params.authSig) {
throw new Error('authSig is required');
Expand All @@ -597,7 +597,7 @@ export class LitNodeClientNodeJs extends LitCore {
params: any,
requestId: string
) => {
log('getPkpSigningShares');
logWithRequestId(requestId, 'getPkpSigningShares');
const urlWithPath = `${url}/web/pkp/claim`;
if (!params.authMethod) {
throw new Error('authMethod is required');
Expand All @@ -624,7 +624,7 @@ export class LitNodeClientNodeJs extends LitCore {
params: SigningAccessControlConditionRequest,
requestId: string
): Promise<NodeCommandResponse> => {
log('getSigningShareForToken');
logWithRequestId(requestId, 'getSigningShareForToken');
const urlWithPath = `${url}/web/signing/access_control_condition`;

return this.sendCommandToNode({
Expand Down Expand Up @@ -703,7 +703,8 @@ export class LitNodeClientNodeJs extends LitCore {
*
*/
combineSharesAndGetJWT = (
signatureShares: Array<NodeBlsSigningShare>
signatureShares: Array<NodeBlsSigningShare>,
requestId: string = ''
): string => {
// ========== Shares Validations ==========
// -- sanity check
Expand All @@ -714,7 +715,7 @@ export class LitNodeClientNodeJs extends LitCore {
) {
const msg =
'Unsigned JWT is not the same from all the nodes. This means the combined signature will be bad because the nodes signed the wrong things';
logError(msg);
logErrorWithRequestId(requestId, msg);
}

// ========== Sorting ==========
Expand All @@ -726,7 +727,7 @@ export class LitNodeClientNodeJs extends LitCore {
signatureShares.map((s) => s.signatureShare)
);

log('signature is', signature);
logWithRequestId(requestId, 'signature is', signature);

const unsignedJwt = mostCommonString(
signatureShares.map((s: any) => s.unsignedJwt)
Expand Down Expand Up @@ -906,6 +907,7 @@ export class LitNodeClientNodeJs extends LitCore {

const handledPromise = (await this.handleNodePromises(
nodePromises,
requestId,
targetNodeRange
)) as SuccessNodePromises<NodeCommandResponse> | RejectedNodePromises;

Expand Down Expand Up @@ -1114,7 +1116,7 @@ export class LitNodeClientNodeJs extends LitCore {
* @returns { any }
*
*/
getSignatures = (signedData: Array<any>): any => {
getSignatures = (signedData: Array<any>, requestId: string = ''): any => {
log(`getSignatures(): ${JSON.stringify(signedData, null, 2)}`);

const validatedSignedData = signedData
Expand All @@ -1135,11 +1137,11 @@ export class LitNodeClientNodeJs extends LitCore {
const requiredFields = ['signatureShare'];

for (const field of requiredFields) {
log('Checking sigShare:', sigShare);
logWithRequestId(requestId, 'Checking sigShare:', sigShare);

if (!sigShare || !sigShare[field] || sigShare[field] === '') {
log(
`Invalid signed data. ${field} is missing. Not a problem, we only need ${this.config.minNodeCount} nodes to sign the session key.`
logWithRequestId(
requestId, `Invalid signed data. ${field} is missing. Not a problem, we only need ${this.config.minNodeCount} nodes to sign the session key.`
);
return null;
}
Expand All @@ -1149,13 +1151,13 @@ export class LitNodeClientNodeJs extends LitCore {
})
.filter((sigObj) => sigObj !== null);

log('requested length:', signedData.length);
log('validated length:', validatedSignedData.length);
log('minimum required length:', this.config.minNodeCount);
logWithRequestId(requestId, 'requested length:', signedData.length);
logWithRequestId(requestId, 'validated length:', validatedSignedData.length);
logWithRequestId(requestId, 'minimum required length:', this.config.minNodeCount);

if (validatedSignedData.length < this.config.minNodeCount) {
logError(
`not enough nodes to get the signatures. Expected ${this.config.minNodeCount}, got ${validatedSignedData.length}`
logErrorWithRequestId(
requestId, `not enough nodes to get the signatures. Expected ${this.config.minNodeCount}, got ${validatedSignedData.length}`
);
return null;
}
Expand Down Expand Up @@ -1186,16 +1188,16 @@ export class LitNodeClientNodeJs extends LitCore {
};
});

log('getSignatures - sigShares', sigShares);
logWithRequestId(requestId, 'getSignatures - sigShares', sigShares);

const validatedSigShares = sigShares
.filter((s: any) => {
const requiredFields = ['sigType', 'signatureShare'];

for (const field of requiredFields) {
if (!s[field] || s[field] === '') {
log(
`Invalid signed data. ${field} is missing. Not a problem, we only need ${this.config.minNodeCount} nodes to sign the session key.`
logWithRequestId(
requestId, `Invalid signed data. ${field} is missing. Not a problem, we only need ${this.config.minNodeCount} nodes to sign the session key.`
);
return null;
}
Expand All @@ -1205,13 +1207,13 @@ export class LitNodeClientNodeJs extends LitCore {
})
.filter((s) => s !== null);

log('requested length:', signedData.length);
log('validated length:', validatedSigShares.length);
log('minimum required length:', this.config.minNodeCount);
logWithRequestId(requestId, 'requested length:', signedData.length);
logWithRequestId(requestId, 'validated length:', validatedSigShares.length);
logWithRequestId(requestId, 'minimum required length:', this.config.minNodeCount);

if (validatedSigShares.length < this.config.minNodeCount) {
logError(
`not enough nodes to get the signatures. Expected ${this.config.minNodeCount}, got ${validatedSigShares.length}`
logErrorWithRequestId(
requestId, `not enough nodes to get the signatures. Expected ${this.config.minNodeCount}, got ${validatedSigShares.length}`
);
}

Expand Down Expand Up @@ -1275,7 +1277,7 @@ export class LitNodeClientNodeJs extends LitCore {
* @returns { string } signature
*
*/
getSignature = async (shareData: Array<any>): Promise<any> => {
getSignature = async (shareData: Array<any>, requestId: string): Promise<any> => {
// R_x & R_y values can come from any node (they will be different per node), and will generate a valid signature
const R_x = shareData[0].local_x;
const R_y = shareData[0].local_y;
Expand All @@ -1287,7 +1289,7 @@ export class LitNodeClientNodeJs extends LitCore {

await wasmECDSA.initWasmEcdsaSdk(); // init WASM
const signature = wasmECDSA.combine_signature(R_x, R_y, shares);
log('raw ecdsa sig', signature);
logWithRequestId(requestId, 'raw ecdsa sig', signature);

return signature;
};
Expand Down Expand Up @@ -1393,7 +1395,7 @@ export class LitNodeClientNodeJs extends LitCore {
return this.getJsExecutionShares(url, reqBody, requestId);
});
// -- resolve promises
res = await this.handleNodePromises(nodePromises);
res = await this.handleNodePromises(nodePromises, requestId);
}

// -- case: promises rejected
Expand Down Expand Up @@ -1449,7 +1451,7 @@ export class LitNodeClientNodeJs extends LitCore {
});

logWithRequestId(requestId, 'signedDataList:', signedDataList);
const signatures = this.getSignatures(signedDataList);
const signatures = this.getSignatures(signedDataList, requestId);

// -- 2. combine responses as a string, and get parse it as JSON
let response: string = mostCommonString(
Expand Down Expand Up @@ -1573,12 +1575,12 @@ export class LitNodeClientNodeJs extends LitCore {
authMethods,
};

log('reqBody:', reqBody);
logWithRequestId(requestId, 'reqBody:', reqBody);

return this.getPkpSignExecutionShares(url, reqBody, requestId);
});

const res = await this.handleNodePromises(nodePromises);
const res = await this.handleNodePromises(nodePromises, requestId);

// -- case: promises rejected
if (res.success === false) {
Expand All @@ -1587,7 +1589,7 @@ export class LitNodeClientNodeJs extends LitCore {

// -- case: promises success (TODO: check the keys of "values")
const responseData = (res as SuccessNodePromises<PKPSignShare>).values;
log('responseData', JSON.stringify(responseData, null, 2));
logWithRequestId(requestId, 'responseData', JSON.stringify(responseData, null, 2));

// ========== Extract shares from response data ==========
// -- 1. combine signed data as a list, and get the signatures from it
Expand Down Expand Up @@ -1630,8 +1632,8 @@ export class LitNodeClientNodeJs extends LitCore {
};
});

const signatures = this.getSignatures(signedDataList);
log(`signature combination`, signatures);
const signatures = this.getSignatures(signedDataList, requestId);
logWithRequestId(requestId, `signature combination`, signatures);

return signatures.signature; // only a single signature is ever present, so we just return it.
};
Expand Down Expand Up @@ -1730,7 +1732,7 @@ export class LitNodeClientNodeJs extends LitCore {
});

// -- resolve promises
const res = await this.handleNodePromises(nodePromises);
const res = await this.handleNodePromises(nodePromises, requestId);

// -- case: promises rejected
if (res.success === false) {
Expand All @@ -1744,7 +1746,7 @@ export class LitNodeClientNodeJs extends LitCore {
log('signatureShares', signatureShares);

// ========== Result ==========
const finalJwt: string = this.combineSharesAndGetJWT(signatureShares);
const finalJwt: string = this.combineSharesAndGetJWT(signatureShares, requestId);

return finalJwt;
};
Expand Down Expand Up @@ -1945,7 +1947,7 @@ export class LitNodeClientNodeJs extends LitCore {
});

// -- resolve promises
const res = await this.handleNodePromises(nodePromises);
const res = await this.handleNodePromises(nodePromises, requestId);

// -- case: promises rejected
if (res.success === false) {
Expand Down Expand Up @@ -2092,7 +2094,7 @@ export class LitNodeClientNodeJs extends LitCore {

if (shareData[0].result == 'failure') return 'Condition Failed';

const signature = this.getSignature(shareData);
const signature = this.getSignature(shareData, requestId);

return signature;
} catch (e) {
Expand Down Expand Up @@ -2189,7 +2191,7 @@ export class LitNodeClientNodeJs extends LitCore {
// -- resolve promises
let res;
try {
res = await this.handleNodePromises(nodePromises);
res = await this.handleNodePromises(nodePromises, requestId);
log('signSessionKey node promises:', res);
} catch (e) {
throw new Error(`Error when handling node promises: ${e}`);
Expand Down Expand Up @@ -2484,6 +2486,7 @@ export class LitNodeClientNodeJs extends LitCore {

const responseData = await this.handleNodePromises(
nodePromises,
requestId,
this.connectedNodes.size // require from all connected nodes
);

Expand Down

0 comments on commit 3e1b20c

Please sign in to comment.