Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 26 additions & 18 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,60 @@ export class ApiClient {
};
}

public async issueGetRequest<T>(path: string): Promise<T> {
public async issueGetRequest<T>(path: string, requestOptions?: RequestOptions): Promise<T> {
const token = this.authProvider.signJwt(path);
const res = await this.axiosInstance.get(path, {
headers: {"Authorization": `Bearer ${token}`}
headers: this.getHeaders(token, requestOptions)
});
return res.data;
}

public async issuePostRequest<T>(path: string, body: any, requestOptions?: RequestOptions): Promise<T> {
const token = this.authProvider.signJwt(path, body);
const headers: any = {"Authorization": `Bearer ${token}`};
const idempotencyKey = requestOptions?.idempotencyKey;
if (idempotencyKey) {
headers["Idempotency-Key"] = idempotencyKey;
}

const ncwWalletId = requestOptions?.ncw?.walletId;
if (ncwWalletId) {
headers["X-End-User-Wallet-Id"] = ncwWalletId;
}

const response = await this.axiosInstance.post<T>(path, body, {headers});
const response = await this.axiosInstance.post<T>(path, body, {
headers: this.getHeaders(token, requestOptions)
});
return response.data;
}

public async issuePutRequest<T>(path: string, body: any): Promise<T> {
public async issuePutRequest<T>(path: string, body: any, requestOptions?: RequestOptions): Promise<T> {
const token = this.authProvider.signJwt(path, body);
const res = (await this.axiosInstance.put<T>(path, body, {
headers: {"Authorization": `Bearer ${token}`}
headers: this.getHeaders(token, requestOptions)
}));
return res.data;
}

public async issuePatchRequest<T>(path: string, body: any): Promise<T> {
const token = this.authProvider.signJwt(path, body);
const res = (await this.axiosInstance.patch<T>(path, body, {
headers: {"Authorization": `Bearer ${token}`}
headers: this.getHeaders(token)
}));
return res.data;
}

public async issueDeleteRequest<T>(path: string): Promise<T> {
const token = this.authProvider.signJwt(path);
const res = (await this.axiosInstance.delete<T>(path, {
headers: {"Authorization": `Bearer ${token}`}
headers: this.getHeaders(token),
}));
return res.data;
}

private getHeaders(token: string, requestOptions?: RequestOptions) {
const headers: any = {
"Authorization": `Bearer ${token}`,
};

const idempotencyKey = requestOptions?.idempotencyKey;
if (idempotencyKey) {
headers["Idempotency-Key"] = idempotencyKey;
}
const ncwWalletId = requestOptions?.ncw?.walletId;
if (ncwWalletId) {
headers["X-End-User-Wallet-Id"] = ncwWalletId;
}

return headers;
}
}
8 changes: 4 additions & 4 deletions src/fireblocks-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ import {
} from "./types";
import { AxiosProxyConfig, AxiosResponse } from "axios";
import { PIIEncryption } from "./pii-client";
import { NcwApiClient } from "./ncw-api-client";
import { NcwSdk } from "./ncw-sdk";
import { NcwApiClient } from "./ncw/ncw-api-client";

export * from "./types";
export * from "./ncw/types";

export interface SDKOptions {
/** HTTP request timeout */
Expand Down Expand Up @@ -178,9 +178,9 @@ export class FireblocksSDK {
* NCW API Namespace
*
* @readonly
* @type {NcwSdk}
* @type {NcwApiClient}
*/
public get NCW(): NcwSdk {
public get NCW(): NcwApiClient {
return this.apiNcw;
}

Expand Down
77 changes: 37 additions & 40 deletions src/ncw-api-client.ts → src/ncw/api-client-admin.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,45 @@
import { ApiClient } from "./api-client";
import {
AssetResponse,
Web3PagedResponse,
NCW,
} from "./types";
import { NcwSdk } from "./ncw-sdk";
import { ApiClient } from "../api-client";
import { AssetResponse, NCW, Web3PagedResponse } from "../types";
import { INcwAdminSdk } from "./types";

export class NcwApiClient implements NcwSdk {
private readonly NCW_BASE_PATH = "/v1/ncw/wallets";
export class NcwAdminSdk implements INcwAdminSdk {

constructor(private readonly apiClient: ApiClient) { }
constructor(private readonly apiClient: ApiClient, private readonly BASE_PATH: string) { }

public async createWallet(): Promise<{ walletId: string; enabled: boolean; }> {
return await this.apiClient.issuePostRequest(
`${this.NCW_BASE_PATH}`,
{});
`${this.BASE_PATH}`,
{},
);
}

public async getWallet(walletId: string): Promise<{ walletId: string; enabled: boolean; }> {
return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}`);
}

public async enableWallet(walletId: string, enabled: boolean): Promise<void> {
return await this.apiClient.issuePutRequest(
`${this.NCW_BASE_PATH}/${walletId}/enable`,
{ enabled });
`${this.BASE_PATH}/${walletId}`,
);
}

public async getWalletDevices(walletId: string): Promise<NCW.Device> {
return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/devices/`);
`${this.BASE_PATH}/${walletId}/devices/`,
);
}

public async enableWalletDevice(walletId: string, deviceId: string, enabled: boolean): Promise<void> {
return await this.apiClient.issuePutRequest(
`${this.NCW_BASE_PATH}/${walletId}/devices/${deviceId}/enable`,
{ enabled });
}

public async invokeWalletRpc(walletId: string, deviceId: string, payload: string): Promise<{ result: string; } | { error: { message: string; code?: number; }; }> {
return await this.apiClient.issuePostRequest(
`${this.NCW_BASE_PATH}/${walletId}/devices/${deviceId}/invoke`,
{ payload });
`${this.BASE_PATH}/${walletId}/devices/${deviceId}/enable`,
{ enabled },
);
}

public async createWalletAccount(walletId: string): Promise<{
walletId: string;
accountId: number;
}> {
return await this.apiClient.issuePostRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts`,
{});
`${this.BASE_PATH}/${walletId}/accounts`,
{},
);
}

public async getWallets({ pageCursor, pageSize, sort, order }: NCW.GetWalletsPayload = {}): Promise<Web3PagedResponse<NCW.WalletInfo>> {
Expand All @@ -62,7 +50,7 @@ export class NcwApiClient implements NcwSdk {
...(order && { order }),
});

return await this.apiClient.issueGetRequest(`${this.NCW_BASE_PATH}?${params.toString()}`);
return await this.apiClient.issueGetRequest(`${this.BASE_PATH}?${params.toString()}`);
}

public async getWalletAccounts(walletId: string, { pageCursor, pageSize, sort, order }: NCW.GetWalletsPayload = {}): Promise<Web3PagedResponse<{
Expand All @@ -77,15 +65,17 @@ export class NcwApiClient implements NcwSdk {
});

return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts?${params.toString()}`);
`${this.BASE_PATH}/${walletId}/accounts?${params.toString()}`,
);
}

