Skip to content

Commit d141b0d

Browse files
committed
feat: add customResources to sessionSigs
1 parent 593fb9e commit d141b0d

File tree

5 files changed

+97
-71
lines changed

5 files changed

+97
-71
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export function parseCustomResources(nodeResponses: any) {
2+
return ((nodeResponses.values.map((item: any) => {
3+
return {
4+
signedMessage: item.siweMessage
5+
};
6+
}).map((v: any) => {
7+
const signedMessage = v.signedMessage;
8+
const urnLine = signedMessage.match(/urn:recap:[\w\d]+/)![0];
9+
10+
const authContext = JSON.parse(atob(urnLine.split(':')[2])).att['lit-resolvedauthcontext://*']['Auth/Auth'][0]['auth_context'];
11+
12+
const extractedCustomAuthResource = (authContext['customAuthResource']).slice(8, -2);
13+
const formattedCustomAuthResource = extractedCustomAuthResource.replace(/\\"/g, '"');
14+
let result;
15+
16+
try {
17+
result = JSON.parse(formattedCustomAuthResource);
18+
} catch (e) {
19+
result = extractedCustomAuthResource
20+
}
21+
22+
return {
23+
authContext,
24+
formattedCustomAuthResource: result,
25+
};
26+
}).find((item: any) => item.formattedCustomAuthResource !== 'undefined')))?.formattedCustomAuthResource
27+
}

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import { parsePkpSignResponse } from './helpers/parse-pkp-sign-response';
7676
import { processLitActionResponseStrategy } from './helpers/process-lit-action-response-strategy';
7777
import { removeDoubleQuotes } from './helpers/remove-double-quotes';
7878
import { blsSessionSigVerify } from './helpers/validate-bls-session-sig';
79+
import { parseCustomResources } from './helpers/parseCustomResources';
7980

8081
import type {
8182
AuthCallback,
@@ -135,8 +136,7 @@ import type {
135136

136137
export class LitNodeClientNodeJs
137138
extends LitCore
138-
implements LitClientSessionManager, ILitNodeClient
139-
{
139+
implements LitClientSessionManager, ILitNodeClient {
140140
defaultAuthCallback?: (authSigParams: AuthCallbackParams) => Promise<AuthSig>;
141141

142142
// ========== Constructor ==========
@@ -1293,8 +1293,8 @@ export class LitNodeClientNodeJs
12931293
// -- optional params
12941294
...(params.authMethods &&
12951295
params.authMethods.length > 0 && {
1296-
authMethods: params.authMethods,
1297-
}),
1296+
authMethods: params.authMethods,
1297+
}),
12981298
};
12991299

13001300
logWithRequestId(requestId, 'reqBody:', reqBody);
@@ -1880,18 +1880,35 @@ export class LitNodeClientNodeJs
18801880
});
18811881

18821882
// -- resolve promises
1883+
const numberToResolve = params?.handleAllResponses ? this.connectedNodes.size : this.config.minNodeCount;
1884+
1885+
console.log("numberToResolve:", numberToResolve);
18831886
let res;
18841887
try {
18851888
res = await this.handleNodePromises(
18861889
nodePromises,
18871890
requestId,
1888-
this.config.minNodeCount
1891+
numberToResolve,
18891892
);
18901893
log('signSessionKey node promises:', res);
18911894
} catch (e) {
18921895
throw new Error(`Error when handling node promises: ${e}`);
18931896
}
18941897

1898+
// -- handle all responses
1899+
let customResources;
1900+
1901+
1902+
console.log("res:", res);
1903+
1904+
try {
1905+
customResources = parseCustomResources(res);
1906+
} catch (e) {
1907+
log(`Failed to parse custom resources, not a problem.`)
1908+
}
1909+
1910+
console.log("customResources:", customResources);
1911+
18951912
logWithRequestId(requestId, 'handleNodePromises res:', res);
18961913

18971914
// -- case: promises rejected
@@ -2035,6 +2052,7 @@ export class LitNodeClientNodeJs
20352052
derivedVia: 'lit.bls',
20362053
signedMessage,
20372054
address: computeAddress(hexPrefixed(publicKey)),
2055+
...(customResources && { customResources: customResources })
20382056
},
20392057
pkpPublicKey: publicKey,
20402058
};
@@ -2096,8 +2114,8 @@ export class LitNodeClientNodeJs
20962114
const sessionCapabilityObject = params.sessionCapabilityObject
20972115
? params.sessionCapabilityObject
20982116
: await this.generateSessionCapabilityObjectWithWildcards(
2099-
params.resourceAbilityRequests.map((r) => r.resource)
2100-
);
2117+
params.resourceAbilityRequests.map((r) => r.resource)
2118+
);
21012119
const expiration = params.expiration || LitNodeClientNodeJs.getExpiration();
21022120

21032121
// -- (TRY) to get the wallet signature
@@ -2180,10 +2198,10 @@ export class LitNodeClientNodeJs
21802198

