Skip to content

Commit

Permalink
GetPublicKey For NCW (#294)
Browse files Browse the repository at this point in the history
* WLT-658 - GetPublicKey For NCW

added two new functions for NCW GetPublicKey

* WLT-658 - GetPublicKey For NCW

code review fixes

* WLT-658 - GetPublicKey For NCW

* WLT-658 - GetPublicKey For NCW
  • Loading branch information
goweiss authored Nov 27, 2024
1 parent 8dad09a commit 65d138d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 23 deletions.
13 changes: 9 additions & 4 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ export class ApiClient {
};
}

public async issueGetRequest<T>(rawPath: string, queryStringParams?: object): Promise<T> {
public async issueGetRequest<T>(rawPath: string, queryStringParams?: object, requestOptions?: RequestOptions): Promise<T> {
const pathWithParams = queryStringParams ? `${rawPath}?${queryString.stringify(queryStringParams)}` : rawPath;
const path = normalizePath(pathWithParams);

const token = this.authProvider.signJwt(path);
const res = await this.axiosInstance.get(path, {
headers: {"Authorization": `Bearer ${token}`}
});
const headers: any = {"Authorization": `Bearer ${token}`};

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

const res = await this.axiosInstance.get(path, { headers });
return res.data;
}

Expand Down
45 changes: 45 additions & 0 deletions src/common/public_key_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ApiClient } from "../api-client";
import { PeerType, PublicKeyInfoArgs, PublicKeyInfoByAccountAssetArgs, PublicKeyInfoForVaultAccountArgs, PublicKeyInformation, PublicKeyResponse, RequestOptions } from "../types";
import queryString from "query-string";

export async function getPublicKeyInfoImpl(peerType: PeerType, args: PublicKeyInfoArgs, apiClient: ApiClient, walletId?: string): Promise<PublicKeyInformation> {
let url: string;
let requestOptions: RequestOptions;
if (peerType === PeerType.VAULT_ACCOUNT) {
url = `/v1/vault/public_key_info`;
} else if (peerType === PeerType.END_USER_WALLET) {
requestOptions = { ncw: { walletId } };
url = `/v1/ncw/${walletId}/public_key_info`;
} else {
throw new Error(`Unsupported peer type: ${peerType}`);
}

const query = queryString.stringify({
algorithm: args.algorithm,
derivationPath: JSON.stringify(args.derivationPath),
compressed: args.compressed,
});
url += `?${query}`;

return await apiClient.issueGetRequest(url, undefined, requestOptions);
}

export async function getPublicKeyInfoByAccountAssetImpl(peerType: PeerType, args: PublicKeyInfoForVaultAccountArgs | PublicKeyInfoByAccountAssetArgs, apiClient: ApiClient, walletId?: string): Promise<PublicKeyResponse> {
let url: string;
let requestOptions: RequestOptions;
if (peerType === PeerType.VAULT_ACCOUNT) {
url = `/v1/vault/accounts/${(args as PublicKeyInfoForVaultAccountArgs).vaultAccountId}/${args.assetId}/${args.change}/${args.addressIndex}/public_key_info`;
} else if (peerType === PeerType.END_USER_WALLET) {
requestOptions = { ncw: { walletId } };
url = `/v1/ncw/${walletId}/accounts/${(args as PublicKeyInfoByAccountAssetArgs).accountId}/${args.assetId}/${args.change}/${args.addressIndex}/public_key_info`;
} else {
throw new Error(`Unsupported peer type: ${peerType}`);
}

const query = queryString.stringify({
compressed: args.compressed,
});
url += `?${query}`;

return await apiClient.issueGetRequest(url, undefined, requestOptions);
}
19 changes: 3 additions & 16 deletions src/fireblocks-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ import {
WithdrawRequestDto,
WithdrawResponse
} from "./staking";
import { getPublicKeyInfoByAccountAssetImpl, getPublicKeyInfoImpl } from "./common/public_key_info";

export * from "./types";

