Skip to content

Commit

Permalink
WLT-658 - GetPublicKey For NCW
Browse files Browse the repository at this point in the history
code review fixes
  • Loading branch information
goweiss committed Oct 7, 2024
1 parent 120fc00 commit 8f8a560
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
41 changes: 41 additions & 0 deletions src/common/public_key_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ApiClient } from "src/api-client";
import { PeerType, PublicKeyInfoArgs, PublicKeyInfoByAccountAssetArgs, PublicKeyInfoForVaultAccountArgs, PublicKeyInformation, PublicKeyResponse, RequestOptions } from "src/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 {
requestOptions = { ncw: { walletId } };
url = `/v1/ncw/${walletId}/public_key_info`;
}

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 {
requestOptions = { ncw: { walletId } };
url = `/v1/ncw/${walletId}/accounts/${(args as PublicKeyInfoByAccountAssetArgs).accountId}/${args.assetId}/${args.change}/${args.addressIndex}/public_key_info`;
}

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 @@ -171,6 +171,7 @@ import {
WithdrawRequestDto,
WithdrawResponse
} from "./staking";
import { getPublicKeyInfoByAccountAssetImpl, getPublicKeyInfoImpl } from "./common/public_key_info";

export * from "./types";

Expand Down Expand Up @@ -1160,17 +1161,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 @@ -1204,11 +1195,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
20 changes: 4 additions & 16 deletions src/ncw-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
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 @@ -175,24 +177,10 @@ export class NcwApiClient implements NcwSdk {
}

public async getPublicKeyInfo(walletId: string, args: PublicKeyInfoArgs): Promise<PublicKeyInformation> {
let url = `/v1/ncw/${walletId}/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, undefined, { ncw: { walletId } });
return await getPublicKeyInfoImpl(PeerType.END_USER_WALLET, args, this.apiClient, walletId);
}

public async getPublicKeyInfoByAccountAsset(walletId: string, args: PublicKeyInfoByAccountAssetArgs): Promise<PublicKeyResponse> {
let url = `/v1/ncw/${walletId}/accounts/${args.accountId}/${args.assetId}/${args.change}/${args.addressIndex}/public_key_info`;
if (args.compressed) {
url += `?compressed=${args.compressed}`;
}
return await this.apiClient.issueGetRequest(url, undefined, { ncw: { walletId } });
return await getPublicKeyInfoByAccountAssetImpl(PeerType.END_USER_WALLET, args, this.apiClient, walletId);
}
}
2 changes: 1 addition & 1 deletion src/ncw-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export interface NcwSdk {
getPublicKeyInfo(walletId: string, args: PublicKeyInfoArgs): Promise<PublicKeyInformation>;

/**
* Get the public key information for a vault account
* Get the public key information for an NCW account
* @param {string} walletId
* @param {PublicKeyInfoByAccountAssetArgs} args
* @return {*} {Promise<PublicKeyResponse>}
Expand Down

0 comments on commit 8f8a560

Please sign in to comment.