public async getWalletAccount(walletId: string, accountId: number): Promise<{
walletId: string;
accountId: number;
}> {
return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}`);
`${this.BASE_PATH}/${walletId}/accounts/${accountId}`,
);
}

public async getWalletAssets(walletId: string, accountId: number, { pageCursor, pageSize, sort, order }: NCW.GetWalletAssetsPayload = {}): Promise<Web3PagedResponse<NCW.WalletAssetResponse>> {
Expand All @@ -97,17 +87,21 @@ export class NcwApiClient implements NcwSdk {
});

return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}/assets?${params.toString()}`);
`${this.BASE_PATH}/${walletId}/accounts/${accountId}/assets?${params.toString()}`,
);
}

public async getWalletAsset(walletId: string, accountId: number, assetId: string): Promise<NCW.WalletAssetResponse> {
return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}`);
`${this.BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}`,
);
}

public async activateWalletAsset(walletId: string, accountId: number, assetId: string): Promise<NCW.WalletAssetAddress> {
return await this.apiClient.issuePostRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}`, {});
`${this.BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}`,
{},
);
}

public async getWalletAssetAddresses(walletId: string, accountId: number, assetId: string, { pageCursor, pageSize, sort, order }: NCW.GetWalletAddressesPayload = {}): Promise<Web3PagedResponse<NCW.WalletAssetAddress>> {
Expand All @@ -119,17 +113,20 @@ export class NcwApiClient implements NcwSdk {
});

return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}/addresses?${params.toString()}`);
`${this.BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}/addresses?${params.toString()}`,
);
}

public async getWalletAssetBalance(walletId: string, accountId: number, assetId: string): Promise<AssetResponse> {
return await this.apiClient.issueGetRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}/balance`);
`${this.BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}/balance`,
);
}

public async refreshWalletAssetBalance(walletId: string, accountId: number, assetId: string): Promise<AssetResponse> {
return await this.apiClient.issuePutRequest(
`${this.NCW_BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}/balance`,
{});
`${this.BASE_PATH}/${walletId}/accounts/${accountId}/assets/${assetId}/balance`,
{},
);
}
}
Loading