Expand Down Expand Up @@ -1161,17 +1162,7 @@ export class FireblocksSDK {
* @param args
*/
public async getPublicKeyInfo(args: PublicKeyInfoArgs): Promise<PublicKeyInformation> {
let url = `/v1/vault/public_key_info`;
if (args.algorithm) {
url += `?algorithm=${args.algorithm}`;
}
if (args.derivationPath) {
url += `&derivationPath=${JSON.stringify(args.derivationPath)}`;
}
if (args.compressed) {
url += `&compressed=${args.compressed}`;
}
return await this.apiClient.issueGetRequest(url);
return await getPublicKeyInfoImpl(PeerType.VAULT_ACCOUNT, args, this.apiClient);
}

/**
Expand Down Expand Up @@ -1205,11 +1196,7 @@ export class FireblocksSDK {
* @param args
*/
public async getPublicKeyInfoForVaultAccount(args: PublicKeyInfoForVaultAccountArgs): Promise<PublicKeyResponse> {
let url = `/v1/vault/accounts/${args.vaultAccountId}/${args.assetId}/${args.change}/${args.addressIndex}/public_key_info`;
if (args.compressed) {
url += `?compressed=${args.compressed}`;
}
return await this.apiClient.issueGetRequest(url);
return await getPublicKeyInfoByAccountAssetImpl(PeerType.VAULT_ACCOUNT, args, this.apiClient);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/ncw-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import {
NCW,
UnspentInputsResponse,
SigningAlgorithm,
PublicKeyInfoArgs,
PublicKeyInformation,
PublicKeyResponse,
PublicKeyInfoByAccountAssetArgs,
PeerType,
} from "./types";
import { NcwSdk } from "./ncw-sdk";
import { getPublicKeyInfoByAccountAssetImpl, getPublicKeyInfoImpl } from "./common/public_key_info";

export class NcwApiClient implements NcwSdk {
private readonly NCW_BASE_PATH = "/v1/ncw/wallets";
Expand Down Expand Up @@ -169,4 +175,12 @@ export class NcwApiClient implements NcwSdk {
public async getUnspentInputs(walletId: string, accountId: number, assetId: string): Promise<UnspentInputsResponse[]> {
return await this.apiClient.issueGetRequest(`/v1/ncw/${walletId}/accounts/${accountId}/${assetId}/unspent_inputs`);
}

public async getPublicKeyInfo(walletId: string, args: PublicKeyInfoArgs): Promise<PublicKeyInformation> {
return await getPublicKeyInfoImpl(PeerType.END_USER_WALLET, args, this.apiClient, walletId);
}

public async getPublicKeyInfoByAccountAsset(walletId: string, args: PublicKeyInfoByAccountAssetArgs): Promise<PublicKeyResponse> {
return await getPublicKeyInfoByAccountAssetImpl(PeerType.END_USER_WALLET, args, this.apiClient, walletId);
}
}
21 changes: 20 additions & 1 deletion src/ncw-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {
AssetResponse, Web3PagedResponse, NCW, UnspentInputsResponse,
SigningAlgorithm,
PublicKeyInfoArgs,
PublicKeyInformation,
PublicKeyResponse,
PublicKeyInfoByAccountAssetArgs,
} from "./types";

export interface NcwSdk {
Expand Down Expand Up @@ -198,9 +202,24 @@ export interface NcwSdk {
* refresh a NCW asset balance
*
* @param {string} walletId
* @param {string} walletId
* @param {SigningAlgorithm[]} algorithms
* @return {*} {Promise<void>}
*/
setWalletRequiredAlgorithms(walletId: string, algorithms: SigningAlgorithm[]): Promise<void>;

/**
* Get the public key information
* @param {string} walletId
* @param {PublicKeyInfoArgs} args
* @return {*} {Promise<PublicKeyInformation>}
*/
getPublicKeyInfo(walletId: string, args: PublicKeyInfoArgs): Promise<PublicKeyInformation>;

/**
* Get the public key information for an NCW account
* @param {string} walletId
* @param {PublicKeyInfoByAccountAssetArgs} args
* @return {*} {Promise<PublicKeyResponse>}
*/
getPublicKeyInfoByAccountAsset(walletId: string, args: PublicKeyInfoByAccountAssetArgs): Promise<PublicKeyResponse>;
}
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1223,14 +1223,21 @@ export interface PublicKeyInfoArgs {
compressed?: boolean;
}

export interface PublicKeyInfoForVaultAccountArgs {
export interface BasePublicKeyInfoByAccountAssetArgs {
assetId: string;
vaultAccountId: number;
change: number;
addressIndex: number;
compressed?: boolean;
}

export interface PublicKeyInfoByAccountAssetArgs extends BasePublicKeyInfoByAccountAssetArgs {
accountId: number;
}

export interface PublicKeyInfoForVaultAccountArgs extends BasePublicKeyInfoByAccountAssetArgs {
vaultAccountId: number;
}

export interface GasStationInfo {
balance: { [asset: string]: string };
configuration: {
Expand Down

0 comments on commit 65d138d

Please sign in to comment.