From e249f4b7f09985c501b0997d96dcaaeb8d517462 Mon Sep 17 00:00:00 2001 From: David Kaplan Date: Wed, 25 Sep 2024 08:30:18 -0400 Subject: [PATCH] chore(sdk-core): improve url handling Clean this up a bit BTC-1504 TICKET: BTC-1504 --- modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts | 11 +++++++++-- modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts | 6 +++++- modules/sdk-core/src/bitgo/wallet/iWallet.ts | 3 ++- modules/sdk-core/src/bitgo/wallet/wallet.ts | 9 +++++---- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts b/modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts index 7425fd7331..c34760cfa9 100644 --- a/modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts +++ b/modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts @@ -42,6 +42,7 @@ import { BuildNftTransferDataOptions, BaseBroadcastTransactionOptions, BaseBroadcastTransactionResult, + UrlOptions, } from './iBaseCoin'; import { IInscriptionBuilder } from '../inscriptionBuilder'; import { Hash } from 'crypto'; @@ -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 { diff --git a/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts b/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts index b8c84f3b69..9e4938d515 100644 --- a/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts +++ b/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts @@ -458,11 +458,15 @@ export interface BaseBroadcastTransactionResult { txIds?: string[]; } +export interface UrlOptions { + removeChain?: boolean; +} + export interface IBaseCoin { type: string; tokenConfig?: BaseTokenConfig; getConfig(): Readonly; - url(suffix: string): string; + url(suffix: string, options?: UrlOptions): string; wallets(): IWallets; enterprises(): IEnterprises; keychains(): IKeychains; diff --git a/modules/sdk-core/src/bitgo/wallet/iWallet.ts b/modules/sdk-core/src/bitgo/wallet/iWallet.ts index 608a8fa652..22c93de373 100644 --- a/modules/sdk-core/src/bitgo/wallet/iWallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/iWallet.ts @@ -8,6 +8,7 @@ import { VerificationOptions, TypedData, NFTTransferOptions, + UrlOptions, } from '../baseCoin'; import { BitGoBase } from '../bitgoBase'; import { Keychain, KeychainWithEncryptedPrv } from '../keychain'; @@ -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; diff --git a/modules/sdk-core/src/bitgo/wallet/wallet.ts b/modules/sdk-core/src/bitgo/wallet/wallet.ts index 9d7a88e28e..a1c65266f3 100644 --- a/modules/sdk-core/src/bitgo/wallet/wallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/wallet.ts @@ -12,6 +12,7 @@ import { SignedTransaction, SignedTransactionRequest, TransactionPrebuild, + UrlOptions, VerifyAddressOptions, } from '../baseCoin'; import { makeRandomKey } from '../bitcoin'; @@ -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); } /** @@ -753,8 +755,7 @@ 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 }); if (filteredParams.create) { const filteredCreateParams = _.pick(params.create, ['unspentIds', 'expireTime']); return this.bitgo.post(url).send(filteredCreateParams).result();