21812199
const capabilities = params.capacityDelegationAuthSig
21822200
? [
2183-
...(params.capabilityAuthSigs ?? []),
2184-
params.capacityDelegationAuthSig,
2185-
authSig,
2186-
]
2201+
...(params.capabilityAuthSigs ?? []),
2202+
params.capacityDelegationAuthSig,
2203+
authSig,
2204+
]
21872205
: [...(params.capabilityAuthSigs ?? []), authSig];
21882206

21892207
const signingTemplate = {
@@ -2318,6 +2336,8 @@ export class LitNodeClientNodeJs
23182336
litActionIpfsId: props.litActionIpfsId,
23192337
}),
23202338
...(props.jsParams && { jsParams: props.jsParams }),
2339+
2340+
...(params.handleAllResponses && { handleAllResponses: params.handleAllResponses })
23212341
});
23222342

23232343
return response.authSig;

packages/types/src/lib/interfaces.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export interface JsonSignChainDataRequest {
314314

315315
export interface JsonSignSessionKeyRequestV1
316316
extends Pick<LitActionSdkParams, 'jsParams'>,
317-
Pick<LitActionSdkParams, 'litActionIpfsId'> {
317+
Pick<LitActionSdkParams, 'litActionIpfsId'> {
318318
sessionKey: string;
319319
authMethods: AuthMethod[];
320320
pkpPublicKey?: string;
@@ -503,7 +503,7 @@ export interface JsonExecutionSdkParamsTargetNode
503503

504504
export interface JsonExecutionSdkParams
505505
extends Pick<LitActionSdkParams, 'jsParams'>,
506-
ExecuteJsAdvancedOptions {
506+
ExecuteJsAdvancedOptions {
507507
/**
508508
* JS code to run on the nodes
509509
*/
@@ -579,7 +579,7 @@ export interface SessionSigsOrAuthSig {
579579

580580
export interface DecryptRequestBase
581581
extends SessionSigsOrAuthSig,
582-
MultipleAccessControlConditions {
582+
MultipleAccessControlConditions {
583583
/**
584584
* The chain name of the chain that this contract is deployed on. See LIT_CHAINS for currently supported chains.
585585
*/
@@ -624,7 +624,7 @@ export interface EncryptFileRequest extends DecryptRequestBase {
624624
file: AcceptedFileType;
625625
}
626626

627-
export interface DecryptRequest extends EncryptResponse, DecryptRequestBase {}
627+
export interface DecryptRequest extends EncryptResponse, DecryptRequestBase { }
628628

629629
export interface DecryptResponse {
630630
// The decrypted data as a Uint8Array
@@ -646,10 +646,10 @@ export interface SigResponse {
646646

647647
export interface ExecuteJsResponseBase {
648648
signatures:
649-
| {
650-
sig: SigResponse;
651-
}
652-
| any;
649+
| {
650+
sig: SigResponse;
651+
}
652+
| any;
653653
}
654654

655655
/**
@@ -679,7 +679,7 @@ export interface ExecuteJsNoSigningResponse extends ExecuteJsResponseBase {
679679
logs: string;
680680
}
681681

682-
export interface LitNodePromise {}
682+
export interface LitNodePromise { }
683683

684684
export interface SendNodeCommand {
685685
url: string;
@@ -688,10 +688,10 @@ export interface SendNodeCommand {
688688
}
689689
export interface SigShare {
690690
sigType:
691-
| 'BLS'
692-
| 'K256'
693-
| 'ECDSA_CAIT_SITH' // Legacy alias of K256
694-
| 'EcdsaCaitSithP256';
691+
| 'BLS'
692+
| 'K256'
693+
| 'ECDSA_CAIT_SITH' // Legacy alias of K256
694+
| 'EcdsaCaitSithP256';
695695

696696
signatureShare: string;
697697
shareIndex?: number;
@@ -1091,6 +1091,11 @@ export interface SignSessionKeyProp extends LitActionSdkParams {
10911091
* A LIT resource ability is a combination of a LIT resource and a LIT ability.
10921092
*/
10931093
resourceAbilityRequests?: LitResourceAbilityRequest[];
1094+
1095+
/**
1096+
* By default, it handles minNodeCount of responses
1097+
*/
1098+
handleAllResponses?: boolean;
10941099
}
10951100

10961101
export interface SignSessionKeyResponse {
@@ -1159,7 +1164,7 @@ export interface CommonGetSessionSigsProps {
11591164

11601165
export interface BaseProviderGetSessionSigsProps
11611166
extends CommonGetSessionSigsProps,
1162-
LitActionSdkParams {
1167+
LitActionSdkParams {
11631168
/**
11641169
* This is a callback that will be used to generate an AuthSig within the session signatures. It's inclusion is required, as it defines the specific resources and abilities that will be allowed for the current session.
11651170
*/
@@ -1168,7 +1173,7 @@ export interface BaseProviderGetSessionSigsProps
11681173

11691174
export interface GetSessionSigsProps
11701175
extends CommonGetSessionSigsProps,
1171-
LitActionSdkParams {
1176+
LitActionSdkParams {
11721177
/**
11731178
* This is a callback that will be used to generate an AuthSig within the session signatures. It's inclusion is required, as it defines the specific resources and abilities that will be allowed for the current session.
11741179
*/
@@ -1692,7 +1697,7 @@ export interface LoginUrlParams {
16921697
error: string | null;
16931698
}
16941699

1695-
export interface BaseAuthenticateOptions {}
1700+
export interface BaseAuthenticateOptions { }
16961701

16971702
export interface EthWalletAuthenticateOptions extends BaseAuthenticateOptions {
16981703
/**
@@ -1758,9 +1763,9 @@ export interface MintCapacityCreditsPerKilosecond
17581763
}
17591764
export interface MintCapacityCreditsContext
17601765
extends MintCapacityCreditsPerDay,
1761-
MintCapacityCreditsPerSecond,
1762-
MintCapacityCreditsPerKilosecond,
1763-
GasLimitParam {}
1766+
MintCapacityCreditsPerSecond,
1767+
MintCapacityCreditsPerKilosecond,
1768+
GasLimitParam { }
17641769
export interface MintCapacityCreditsRes {
17651770
rliTxHash: string;
17661771
capacityTokenId: any;
@@ -1883,12 +1888,12 @@ export interface LitActionSdkParams {
18831888
* An object that contains params to expose to the Lit Action. These will be injected to the JS runtime before your code runs, so you can use any of these as normal variables in your Lit Action.
18841889
*/
18851890
jsParams?:
1886-
| {
1887-
[key: string]: any;
1888-
publicKey?: string;
1889-
sigName?: string;
1890-
}
1891-
| any;
1891+
| {
1892+
[key: string]: any;
1893+
publicKey?: string;
1894+
sigName?: string;
1895+
}
1896+
| any;
18921897
}
18931898

18941899
export interface LitEndpoint {
@@ -1910,7 +1915,7 @@ export interface SignerLike {
19101915

19111916
export interface GetPkpSessionSigs
19121917
extends CommonGetSessionSigsProps,
1913-
LitActionSdkParams {
1918+
LitActionSdkParams {
19141919
pkpPublicKey: string;
19151920

19161921
/**
@@ -1924,6 +1929,8 @@ export interface GetPkpSessionSigs
19241929
authMethods?: AuthMethod[];
19251930

19261931
ipfsOptions?: IpfsOptions;
1932+
1933+
handleAllResponses?: boolean;
19271934
}
19281935

19291936
/**
@@ -1936,18 +1943,18 @@ export type GetLitActionSessionSigs = CommonGetSessionSigsProps &
19361943
Pick<Required<LitActionSdkParams>, 'jsParams'> &
19371944
(
19381945
| (Pick<Required<LitActionSdkParams>, 'litActionCode'> & {
1939-
litActionIpfsId?: never;
1940-
})
1946+
litActionIpfsId?: never;
1947+
})
19411948
| (Pick<Required<LitActionSdkParams>, 'litActionIpfsId'> & {
1942-
litActionCode?: never;
1943-
})
1949+
litActionCode?: never;
1950+
})
19441951
) & {
19451952
ipfsOptions?: IpfsOptions;
19461953

19471954
/**
19481955
* Special property to return all node responses
19491956
*/
1950-
getNodeResponses?: boolean;
1957+
handleAllResponses?: boolean;
19511958
};
19521959

19531960
export interface SessionKeyCache {

packages/wrapped-keys/src/lib/api/tria-batch-generate-private-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export async function triaBatchGeneratePrivateKeys(
9393
],
9494
litActionIpfsId: params.ipfsId,
9595
jsParams: _jsParams,
96-
getNodeResponses: true,
96+
handleAllResponses: true,
9797
});
9898
} catch (e) {
9999
throw new Error(`Error getting Lit Action Session Sigs: ${e}`);

packages/wrapped-keys/src/lib/api/utils.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,4 @@ export function getPkpAccessControlCondition(
107107
value: pkpAddress,
108108
},
109109
};
110-
}
111-
112-
export function parseCustoemrResourcesFromNodeResponses(responses: any) {
113-
return ((responses.map((item: any) => {
114-
return {
115-
signedMessage: item.siweMessage
116-
};
117-
}).map((v: any) => {
118-
const signedMessage = v.signedMessage;
119-
const urnLine = signedMessage.match(/urn:recap:[\w\d]+/)![0];
120-
121-
const authContext = JSON.parse(atob(urnLine.split(':')[2])).att['lit-resolvedauthcontext://*']['Auth/Auth'][0]['auth_context'];
122-
123-
const extractedCustomAuthResource = (authContext['customAuthResource']).slice(8, -2);
124-
const formattedCustomAuthResource = extractedCustomAuthResource.replace(/\\"/g, '"');
125-
let result;
126-
127-
try {
128-
result = JSON.parse(formattedCustomAuthResource);
129-
} catch (e) {
130-
result = extractedCustomAuthResource
131-
}
132-
133-
return {
134-
authContext,
135-
formattedCustomAuthResource: result,
136-
};
137-
}).find((item: any) => item.formattedCustomAuthResource !== 'undefined')))?.formattedCustomAuthResource
138110
}

0 commit comments

Comments
 (0)