1
1
/* eslint-disable unicorn/consistent-destructuring, sonarjs/no-duplicate-string, @typescript-eslint/no-floating-promises, promise/no-nesting, promise/always-return */
2
2
import * as Crypto from '@cardano-sdk/crypto' ;
3
+ import { AddressDiscovery , PersonalWallet , TxInFlight } from '../../src' ;
3
4
import { AddressType , Bip32Account , GroupedAddress , Witnesser , util } from '@cardano-sdk/key-management' ;
4
5
import { AssetId , createStubStakePoolProvider , mockProviders as mocks } from '@cardano-sdk/util-dev' ;
5
6
import { BehaviorSubject , Subscription , firstValueFrom , skip } from 'rxjs' ;
@@ -18,7 +19,6 @@ import {
18
19
} from '@cardano-sdk/core' ;
19
20
import { HexBlob } from '@cardano-sdk/util' ;
20
21
import { InitializeTxProps } from '@cardano-sdk/tx-construction' ;
21
- import { PersonalWallet , TxInFlight } from '../../src' ;
22
22
import { buildDRepIDFromDRepKey , toOutgoingTx , waitForWalletStateSettle } from '../util' ;
23
23
import { getPassphrase , stakeKeyDerivationPath , testAsyncKeyAgent } from '../../../key-management/test/mocks' ;
24
24
import { dummyLogger as logger } from 'ts-log' ;
@@ -60,6 +60,15 @@ const promiseTimeout = (p: Promise<unknown>, timeout = 10): Promise<unknown> =>
60
60
61
61
describe ( 'PersonalWallet methods' , ( ) => {
62
62
const address = mocks . utxo [ 0 ] [ 0 ] . address ! ;
63
+ const groupedAddress : GroupedAddress = {
64
+ accountIndex : 0 ,
65
+ address,
66
+ index : 0 ,
67
+ networkId : Cardano . NetworkId . Testnet ,
68
+ rewardAccount : mocks . rewardAccount ,
69
+ stakeKeyDerivationPath,
70
+ type : AddressType . External
71
+ } ;
63
72
let txSubmitProvider : mocks . TxSubmitProviderStub ;
64
73
let networkInfoProvider : mocks . NetworkInfoProviderStub ;
65
74
let assetProvider : mocks . MockAssetProvider ;
@@ -71,33 +80,27 @@ describe('PersonalWallet methods', () => {
71
80
let utxoProvider : mocks . UtxoProviderStub ;
72
81
let witnesser : Witnesser ;
73
82
let bip32Account : Bip32Account ;
83
+ let addressDiscovery : jest . Mocked < AddressDiscovery > ;
74
84
75
85
beforeEach ( async ( ) => {
76
86
txSubmitProvider = mocks . mockTxSubmitProvider ( ) ;
77
87
networkInfoProvider = mocks . mockNetworkInfoProvider ( ) ;
78
88
utxoProvider = mocks . mockUtxoProvider ( ) ;
79
-
80
89
assetProvider = mocks . mockAssetProvider ( ) ;
81
90
stakePoolProvider = createStubStakePoolProvider ( ) ;
82
91
rewardsProvider = mockRewardsProvider ( ) ;
83
92
chainHistoryProvider = mockChainHistoryProvider ( ) ;
84
93
handleProvider = mocks . mockHandleProvider ( ) ;
85
- const groupedAddress : GroupedAddress = {
86
- accountIndex : 0 ,
87
- address,
88
- index : 0 ,
89
- networkId : Cardano . NetworkId . Testnet ,
90
- rewardAccount : mocks . rewardAccount ,
91
- stakeKeyDerivationPath,
92
- type : AddressType . External
93
- } ;
94
+ addressDiscovery = { discover : jest . fn ( ) . mockImplementation ( async ( ) => [ groupedAddress ] ) } ;
95
+
94
96
const asyncKeyAgent = await testAsyncKeyAgent ( ) ;
95
97
bip32Account = await Bip32Account . fromAsyncKeyAgent ( asyncKeyAgent ) ;
96
98
bip32Account . deriveAddress = jest . fn ( ) . mockResolvedValue ( groupedAddress ) ;
97
99
witnesser = util . createBip32Ed25519Witnesser ( asyncKeyAgent ) ;
98
100
wallet = new PersonalWallet (
99
101
{ name : 'Test Wallet' } ,
100
102
{
103
+ addressDiscovery,
101
104
assetProvider,
102
105
bip32Account,
103
106
chainHistoryProvider,
@@ -465,4 +468,22 @@ describe('PersonalWallet methods', () => {
465
468
expect ( response ) . toBe ( 'string' ) ;
466
469
expect ( bip32Account . derivePublicKey ) . toHaveBeenCalledTimes ( 3 ) ;
467
470
} ) ;
471
+
472
+ describe ( 'discoverAddresses' , ( ) => {
473
+ it ( 'discovers new addreses and emits them from addresses$' , async ( ) => {
474
+ const newAddresses : GroupedAddress [ ] = [
475
+ groupedAddress ,
476
+ {
477
+ ...groupedAddress ,
478
+ address : Cardano . PaymentAddress (
479
+ 'addr_test1qzs0umu0s2ammmpw0hea0w2crtcymdjvvlqngpgqy76gpfnuzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475qp3y3vz'
480
+ ) ,
481
+ index : groupedAddress . index + 1
482
+ }
483
+ ] ;
484
+ addressDiscovery . discover . mockResolvedValueOnce ( newAddresses ) ;
485
+ await expect ( wallet . discoverAddresses ( ) ) . resolves . toEqual ( newAddresses ) ;
486
+ await expect ( firstValueFrom ( wallet . addresses$ ) ) . resolves . toEqual ( newAddresses ) ;
487
+ } ) ;
488
+ } ) ;
468
489
} ) ;
0 commit comments