Skip to content

Commit

Permalink
Merge pull request #108 from torusresearch/feat/fnd-sig-type
Browse files Browse the repository at this point in the history
add sigType option to fetch node details
  • Loading branch information
himanshuchawla009 authored Nov 25, 2024
2 parents a3c3bc6 + 5e0dea3 commit 33f2a15
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/constants/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ export const KEY_TYPE = {
SECP256K1: "secp256k1",
ED25519: "ed25519",
} as const;

export const SIG_TYPE = {
ECDSA_SECP256K1: "ecdsa-secp256k1",
ED25519: "ed25519",
BIP340: "bip340",
} as const;
4 changes: 3 additions & 1 deletion packages/constants/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KEY_TYPE, TORUS_LEGACY_NETWORK, TORUS_SAPPHIRE_NETWORK } from "./constants";
import { KEY_TYPE, SIG_TYPE, TORUS_LEGACY_NETWORK, TORUS_SAPPHIRE_NETWORK } from "./constants";

export interface JRPCResponse<T> {
id: number;
Expand Down Expand Up @@ -35,6 +35,8 @@ export type TORUS_NETWORK_TYPE = TORUS_LEGACY_NETWORK_TYPE | TORUS_SAPPHIRE_NETW

export type WEB3AUTH_KEY_TYPE = (typeof KEY_TYPE)[keyof typeof KEY_TYPE];

export type WEB3AUTH_SIG_TYPE = (typeof SIG_TYPE)[keyof typeof SIG_TYPE];

export type NODE = {
address: string;
node_index: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/fetch-node-details/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TORUS_NETWORK_TYPE, WEB3AUTH_KEY_TYPE } from "@toruslabs/constants";
import { TORUS_NETWORK_TYPE, WEB3AUTH_KEY_TYPE, WEB3AUTH_SIG_TYPE } from "@toruslabs/constants";

export type NodeDetailManagerParams = {
network?: TORUS_NETWORK_TYPE;
keyType?: WEB3AUTH_KEY_TYPE;
sigType?: WEB3AUTH_SIG_TYPE;
fndServerEndpoint?: string;
enableLogging?: boolean;
};
10 changes: 8 additions & 2 deletions packages/fetch-node-details/src/nodeDetailManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
KEY_TYPE,
METADATA_MAP,
MULTI_CLUSTER_NETWORKS,
SIG_TYPE,
TORUS_LEGACY_NETWORK,
TORUS_LEGACY_NETWORK_TYPE,
TORUS_NETWORK_TYPE,
TORUS_SAPPHIRE_NETWORK,
WEB3AUTH_KEY_TYPE,
WEB3AUTH_SIG_TYPE,
} from "@toruslabs/constants";
import { fetchLocalConfig } from "@toruslabs/fnd-base";
import { get } from "@toruslabs/http-helpers";
Expand All @@ -26,6 +28,8 @@ class NodeDetailManager {

private _keyType: WEB3AUTH_KEY_TYPE;

private _sigType: WEB3AUTH_SIG_TYPE;

private _torusNodeEndpoints: string[] = [];

private _torusNodeRSSEndpoints: string[] = [];
Expand All @@ -45,6 +49,7 @@ class NodeDetailManager {
constructor({
network = TORUS_SAPPHIRE_NETWORK.SAPPHIRE_MAINNET,
keyType = KEY_TYPE.SECP256K1,
sigType = SIG_TYPE.ECDSA_SECP256K1,
fndServerEndpoint,
enableLogging = false,
}: NodeDetailManagerParams = {}) {
Expand All @@ -53,6 +58,7 @@ class NodeDetailManager {
}
this.network = network;
this._keyType = keyType;
this._sigType = sigType;
this.updated = false;
if (fndServerEndpoint) {
this.fndServerEndpoint = fndServerEndpoint;
Expand Down Expand Up @@ -83,7 +89,7 @@ class NodeDetailManager {

try {
const { nodeDetails } = await get<{ nodeDetails: INodeDetails }>(
`${this.fndServerEndpoint}?network=${this.network}&verifier=${verifier}&verifierId=${verifierId}&keyType=${this._keyType}`
`${this.fndServerEndpoint}?network=${this.network}&verifier=${verifier}&verifierId=${verifierId}&keyType=${this._keyType}&sigType=${this._sigType}`
);
this.setNodeDetails(nodeDetails);

Expand All @@ -92,7 +98,7 @@ class NodeDetailManager {
log.error("Failed to fetch node details from server, using local.", error);
}

const nodeDetails = fetchLocalConfig(this.network as TORUS_NETWORK_TYPE, this._keyType);
const nodeDetails = fetchLocalConfig(this.network as TORUS_NETWORK_TYPE, this._keyType, this._sigType);
if (!nodeDetails) throw new Error("Failed to fetch node details");
this.setNodeDetails(nodeDetails);
return this._nodeDetails;
Expand Down
35 changes: 33 additions & 2 deletions packages/fnd-base/src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
KEY_TYPE,
LEGACY_NETWORKS_ROUTE_MAP,
SIG_TYPE,
TORUS_LEGACY_NETWORK_TYPE,
TORUS_SAPPHIRE_NETWORK,
TORUS_SAPPHIRE_NETWORK_TYPE,
WEB3AUTH_KEY_TYPE,
WEB3AUTH_SIG_TYPE,
} from "@toruslabs/constants";

export const SAPPHIRE_NETWORK_URLS: Record<TORUS_SAPPHIRE_NETWORK_TYPE, string[]> = {
Expand Down Expand Up @@ -56,14 +58,43 @@ export const getRSSEndpoints = (sapphireNetwork: TORUS_SAPPHIRE_NETWORK_TYPE, le
export const getTSSEndpoints = (
sapphireNetwork: TORUS_SAPPHIRE_NETWORK_TYPE,
legacyNetwork?: TORUS_LEGACY_NETWORK_TYPE,
keyType = KEY_TYPE.SECP256K1 as WEB3AUTH_KEY_TYPE
keyType = KEY_TYPE.SECP256K1 as WEB3AUTH_KEY_TYPE,
sigType?: WEB3AUTH_SIG_TYPE
) => {
const endpoints = SAPPHIRE_NETWORK_URLS[sapphireNetwork];
if (!endpoints || endpoints.length === 0) {
throw new Error(`Unsupported network: ${sapphireNetwork}`);
}

const tssPath = keyType === KEY_TYPE.ED25519 ? "tss-frost" : "tss";
const tssPath = (() => {
const dklsPath = "tss";
const frostPath = "tss-frost";
if (sigType) {
if (sigType === SIG_TYPE.ECDSA_SECP256K1) {
if (keyType !== KEY_TYPE.SECP256K1) {
throw new Error("Invalid key type for ecdsa-secp256k1");
}
return dklsPath;
} else if (sigType === SIG_TYPE.ED25519) {
if (keyType !== KEY_TYPE.ED25519) {
throw new Error("Invalid key type for ed25519");
}
return frostPath;
} else if (sigType === SIG_TYPE.BIP340) {
if (keyType !== KEY_TYPE.SECP256K1) {
throw new Error("Invalid key type for bip340");
}
return frostPath;
}
throw new Error("Invalid sig type");
} else if (keyType === KEY_TYPE.SECP256K1) {
return dklsPath;
} else if (keyType === KEY_TYPE.ED25519) {
return frostPath;
}
throw new Error("Invalid key type");
})();

const routeIdentifier = LEGACY_NETWORKS_ROUTE_MAP[legacyNetwork as TORUS_LEGACY_NETWORK_TYPE];
return endpoints.map((e) => {
if (routeIdentifier && routeIdentifier.networkIdentifier) {
Expand Down
7 changes: 4 additions & 3 deletions packages/fnd-base/src/sapphireNetworkConfig.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { INodeDetails, TORUS_LEGACY_NETWORK_TYPE, TORUS_SAPPHIRE_NETWORK_TYPE, WEB3AUTH_KEY_TYPE } from "@toruslabs/constants";
import { INodeDetails, TORUS_LEGACY_NETWORK_TYPE, TORUS_SAPPHIRE_NETWORK_TYPE, WEB3AUTH_KEY_TYPE, WEB3AUTH_SIG_TYPE } from "@toruslabs/constants";

import { getRSSEndpoints, getSSSEndpoints, getTSSEndpoints } from "./endpoints";
import { SAPPHIRE_NODE_PUB_KEYS } from "./pubKeys";

export const getSapphireNodeDetails = (
sapphireNetwork: TORUS_SAPPHIRE_NETWORK_TYPE,
legacyNetwork?: TORUS_LEGACY_NETWORK_TYPE,
keyType?: WEB3AUTH_KEY_TYPE
keyType?: WEB3AUTH_KEY_TYPE,
sigType?: WEB3AUTH_SIG_TYPE
): INodeDetails => {
return {
currentEpoch: "1",
Expand All @@ -17,7 +18,7 @@ export const getSapphireNodeDetails = (

torusNodeRSSEndpoints: getRSSEndpoints(sapphireNetwork, legacyNetwork),

torusNodeTSSEndpoints: getTSSEndpoints(sapphireNetwork, legacyNetwork, keyType),
torusNodeTSSEndpoints: getTSSEndpoints(sapphireNetwork, legacyNetwork, keyType, sigType),
torusIndexes: [1, 2, 3, 4, 5],

torusNodePub: SAPPHIRE_NODE_PUB_KEYS[sapphireNetwork],
Expand Down
5 changes: 3 additions & 2 deletions packages/fnd-base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
TORUS_SAPPHIRE_NETWORK,
TORUS_SAPPHIRE_NETWORK_TYPE,
WEB3AUTH_KEY_TYPE,
WEB3AUTH_SIG_TYPE,
} from "@toruslabs/constants";

import { getSapphireNodeDetails } from "./sapphireNetworkConfig";

export function fetchLocalConfig(network: TORUS_NETWORK_TYPE, keyType: WEB3AUTH_KEY_TYPE): INodeDetails | undefined {
export function fetchLocalConfig(network: TORUS_NETWORK_TYPE, keyType: WEB3AUTH_KEY_TYPE, sigType?: WEB3AUTH_SIG_TYPE): INodeDetails | undefined {
if (Object.values(TORUS_SAPPHIRE_NETWORK).includes(network as TORUS_SAPPHIRE_NETWORK_TYPE)) {
return getSapphireNodeDetails(network as TORUS_SAPPHIRE_NETWORK_TYPE, undefined, keyType);
return getSapphireNodeDetails(network as TORUS_SAPPHIRE_NETWORK_TYPE, undefined, keyType, sigType);
}

if (Object.values(TORUS_LEGACY_NETWORK).includes(network as TORUS_LEGACY_NETWORK_TYPE)) {
Expand Down
17 changes: 14 additions & 3 deletions packages/fnd-server/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { KEY_TYPE, TORUS_LEGACY_NETWORK, TORUS_NETWORK_TYPE, TORUS_SAPPHIRE_NETWORK, WEB3AUTH_KEY_TYPE } from "@toruslabs/constants";
import {
KEY_TYPE,
SIG_TYPE,
TORUS_LEGACY_NETWORK,
TORUS_NETWORK_TYPE,
TORUS_SAPPHIRE_NETWORK,
WEB3AUTH_KEY_TYPE,
WEB3AUTH_SIG_TYPE,
} from "@toruslabs/constants";
import { fetchLocalConfig } from "@toruslabs/fnd-base";
import { celebrate, Joi } from "celebrate";
import express, { Request, Response } from "express";
Expand Down Expand Up @@ -27,16 +35,19 @@ router.get(
keyType: Joi.string()
.optional()
.allow(...Object.values(KEY_TYPE)),
sigType: Joi.string()
.optional()
.allow(...Object.values(SIG_TYPE)),
}),
},
{ allowUnknown: true }
),
async (req: Request, res: Response) => {
try {
const { network, keyType = "secp256k1" } = req.query as Record<string, string>;
const { network, keyType = KEY_TYPE.SECP256K1, sigType = SIG_TYPE.ECDSA_SECP256K1 } = req.query as Record<string, string>;
const finalNetwork = network.toLowerCase();
// use static details for sapphire mainnet and testnet
const nodeDetails = fetchLocalConfig(finalNetwork as TORUS_NETWORK_TYPE, keyType as WEB3AUTH_KEY_TYPE);
const nodeDetails = fetchLocalConfig(finalNetwork as TORUS_NETWORK_TYPE, keyType as WEB3AUTH_KEY_TYPE, sigType as WEB3AUTH_SIG_TYPE);

return res.status(200).json({
nodeDetails,
Expand Down

0 comments on commit 33f2a15

Please sign in to comment.