Skip to content

Commit

Permalink
add sigType option to fetch node details
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeihs committed Nov 25, 2024
1 parent a3c3bc6 commit 01f6978
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 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
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

0 comments on commit 01f6978

Please sign in to comment.