diff --git a/packages/api-http/integration/routes/legacy.test.ts b/packages/api-http/integration/routes/legacy.test.ts new file mode 100644 index 000000000..59bb03fca --- /dev/null +++ b/packages/api-http/integration/routes/legacy.test.ts @@ -0,0 +1,65 @@ +import { describe } from "@mainsail/test-runner"; +import { Application } from "@mainsail/kernel"; +import { prepareSandbox, ApiContext } from "../../test/helpers/prepare-sandbox"; +import { request } from "../../test/helpers/request"; + +import legacyColdWallets from "../../test/fixtures/legacy_cold_wallets.json"; + +describe<{ + app: Application; +}>("Legacy", ({ it, afterAll, assert, afterEach, beforeAll, beforeEach, nock }) => { + let apiContext: ApiContext; + + let options = {}; + + beforeAll(async (context) => { + nock.enableNetConnect(); + apiContext = await prepareSandbox(context); + }); + + afterAll((context) => { + nock.disableNetConnect(); + apiContext.dispose(); + }); + + beforeEach(async (context) => { + await apiContext.reset(); + }); + + afterEach(async (context) => { + await apiContext.reset(); + }); + + it("/legacy/cold-wallets", async () => { + await apiContext.legacyColdWalletRepository.save(legacyColdWallets); + + const { statusCode, data } = await request("/legacy/cold-wallets", options); + assert.equal(statusCode, 200); + assert.equal(data.data, legacyColdWallets); + }); + + it("/legacy/cold-wallets/{address}", async () => { + await apiContext.legacyColdWalletRepository.save(legacyColdWallets); + + const wallet = legacyColdWallets[0]; + + const { + statusCode, + data: { data }, + } = await request(`/legacy/cold-wallets/${wallet.address}`, options); + + assert.equal(statusCode, 200); + assert.equal(data, wallet); + }); + + it("/legacy/cold-wallets/{address} (404)", async () => { + await apiContext.legacyColdWalletRepository.save(legacyColdWallets); + + const fake = "1".repeat(34); + + await assert.rejects( + async () => request(`/legacy/cold-wallets/${fake}`, options), + "Response code 404 (Not Found)", + ); + }); +}); diff --git a/packages/api-http/source/controllers/legacy.ts b/packages/api-http/source/controllers/legacy.ts index de278f50b..56fe02904 100644 --- a/packages/api-http/source/controllers/legacy.ts +++ b/packages/api-http/source/controllers/legacy.ts @@ -1,3 +1,4 @@ +import Boom from "@hapi/boom"; import Hapi from "@hapi/hapi"; import { Contracts as ApiDatabaseContracts, Identifiers as ApiDatabaseIdentifiers } from "@mainsail/api-database"; import { inject, injectable } from "@mainsail/container"; @@ -30,4 +31,20 @@ export class LegacyController extends Controller { LegacyColdWalletResource, ); } + + public async showColdWallet(request: Hapi.Request): Promise { + const legacyAddress = request.params.address; + + const wallet = await this.legacyColdWalletRepositoryFactory() + .createQueryBuilder() + .select() + .where("address = :legacyAddress", { legacyAddress }) + .getOne(); + + if (!wallet) { + return Boom.notFound("Cold Wallet not found"); + } + + return this.respondWithResource(wallet, LegacyColdWalletResource); + } } diff --git a/packages/api-http/source/routes/legacy.ts b/packages/api-http/source/routes/legacy.ts index e6ec76560..6bc974a76 100644 --- a/packages/api-http/source/routes/legacy.ts +++ b/packages/api-http/source/routes/legacy.ts @@ -4,6 +4,7 @@ import type { Contracts } from "@mainsail/contracts"; import Joi from "joi"; import { LegacyController } from "../controllers/legacy.js"; +import { legacyAddressSchema } from "../schemas/legacy.js"; export const register = (server: Contracts.Api.ApiServer): void => { const controller = server.app.app.resolve(LegacyController); @@ -24,4 +25,18 @@ export const register = (server: Contracts.Api.ApiServer): void => { }, path: "/legacy/cold-wallets", }); + + server.route({ + handler: (request: Hapi.Request) => controller.showColdWallet(request), + method: "GET", + options: { + validate: { + params: Joi.object({ + address: legacyAddressSchema, + }), + query: Joi.object({}), + }, + }, + path: "/legacy/cold-wallets/{address}", + }); }; diff --git a/packages/api-http/source/schemas/index.ts b/packages/api-http/source/schemas/index.ts index 338dfd375..83d332265 100644 --- a/packages/api-http/source/schemas/index.ts +++ b/packages/api-http/source/schemas/index.ts @@ -1,4 +1,5 @@ export * from "./blocks.js"; +export * from "./legacy.js"; export * from "./schemas.js"; export * from "./tokens.js"; export * from "./transactions.js"; diff --git a/packages/api-http/source/schemas/legacy.ts b/packages/api-http/source/schemas/legacy.ts new file mode 100644 index 000000000..4ea1fd9ff --- /dev/null +++ b/packages/api-http/source/schemas/legacy.ts @@ -0,0 +1,6 @@ +import Joi from "joi"; + +export const legacyAddressSchema = Joi.string() + .min(33) + .max(34) + .pattern(/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/); diff --git a/packages/api-http/test/fixtures/legacy_cold_wallets.json b/packages/api-http/test/fixtures/legacy_cold_wallets.json new file mode 100644 index 000000000..f32a5716c --- /dev/null +++ b/packages/api-http/test/fixtures/legacy_cold_wallets.json @@ -0,0 +1,20 @@ +[ + { + "address": "D598Pv4evuAPsvcXTaBq6Ru1by5sTXpX8H", + "balance": "10000000000", + "attributes": { + "legacyNonce": "0" + }, + "mergeInfoWalletAddress": null, + "mergeInfoTransactionHash": null + }, + { + "address": "D599XZcmRs5D6FALivh43uThAFKCT9vST8", + "balance": "10000000000", + "attributes": { + "legacyNonce": "0" + }, + "mergeInfoWalletAddress": null, + "mergeInfoTransactionHash": null + } +] diff --git a/packages/api-http/test/helpers/prepare-sandbox.ts b/packages/api-http/test/helpers/prepare-sandbox.ts index c2f046c0d..01f5c172d 100644 --- a/packages/api-http/test/helpers/prepare-sandbox.ts +++ b/packages/api-http/test/helpers/prepare-sandbox.ts @@ -138,6 +138,12 @@ export class ApiContext { )(); } + public get legacyColdWalletRepository(): ApiDatabaseContracts.LegacyColdWalletRepository { + return this.app.get( + ApiDatabaseIdentifiers.LegacyColdWalletRepositoryFactory, + )(); + } + public get validatorRoundRepository(): ApiDatabaseContracts.ValidatorRoundRepository { return this.app.get( ApiDatabaseIdentifiers.ValidatorRoundRepositoryFactory,