Skip to content

Commit

Permalink
[DDW-669] Introduce a fix for HW transactions issue with the native t…
Browse files Browse the repository at this point in the history
…okens
  • Loading branch information
tomislavhoracek committed May 3, 2021
1 parent e2747c9 commit d49e867
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ Changelog

### Fixes

- Fixed hardware wallets transactions issue with native tokens ([PR 2543](https://github.com/input-output-hk/daedalus/pull/2543))
- Fixed hardware wallets transaction initialization issue on device reconnect ([PR 2541](https://github.com/input-output-hk/daedalus/pull/2541))
- Fixed blank currency selection after updating to 4.0.5 ([PR 2529](https://github.com/input-output-hk/daedalus/pull/2529))
- Fixed hardware wallets confirmation crashing issue when device is in "Busy" state ([PR 2522](https://github.com/input-output-hk/daedalus/pull/2522))
Expand Down
46 changes: 33 additions & 13 deletions source/renderer/app/utils/shelleyLedger.js
Expand Up @@ -6,7 +6,7 @@ import {
} from '@cardano-foundation/ledgerjs-hw-app-cardano';
import { encode } from 'borc';
import blakejs from 'blakejs';
import { map, groupBy, sortBy } from 'lodash';
import _ from 'lodash';
import {
derivationPathToLedgerPath,
CERTIFICATE_TYPE,
Expand Down Expand Up @@ -120,15 +120,35 @@ export const ShelleyTxInputFromUtxo = (utxoInput: CoinSelectionInput) => {
};

export const groupTokensByPolicyId = (assets: CoinSelectionAssetsType) => {
const sortedAssets = sortBy(
assets,
(asset) => {
return asset.assetName.length;
},
['asc'],
['assetName', 'desc']
);
return groupBy(sortedAssets, 'policyId');
const compareStringsCanonically = (string1: string, string2: string) =>
string1.length - string2.length || string1.localeCompare(string2);

const groupedAssets = {};
_(assets)
.orderBy(['policyId', 'assetName'], ['asc', 'asc'])
.groupBy(({ policyId }) => policyId)
.mapValues((tokens) =>
tokens.map(({ assetName, quantity, policyId }) => ({
assetName,
quantity,
policyId,
}))
)
.map((tokens, policyId) => ({
policyId,
assets: tokens.sort((token1, token2) =>
compareStringsCanonically(token1.assetName, token2.assetName)
),
}))
.sort((token1, token2) =>
compareStringsCanonically(token1.policyId, token2.policyId)
)
.value()
.map((sortedAssetsGroup) => {
groupedAssets[sortedAssetsGroup.policyId] = sortedAssetsGroup.assets;
return groupedAssets;
});
return groupedAssets;
};

export const ShelleyTxOutputAssets = (assets: CoinSelectionAssetsType) => {
Expand All @@ -138,7 +158,7 @@ export const ShelleyTxOutputAssets = (assets: CoinSelectionAssetsType) => {

Object.entries(tokenObject).forEach(([policyId, tokens]) => {
const assetMap = new Map<Buffer, number>();
map(tokens, (token) => {
_.map(tokens, (token) => {
assetMap.set(Buffer.from(token.assetName, 'hex'), token.quantity);
});
policyIdMap.set(Buffer.from(policyId, 'hex'), assetMap);
Expand All @@ -150,7 +170,7 @@ export const prepareTokenBundle = (assets: CoinSelectionAssetsType) => {
const tokenObject = groupTokensByPolicyId(assets);
const tokenObjectEntries = Object.entries(tokenObject);

const tokenBundle = map(tokenObjectEntries, ([policyId, tokens]) => {
const tokenBundle = _.map(tokenObjectEntries, ([policyId, tokens]) => {
const tokensList = tokens.map(({ assetName, quantity }) => ({
assetNameHex: assetName,
amount: quantity.toString(),
Expand Down Expand Up @@ -233,7 +253,7 @@ export const ShelleyTxWithdrawal = (
) => {
function encodeCBOR(encoder: any) {
const withdrawalMap = new Map();
map(withdrawals, (withdrawal) => {
_.map(withdrawals, (withdrawal) => {
const rewardAccount = utils.bech32_decodeAddress(withdrawal.stakeAddress);
const coin = withdrawal.amount.quantity;
withdrawalMap.set(rewardAccount, coin);
Expand Down

0 comments on commit d49e867

Please sign in to comment.