Skip to content

Commit

Permalink
Merge pull request #554 from LIT-Protocol/feature/lit-3660-js-sdk-fea…
Browse files Browse the repository at this point in the history
…ture-allow-custom-port-on-custom-or-localhost

feat: add custom `nodeProtocol` and enable `https` for port `443`
  • Loading branch information
Ansonhkg authored Jul 24, 2024
2 parents 66c6af0 + 3d4f710 commit 79167a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
NETWORK: datil-dev
DEBUG: true
MAX_ATTEMPTS: 3
CHUNK_SIZE: 9
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand All @@ -55,4 +56,4 @@ jobs:
run: yarn build:dev
- name: Run End to End Tests
if: steps.build.outputs.exit_code == 0
run: yarn test:local --filter=testUseEoaSessionSigsToExecuteJsSigning,testUseEoaSessionSigsToPkpSign,testUsePkpSessionSigsToExecuteJsSigning,testUsePkpSessionSigsToPkpSign,testUseValidLitActionCodeGeneratedSessionSigsToPkpSign,testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning,testDelegatingCapacityCreditsNFTToAnotherWalletToExecuteJs,testEthAuthSigToEncryptDecryptString,testExecuteJsSignAndCombineEcdsa,testExecutJsDecryptAndCombine,testExecuteJsBroadcastAndCollect --exclude=Parallel
run: yarn test:local --filter=testUseEoaSessionSigsToExecuteJsSigning,testUseEoaSessionSigsToPkpSign,testUsePkpSessionSigsToExecuteJsSigning,testUsePkpSessionSigsToPkpSign,testUseValidLitActionCodeGeneratedSessionSigsToPkpSign,testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning,testDelegatingCapacityCreditsNFTToAnotherWalletToExecuteJs,testEthAuthSigToEncryptDecryptString,testExecuteJsSignAndCombineEcdsa,testExecutJsDecryptAndCombine,testExecuteJsBroadcastAndCollect --exclude=Parallel
9 changes: 5 additions & 4 deletions packages/constants/src/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,8 @@ export const METAMASK_CHAIN_INFO_BY_NETWORK: Record<
localhost: metamaskChainInfo.yellowstone,
};

const HTTP = 'http://';
const HTTPS = 'https://';
export const HTTP = 'http://';
export const HTTPS = 'https://';

/**
* Mapping of network values to corresponding http protocol.
Expand All @@ -804,8 +804,9 @@ export const HTTP_BY_NETWORK: Record<
habanero: HTTPS,
'datil-dev': HTTPS,
'datil-test': HTTPS,
custom: HTTP,
localhost: HTTP,
internalDev: HTTPS,
custom: HTTP, // default, can be changed by config
localhost: HTTP, // default, can be changed by config
};

/**
Expand Down
24 changes: 19 additions & 5 deletions packages/contracts-sdk/src/lib/contracts-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import {
HTTP_BY_NETWORK,
CENTRALISATION_BY_NETWORK,
LIT_NETWORK,
HTTP,
HTTPS,
} from '@lit-protocol/constants';
import { LogManager, Logger } from '@lit-protocol/logger';
import { computeAddress } from 'ethers/lib/utils';
Expand Down Expand Up @@ -888,7 +890,8 @@ export class LitContracts {
public static getValidators = async (
network: LIT_NETWORKS_KEYS,
context?: LitContractContext | LitContractResolverContext,
rpcUrl?: string
rpcUrl?: string,
nodeProtocol?: typeof HTTP | typeof HTTPS | null
): Promise<string[]> => {
const contract = await LitContracts.getStakingContract(
network,
Expand Down Expand Up @@ -941,13 +944,24 @@ export class LitContracts {
});

const networks = activeValidatorStructs.map((item: ValidatorStruct) => {
const protocol = HTTP_BY_NETWORK[network];
const centralisation = CENTRALISATION_BY_NETWORK[network];
const ip = intToIP(item.ip);
const port = item.port;

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

// Determine the protocol to use based on various conditions
const protocol =
// If nodeProtocol is defined, use it
nodeProtocol ||
// If port is 443, use HTTPS, otherwise use network-specific HTTP
(port === 443 ? HTTPS : HTTP_BY_NETWORK[network]) ||
// Fallback to HTTP if no other conditions are met
HTTP;

// Check for specific conditions in centralised networks
if (centralisation === 'centralised') {
// -- validate if it's cayenne AND port range is 8470 - 8479, if not, throw error
// Validate if it's cayenne AND port range is 8470 - 8479, if not, throw error
if (
network === LIT_NETWORK.Cayenne &&
!port.toString().startsWith('8')
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
RPC_URL_BY_NETWORK,
StakingStates,
version,
HTTP,
HTTPS,
} from '@lit-protocol/constants';
import { LitContracts } from '@lit-protocol/contracts-sdk';
import {
Expand Down Expand Up @@ -99,6 +101,8 @@ export type LitNodeClientConfigWithDefaults = Required<
Pick<LitNodeClientConfig, 'storageProvider' | 'contractContext' | 'rpcUrl'>
> & {
bootstrapUrls: string[];
} & {
nodeProtocol?: typeof HTTP | typeof HTTPS | null;
};

// On epoch change, we wait this many seconds for the nodes to update to the new epoch before using the new epoch #
Expand Down Expand Up @@ -131,6 +135,7 @@ export class LitCore {
litNetwork: 'cayenne', // Default to cayenne network. will be replaced by custom config.
minNodeCount: 2, // Default value, should be replaced
bootstrapUrls: [], // Default value, should be replaced
nodeProtocol: null,
};
connectedNodes = new Set<string>();
serverKeys: Record<string, JsonHandshakeResponse> = {};
Expand Down Expand Up @@ -234,7 +239,8 @@ export class LitCore {
LitContracts.getValidators(
this.config.litNetwork,
this.config.contractContext,
this.config.rpcUrl
this.config.rpcUrl,
this.config.nodeProtocol
),
]);

Expand Down

0 comments on commit 79167a1

Please sign in to comment.