Skip to content

Commit

Permalink
chore(sdk-core): improve url handling
Browse files Browse the repository at this point in the history
Clean this up a bit

BTC-1504

TICKET: BTC-1504
  • Loading branch information
davidkaplanbitgo committed Sep 25, 2024
1 parent b61f988 commit c1e2f93
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
11 changes: 9 additions & 2 deletions modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
BuildNftTransferDataOptions,
BaseBroadcastTransactionOptions,
BaseBroadcastTransactionResult,
UrlOptions,
} from './iBaseCoin';
import { IInscriptionBuilder } from '../inscriptionBuilder';
import { Hash } from 'crypto';
Expand Down Expand Up @@ -70,8 +71,14 @@ export abstract class BaseCoin implements IBaseCoin {
this._markets = new Markets(this.bitgo, this);
}

public url(suffix: string): string {
return this._url + this.getChain() + suffix;
/**
*
* @param suffix Path for the specific URL
* @param options Additional options for building the URL
* @param options.removeChain If true, the chain will not be added into the URL
*/
public url(suffix: string, options?: UrlOptions): string {
return `${this._url}${options?.removeChain ? '' : `${this.getChain()}/`}${suffix}`;
}

public wallets(): Wallets {
Expand Down
6 changes: 5 additions & 1 deletion modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,15 @@ export interface BaseBroadcastTransactionResult {
txIds?: string[];
}

export interface UrlOptions {
removeChain?: boolean;
}

export interface IBaseCoin {
type: string;
tokenConfig?: BaseTokenConfig;
getConfig(): Readonly<StaticsBaseCoin>;
url(suffix: string): string;
url(suffix: string, options?: UrlOptions): string;
wallets(): IWallets;
enterprises(): IEnterprises;
keychains(): IKeychains;
Expand Down
3 changes: 2 additions & 1 deletion modules/sdk-core/src/bitgo/wallet/iWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
VerificationOptions,
TypedData,
NFTTransferOptions,
UrlOptions,
} from '../baseCoin';
import { BitGoBase } from '../bitgoBase';
import { Keychain, KeychainWithEncryptedPrv } from '../keychain';
Expand Down Expand Up @@ -770,7 +771,7 @@ export type SendNFTResult = {
export interface IWallet {
bitgo: BitGoBase;
baseCoin: IBaseCoin;
url(extra?: string): string;
url(extra?: string, options?: UrlOptions): string;
id(): string;
approvalsRequired(): number;
balance(): number;
Expand Down
10 changes: 6 additions & 4 deletions modules/sdk-core/src/bitgo/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SignedTransaction,
SignedTransactionRequest,
TransactionPrebuild,
UrlOptions,
VerifyAddressOptions,
} from '../baseCoin';
import { makeRandomKey } from '../bitcoin';
Expand Down Expand Up @@ -166,9 +167,10 @@ export class Wallet implements IWallet {
/**
* Build a URL using this wallet's id which can be used for BitGo API operations
* @param extra API specific string to append to the wallet id
* @param options Options on how to build the URL
*/
url(extra = ''): string {
return this.baseCoin.url('/wallet/' + this.id() + extra);
url(extra = '', options?: UrlOptions): string {
return this.baseCoin.url('wallet/' + this.id() + extra, options);
}

/**
Expand Down Expand Up @@ -753,8 +755,8 @@ export class Wallet implements IWallet {
): Promise<{ unspents: { id: string; walletId: string; expireTime: string; userId?: string }[] }> {
const filteredParams = _.pick(params, ['create', 'modify', 'delete']);
this.bitgo.setRequestTracer(new RequestTracer());
// The URL cannot contain the coinName, so we remove it from the URL
const url = this.url(`/reservedunspents`).replace(`/${this.baseCoin.getChain()}`, '');
const url = this.url(`/reservedunspents`, { removeChain: true });
console.log(url);
if (filteredParams.create) {
const filteredCreateParams = _.pick(params.create, ['unspentIds', 'expireTime']);
return this.bitgo.post(url).send(filteredCreateParams).result();
Expand Down

0 comments on commit c1e2f93

Please sign in to comment.