Skip to content

Commit 2b88caf

Browse files
committed
feat: add nodeSet to the requests
1 parent f781d9e commit 2b88caf

File tree

4 files changed

+100
-65
lines changed

4 files changed

+100
-65
lines changed

packages/contracts-sdk/src/lib/contracts-sdk.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,6 @@ export class LitContracts {
11291129
});
11301130

11311131
const networks = activeValidatorStructs.map((item: ValidatorStruct) => {
1132-
const centralisation = CENTRALISATION_BY_NETWORK[litNetwork];
11331132

11341133
// Convert the integer IP to a string format
11351134
const ip = intToIP(item.ip);

packages/core/src/lib/lit-core.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
NodeClientErrorV1,
7171
NodeCommandServerKeysResponse,
7272
NodeErrorV3,
73+
NodeSet,
7374
RejectedNodePromises,
7475
SendNodeCommand,
7576
SessionSigsMap,
@@ -387,6 +388,30 @@ export class LitCore {
387388
}
388389
}
389390

391+
/**
392+
* Gets the set of nodes from validator data, transforming bootstrap URLs into NodeSet objects.
393+
*
394+
* @returns {Promise<NodeSet[]>} A promise that resolves with an array of NodeSet objects.
395+
*/
396+
protected _getNodeSet = async (): Promise<NodeSet[]> => {
397+
const validatorData = await this._getValidatorData();
398+
const bootstrapUrls = validatorData.bootstrapUrls;
399+
400+
const nodeSet = bootstrapUrls.map((url) => {
401+
// remove protocol from the url as we only need ip:port
402+
const urlWithoutProtocol = url.replace(/(^\w+:|^)\/\//, '') as string;
403+
404+
return {
405+
socketAddress: urlWithoutProtocol,
406+
407+
// FIXME: This is a placeholder value, we need to get the actual value from the contract
408+
value: 1,
409+
};
410+
});
411+
412+
return nodeSet;
413+
};
414+
390415
/**
391416
* Stops internal listeners/polling that refresh network state and watch for epoch changes.
392417
* Removes global objects created internally
@@ -674,11 +699,9 @@ export class LitCore {
674699
await Promise.race([
675700
new Promise((_resolve, reject) => {
676701
timeoutHandle = setTimeout(() => {
677-
const msg = `Error: Could not handshake with nodes after timeout of ${
678-
this.config.connectTimeout
679-
}ms. Could only connect to ${Object.keys(serverKeys).length} of ${
680-
this.config.bootstrapUrls.length
681-
} nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
702+
const msg = `Error: Could not handshake with nodes after timeout of ${this.config.connectTimeout
703+
}ms. Could only connect to ${Object.keys(serverKeys).length} of ${this.config.bootstrapUrls.length
704+
} nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
682705

683706
try {
684707
throw new InitError({}, msg);
@@ -1006,8 +1029,8 @@ export class LitCore {
10061029
this._epochCache.currentNumber &&
10071030
this._epochCache.startTime &&
10081031
Math.floor(Date.now() / 1000) <
1009-
this._epochCache.startTime +
1010-
Math.floor(EPOCH_PROPAGATION_DELAY / 1000) &&
1032+
this._epochCache.startTime +
1033+
Math.floor(EPOCH_PROPAGATION_DELAY / 1000) &&
10111034
this._epochCache.currentNumber >= 3 // FIXME: Why this check?
10121035
) {
10131036
return this._epochCache.currentNumber - 1;
@@ -1038,7 +1061,7 @@ export class LitCore {
10381061
data,
10391062
requestId,
10401063
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
1041-
SendNodeCommand): Promise<any> => {
1064+
SendNodeCommand): Promise<any> => {
10421065
// FIXME: Replace <any> usage with explicit, strongly typed handlers
10431066
data = { ...data, epoch: this.currentEpochNumber };
10441067

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ import type {
136136

137137
export class LitNodeClientNodeJs
138138
extends LitCore
139-
implements LitClientSessionManager, ILitNodeClient
140-
{
139+
implements LitClientSessionManager, ILitNodeClient {
141140
defaultAuthCallback?: (authSigParams: AuthCallbackParams) => Promise<AuthSig>;
142141

143142
// ========== Constructor ==========
@@ -785,6 +784,8 @@ export class LitNodeClientNodeJs
785784
url,
786785
});
787786

787+
// FIXME - will be removing this function in another PR. Temporary fix.
788+
// @ts-ignore
788789
const reqBody: JsonExecutionRequestTargetNode = {
789790
...params,
790791
targetNodeRange: params.targetNodeRange,
@@ -865,9 +866,12 @@ export class LitNodeClientNodeJs
865866
url,
866867
});
867868

869+
const nodeSet = await this._getNodeSet();
870+
868871
const reqBody: JsonExecutionRequest = {
869872
...formattedParams,
870873
authSig: sessionSig,
874+
nodeSet
871875
};
872876

873877
const urlWithPath = composeLitUrl({
@@ -1139,6 +1143,8 @@ export class LitNodeClientNodeJs
11391143
);
11401144
}
11411145

1146+
const nodeSet = await this._getNodeSet();
1147+
11421148
// ========== Get Node Promises ==========
11431149
// Handle promises for commands sent to Lit nodes
11441150

@@ -1157,8 +1163,10 @@ export class LitNodeClientNodeJs
11571163
// -- optional params
11581164
...(params.authMethods &&
11591165
params.authMethods.length > 0 && {
1160-
authMethods: params.authMethods,
1161-
}),
1166+
authMethods: params.authMethods,
1167+
}),
1168+
1169+
nodeSet
11621170
};
11631171

11641172
logWithRequestId(requestId, 'reqBody:', reqBody);
@@ -1884,8 +1892,8 @@ export class LitNodeClientNodeJs
18841892
const sessionCapabilityObject = params.sessionCapabilityObject
18851893
? params.sessionCapabilityObject
18861894
: await this.generateSessionCapabilityObjectWithWildcards(
1887-
params.resourceAbilityRequests.map((r) => r.resource)
1888-
);
1895+
params.resourceAbilityRequests.map((r) => r.resource)
1896+
);
18891897
const expiration = params.expiration || LitNodeClientNodeJs.getExpiration();
18901898

18911899
// -- (TRY) to get the wallet signature
@@ -1916,8 +1924,6 @@ export class LitNodeClientNodeJs
19161924
resourceAbilityRequests: params.resourceAbilityRequests,
19171925
});
19181926

1919-
// console.log('XXX needToResignSessionKey:', needToResignSessionKey);
1920-
19211927
// -- (CHECK) if we need to resign the session key
19221928
if (needToResignSessionKey) {
19231929
log('need to re-sign session key. Signing...');
@@ -1969,28 +1975,30 @@ export class LitNodeClientNodeJs
19691975

19701976
const capabilities = params.capacityDelegationAuthSig
19711977
? [
1972-
...(params.capabilityAuthSigs ?? []),
1973-
params.capacityDelegationAuthSig,
1974-
authSig,
1975-
]
1978+
...(params.capabilityAuthSigs ?? []),
1979+
params.capacityDelegationAuthSig,
1980+
authSig,
1981+
]
19761982
: [...(params.capabilityAuthSigs ?? []), authSig];
19771983

1978-
const signingTemplate = {
1984+
// This is the template that will be combined with the node address as a single object, then signed by the session key
1985+
// so that the node can verify the session signature
1986+
const sessionSigningTemplate = {
19791987
sessionKey: sessionKey.publicKey,
19801988
resourceAbilityRequests: params.resourceAbilityRequests,
19811989
capabilities,
19821990
issuedAt: new Date().toISOString(),
19831991
expiration: sessionExpiration,
19841992

1985-
// FIX ME: This is a dummy value for now
1993+
// FIXME: This is a dummy value for now
19861994
maxPrice: '0x1234567890abcdef1234567890abcdef12345678',
19871995
};
19881996

1989-
const signatures: SessionSigsMap = {};
1997+
const sessionSigs: SessionSigsMap = {};
19901998

19911999
this.connectedNodes.forEach((nodeAddress: string) => {
19922000
const toSign: SessionSigningTemplate = {
1993-
...signingTemplate,
2001+
...sessionSigningTemplate,
19942002
nodeAddress,
19952003
};
19962004

@@ -2004,7 +2012,7 @@ export class LitNodeClientNodeJs
20042012
const uint8arrayMessage = uint8arrayFromString(signedMessage, 'utf8');
20052013
const signature = nacl.sign.detached(uint8arrayMessage, uint8arrayKey);
20062014

2007-
signatures[nodeAddress] = {
2015+
sessionSigs[nodeAddress] = {
20082016
sig: uint8arrayToString(signature, 'base16'),
20092017
derivedVia: 'litSessionSignViaNacl',
20102018
signedMessage: signedMessage,
@@ -2013,19 +2021,19 @@ export class LitNodeClientNodeJs
20132021
};
20142022
});
20152023

2016-
log('signatures:', signatures);
2024+
log('sessionSigs:', sessionSigs);
20172025

20182026
try {
20192027
const formattedSessionSigs = formatSessionSigs(
2020-
JSON.stringify(signatures)
2028+
JSON.stringify(sessionSigs)
20212029
);
20222030
log(formattedSessionSigs);
20232031
} catch (e) {
20242032
// swallow error
20252033
log('Error formatting session signatures: ', e);
20262034
}
20272035

2028-
return signatures;
2036+
return sessionSigs;
20292037
};
20302038

20312039
/**

0 commit comments

Comments
 (0)