/* eslint-disable class-methods-use-this */ import { WyreClient } from '@beamnetwork/wyre-node-api' import { WyreAPIInterface } from './WyreAPIInterface' import { CreateSubscriptionRequest } from './CreateSubscriptionRequest' import { APIResponse } from './APIResponse' import { Subscription } from './Subscription' import { SubscriptionsList } from './SubscriptionsList' import { GetSubscriptionsRequest } from './GetSubscriptionsRequest' import { Transfer } from './Transfer' import { getErrorResponse } from './api-response-utils' import { TransferRequest } from './TransferRequest' import { WyreAccount } from './WyreAccount' import { CreateAccountRequest } from './CreateAccountRequest' import { WyreAPIOptions } from './WyreAPIOptions' import { UploadDocumentParams } from './UploadDocumentParams' import { CreateWyreWalletParams } from './CreateWyreWalletParams' import { WyreWallet } from './WyreWallet' import { CreateMultipleWalletsParams } from './CreateMultipleWalletsParams' import { EditWalletParams } from './EditWalletParams' import { PaymentMethodParams } from './PaymentMethodParams' import { PaymentMethod } from './PaymentMethod' import { UploadPaymentMethodDocumentParams } from './UploadPaymentMethodDocumentParams' import { UpdateAccountRequest } from './UpdateAccountRequest' import { CreateUserRequest } from './CreateUserRequest' import { WyreUser } from './WyreUser' import { WyreUserKYCData } from './WyreUserKYCData' import { MigrateAccountResponse } from './MigrateAccountResponse' export class SecretKeyWyreAPI implements WyreAPIInterface { private wyre: WyreClient constructor( private options: WyreAPIOptions, wyreClient?: WyreClient, ) { this.wyre = wyreClient || new WyreClient(options) } // tslint:disable-next-line: no-empty async initialize() { } async createUser(params: CreateUserRequest): Promise> { const req = this.wyre.post('/v3/users', params) return await this.executeRequest(req) } async getUser( userID: string, scopes: string = 'TRANSFER', ): Promise> { const req = this.wyre.get(`/v3/users/${userID}?scopes=${scopes}`) return await this.executeRequest(req) } async uploadUserKYCData( userID: string, kycData: WyreUserKYCData, ): Promise> { const req = this.wyre.post(`/v3/users/${userID}`, kycData) return await this.executeRequest(req) } async createAccount(params: CreateAccountRequest): Promise> { const req = this.wyre.post('/v3/accounts', params) return await this.executeRequest(req) } async updateAccount( accountID: string, updates: UpdateAccountRequest, ): Promise> { const req = this.wyre.post(`/v3/accounts/${accountID}`, updates) return await this.executeRequest(req) } async migrateAccount( accountID: string, ): Promise> { const params = { accountId: accountID, } const req = this.wyre.post(`/v3/users/migrateAccount`, params) return await this.executeRequest(req) } async uploadDocument( params: UploadDocumentParams, buff: Buffer, ): Promise> { const { accountId, fieldId, contentType, documentType, documentSubType } = params const headers = { 'Content-Type': contentType, 'Content-Length': buff.length, } let reqUri = `/v3/accounts/${accountId}/${fieldId}` if (documentType) { reqUri = `${reqUri}?documentType=${documentType}&documentSubType=${documentSubType}` } const req = this.wyre.post( reqUri, buff, { headers } ) return await this.executeRequest(req) } async uploadPaymentMethodDocument( params: UploadPaymentMethodDocumentParams, buff: Buffer, ): Promise> { const { paymentMethodId, contentType } = params const headers = { 'Content-Type': contentType, 'Content-Length': buff.length, } const req = this.wyre.post( `/v2/paymentMethod/${paymentMethodId}/followup`, buff, { headers } ) return await this.executeRequest(req) } async addPaymentMethod( params: PaymentMethodParams, ): Promise> { const req = this.wyre.post('/v2/paymentMethods', params) return await this.executeRequest(req) } async getPaymentMethod( paymentMethodID: string, ): Promise> { const req = this.wyre.get(`/v2/paymentMethod/${paymentMethodID}`) return await this.executeRequest(req) } async listPaymentMethods(): Promise> { const req = this.wyre.get(`/v2/paymentMethods`) return await this.executeRequest(req) } async deletePaymentMethod( paymentMethodID: string, ): Promise> { const req = this.wyre.delete(`/v2/paymentMethod/${paymentMethodID}`) return await this.executeRequest(req) } async attachBlockchainToPaymentMethod(paymentMethodId, params) { const req = this.wyre.post(`/v2/paymentMethod/${paymentMethodId}/attach`, params) return await this.executeRequest(req) } async createTransfer(transferRequest: TransferRequest): Promise> { const req = this.wyre.post('/v3/transfers', transferRequest) return await this.executeRequest(req) } async confirmTransfer(transferID): Promise> { const req = this.wyre.post(`/v3/transfers/${transferID}/confirm`, {}) return await this.executeRequest(req) } async getAccountInfoV2(): Promise> { const req = this.wyre.get('/v2/account') return await this.executeRequest(req) } async getAccountInfoV3(accountID): Promise> { const req = this.wyre.get(`/v3/accounts/${accountID}`) return await this.executeRequest(req) } async getTransfers(transferID): Promise> { let url = '/v3/transfers' if (transferID) { url += `/${transferID}` } const req = this.wyre.get(url) return await this.executeRequest(req) } async createSubscription( subscribeRequest: CreateSubscriptionRequest ): Promise> { const req = this.wyre.post('/v3/subscriptions', subscribeRequest) const { response: subscription, error, } = await this.executeRequest(req) if (error) { return { error } } if (Object.keys(subscription).length === 0) { return { error: 'subscribeWebhook failed' } } return { response: subscription } } async deleteSubscription(subscriptionID): Promise> { const req = this.wyre.delete(`/v3/subscriptions/${subscriptionID}`) return await this.executeRequest(req) } async getSubscriptions( subscriptionsReq: GetSubscriptionsRequest ): Promise> { const { offset, length } = subscriptionsReq const req = this.wyre.get( `/v3/subscriptions?offset=${offset}&length=${length}`, ) const { response, error, } = await this.executeRequest(req) if (error) { return { error } } return { response } } async getRates(as, pair) { const req = this.wyre.get(`/v3/rates?as=${as}`) const { response: rates, error } = await this.executeRequest(req) if (error) { return { error } } if (pair) { return { rates: rates[pair] } } return { response: rates } } // Wallet Endpoints async createWallet(walletParams: CreateWyreWalletParams): Promise> { const req = this.wyre.post('/v2/wallets', walletParams) return this.executeRequest(req) } async createMultipleWallets(params: CreateMultipleWalletsParams): Promise> { const req = this.wyre.post('/v2/wallets/batch', params) return this.executeRequest(req) } async lookupWallet(walletID: string): Promise> { const req = this.wyre.get(`/v2/wallet/${walletID}`) return this.executeRequest(req) } async editWallet(walletID: string, params: EditWalletParams): Promise> { const req = this.wyre.post(`/v2/wallet/${walletID}/update`, params) return await this.executeRequest(req) } async deleteWallet(walletID: string): Promise> { const req = this.wyre.delete(`/v2/wallet/${walletID}`) return await this.executeRequest(req) } async listWallets(limit: number, offset: number): Promise> { const req = this.wyre.get(`/v2/wallets?limit=${limit}&offset=${offset}`) return this.executeRequest(req) } getMasqueraded(entityID: string): WyreAPIInterface { if (this.options.isCreatedUsingBearerToken) { return this } return new SecretKeyWyreAPI(this.options, this.wyre.masqueraded(this.getMasqueradeTarget(entityID))) } private getMasqueradeTarget( entityID: string, ): string { // Check to see what kind of token was passed in. if (this.isUserID(entityID)) { return `user:${entityID}` } return entityID } private isAccountID( token: string ): boolean { return token.startsWith('AC_') } private isUserID( token: string ): boolean { return token.startsWith('US_') } // async executeRequest(req): Promise { // try { // const response = await req // return { response } // } catch (ex) { // return { error: getErrorResponse(ex) } // } // } async executeRequest(req: any): Promise> { try { const response = await req return { response } } catch (ex) { return { error: getErrorResponse(ex) } } // try { // const response = await req // const { data } = response // return { response: data } // } catch (ex) { // return { error: this.getError(ex) } // } } // private getError(ex: any): any { // const { response } = ex // if (response) { // const { data } = response // if (data) { // const error1 = { // message: data, // } // return getErrorResponse(error1) // } // } // const error = { // message: ex.error || ex, // } // return getErrorResponse(error) // } }