Skip to content

Commit d617a04

Browse files
committed
feat: removed unused types and executeJs return fixes
1 parent 42a380f commit d617a04

File tree

6 files changed

+150
-142
lines changed

6 files changed

+150
-142
lines changed

packages/lit-node-client-nodejs/src/lib/helpers/pocess-lit-action-response-strategy.spec.ts

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import { assert } from 'console';
2-
3-
import { NodeShare } from '@lit-protocol/types';
1+
import { ExecuteJsValueResponse } from '@lit-protocol/types';
42

53
import { processLitActionResponseStrategy } from './process-lit-action-response-strategy';
64

75
describe('processLitActionResponseStrategy', () => {
8-
const litActionResponses: any[] = [
6+
const litActionResponses: ExecuteJsValueResponse[] = [
97
{
108
success: true,
119
signedData: {
1210
sig: {
13-
sigType: 'K256',
14-
dataSigned: 'fail',
11+
sigType: 'EcdsaK256Sha256',
1512
signatureShare: '',
16-
bigR: '',
1713
publicKey: '',
1814
sigName: 'sig',
1915
},
@@ -27,10 +23,8 @@ describe('processLitActionResponseStrategy', () => {
2723
success: true,
2824
signedData: {
2925
sig: {
30-
sigType: 'K256',
31-
dataSigned: 'fail',
26+
sigType: 'EcdsaK256Sha256',
3227
signatureShare: '',
33-
bigR: '',
3428
publicKey: '',
3529
sigName: 'sig',
3630
},
@@ -44,12 +38,9 @@ describe('processLitActionResponseStrategy', () => {
4438
success: true,
4539
signedData: {
4640
sig: {
47-
sigType: 'K256',
48-
dataSigned:
49-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
41+
sigType: 'EcdsaK256Sha256',
5042
signatureShare:
5143
'"E90BAE64AFA7C571CE41BEF25FF771CA2F1BC20FC09A7762200552B30ACC0CDC"',
52-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
5344
publicKey:
5445
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
5546
sigName: 'sig',
@@ -64,12 +55,9 @@ describe('processLitActionResponseStrategy', () => {
6455
success: true,
6556
signedData: {
6657
sig: {
67-
sigType: 'K256',
68-
dataSigned:
69-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
58+
sigType: 'EcdsaK256Sha256',
7059
signatureShare:
7160
'"31977D4BE7F49C0CD97CC0756CCA3244A949EA7D591F79B64F324846507448CD"',
72-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
7361
publicKey:
7462
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
7563
sigName: 'sig',
@@ -84,12 +72,9 @@ describe('processLitActionResponseStrategy', () => {
8472
success: true,
8573
signedData: {
8674
sig: {
87-
sigType: 'K256',
88-
dataSigned:
89-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
75+
sigType: 'EcdsaK256Sha256',
9076
signatureShare:
9177
'"F21798A1A37CC86566EA0D751F37CC144774A1A8A4FCD5E6E64287690FB60119"',
92-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
9378
publicKey:
9479
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
9580
sigName: 'sig',
@@ -104,12 +89,9 @@ describe('processLitActionResponseStrategy', () => {
10489
success: true,
10590
signedData: {
10691
sig: {
107-
sigType: 'K256',
108-
dataSigned:
109-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
92+
sigType: 'EcdsaK256Sha256',
11093
signatureShare:
11194
'"7ECB0E020BED801905D3FE941751E4313086603BBBF21F1756832F02A6FBE567"',
112-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
11395
publicKey:
11496
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
11597
sigName: 'sig',
@@ -122,15 +104,13 @@ describe('processLitActionResponseStrategy', () => {
122104
},
123105
];
124106

125-
const litActionResponsesNonJson: any[] = [
107+
const litActionResponsesNonJson: ExecuteJsValueResponse[] = [
126108
{
127109
success: true,
128110
signedData: {
129111
sig: {
130-
sigType: 'K256',
131-
dataSigned: 'fail',
112+
sigType: 'EcdsaK256Sha256',
132113
signatureShare: '',
133-
bigR: '',
134114
publicKey: '',
135115
sigName: 'sig',
136116
},
@@ -144,10 +124,8 @@ describe('processLitActionResponseStrategy', () => {
144124
success: true,
145125
signedData: {
146126
sig: {
147-
sigType: 'K256',
148-
dataSigned: 'fail',
127+
sigType: 'EcdsaK256Sha256',
149128
signatureShare: '',
150-
bigR: '',
151129
publicKey: '',
152130
sigName: 'sig',
153131
},
@@ -161,12 +139,9 @@ describe('processLitActionResponseStrategy', () => {
161139
success: true,
162140
signedData: {
163141
sig: {
164-
sigType: 'K256',
165-
dataSigned:
166-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
142+
sigType: 'EcdsaK256Sha256',
167143
signatureShare:
168144
'"E90BAE64AFA7C571CE41BEF25FF771CA2F1BC20FC09A7762200552B30ACC0CDC"',
169-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
170145
publicKey:
171146
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
172147
sigName: 'sig',
@@ -181,12 +156,9 @@ describe('processLitActionResponseStrategy', () => {
181156
success: true,
182157
signedData: {
183158
sig: {
184-
sigType: 'K256',
185-
dataSigned:
186-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
159+
sigType: 'EcdsaK256Sha256',
187160
signatureShare:
188161
'"31977D4BE7F49C0CD97CC0756CCA3244A949EA7D591F79B64F324846507448CD"',
189-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
190162
publicKey:
191163
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
192164
sigName: 'sig',
@@ -201,12 +173,9 @@ describe('processLitActionResponseStrategy', () => {
201173
success: true,
202174
signedData: {
203175
sig: {
204-
sigType: 'K256',
205-
dataSigned:
206-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
176+
sigType: 'EcdsaK256Sha256',
207177
signatureShare:
208178
'"F21798A1A37CC86566EA0D751F37CC144774A1A8A4FCD5E6E64287690FB60119"',
209-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
210179
publicKey:
211180
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
212181
sigName: 'sig',
@@ -221,12 +190,9 @@ describe('processLitActionResponseStrategy', () => {
221190
success: true,
222191
signedData: {
223192
sig: {
224-
sigType: 'K256',
225-
dataSigned:
226-
'"7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4"',
193+
sigType: 'EcdsaK256Sha256',
227194
signatureShare:
228195
'"7ECB0E020BED801905D3FE941751E4313086603BBBF21F1756832F02A6FBE567"',
229-
bigR: '"02330092EBF809B05EA0A032A42AD2FE32579D997A739D7BB4CF40EBA83B4355D3"',
230196
publicKey:
231197
'"047E3AC46588256338E62D8763592B8AA9BD13C31C9326D51CE82254A1839759A4FE7C1281AA1A9F8E810DA52B72046731CB3EE4D213799F7CE26C55A63783DB78"',
232198
sigName: 'sig',

packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ import type {
128128
NodeBlsSigningShare,
129129
NodeCommandResponse,
130130
NodeSet,
131-
NodeShare,
132131
RejectedNodePromises,
133132
SessionKeyPair,
134133
SessionSigningTemplate,
@@ -740,28 +739,21 @@ export class LitNodeClientNodeJs extends LitCore implements ILitNodeClient {
740739
);
741740

742741
// -- find the responseData that has the most common response
743-
const mostCommonResponse = findMostCommonResponse(
744-
responseData
745-
) as NodeShare;
742+
const mostCommonResponse = findMostCommonResponse(responseData);
746743

747744
const responseFromStrategy = processLitActionResponseStrategy(
748745
responseData,
749746
params.responseStrategy ?? { strategy: 'leastCommon' }
750747
);
751748
mostCommonResponse.response = responseFromStrategy;
752749

753-
const isSuccess = mostCommonResponse.success;
754750
const hasSignedData = Object.keys(mostCommonResponse.signedData).length > 0;
755751
const hasClaimData = Object.keys(mostCommonResponse.claimData).length > 0;
756752

757-
// -- we must also check for claim responses as a user may have submitted for a claim and signatures must be aggregated before returning
758-
if (isSuccess && !hasSignedData && !hasClaimData) {
759-
return mostCommonResponse as unknown as ExecuteJsResponse;
760-
}
761-
762753
// -- in the case where we are not signing anything on Lit action and using it as purely serverless function
763754
if (!hasSignedData && !hasClaimData) {
764755
return {
756+
success: mostCommonResponse.success,
765757
claims: {},
766758
signatures: {},
767759
decryptions: [],

packages/misc/src/lib/misc.spec.ts

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,83 @@ describe('utils', () => {
2828
expect((console.log as any).mock.calls[2][0]).toBe('Error Message');
2929
});
3030

31-
it('should get the most common string in an array', () => {
32-
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8];
31+
describe('mostCommonString', () => {
32+
it('should get the most common string in an array', () => {
33+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8];
3334

34-
const mostOccured = utilsModule.mostCommonString(arr);
35+
const mostOccured = utilsModule.mostCommonString(arr);
3536

36-
expect(mostOccured).toBe(8);
37-
});
37+
expect(mostOccured).toBe(8);
38+
});
39+
40+
it('should get the last element of the array if every element only appears once', () => {
41+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
42+
43+
const mostOccured = utilsModule.mostCommonString(arr);
44+
45+
expect(mostOccured).toBe(0);
46+
});
3847

39-
it('should get the last element of the array if every element only appears once', () => {
40-
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
48+
it('returns the most common element in an array of strings', () => {
49+
const arr = ['apple', 'banana', 'apple', 'orange'];
50+
expect(utilsModule.mostCommonString(arr)).toBe('apple');
51+
});
4152

42-
const mostOccured = utilsModule.mostCommonString(arr);
53+
it('returns the most common element in an array of numbers', () => {
54+
const arr = [1, 2, 2, 3, 2];
55+
expect(utilsModule.mostCommonString(arr)).toBe(2);
56+
});
57+
58+
it('returns undefined for an empty array', () => {
59+
expect(utilsModule.mostCommonString([])).toBeUndefined();
60+
});
61+
62+
it('handles tie scenarios by returning one of the tied elements', () => {
63+
const result = utilsModule.mostCommonString(['x', 'y']);
64+
expect(['x', 'y']).toContain(result);
65+
});
66+
});
4367

44-
expect(mostOccured).toBe(0);
68+
describe('findMostCommonResponse', () => {
69+
it('returns the most common responses for flat objects', () => {
70+
const responses = [
71+
{ a: 'x', b: 'y' },
72+
{ a: 'x', b: 'z' },
73+
{ a: 'w', b: 'y' },
74+
];
75+
const expected = { a: 'x', b: 'y' };
76+
expect(utilsModule.findMostCommonResponse(responses)).toEqual(expected);
77+
});
78+
79+
it('throws when given an empty array, there is no most common response', () => {
80+
expect(utilsModule.findMostCommonResponse([])).toThrow();
81+
});
82+
83+
it('handles nested objects recursively', () => {
84+
const responses = [
85+
{ a: { x: 1, y: 2 } },
86+
{ a: { x: 1, y: 3 } },
87+
{ a: { x: 2, y: 2 } },
88+
];
89+
const expected = { a: { x: 1, y: 2 } };
90+
expect(utilsModule.findMostCommonResponse(responses)).toEqual(expected);
91+
});
92+
93+
it('filters out undefined and empty string values', () => {
94+
const responses = [
95+
{ a: '', b: 10 },
96+
{ a: 5, b: 10 },
97+
{ a: 5, b: undefined },
98+
];
99+
const expected = { a: 5, b: 10 };
100+
expect(utilsModule.findMostCommonResponse(responses)).toEqual(expected);
101+
});
102+
103+
it('assigns undefined when all values for a key are filtered out', () => {
104+
const responses = [{ a: '' }, { a: undefined }];
105+
const expected = { a: undefined };
106+
expect(utilsModule.findMostCommonResponse(responses)).toEqual(expected);
107+
});
45108
});
46109

47110
it('should get value type by a given value', () => {

packages/misc/src/lib/misc.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,57 +70,61 @@ export const mostCommonString = <T>(arr: T[]): T | undefined => {
7070
.pop();
7171
};
7272

73-
export const findMostCommonResponse = (responses: object[]): object => {
73+
/**
74+
* Recursively finds the most common value for each key across an array of response objects.
75+
*
76+
* For each key found in any response object, the function aggregates all non-empty values (ignoring
77+
* `undefined` and empty strings) and determines the most frequently occurring value. If the value is an object
78+
* (and not an array), the function recurses into that object.
79+
*
80+
* @template T - The shape of the input objects in the array.
81+
* @param {T[]} responses - An array of response objects.
82+
* @returns {T} An object with each key set to its most common value across all responses.
83+
*/
84+
export const findMostCommonResponse = <T extends Record<string, any>>(
85+
responses: T[]
86+
): T => {
87+
if (responses.length === 0) {
88+
throw new Error(
89+
'findMostCommonResponse requires at least one response object'
90+
);
91+
}
92+
7493
const result: Record<string, any> = {};
7594

7695
// Aggregate all values for each key across all responses
7796
const keys = new Set(responses.flatMap(Object.keys));
7897

7998
for (const key of keys) {
80-
const values = responses.map(
81-
(response: Record<string, any>) => response[key]
82-
);
99+
const values = responses.map((response) => response[key]);
83100

84-
// Filter out undefined values before processing
101+
// Filter out undefined and empty string values before processing
85102
const filteredValues = values.filter(
86103
(value) => value !== undefined && value !== ''
87104
);
88105

89106
if (filteredValues.length === 0) {
90-
result[key] = undefined; // or set a default value if needed
107+
result[key] = undefined;
91108
} else if (
92109
typeof filteredValues[0] === 'object' &&
93110
!Array.isArray(filteredValues[0])
94111
) {
95-
// Recursive case for objects
112+
// Recursive case for nested objects
96113
result[key] = findMostCommonResponse(filteredValues);
97114
} else {
98-
// Most common element from filtered values
115+
// Determine the most common element from filtered values
99116
result[key] = mostCommonString(filteredValues);
100117
}
101118
}
102119

103-
return result;
120+
return result as T;
104121
};
105122

106123
declare global {
107-
var wasmExport: any;
108-
var wasmECDSA: any;
109124
var logger: any;
110125
var logManager: any;
111126
}
112127

113-
export const throwRemovedFunctionError = (functionName: string) => {
114-
throw new RemovedFunctionError(
115-
{
116-
info: {
117-
functionName,
118-
},
119-
},
120-
`This function "${functionName}" has been removed. Please use the old SDK.`
121-
);
122-
};
123-
124128
export const bootstrapLogManager = (
125129
id: string,
126130
level: LOG_LEVEL_VALUES = LOG_LEVEL.DEBUG

0 commit comments

Comments
 (0)