Skip to content

Commit

Permalink
Fix captcha storage mechanism. Add tests (#1337)
Browse files Browse the repository at this point in the history
* Fix captcha storage mechanism. Add tests

* Fix up tests. Move verifyRecency function to util

* Update test env template

* Fix build
  • Loading branch information
forgetso authored Aug 28, 2024
1 parent 4feb72c commit da57633
Show file tree
Hide file tree
Showing 25 changed files with 443 additions and 305 deletions.
1 change: 1 addition & 0 deletions dev/scripts/env.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ PROSOPO_PROVIDER_MNEMONIC=puppy cream effort carbon despair leg pyramid cotton e
NODE_ENV=test
PROSOPO_LOG_LEVEL=info
PROSOPO_DEFAULT_ENVIRONMENT=development
PROSOPO_MONGO_CAPTCHA_URI=mongodb://root:root@localhost:27017/captchastorage?authSource=admin
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 25 additions & 20 deletions packages/cli/src/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@ import { LogLevel, getLogger } from "@prosopo/common";
import type { ProsopoConfigOutput } from "@prosopo/types";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { commandProviderSetDataset, commandVersion } from "./commands/index.js";
import {
commandProviderSetDataset,
commandStoreCaptchasExternally,
commandVersion,
} from "./commands/index.js";

export type AwaitedProcessedArgs = {
[x: string]: unknown;
api: boolean;
_: (string | number)[];
$0: string;
[x: string]: unknown;
api: boolean;
_: (string | number)[];
$0: string;
};

export function processArgs(
args: string[],
pair: KeyringPair,
config: ProsopoConfigOutput,
args: string[],
pair: KeyringPair,
config: ProsopoConfigOutput,
) {
const logger = getLogger(LogLevel.enum.info, "CLI");
return yargs(hideBin(args))
.usage("Usage: $0 [global options] <command> [options]")
.option("api", { demand: false, default: false, type: "boolean" } as const)
.option("adminApi", {
demand: false,
default: false,
type: "boolean",
} as const)
.command(commandProviderSetDataset(pair, config, { logger }))
.command(commandVersion(pair, config, { logger }))
.parse();
const logger = getLogger(LogLevel.enum.info, "CLI");
return yargs(hideBin(args))
.usage("Usage: $0 [global options] <command> [options]")
.option("api", { demand: false, default: false, type: "boolean" } as const)
.option("adminApi", {
demand: false,
default: false,
type: "boolean",
} as const)
.command(commandProviderSetDataset(pair, config, { logger }))
.command(commandStoreCaptchasExternally(pair, config, { logger }))
.command(commandVersion(pair, config, { logger }))
.parse();
}
2 changes: 1 addition & 1 deletion packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export { default as commandProviderDeregister } from "./providerDeregister.js";
export { default as commandProviderSetDataset } from "./providerSetDataset.js";
export { default as commandStoreCaptchasExternally } from "./storeCaptchasExternally.js";
export { default as commandVersion } from "./version.js";
71 changes: 36 additions & 35 deletions packages/cli/src/commands/providerDeregister.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import type { KeyringPair } from "@polkadot/keyring/types";
import { LogLevel, type Logger, getLogger } from "@prosopo/common";
import { ProsopoEnvError } from "@prosopo/common";
import { ProviderEnvironment } from "@prosopo/env";
import { Tasks } from "@prosopo/provider";
import type { ProsopoConfigOutput } from "@prosopo/types";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,42 +11,49 @@ import type { ProsopoConfigOutput } from "@prosopo/types";
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import type { KeyringPair } from "@polkadot/keyring/types";
import { LogLevel, type Logger, getLogger } from "@prosopo/common";
import { ProsopoEnvError } from "@prosopo/common";
import { ProviderEnvironment } from "@prosopo/env";
import { Tasks } from "@prosopo/provider";
import type { ProsopoConfigOutput } from "@prosopo/types";
import type { ArgumentsCamelCase, Argv } from "yargs";
import { validateAddress } from "./validators.js";

export default (
pair: KeyringPair,
config: ProsopoConfigOutput,
cmdArgs?: { logger?: Logger },
pair: KeyringPair,
config: ProsopoConfigOutput,
cmdArgs?: { logger?: Logger },
) => {
const logger =
cmdArgs?.logger || getLogger(LogLevel.enum.info, "cli.provider_deregister");
const logger =
cmdArgs?.logger || getLogger(LogLevel.enum.info, "cli.provider_deregister");

return {
command: "provider_deregister",
describe: "Deregister a Provider",
builder: (yargs: Argv) =>
yargs.option("address", {
type: "string" as const,
demand: true,
desc: "The AccountId of the Provider",
} as const),
handler: async (argv: ArgumentsCamelCase) => {
try {
const env = new ProviderEnvironment(config, pair);
await env.isReady();
const tasks = new Tasks(env);
return {
command: "provider_deregister",
describe: "Deregister a Provider",
builder: (yargs: Argv) =>
yargs.option("address", {
type: "string" as const,
demand: true,
desc: "The AccountId of the Provider",
} as const),
handler: async (argv: ArgumentsCamelCase) => {
try {
const env = new ProviderEnvironment(config, pair);
await env.isReady();
const tasks = new Tasks(env);

// TODO provider deregister does not accept params... it should?
// await tasks.contract.tx.providerDeregister(argv.address)
// TODO provider deregister does not accept params... it should?
// await tasks.contract.tx.providerDeregister(argv.address)

// logger.info('Provider registered')
// logger.info('Provider registered')

throw new ProsopoEnvError("GENERAL.NOT_IMPLEMENTED");
} catch (err) {
logger.error(err);
}
},
middlewares: [validateAddress],
};
throw new ProsopoEnvError("GENERAL.NOT_IMPLEMENTED");
} catch (err) {
logger.error(err);
}
},
middlewares: [validateAddress],
};
};
77 changes: 39 additions & 38 deletions packages/cli/src/commands/providerSetDataset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import type { KeyringPair } from "@polkadot/keyring/types";
import { LogLevel, type Logger, getLogger } from "@prosopo/common";
import { ProviderEnvironment } from "@prosopo/env";
import { Tasks } from "@prosopo/provider";
import type { ProsopoConfigOutput } from "@prosopo/types";
import type { ArgumentsCamelCase, Argv } from "yargs";
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,42 +11,49 @@ import type { ArgumentsCamelCase, Argv } from "yargs";
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import type { KeyringPair } from "@polkadot/keyring/types";
import { LogLevel, type Logger, getLogger } from "@prosopo/common";
import { ProviderEnvironment } from "@prosopo/env";
import { Tasks } from "@prosopo/provider";
import type { ProsopoConfigOutput } from "@prosopo/types";
import type { ArgumentsCamelCase, Argv } from "yargs";
import * as z from "zod";
import { loadJSONFile } from "../files.js";

export default (
pair: KeyringPair,
config: ProsopoConfigOutput,
cmdArgs?: { logger?: Logger },
pair: KeyringPair,
config: ProsopoConfigOutput,
cmdArgs?: { logger?: Logger },
) => {
const logger =
cmdArgs?.logger ||
getLogger(LogLevel.enum.info, "cli.provider_set_data_set");
const logger =
cmdArgs?.logger ||
getLogger(LogLevel.enum.info, "cli.provider_set_data_set");

return {
command: "provider_set_data_set",
describe: "Add a dataset as a Provider",
builder: (yargs: Argv) =>
yargs.option("file", {
type: "string" as const,
demand: true,
desc: "The file path of a JSON dataset file",
} as const),
handler: async (argv: ArgumentsCamelCase) => {
try {
const env = new ProviderEnvironment(config, pair);
await env.isReady();
const tasks = new Tasks(env);
const file = z.string().parse(argv.file);
const jsonFile = loadJSONFile(file) as JSON;
logger.info(`Loaded JSON from ${file}`);
const result =
await tasks.datasetManager.providerSetDatasetFromFile(jsonFile);
logger.info(JSON.stringify(result, null, 2));
} catch (err) {
logger.error(err);
}
},
middlewares: [],
};
return {
command: "provider_set_data_set",
describe: "Add a dataset as a Provider",
builder: (yargs: Argv) =>
yargs.option("file", {
type: "string" as const,
demand: true,
desc: "The file path of a JSON dataset file",
} as const),
handler: async (argv: ArgumentsCamelCase) => {
try {
const env = new ProviderEnvironment(config, pair);
await env.isReady();
const tasks = new Tasks(env);
const file = z.string().parse(argv.file);
const jsonFile = loadJSONFile(file) as JSON;
logger.info(`Loaded JSON from ${file}`);
const result =
await tasks.datasetManager.providerSetDatasetFromFile(jsonFile);
logger.info(JSON.stringify(result, null, 2));
} catch (err) {
logger.error(err);
}
},
middlewares: [],
};
};
46 changes: 46 additions & 0 deletions packages/cli/src/commands/storeCaptchasExternally.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import type { KeyringPair } from "@polkadot/keyring/types";
import { LogLevel, type Logger, getLogger } from "@prosopo/common";
import { ProviderEnvironment } from "@prosopo/env";
import { Tasks } from "@prosopo/provider";
import type { ProsopoConfigOutput } from "@prosopo/types";

export default (
pair: KeyringPair,
config: ProsopoConfigOutput,
cmdArgs?: { logger?: Logger },
) => {
const logger =
cmdArgs?.logger || getLogger(LogLevel.enum.info, "cli.store_captchas");

return {
command: "store_captchas",
describe: "Store captcha records externally for billing purposes",
handler: async () => {
try {
const env = new ProviderEnvironment(config, pair);
await env.isReady();
const tasks = new Tasks(env);
await tasks.datasetManager.storeCommitmentsExternal().catch((err) => {
env.logger.error(err);
});
} catch (err) {
logger.error(err);
}
},
middlewares: [],
};
};
Loading

0 comments on commit da57633

Please sign in to comment.