diff --git a/src/utils/api/account/lsk.js b/src/utils/api/account/lsk.js index 4421676ec5..5a1a4c2937 100644 --- a/src/utils/api/account/lsk.js +++ b/src/utils/api/account/lsk.js @@ -36,6 +36,7 @@ const getAccountParams = (params) => { passphrase, publicKey, } = params; + // Pick username, cause the address is not obtainable from the username if (username) return { username }; // If you have the address, you don't need anything else @@ -79,26 +80,35 @@ export const getAccount = async ({ params: normParams, }); - return response.data[0]; + if (response.data[0]) { + const account = { ...response.data[0] }; + const isAccountUninitialized = !account.summary.publicKey; + if (isAccountUninitialized && (params.publicKey || params.passphrase)) { + const publicKey = params.publicKey ?? extractPublicKey(params.passphrase); + account.summary.publicKey = publicKey; + } + + return account; + } } catch (e) { // eslint-disable-next-line no-console console.log('Lisk account not found.'); - let publicKey = params.publicKey; - if (!publicKey && params.passphrase) { - publicKey = extractPublicKey(params.passphrase); - } - const account = { - summary: { - publicKey, - balance: 0, - address: normParams.address, - token: tokenMap.LSK.key, - }, - }; - - return account; + if (params.publicKey || params.passphrase) { + const publicKey = params.publicKey ?? extractPublicKey(params.passphrase); + const account = { + summary: { + publicKey, + balance: 0, + address: normParams.address, + token: tokenMap.LSK.key, + }, + }; + return account; + } } + + throw Error('Error retrieving account'); }; const accountFilters = { diff --git a/src/utils/api/account/lsk.test.js b/src/utils/api/account/lsk.test.js index f456aa4df9..654ae8910a 100644 --- a/src/utils/api/account/lsk.test.js +++ b/src/utils/api/account/lsk.test.js @@ -154,17 +154,17 @@ describe('API: LSK Account', () => { }); }); - it('should call http without parameters', async () => { + it('should call http without base url if not passed', async () => { http.mockImplementation(() => Promise.resolve({ data: [{}] })); // Checks with no baseUrl await getAccount({ network, - params: { }, + params: { passphrase }, }); expect(http).toHaveBeenCalledWith({ network, - params: { }, + params: { address }, baseUrl: undefined, path, }); @@ -209,5 +209,59 @@ describe('API: LSK Account', () => { }, }); }); + + it('should use the public key from params if the account is uninitialized', async () => { + http.mockImplementation(() => Promise.resolve({ + data: [{ + summary: { + publicKey: '', address, balance: 0, token: 'LSK', + }, + }], + })); + // Checks the baseUrl too + const result = await getAccount({ + network, + params: { + publicKey, + }, + baseUrl, + }); + + expect(result).toEqual({ + summary: { + address, + balance: 0, + token: 'LSK', + publicKey, + }, + }); + }); + + it('should use extract the public key from params.passphrase if the account is uninitialized', async () => { + http.mockImplementation(() => Promise.resolve({ + data: [{ + summary: { + publicKey: '', address, balance: 0, token: 'LSK', + }, + }], + })); + // Checks the baseUrl too + const result = await getAccount({ + network, + params: { + passphrase, + }, + baseUrl, + }); + + expect(result).toEqual({ + summary: { + address, + balance: 0, + token: 'LSK', + publicKey, + }, + }); + }); }); }); diff --git a/src/utils/transaction.js b/src/utils/transaction.js index 8a3e2542eb..2b463583f4 100644 --- a/src/utils/transaction.js +++ b/src/utils/transaction.js @@ -76,11 +76,11 @@ const transformTransaction = (transaction) => { case unlockToken: { transformedTransaction.asset = { - // unlockObjects: transaction.unlockObjects.map(unlockingObject => ({ - // delegateAddress: unlockingObject.delegateAddress, - // amount: unlockingObject.amount, - // unvoteHeight: unlockingObject.height.start - // })), + unlockObjects: transaction.asset.unlockObjects.map(unlockingObject => ({ + delegateAddress: getBase32AddressFromAddress(unlockingObject.delegateAddress), + amount: Number(unlockingObject.amount), + unvoteHeight: unlockingObject.height.start, + })), }; break; } @@ -150,7 +150,11 @@ const createTransactionObject = (tx, moduleAssetId) => { case unlockToken: { transaction.asset = { - unlockObjects: tx.unlockObjects, + unlockObjects: tx.unlockingObjects.map(unlockingObject => ({ + amount: BigInt(unlockingObject.amount), + delegateAddress: getAddressFromBase32Address(unlockingObject.delegateAddress), + unvoteHeight: unlockingObject.unvoteHeight, + })), }; break; }