UNPKG

zumokit

Version:

ZumoKit is a Wallet as a Service SDK

563 lines (562 loc) 26.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.User = void 0; var utility_1 = require("./utility"); var Wallet_1 = require("./Wallet"); var ZumoKitError_1 = require("./ZumoKitError"); var models_1 = require("./models"); /** * User instance, obtained via {@link ZumoKit.signIn} method, provides methods for managing user wallet and accounts. * <p> * Refer to * <a href="https://developers.zumo.money/docs/guides/manage-user-wallet">Manage User Wallet</a>, * <a href="https://developers.zumo.money/docs/guides/create-fiat-account">Create Fiat Account</a>, * <a href="https://developers.zumo.money/docs/guides/view-user-accounts">View User Accounts</a> and * <a href="https://developers.zumo.money/docs/guides/get-account-data">Get Account Data</a> * guides for usage details. */ var User = /** @class */ (function () { /** @internal */ function User(zumoCoreModule, userImpl) { var _this = this; this.accountDataListeners = []; this.accountDataListenersImpl = []; this.zumoCoreModule = zumoCoreModule; this.userImpl = userImpl; this.id = userImpl.getId(); this.integratorId = userImpl.getIntegratorId(); this.hasWallet = userImpl.hasWallet(); this.accounts = JSON.parse(userImpl.getAccounts()).map(function (json) { return new models_1.Account(json); }); this.addAccountDataListener(function (snapshots) { _this.accounts = snapshots.map(function (snapshot) { return snapshot.account; }); }); } /** * Create user wallet seeded by provided mnemonic and encrypted with user's password. * <p> * Mnemonic can be generated by {@link Utils.generateMnemonic} utility method. * @param mnemonic mnemonic seed phrase * @param password user provided password */ User.prototype.createWallet = function (mnemonic, password) { var _this = this; var zumoCoreModule = this.zumoCoreModule; return utility_1.errorProxy(zumoCoreModule, function (resolve, reject) { _this.userImpl.createWallet(mnemonic, password, new zumoCoreModule.WalletCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (wallet) { _this.hasWallet = true; resolve(new Wallet_1.Wallet(zumoCoreModule, wallet)); }, })); }); }; /** * Recover user wallet with mnemonic seed phrase corresponding to user's wallet. * This can be used if user forgets his password or wants to change his wallet password. * @param mnemonic mnemonic seed phrase corresponding to user's wallet * @param password user provided password */ User.prototype.recoverWallet = function (mnemonic, password) { var _this = this; var zumoCoreModule = this.zumoCoreModule; return utility_1.errorProxy(zumoCoreModule, function (resolve, reject) { _this.userImpl.recoverWallet(mnemonic, password, new zumoCoreModule.WalletCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (wallet) { resolve(new Wallet_1.Wallet(zumoCoreModule, wallet)); }, })); }); }; /** * Unlock user wallet with user's password. * @param password user provided password */ User.prototype.unlockWallet = function (password) { var _this = this; var zumoCoreModule = this.zumoCoreModule; return utility_1.errorProxy(zumoCoreModule, function (resolve, reject) { _this.userImpl.unlockWallet(password, new zumoCoreModule.WalletCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (wallet) { resolve(new Wallet_1.Wallet(zumoCoreModule, wallet)); }, })); }); }; /** * Reveal mnemonic seed phrase used to seed user wallet. * @param password user provided password */ User.prototype.revealMnemonic = function (password) { var _this = this; var zumoCoreModule = this.zumoCoreModule; return utility_1.errorProxy(zumoCoreModule, function (resolve, reject) { _this.userImpl.revealMnemonic(password, new zumoCoreModule.MnemonicCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (mnemonic) { resolve(mnemonic); }, })); }); }; /** * Check if mnemonic seed phrase corresponds to user's wallet. * This is useful for validating seed phrase before trying to recover wallet. * @param mnemonic mnemonic seed phrase */ User.prototype.isRecoveryMnemonic = function (mnemonic) { try { return this.userImpl.isRecoveryMnemonic(mnemonic); } catch (exception) { throw new ZumoKitError_1.ZumoKitError(this.zumoCoreModule.getException(exception)); } }; /** * Get account in specific currency, on specific network, with specific type. * @param currencyCode currency code, e.g. 'BTC', 'ETH' or 'GBP' * @param network network type, e.g. 'MAINNET', 'TESTNET' or 'RINKEBY' * @param type account type, e.g. 'STANDARD', 'COMPATIBILITY' or 'SEGWIT' * @param custodyType custody type, e.g. 'CUSTODY' or 'NON-CUSTODY' */ User.prototype.getAccount = function (currencyCode, network, type, custodyType) { var account = this.userImpl.getAccount(currencyCode, network, type, custodyType); if (account.hasValue()) { return new models_1.Account(JSON.parse(account.get())); } return null; }; /** * Check if user is a registered fiat customer. */ User.prototype.isFiatCustomer = function () { return this.userImpl.isFiatCustomer(); }; /** * Make user fiat customer by providing user's personal details. * @param firstName first name * @param middleName middle name or null * @param lastName last name * @param dateOfBirth date of birth in ISO 8601 format, e.g '2020-08-12' * @param email email * @param phone phone number * @param address home address */ User.prototype.makeFiatCustomer = function (firstName, middleName, lastName, dateOfBirth, email, phone, address) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { var optionalMiddleName = new _this.zumoCoreModule.OptionalString(); if (middleName) optionalMiddleName.set(middleName); _this.userImpl.makeFiatCustomer(firstName, optionalMiddleName, lastName, dateOfBirth, email, phone, JSON.stringify(address), new _this.zumoCoreModule.SuccessCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function () { resolve(); }, })); }); }; /** * Create custody or fiat account for specified currency. When creating a fiat account, * user must already be fiat customer. * @param currencyCode country code in ISO 4217 format, e.g. 'GBP' */ User.prototype.createAccount = function (currencyCode) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.createAccount(currencyCode, new _this.zumoCoreModule.AccountCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (account) { resolve(new models_1.Account(JSON.parse(account))); }, })); }); }; /** * Get nominated account details for specified account if it exists. * Refer to * <a href="https://developers.zumo.money/docs/guides/send-transactions#bitcoin">Create Fiat Account</a> * for explanation about nominated account. * @param accountId {@link Account Account} identifier */ User.prototype.getNominatedAccountFiatProperties = function (accountId) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve) { _this.userImpl.getNominatedAccountFiatProperties(accountId, new _this.zumoCoreModule.AccountFiatPropertiesCallbackWrapper({ onError: function () { resolve(null); }, onSuccess: function (accountFiatProperties) { resolve(new models_1.AccountFiatProperties(JSON.parse(accountFiatProperties))); }, })); }); }; /** * Fetch Strong Customer Authentication (SCA) config. */ User.prototype.fetchAuthenticationConfig = function () { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.fetchAuthenticationConfig(new _this.zumoCoreModule.AuthenticationConfigCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (authConfig) { resolve(JSON.parse(authConfig)); }, })); }); }; /** * Create card for a fiat account. * <p> * At least one Knowledge-Based Authentication (KBA) answers should be defined, * answers are limited to 256 characters and cannot be null or empty and only * one answer per question type should be provided. * @param fiatAccountId fiat {@link Account account} identifier * @param cardType 'VIRTUAL' or 'PHYSICAL' * @param mobileNumber card holder mobile number, starting with a '+', followed by the country code and then the mobile number, or null * @param knowledgeBase list of KBA answers */ User.prototype.createCard = function (fiatAccountId, cardType, mobileNumber, knowledgeBase) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.createCard(fiatAccountId, cardType, mobileNumber, JSON.stringify(knowledgeBase), new _this.zumoCoreModule.CardCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (card) { resolve(new models_1.Card(JSON.parse(card))); }, })); }); }; /** * Set card status to 'ACTIVE', 'BLOCKED' or 'CANCELLED'. * - To block card, set card status to 'BLOCKED'. * - To activate a physical card, set card status to 'ACTIVE' and provide PAN and CVC2 fields. * - To cancel a card, set card status to 'CANCELLED'. * - To unblock a card, set card status to 'ACTIVE.'. * @param cardId {@link Card card} identifier * @param cardStatus new card status * @param pan PAN when activating a physical card, null otherwise (defaults to null) * @param cvv2 CVV2 when activating a physical card, null otherwise (defaults to null) */ User.prototype.setCardStatus = function (cardId, cardStatus, pan, cvv2) { var _this = this; if (pan === void 0) { pan = null; } if (cvv2 === void 0) { cvv2 = null; } return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { var optionalPan = new _this.zumoCoreModule.OptionalString(); if (pan) optionalPan.set(pan); var optionalCvv2 = new _this.zumoCoreModule.OptionalString(); if (cvv2) optionalCvv2.set(cvv2); _this.userImpl.setCardStatus(cardId, cardStatus, optionalPan, optionalCvv2, new _this.zumoCoreModule.SuccessCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function () { resolve(); }, })); }); }; /** * Reveals sensitive card details. * @param cardId card identifier */ User.prototype.revealCardDetails = function (cardId) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.revealCardDetails(cardId, new _this.zumoCoreModule.CardDetailsCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (cardDetails) { resolve(JSON.parse(cardDetails)); }, })); }); }; /** * Reveal card PIN. * @param cardId {@link Card card} identifier */ User.prototype.revealPin = function (cardId) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.revealPin(cardId, new _this.zumoCoreModule.PinCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (pin) { resolve(pin); }, })); }); }; /** * Unblock card PIN. * @param cardId {@link Card card} identifier */ User.prototype.unblockPin = function (cardId) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.unblockPin(cardId, new _this.zumoCoreModule.SuccessCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function () { resolve(); }, })); }); }; /** * Add KBA answers to a card without SCA. * <p> * This endpoint is used to set Knowledge-Based Authentication (KBA) answers to * a card without Strong Customer Authentication (SCA). Once it is set SCA flag * on corresponding card is set to true. * <p> * At least one answer should be defined, answers are limited to 256 characters and * cannot be null or empty and only one answer per question type should be provided. * * @param cardId card id * @param knowledgeBase list of KBA answers */ User.prototype.setAuthentication = function (cardId, knowledgeBase) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.setAuthentication(cardId, JSON.stringify(knowledgeBase), new _this.zumoCoreModule.SuccessCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function () { resolve(); }, })); }); }; /** * Listen to all account data changes. * * @param listener interface to listen to user changes */ User.prototype.addAccountDataListener = function (listener) { var listenerImpl = new this.zumoCoreModule.AccountDataListenerWrapper({ onDataChange: function (snapshots) { listener(JSON.parse(snapshots).map(function (json) { return new models_1.AccountDataSnapshot(json); })); }, }); this.userImpl.addAccountDataListener(listenerImpl); this.accountDataListeners.push(listener); this.accountDataListenersImpl.push(listenerImpl); }; /** * Remove listener to state changes. * * @param listener interface to listen to state changes */ User.prototype.removeAccountDataListener = function (listener) { var index; // eslint-disable-next-line no-cond-assign while ((index = this.accountDataListeners.indexOf(listener)) !== -1) { this.accountDataListeners.splice(index, 1); this.userImpl.removeAccountDataListener(this.accountDataListenersImpl.splice(index, 1)[0]); } }; /** * Compose transaction between custody or fiat accounts in Zumo ecosystem. * Refer to <a href="https://developers.zumo.money/docs/guides/send-transactions#internal-transaction">Send Transactions</a> * guide for usage details. * * @param fromAccountId custody or fiat {@link Account Account} identifier * @param toAccountId custody or fiat {@link Account Account} identifier * @param amount amount in source account currency * @param sendMax send maximum possible funds to destination (defaults to false) */ User.prototype.composeTransaction = function (fromAccountId, toAccountId, amount, sendMax) { var _this = this; if (sendMax === void 0) { sendMax = false; } return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { var amountOptional = new _this.zumoCoreModule.OptionalDecimal(); if (amount) amountOptional.set(new _this.zumoCoreModule.Decimal(amount.toString())); _this.userImpl.composeTransaction(fromAccountId, toAccountId, amountOptional, sendMax, new _this.zumoCoreModule.ComposeTransactionCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (composedTransaction) { resolve(new models_1.ComposedTransaction(JSON.parse(composedTransaction))); }, })); }); }; /** * Compose custody withdraw transaction from custody account. * Refer to <a href="https://developers.zumo.money/docs/guides/send-transactions#custody-withdraw-transaction">Send Transactions</a> * guide for usage details. * * @param fromAccountId custody or fiat {@link Account Account} identifier * @param destination destination address or non-custodial account identifier * @param amount amount in source account currency * @param sendMax send maximum possible funds to destination (defaults to false) */ User.prototype.composeCustodyWithdrawTransaction = function (fromAccountId, destination, amount, sendMax) { var _this = this; if (sendMax === void 0) { sendMax = false; } return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { var amountOptional = new _this.zumoCoreModule.OptionalDecimal(); if (amount) amountOptional.set(new _this.zumoCoreModule.Decimal(amount.toString())); _this.userImpl.composeCustodyWithdrawTransaction(fromAccountId, destination, amountOptional, sendMax, new _this.zumoCoreModule.ComposeTransactionCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (composedTransaction) { resolve(new models_1.ComposedTransaction(JSON.parse(composedTransaction))); }, })); }); }; /** * Compose transaction from user fiat account to user's nominated account. * Refer to <a href="https://developers.zumo.money/docs/guides/send-transactions#nominated-transaction">Send Transactions</a> * guide for usage details. * * @param fromAccountId {@link Account Account} identifier * @param amount amount in source account currency * @param sendMax send maximum possible funds to destination (defaults to false) */ User.prototype.composeNominatedTransaction = function (fromAccountId, amount, sendMax) { var _this = this; if (sendMax === void 0) { sendMax = false; } return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { var amountOptional = new _this.zumoCoreModule.OptionalDecimal(); if (amount) amountOptional.set(new _this.zumoCoreModule.Decimal(amount.toString())); _this.userImpl.composeNominatedTransaction(fromAccountId, amountOptional, sendMax, new _this.zumoCoreModule.ComposeTransactionCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (composedTransaction) { resolve(new models_1.ComposedTransaction(JSON.parse(composedTransaction))); }, })); }); }; /** * Submit a transaction asynchronously. * Refer to <a href="https://developers.zumo.money/docs/guides/send-transactions#submit-transaction">Send Transactions</a> * guide for usage details. * * @param composedTransaction Composed transaction retrieved as a result * of one of the compose transaction methods * @param toAccountId Debit account id override, only applicable to direct custody deposits. * In case no account id is specified senders custody account will be debited. * @param metadata Optional metadata that will be attached to transaction */ User.prototype.submitTransaction = function (composedTransaction, toAccountId, metadata) { var _this = this; if (toAccountId === void 0) { toAccountId = null; } if (metadata === void 0) { metadata = null; } var optionalToAccountId = new this.zumoCoreModule.OptionalString(); if (toAccountId) optionalToAccountId.set(toAccountId); return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.submitTransaction(JSON.stringify(composedTransaction.json), optionalToAccountId, JSON.stringify(metadata), new _this.zumoCoreModule.SubmitTransactionCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (transaction) { resolve(new models_1.Transaction(JSON.parse(transaction))); }, })); }); }; /** * Fetch trading pairs that are currently supported. */ User.prototype.fetchTradingPairs = function () { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.fetchTradingPairs(new _this.zumoCoreModule.StringifiedJsonCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (stringifiedJSON) { var tradingPairsJSON = JSON.parse(stringifiedJSON); resolve(tradingPairsJSON.map(function (json) { return new models_1.TradingPair(json); })); }, })); }); }; /** * Compose exchange asynchronously. * Refer to <a href="https://developers.zumo.money/docs/guides/make-exchanges#compose-exchange">Make Exchanges</a> * guide for usage details. * * @param debitAccountId {@link Account Account} identifier * @param creditAccountId {@link Account Account} identifier * @param debitAmount amount to be debited from debit account * @param sendMax exchange maximum possible funds (defaults to false) */ User.prototype.composeExchange = function (debitAccountId, creditAccountId, debitAmount, sendMax) { var _this = this; if (sendMax === void 0) { sendMax = false; } return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { var amountOptional = new _this.zumoCoreModule.OptionalDecimal(); if (debitAmount) amountOptional.set(new _this.zumoCoreModule.Decimal(debitAmount.toString())); _this.userImpl.composeExchange(debitAccountId, creditAccountId, amountOptional, sendMax, new _this.zumoCoreModule.ComposeExchangeCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (composedExchange) { resolve(new models_1.ComposedExchange(JSON.parse(composedExchange))); }, })); }); }; /** * Submit an exchange asynchronously. * Refer to <a href="https://developers.zumo.money/docs/guides/make-exchanges#submit-exchange">Make Exchanges</a> * guide for usage details. * * @param composedExchange Composed exchange retrieved as the result * of {@link composeExchange} method */ User.prototype.submitExchange = function (composedExchange) { var _this = this; return utility_1.errorProxy(this.zumoCoreModule, function (resolve, reject) { _this.userImpl.submitExchange(JSON.stringify(composedExchange.json), new _this.zumoCoreModule.SubmitExchangeCallbackWrapper({ onError: function (error) { reject(new ZumoKitError_1.ZumoKitError(error)); }, onSuccess: function (exchange) { resolve(new models_1.Exchange(JSON.parse(exchange))); }, })); }); }; return User; }()); exports.User = User;