Skip to content

Commit d64543f

Browse files
authored
Merge pull request #763 from input-output-hk/fix/ledger-assets-ordering
fix: correct ledger mapping canonical asset and asset group ordering
2 parents 9567993 + 2095877 commit d64543f

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

packages/hardware-ledger/src/transformers/assets.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import * as Ledger from '@cardano-foundation/ledgerjs-hw-app-cardano';
22
import { Cardano } from '@cardano-sdk/core';
33

44
const compareAssetNameCanonically = (a: Ledger.Token, b: Ledger.Token) => {
5-
if (a.assetNameHex === b.assetNameHex) {
5+
if (a.assetNameHex.length === b.assetNameHex.length) {
66
return a.assetNameHex > b.assetNameHex ? 1 : -1;
77
} else if (a.assetNameHex.length > b.assetNameHex.length) return 1;
88
return -1;
99
};
1010

11-
const comparePolicyIdCanonically = (a: Ledger.AssetGroup, b: Ledger.AssetGroup) => {
12-
if (a.policyIdHex === b.policyIdHex) {
13-
return a.policyIdHex > b.policyIdHex ? 1 : -1;
14-
} else if (a.policyIdHex.length > b.policyIdHex.length) return 1;
15-
return -1;
16-
};
11+
const comparePolicyIdCanonically = (a: Ledger.AssetGroup, b: Ledger.AssetGroup) =>
12+
// PolicyId is always of the same length
13+
a.policyIdHex > b.policyIdHex ? 1 : -1;
1714

1815
const tokenMapToAssetGroup = (tokenMap: Cardano.TokenMap): Ledger.AssetGroup[] => {
1916
const map = new Map<string, Array<Ledger.Token>>();

packages/hardware-ledger/test/transformers/assets.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ describe('assets', () => {
1616

1717
expect(ledgerAssets).toEqual([
1818
{
19-
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
20-
tokens: [
21-
{ amount: 40n, assetNameHex: '' },
22-
{ amount: 30n, assetNameHex: '504154415445' }
23-
]
19+
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
20+
tokens: [{ amount: 20n, assetNameHex: '' }]
2421
},
2522
{
2623
policyIdHex: '659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82',
2724
tokens: [{ amount: -50n, assetNameHex: '54534c41' }]
2825
},
2926
{
30-
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
31-
tokens: [{ amount: 20n, assetNameHex: '' }]
27+
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
28+
tokens: [
29+
{ amount: 40n, assetNameHex: '' },
30+
{ amount: 30n, assetNameHex: '504154415445' }
31+
]
3232
}
3333
]);
3434
});

packages/hardware-ledger/test/transformers/tx.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,11 @@ describe('tx', () => {
4747
],
4848
mint: [
4949
{
50-
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
50+
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
5151
tokens: [
5252
{
53-
amount: 40n,
53+
amount: 20n,
5454
assetNameHex: ''
55-
},
56-
{
57-
amount: 30n,
58-
assetNameHex: '504154415445'
5955
}
6056
]
6157
},
@@ -69,11 +65,15 @@ describe('tx', () => {
6965
]
7066
},
7167
{
72-
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
68+
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
7369
tokens: [
7470
{
75-
amount: 20n,
71+
amount: 40n,
7672
assetNameHex: ''
73+
},
74+
{
75+
amount: 30n,
76+
assetNameHex: '504154415445'
7777
}
7878
]
7979
}
@@ -96,15 +96,11 @@ describe('tx', () => {
9696
format: Ledger.TxOutputFormat.ARRAY_LEGACY,
9797
tokenBundle: [
9898
{
99-
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
99+
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
100100
tokens: [
101101
{
102-
amount: 40n,
102+
amount: 20n,
103103
assetNameHex: ''
104-
},
105-
{
106-
amount: 30n,
107-
assetNameHex: '504154415445'
108104
}
109105
]
110106
},
@@ -118,11 +114,15 @@ describe('tx', () => {
118114
]
119115
},
120116
{
121-
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
117+
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
122118
tokens: [
123119
{
124-
amount: 20n,
120+
amount: 40n,
125121
assetNameHex: ''
122+
},
123+
{
124+
amount: 30n,
125+
assetNameHex: '504154415445'
126126
}
127127
]
128128
}

packages/hardware-ledger/test/transformers/txOut.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,11 @@ describe('txOut', () => {
7979
format: Ledger.TxOutputFormat.ARRAY_LEGACY,
8080
tokenBundle: [
8181
{
82-
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
82+
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
8383
tokens: [
8484
{
85-
amount: 40n,
85+
amount: 20n,
8686
assetNameHex: ''
87-
},
88-
{
89-
amount: 30n,
90-
assetNameHex: '504154415445'
9187
}
9288
]
9389
},
@@ -101,11 +97,15 @@ describe('txOut', () => {
10197
]
10298
},
10399
{
104-
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
100+
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
105101
tokens: [
106102
{
107-
amount: 20n,
103+
amount: 40n,
108104
assetNameHex: ''
105+
},
106+
{
107+
amount: 30n,
108+
assetNameHex: '504154415445'
109109
}
110110
]
111111
}
@@ -144,15 +144,11 @@ describe('txOut', () => {
144144
format: Ledger.TxOutputFormat.ARRAY_LEGACY,
145145
tokenBundle: [
146146
{
147-
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
147+
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
148148
tokens: [
149149
{
150-
amount: 40n,
150+
amount: 20n,
151151
assetNameHex: ''
152-
},
153-
{
154-
amount: 30n,
155-
assetNameHex: '504154415445'
156152
}
157153
]
158154
},
@@ -166,11 +162,15 @@ describe('txOut', () => {
166162
]
167163
},
168164
{
169-
policyIdHex: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740',
165+
policyIdHex: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373',
170166
tokens: [
171167
{
172-
amount: 20n,
168+
amount: 40n,
173169
assetNameHex: ''
170+
},
171+
{
172+
amount: 30n,
173+
assetNameHex: '504154415445'
174174
}
175175
]
176176
}

packages/util-dev/src/mockProviders/mockUtxoProvider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export const utxo: Cardano.Utxo[] = [
3838
'addr_test1qq585l3hyxgj3nas2v3xymd23vvartfhceme6gv98aaeg9muzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475q2g7k3g'
3939
),
4040
value: {
41-
assets: new Map([[AssetId.TSLA, 15n]]),
41+
assets: new Map([
42+
[AssetId.TSLA, 15n],
43+
[AssetId.PXL, 20n]
44+
]),
4245
coins: 3_289_566n
4346
}
4447
}

packages/wallet/test/hardware/ledger/LedgerKeyAgent.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ describe('LedgerKeyAgent', () => {
110110
'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp'
111111
),
112112
value: {
113-
assets: new Map([[AssetId.TSLA, 6n]]),
113+
assets: new Map([
114+
[AssetId.PXL, 10n],
115+
[AssetId.TSLA, 6n]
116+
]),
114117
coins: 5n
115118
}
116119
}

0 commit comments

Comments
 (0)