bitgo
Version:
BitGo JavaScript SDK
116 lines (106 loc) • 3.42 kB
text/typescript
//
// BitGo JavaScript SDK
//
// Copyright 2014, BitGo, Inc. All Rights Reserved.
//
import pjson = require('../package.json');
import * as _ from 'lodash';
import { BaseCoin, CoinFactory, common } from '@bitgo/sdk-core';
import { BitGoAPI, BitGoAPIOptions } from '@bitgo/sdk-api';
import {
createTokenMapUsingTrimmedConfigDetails,
TrimmedAmsTokenConfig,
createToken,
getFormattedTokenConfigForCoin,
} from '@bitgo/statics';
import { GlobalCoinFactory, registerCoinConstructors, getTokenConstructor } from './v2/coinFactory';
// constructor params used exclusively for BitGo class
export type BitGoOptions = BitGoAPIOptions & {
useAms?: boolean;
};
export class BitGo extends BitGoAPI {
private _coinFactory: CoinFactory;
private _useAms: boolean;
/**
* Constructor for BitGo Object
*/
constructor(params: BitGoOptions = {}) {
super(params);
if (
!common.validateParams(
params,
[],
[
'clientId',
'clientSecret',
'refreshToken',
'accessToken',
'userAgent',
'customRootURI',
'customBitcoinNetwork',
'serverXpub',
'stellarFederationServerUrl',
]
) ||
(params.useProduction && !_.isBoolean(params.useProduction)) ||
(params.useAms && !_.isBoolean(params.useAms))
) {
throw new Error('invalid argument');
}
if (!params.clientId !== !params.clientSecret) {
throw new Error('invalid argument - must provide both client id and secret');
}
this._useAms = !!params.useAms;
this._version = pjson.version;
this._userAgent = params.userAgent || 'BitGoJS/' + this.version();
this._coinFactory = new CoinFactory();
}
/**
* Initialize the coin factory with token configurations
* @param tokenConfigMap - A map of token metadata from AMS
*/
initCoinFactory(tokenConfigMap: Record<string, TrimmedAmsTokenConfig[]>): void {
// TODO(WIN-5839): use AMS endpoint to fetch config details
const coinMap = createTokenMapUsingTrimmedConfigDetails(tokenConfigMap);
this._coinFactory = new CoinFactory();
registerCoinConstructors(this._coinFactory, coinMap);
}
/**
* Create a basecoin object
* @param coinName
*/
coin(coinName: string): BaseCoin {
if (this._useAms) {
return this._coinFactory.getInstance(this, coinName);
}
return GlobalCoinFactory.getInstance(this, coinName);
}
/**
* Register a token in the coin factory
* @param tokenConfig - The token metadata from AMS
*/
registerToken(tokenConfig: TrimmedAmsTokenConfig): void {
if (!this._useAms) {
throw new Error('registerToken is only supported when useAms is set to true');
}
// TODO(WIN-5839): use AMS endpoint to fetch token metadata
const staticsBaseCoin = createToken(tokenConfig);
if (staticsBaseCoin) {
const formattedTokenConfig = getFormattedTokenConfigForCoin(staticsBaseCoin);
if (formattedTokenConfig) {
const tokenConstructor = getTokenConstructor(formattedTokenConfig);
if (tokenConstructor) {
this._coinFactory.registerToken(staticsBaseCoin, tokenConstructor);
}
}
}
}
/**
* Create a basecoin object for a virtual token
* @param tokenName
*/
async token(tokenName: string): Promise<BaseCoin> {
await this.fetchConstants();
return this.coin(tokenName);
}
}