Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cayenne remappings #453

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/constants/src/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ export const SYMM_KEY_ALGO_PARAMS = {
length: 256,
};

/**
* Default node URL for Cayenne network
*/
export const CAYENNE_URL = 'https://cayenne.litgateway.com';

/**
* Default node URLs for each LIT network
* Note: Dynamic networks such as Habanero have no default node URLS; they are always
Expand All @@ -739,9 +744,9 @@ export const LIT_NETWORKS: { [key in LitNetwork]: string[] } & {
internalDev: string[];
} = {
[LitNetwork.Cayenne]: [
'https://cayenne.litgateway.com:7370',
'https://cayenne.litgateway.com:7371',
'https://cayenne.litgateway.com:7372',
`${CAYENNE_URL}:7370'`,
`${CAYENNE_URL}:7371'`,
`${CAYENNE_URL}:7372'`,
Comment on lines +749 to +751

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious why did we make this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array is actually not used anymore, we can remove it. I didn't because there are interfaces/types are relying on it? Need to investigate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay i was thinking it could be removed now. Making a note of this for myself @joshLong145

],
[LitNetwork.Manzano]: [],
[LitNetwork.Habanero]: [],
Expand Down
29 changes: 26 additions & 3 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import {
StakingStates,
version,
LIT_ENDPOINT,
CAYENNE_URL,
} from '@lit-protocol/constants';
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { checkSevSnpAttestation, computeHDPubKey } from '@lit-protocol/crypto';
import {
bootstrapLogManager,
executeWithRetry,
getIpAddress,
isBrowser,
isNode,
log,
Expand Down Expand Up @@ -259,13 +261,34 @@ export class LitCore {
this.config.litNetwork
);

const minNodeCount = (
await LitContracts.getMinNodeCount(this.config.litNetwork)
).toNumber();

const bootstrapUrls = await LitContracts.getValidators(
this.config.litNetwork
);

/**
* FIXME: We need this reformatting because the Cayenne network is not able to handle the node address as a URL.
* from: https://cayenne.litgateway.com
* to: http://207.244.70.36:7474
*/
const remappedBootstrapUrls = await Promise.all(
bootstrapUrls.map(async (url) => {
const rootDomain = CAYENNE_URL.replace('https://', '');
const ipAddress = await getIpAddress(rootDomain);
return `http://${ipAddress}:${new URL(url).port}`;
})
);

// If the network is cayenne it is a centralized testnet, so we use a static config
// This is due to staking contracts holding local ip / port contexts which are innacurate to the ip / port exposed to the world
this.config.bootstrapUrls = LIT_NETWORKS.cayenne;
this.config.bootstrapUrls = remappedBootstrapUrls;
this.config.minNodeCount =
LIT_NETWORKS.cayenne.length == 2
remappedBootstrapUrls.length == 2
? 2
: (LIT_NETWORKS.cayenne.length * 2) / 3;
: (remappedBootstrapUrls.length * 2) / 3;

/**
* Here we are checking if a custom network defined with no node urls (bootstrap urls) defined
Expand Down
23 changes: 19 additions & 4 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 @@ -2418,10 +2418,25 @@ const resourceAbilityRequests = [
const signatures: SessionSigsMap = {};

this.connectedNodes.forEach((nodeAddress: string) => {
const toSign: SessionSigningTemplate = {
...signingTemplate,
nodeAddress,
};
let toSign: SessionSigningTemplate;

// FIXME: We need this reformatting because the Cayenne network is not able to handle the node address as a URL.
// We are converting back
// from: http://207.244.70.36:7474
// to: 127.0.0.1:7474
if (this.config.litNetwork === LitNetwork.Cayenne) {
const url = new URL(nodeAddress);
const newNodeAddress = `127.0.0.1:${url.port}`;
toSign = {
...signingTemplate,
nodeAddress: newNodeAddress,
};
} else {
toSign = {
...signingTemplate,
nodeAddress,
};
}

const signedMessage = JSON.stringify(toSign);

Expand Down
11 changes: 10 additions & 1 deletion packages/misc/src/lib/misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ global.TextEncoder = TextEncoder;
// @ts-ignore
global.TextDecoder = TextDecoder;

import { LitErrorKind, LIT_ERROR } from '@lit-protocol/constants';
import { LitErrorKind, LIT_ERROR, CAYENNE_URL } from '@lit-protocol/constants';
import * as utilsModule from './misc';

describe('utils', () => {
Expand Down Expand Up @@ -288,3 +288,12 @@ it('should not remove hex prefix if it is not present', () => {

expect(result).toBe(expectedOutput);
});

it('should get ip address', async () => {
// polyfill fetch
const fetch = require('node-fetch');
global.fetch = fetch;

const ipAddres = await utilsModule.getIpAddress('cayenne.litgateway.com');
expect(ipAddres).toBe('1');
});
23 changes: 23 additions & 0 deletions packages/misc/src/lib/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,26 @@ export function normalizeAndStringify(input: string): string {
return normalizeAndStringify(unescaped);
}
}

/**
* Retrieves the IP address associated with a given domain.
* @param domain - The domain for which to retrieve the IP address.
* @returns A Promise that resolves to the IP address.
* @throws If no IP address is found or if the domain name is invalid.
*/
export async function getIpAddress(domain: string): Promise<string> {
const apiURL = `https://dns.google/resolve?name=${domain}&type=A`;

try {
const response = await fetch(apiURL);
const data = await response.json();

if (data.Answer && data.Answer.length > 0) {
return data.Answer[0].data;
} else {
throw new Error('No IP Address found or bad domain name');
}
} catch (error: any) {
throw new Error(error);
}
}
Loading