Skip to content

Commit 2569ad8

Browse files
committed
fix: sort addresses
1 parent 7fca80c commit 2569ad8

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

src/utils/addressMap.test.ts

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
22
import { address } from '../fixtures/common';
33
import { Address } from '../serializable/fxs/common';
44
import { AddressMap, AddressMaps, matchOwners } from './addressMap';
@@ -245,67 +245,65 @@ describe('AddressMaps', () => {
245245
});
246246

247247
describe('matchOwners', () => {
248-
it('matches owners', () => {
249-
const owner1 = address();
250-
const owner2 = Address.fromHex('7db97c7cece249c2b98bdc0226cc4c2a57bf52fc');
251-
const ownerAddresses: Uint8Array[] = [owner1.toBytes(), owner2.toBytes()];
252-
const goodOwner = OutputOwners.fromNative(ownerAddresses, 0n, 1);
253-
const threasholdTooHigh = OutputOwners.fromNative(ownerAddresses, 0n, 5);
254-
const wrongOwner = OutputOwners.fromNative(
255-
[hexToBuffer('0x12345123451234512345')],
256-
0n,
257-
5,
258-
);
259-
const locked = OutputOwners.fromNative(
260-
ownerAddresses,
261-
9999999999999999999999999999999999n,
262-
5,
263-
);
248+
const owner1 = address();
249+
const owner2 = Address.fromHex('7db97c7cece249c2b98bdc0226cc4c2a57bf52fc');
250+
const ownerAddresses: Uint8Array[] = [owner1.toBytes(), owner2.toBytes()];
251+
const goodOwner = OutputOwners.fromNative(ownerAddresses, 0n, 1);
252+
const threasholdTooHigh = OutputOwners.fromNative(ownerAddresses, 0n, 5);
253+
const wrongOwner = OutputOwners.fromNative(
254+
[hexToBuffer('0x12345123451234512345')],
255+
0n,
256+
5,
257+
);
258+
const locked = OutputOwners.fromNative(
259+
ownerAddresses,
260+
9999999999999999999999999999999999n,
261+
5,
262+
);
264263

265-
const specs = [
266-
{
267-
testCase: goodOwner,
268-
expectedSigIndices: [0],
269-
expectedAddressMap: new AddressMap([[owner1, 0]]),
270-
},
271-
{
272-
testCase: threasholdTooHigh,
273-
expectedSigIndices: undefined,
274-
expectedAddressMap: undefined,
275-
},
276-
{
277-
testCase: locked,
278-
expectedSigIndices: undefined,
279-
expectedAddressMap: undefined,
280-
},
281-
{
282-
testCase: wrongOwner,
283-
expectedSigIndices: undefined,
284-
expectedAddressMap: undefined,
285-
},
286-
{
287-
testCase: goodOwner,
288-
sigindices: [1],
289-
expectedSigIndices: [1],
290-
expectedAddressMap: new AddressMap([[owner2, 1]]),
291-
},
292-
{
293-
testCase: goodOwner,
294-
sigindices: [2],
295-
expectedSigIndices: undefined,
296-
expectedAddressMap: undefined,
297-
},
298-
];
264+
const specs = [
265+
{
266+
testCase: goodOwner,
267+
expectedSigIndices: [0],
268+
expectedAddressMap: new AddressMap([[owner2, 0]]),
269+
},
270+
{
271+
testCase: threasholdTooHigh,
272+
expectedSigIndices: undefined,
273+
expectedAddressMap: undefined,
274+
},
275+
{
276+
testCase: locked,
277+
expectedSigIndices: undefined,
278+
expectedAddressMap: undefined,
279+
},
280+
{
281+
testCase: wrongOwner,
282+
expectedSigIndices: undefined,
283+
expectedAddressMap: undefined,
284+
},
285+
{
286+
testCase: goodOwner,
287+
sigindices: [1],
288+
expectedSigIndices: [1],
289+
expectedAddressMap: new AddressMap([[owner1, 1]]),
290+
},
291+
{
292+
testCase: goodOwner,
293+
sigindices: [2],
294+
expectedSigIndices: undefined,
295+
expectedAddressMap: undefined,
296+
},
297+
];
299298

300-
specs.forEach((spec) => {
301-
const result = matchOwners(
302-
spec.testCase,
303-
addressesFromBytes(ownerAddresses),
304-
50n,
305-
spec.sigindices,
306-
);
307-
expect(result?.sigIndicies).toEqual(spec.expectedSigIndices);
308-
expect(result?.addressMap).toEqual(spec.expectedAddressMap);
309-
});
299+
test.each(specs)('matchOwners($testCase, $sigIndices)', (spec) => {
300+
const result = matchOwners(
301+
spec.testCase,
302+
addressesFromBytes(ownerAddresses),
303+
50n,
304+
spec.sigindices,
305+
);
306+
expect(result?.sigIndicies).toEqual(spec.expectedSigIndices);
307+
expect(result?.addressMap).toEqual(spec.expectedAddressMap);
310308
});
311309
});

src/utils/addressesFromBytes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Address } from '../serializable/fxs/common';
2+
import { bytesCompare } from './bytesCompare';
23

34
export function addressesFromBytes(bytes: readonly Uint8Array[]): Address[] {
4-
return bytes.map((b) => new Address(b));
5+
const sortedBytes = bytes.toSorted(bytesCompare);
6+
return sortedBytes.map((b) => new Address(b));
57
}

0 commit comments

Comments
 (0)