Skip to content

Commit

Permalink
mend
Browse files Browse the repository at this point in the history
  • Loading branch information
cnupy committed Mar 24, 2024
1 parent 570c4b6 commit f1651c5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/cmds/validator/signers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function getSignersFromArgs(

// Remote keys are declared manually or will be fetched from external signer
else if (args["externalSigner.pubkeys"] || args["externalSigner.fetch"]) {
return getRemoteSigners(args);
return getRemoteSigners(args, logger as Logger);
}

// Read keys from local account manager
Expand Down Expand Up @@ -155,7 +155,7 @@ export function getSignerPubkeyHex(signer: Signer): string {
}
}

async function getRemoteSigners(args: IValidatorCliArgs & GlobalArgs): Promise<Signer[]> {
async function getRemoteSigners(args: IValidatorCliArgs & GlobalArgs, logger?: Logger): Promise<Signer[]> {
const externalSignerUrl = args["externalSigner.url"];
if (!externalSignerUrl) {
throw new YargsError(
Expand All @@ -171,7 +171,7 @@ async function getRemoteSigners(args: IValidatorCliArgs & GlobalArgs): Promise<S
throw new YargsError("externalSigner.pubkeys is set to an empty list");
}

const pubkeys = args["externalSigner.pubkeys"] ?? (await externalSignerGetKeys(externalSignerUrl));
const pubkeys = args["externalSigner.pubkeys"] ?? (await externalSignerGetKeys(externalSignerUrl, logger));
assertValidPubkeysHex(pubkeys);

return pubkeys.map((pubkey) => ({type: SignerType.Remote, pubkey, url: externalSignerUrl}));
Expand Down
56 changes: 48 additions & 8 deletions packages/validator/src/util/externalSignerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function externalSignerGetKeys(externalSignerUrl: string, logger?:
headers: {"Content-Type": "application/json"},
});

return handlerExternalSignerResponse<string[]>(res, logger);
return handlerExternalSignerGetKeysResponse(res, logger);
}

/**
Expand Down Expand Up @@ -149,7 +149,7 @@ export async function externalSignerPostSignature(
body: JSON.stringify(requestObj),
});

const data = await handlerExternalSignerResponse<{signature: string}>(res, logger);
const data = await handlerExternalSignerPostSignatureResponse(res, logger);
return data.signature;
}

Expand All @@ -162,27 +162,67 @@ export async function externalSignerUpCheck(remoteUrl: string): Promise<boolean>
headers: {"Content-Type": "application/json"},
});

const data = await handlerExternalSignerResponse<{status: string}>(res);
const data = await handlerExternalSignerUpCheckResponse(res);
return data.status === "OK";
}

async function handlerExternalSignerResponse<T>(res: Response, logger?: Logger): Promise<T> {
async function handlerExternalSignerGetKeysResponse<T>(res: Response, logger?: Logger): Promise<string[]> {
if (!res.ok) {
const errBody = await res.text();
logger?.debug("External signer error", {body: errBody});
logger?.debug("External signer GetKeys error", {body: errBody});
throw Error(`${errBody}`);
}

const resBody = await res.text();
const resContentType = res.headers.get("Content-Type");

logger?.debug("External signer response", {contentType: resContentType, body: resBody});
logger?.debug("External signer GetKeys response", {contentType: resContentType, body: resBody});

if(resContentType?.startsWith("application/json")) {
return JSON.parse(resBody) as T;
return JSON.parse(resBody) as string[];
}
else {
return {signature: resBody} as T;
return resBody.split(",");
}
}

async function handlerExternalSignerPostSignatureResponse<T>(res: Response, logger?: Logger): Promise<{signature: string}> {
if (!res.ok) {
const errBody = await res.text();
logger?.debug("External signer PostSignature error", {body: errBody});
throw Error(`${errBody}`);
}

const resBody = await res.text();
const resContentType = res.headers.get("Content-Type");

logger?.debug("External signer PostSignature response", {contentType: resContentType, body: resBody});

if(resContentType?.startsWith("application/json")) {
return JSON.parse(resBody) as {signature: string};
}
else {
return {signature: resBody};
}
}

async function handlerExternalSignerUpCheckResponse<T>(res: Response, logger?: Logger): Promise<{status: string}> {
if (!res.ok) {
const errBody = await res.text();
logger?.debug("External signer UpCheck error", {body: errBody});
throw Error(`${errBody}`);
}

const resBody = await res.text();
const resContentType = res.headers.get("Content-Type");

logger?.debug("External signer UpCheck response", {contentType: resContentType, body: resBody});

if(resContentType?.startsWith("application/json")) {
return JSON.parse(resBody) as {status: string};
}
else {
return {status: resBody};
}
}

Expand Down

0 comments on commit f1651c5

Please sign in to comment.