Skip to content

Commit 7d57a32

Browse files
authored
Merge pull request #800 from input-output-hk/fix/lw-7350-hd-wallet-discovery-first-internal-address
fix(wallet): discovery should search for 1/0 too
2 parents 55c4870 + cdf79cb commit 7d57a32

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

packages/e2e/test/wallet/PersonalWallet/multiAddress.test.ts

+24-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { isNotNil } from '@cardano-sdk/util';
1717

1818
const env = getEnv(walletVariables);
1919
const logger = createLogger();
20-
const PAYMENT_ADDRESSES_TO_GENERATE = 60;
20+
const PAYMENT_INDICES_TO_GENERATE = 30;
21+
const PAYMENT_ADDRESSES_TO_GENERATE = PAYMENT_INDICES_TO_GENERATE * 2; // External + Internal address for each payment derivation index
2122
const COINS_PER_ADDRESS = 3_000_000n;
2223

2324
describe('PersonalWallet/multiAddress', () => {
@@ -49,17 +50,28 @@ describe('PersonalWallet/multiAddress', () => {
4950
const addressesToBeDiscovered = new Array<GroupedAddress>();
5051

5152
// Deposit some tADA at some generated addresses from the previously generated mnemonics.
52-
for (let i = 0; i < PAYMENT_ADDRESSES_TO_GENERATE; ++i) {
53-
const address = await multiAddressKeyAgent.deriveAddress(
54-
{
55-
index: i,
56-
type: AddressType.External
57-
},
58-
0
59-
);
60-
61-
addressesToBeDiscovered.push(address);
62-
txBuilder.addOutput(txBuilder.buildOutput().address(address.address).coin(3_000_000n).toTxOut());
53+
for (let i = 0; i < PAYMENT_INDICES_TO_GENERATE; ++i) {
54+
const [addressExternal, addressInternal] = await Promise.all([
55+
multiAddressKeyAgent.deriveAddress(
56+
{
57+
index: i,
58+
type: AddressType.External
59+
},
60+
0
61+
),
62+
multiAddressKeyAgent.deriveAddress(
63+
{
64+
index: i,
65+
type: AddressType.Internal
66+
},
67+
0
68+
)
69+
]);
70+
71+
addressesToBeDiscovered.push(addressExternal, addressInternal);
72+
73+
txBuilder.addOutput(txBuilder.buildOutput().address(addressExternal.address).coin(3_000_000n).toTxOut());
74+
txBuilder.addOutput(txBuilder.buildOutput().address(addressInternal.address).coin(3_000_000n).toTxOut());
6375
}
6476

6577
const { tx: signedTx } = await txBuilder.build().sign();

packages/wallet/src/services/AddressDiscovery/HDSequentialDiscovery.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ export class HDSequentialDiscovery implements AddressDiscovery {
115115
* @returns A promise that will be resolved into a GroupedAddress list containing the discovered addresses.
116116
*/
117117
public async discover(keyAgent: AsyncKeyAgent): Promise<GroupedAddress[]> {
118-
const firstAddress = await keyAgent.deriveAddress({ index: 0, type: AddressType.External }, 0, true);
118+
const firstAddresses = [await keyAgent.deriveAddress({ index: 0, type: AddressType.External }, 0, true)];
119+
const firstInternalAddress = await keyAgent.deriveAddress({ index: 0, type: AddressType.Internal }, 0, true);
120+
if (await addressHasTx(firstInternalAddress, this.#chainHistoryProvider)) {
121+
firstAddresses.push(firstInternalAddress);
122+
}
119123

120124
const stakeKeyAddresses = await discoverAddresses(
121125
keyAgent,
@@ -145,7 +149,7 @@ export class HDSequentialDiscovery implements AddressDiscovery {
145149
})
146150
);
147151

148-
const addresses = uniqBy([firstAddress, ...stakeKeyAddresses, ...paymentKeyAddresses], 'address');
152+
const addresses = uniqBy([...firstAddresses, ...stakeKeyAddresses, ...paymentKeyAddresses], 'address');
149153

150154
// We need to make sure the addresses are sorted since the wallet assumes that the first address
151155
// in the list is the change address (payment cred 0 and stake cred 0).

packages/wallet/test/integration/withdrawal.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PersonalWallet, TransactionFailure } from '../../src';
33
import { createWallet } from './util';
44
import { firstValueFrom, of } from 'rxjs';
55
import { mockProviders as mocks } from '@cardano-sdk/util-dev';
6+
import uniq from 'lodash/uniq';
67

78
describe('integration/withdrawal', () => {
89
let wallet: PersonalWallet;
@@ -77,7 +78,7 @@ describe('integration/withdrawal', () => {
7778

7879
it('can submit transaction', async () => {
7980
const availableRewards = await firstValueFrom(wallet.balance.rewardAccounts.rewards$);
80-
const accounts = (await firstValueFrom(wallet.addresses$)).map((address) => address.rewardAccount);
81+
const accounts = uniq((await firstValueFrom(wallet.addresses$)).map((address) => address.rewardAccount));
8182
const txInternals = await wallet.initializeTx({
8283
certificates: [
8384
{

packages/wallet/test/services/addressDiscovery/HDSequentialDiscovery.test.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('HDSequentialDiscovery', () => {
2424
createMockChainHistoryProvider(
2525
new Map([
2626
[asPaymentAddress('testAddress_0_0_0'), 1],
27+
[asPaymentAddress('testAddress_0_0_1'), 1],
2728
[asPaymentAddress('testAddress_1_0_0'), 1],
2829
[asPaymentAddress('testAddress_1_0_1'), 1],
2930
[asPaymentAddress('testAddress_2_0_0'), 1]
@@ -34,7 +35,7 @@ describe('HDSequentialDiscovery', () => {
3435

3536
const addresses = await discovery.discover(mockKeyAgent);
3637

37-
expect(addresses.length).toEqual(4);
38+
expect(addresses.length).toEqual(5);
3839
expect(addresses[0]).toEqual({
3940
accountIndex: 0,
4041
address: 'testAddress_0_0_0',
@@ -48,6 +49,18 @@ describe('HDSequentialDiscovery', () => {
4849
type: AddressType.External
4950
});
5051
expect(addresses[1]).toEqual({
52+
accountIndex: 0,
53+
address: 'testAddress_0_0_1',
54+
index: 0,
55+
networkId: 0,
56+
rewardAccount: 'testStakeAddress_0',
57+
stakeKeyDerivationPath: {
58+
index: 0,
59+
role: KeyRole.Stake
60+
},
61+
type: AddressType.Internal
62+
});
63+
expect(addresses[2]).toEqual({
5164
accountIndex: 0,
5265
address: 'testAddress_1_0_0',
5366
index: 1,
@@ -59,7 +72,7 @@ describe('HDSequentialDiscovery', () => {
5972
},
6073
type: AddressType.External
6174
});
62-
expect(addresses[2]).toEqual({
75+
expect(addresses[3]).toEqual({
6376
accountIndex: 0,
6477
address: 'testAddress_1_0_1',
6578
index: 1,
@@ -71,7 +84,7 @@ describe('HDSequentialDiscovery', () => {
7184
},
7285
type: AddressType.Internal
7386
});
74-
expect(addresses[3]).toEqual({
87+
expect(addresses[4]).toEqual({
7588
accountIndex: 0,
7689
address: 'testAddress_2_0_0',
7790
index: 2,

0 commit comments

Comments
 (0)