Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add Account address support #979

Merged
merged 19 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/src/models/api/nova/IAccountRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface IAccountRequest {
/**
* The network to search on.
*/
network: string;

/**
* The account id to get the account details for.
*/
accountId: string;
}
11 changes: 11 additions & 0 deletions api/src/models/api/nova/IAccountResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable import/no-unresolved */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { OutputResponse } from "@iota/sdk-nova";
import { IResponse } from "./IResponse";

export interface IAccountResponse extends IResponse {
/**
* The account details response.
*/
accountDetails?: OutputResponse;
}
44 changes: 44 additions & 0 deletions api/src/models/api/nova/IAssociationsResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { IResponse } from "../IResponse";

export enum AssociationType {
BASIC_ADDRESS,
BASIC_STORAGE_RETURN,
BASIC_EXPIRATION_RETURN,
BASIC_SENDER,
ACCOUNT_ADDRESS,
ACCOUNT_ISSUER,
ACCOUNT_SENDER,
ACCOUNT_ID,
ANCHOR_ID,
ANCHOR_STATE_CONTROLLER,
ANCHOR_GOVERNOR,
ANCHOR_ISSUER,
ANCHOR_SENDER,
DELEGATION_ADDRESS,
DELEGATION_VALIDATOR,
FOUNDRY_ACCOUNT,
NFT_ADDRESS,
NFT_STORAGE_RETURN,
NFT_EXPIRATION_RETURN,
NFT_ISSUER,
NFT_SENDER,
NFT_ID,
}

export interface IAssociation {
/**
* The association for the output ids.
*/
type: AssociationType;
/**
* The output ids for the association.
*/
outputIds: string[];
}

export interface IAssociationsResponse extends IResponse {
/**
* The associations to output ids.
*/
associations?: IAssociation[];
}
8 changes: 8 additions & 0 deletions api/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ export const routes: IRoute[] = [
},
// Nova
{ path: "/nova/output/:network/:outputId", method: "get", folder: "nova/output", func: "get" },
{ path: "/nova/account/:network/:accountId", method: "get", folder: "nova/account", func: "get" },
{
path: "/nova/output/associated/:network/:address",
method: "post",
folder: "nova/output/associated",
func: "post",
dataBody: true,
},
{ path: "/nova/block/:network/:blockId", method: "get", folder: "nova/block", func: "get" },
{ path: "/nova/block/metadata/:network/:blockId", method: "get", folder: "nova/block/metadata", func: "get" },
];
29 changes: 29 additions & 0 deletions api/src/routes/nova/account/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ServiceFactory } from "../../../factories/serviceFactory";
import { IAccountRequest } from "../../../models/api/nova/IAccountRequest";
import { IAccountResponse } from "../../../models/api/nova/IAccountResponse";
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { NOVA } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { NovaApi } from "../../../services/nova/novaApi";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
* Get account output details by Account id
* @param config The configuration.
* @param request The request.
* @returns The response.
*/
export async function get(config: IConfiguration, request: IAccountRequest): Promise<IAccountResponse> {
const networkService = ServiceFactory.get<NetworkService>("network");
const networks = networkService.networkNames();
ValidationHelper.oneOf(request.network, networks, "network");
ValidationHelper.string(request.accountId, "accountId");

const networkConfig = networkService.get(request.network);

if (networkConfig.protocolVersion !== NOVA) {
return {};
}

return NovaApi.accountDetails(networkConfig, request.accountId);
}
46 changes: 46 additions & 0 deletions api/src/routes/nova/output/associated/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ServiceFactory } from "../../../../factories/serviceFactory";
import { IAssociation, IAssociationsResponse } from "../../../../models/api/nova/IAssociationsResponse";
import { IAssociationsRequest } from "../../../../models/api/stardust/IAssociationsRequest";
import { IAssociationsRequestBody } from "../../../../models/api/stardust/IAssociationsRequestBody";
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { NOVA } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { AssociatedOutputsHelper } from "../../../../utils/nova/associatedOutputsHelper";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
* Find the associated outputs for the address.
* @param _ The configuration.
* @param request The request.
* @param body The request body
* @returns The response.
*/
export async function post(
_: IConfiguration,
request: IAssociationsRequest,
body: IAssociationsRequestBody,
): Promise<IAssociationsResponse> {
const networkService = ServiceFactory.get<NetworkService>("network");
const networks = networkService.networkNames();
ValidationHelper.oneOf(request.network, networks, "network");
ValidationHelper.string(request.address, "address");

const networkConfig = networkService.get(request.network);

if (networkConfig.protocolVersion !== NOVA) {
return {};
}

const helper = new AssociatedOutputsHelper(networkConfig, body.addressDetails);
await helper.fetch();
const result = helper.associationToOutputIds;

const associations: IAssociation[] = [];
for (const [type, outputIds] of result.entries()) {
associations.push({ type, outputIds: outputIds.reverse() });
}

return {
associations,
};
}
18 changes: 18 additions & 0 deletions api/src/services/nova/novaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { __ClientMethods__, OutputResponse, Client, Block, IBlockMetadata } from "@iota/sdk-nova";
import { ServiceFactory } from "../../factories/serviceFactory";
import logger from "../../logger";
import { IAccountResponse } from "../../models/api/nova/IAccountResponse";
import { IBlockDetailsResponse } from "../../models/api/nova/IBlockDetailsResponse";
import { IBlockResponse } from "../../models/api/nova/IBlockResponse";
import { IOutputDetailsResponse } from "../../models/api/nova/IOutputDetailsResponse";
Expand Down Expand Up @@ -71,6 +72,23 @@ export class NovaApi {
return outputResponse ? { output: outputResponse } : { message: "Output not found" };
}

/**
* Get the account details.
* @param network The network to find the items on.
* @param accountId The accountId to get the details for.
* @returns The account details.
*/
public static async accountDetails(network: INetwork, accountId: string): Promise<IAccountResponse | undefined> {
const accountOutputId = await this.tryFetchNodeThenPermanode<string, string>(accountId, "accountOutputId", network);

if (accountOutputId) {
const outputResponse = await this.outputDetails(network, accountOutputId);
return outputResponse.error ? { error: outputResponse.error } : { accountDetails: outputResponse.output };
}

return { message: "Account output not found" };
}

/**
* Generic helper function to try fetching from node client.
* On failure (or not present), we try to fetch from permanode (if configured).
Expand Down
Loading
Loading