diff --git a/src/client/manager/UserManager.js b/src/client/manager/UserManager.js index ff58e9d..1794085 100644 --- a/src/client/manager/UserManager.js +++ b/src/client/manager/UserManager.js @@ -197,6 +197,10 @@ export default class UserManager { return this.UserApi.addRedeemable(payload) } + mergeAccounts (payload) { + return this.UserApi.mergeAccounts(payload) + } + /** * Checked to see if the refresh token is valid * @return {Boolean} returns True / False if the refresh token is valid diff --git a/src/internal/net/rest/api/UserApi.js b/src/internal/net/rest/api/UserApi.js index 3f0c1cf..35b0060 100644 --- a/src/internal/net/rest/api/UserApi.js +++ b/src/internal/net/rest/api/UserApi.js @@ -12,6 +12,7 @@ import urlParse from 'url-parse' import User from '../../../../model/User' + export default class UserApi { constructor (bv) { this.Blockv = bv @@ -431,4 +432,41 @@ export default class UserApi { userID: profile.id }) } + + async mergeAccounts(payload) { + + try { + + // Begin merge + await this.client.request('POST', '/v1/user/merge_accounts', payload, true) + + // Wait until the specified token has been added to the account + let timeout = Date.now() + 30000 + while (true) { + + // Get current tokens + let tokens = await this.getUserTokens() + if (tokens.find(t => t.properties.token.toLowerCase().trim() == payload.token.toLowerCase().trim())) { + + // Found, account merging is probably finished now + break + + } + + // Not found, check if timeout is exceeded + if (Date.now() > timeout) + throw new Error('Could not find merged token. Please refresh the page and try again.') + + // Wait a bit + await new Promise(c => setTimeout(c, 2000)) + + } + + } catch (err) { + + throw new Error(err) + + } + + } }