Skip to content

Commit

Permalink
fee recipient validation change
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed May 20, 2022
1 parent 9df54ef commit 81d6cc2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
17 changes: 5 additions & 12 deletions packages/cli/src/util/feeRecipient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import {ExecutionAddress} from "@chainsafe/lodestar-types";
import {fromHex} from "@chainsafe/lodestar-utils";

export function parseFeeRecipientHex(feeRecipientHexString: string): string {
const hexPattern = new RegExp(/^(0x|0X)(?<feeRecipientString>[a-fA-F0-9]{40})$/, "g");
const feeRecipientStringMatch = hexPattern.exec(feeRecipientHexString);
const feeRecipientString = feeRecipientStringMatch?.groups?.feeRecipientString;
if (feeRecipientString === undefined)
throw Error(`Invalid feeRecipient= ${feeRecipientHexString}, expected format: ^0x[a-fA-F0-9]{40}$`);
return feeRecipientString;
}

export function parseFeeRecipient(feeRecipientHexString: string): ExecutionAddress {
const feeRecipientHex = parseFeeRecipientHex(feeRecipientHexString);
return fromHex(feeRecipientHex);
export function parseFeeRecipient(feeRecipientHex: string): ExecutionAddress {
if (!/^0x[a-fA-F0-9]{40}$/i.test(feeRecipientHex)) {
throw Error(`Invalid feeRecipient= ${feeRecipientHex}, expected format: ^0x[a-fA-F0-9]{40}$`);
}
return fromHex(feeRecipientHex.toLowerCase());
}
4 changes: 2 additions & 2 deletions packages/cli/test/unit/validator/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {parseFeeRecipient} from "../../../src/util";
const feeRecipient = Buffer.from(Array.from({length: 20}, () => Math.round(Math.random() * 255)));
const feeRecipientString = feeRecipient.toString("hex");

describe("parseFeeRecipient", () => {
describe("validator / parseFeeRecipient", () => {
const testCases: string[] = [`0x${feeRecipientString}`, `0X${feeRecipientString}`];
for (const testCase of testCases) {
it(`parse ${testCase}`, () => {
Expand All @@ -13,7 +13,7 @@ describe("parseFeeRecipient", () => {
}
});

describe("invalid feeRecipient", () => {
describe("validator / invalid feeRecipient", () => {
const testCases: string[] = [
feeRecipientString,
`X0${feeRecipientString}`,
Expand Down

0 comments on commit 81d6cc2

Please sign in to comment.