Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk-core): improve url handling #4945

Closed
wants to merge 1 commit into from
Closed
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
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
9 changes: 5 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,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();
Expand Down
Loading