@@ -10,7 +10,7 @@ import Mnemonic from 'bitcore-mnemonic';
10
10
import { mockAxiosAdapter } from '../__mock_helpers__/axios-adapter.mock' ;
11
11
import HathorWalletServiceWallet from '../../src/wallet/wallet' ;
12
12
import Network from '../../src/models/network' ;
13
- import { GetAddressesObject , WsTransaction , CreateWalletAuthData } from '../../src/wallet/types' ;
13
+ import { GetAddressesObject , WsTransaction , CreateWalletAuthData , AddressInfoObject } from '../../src/wallet/types' ;
14
14
import config from '../../src/config' ;
15
15
import {
16
16
buildSuccessTxByIdTokenDataResponse ,
@@ -102,6 +102,97 @@ test('getAddressAtIndex', async () => {
102
102
await expect ( wallet . getAddressAtIndex ( 0 ) ) . rejects . toThrow ( 'Error getting wallet addresses.' ) ;
103
103
} ) ;
104
104
105
+ describe ( 'onNewTx' , ( ) => {
106
+ const requestPassword = jest . fn ( ) ;
107
+ const network = new Network ( 'testnet' ) ;
108
+ const seed = defaultWalletSeed ;
109
+
110
+ it ( 'should call getNewAddresses if an output address is in newAddresses' , async ( ) => {
111
+ const wallet = new HathorWalletServiceWallet ( {
112
+ requestPassword,
113
+ seed,
114
+ network,
115
+ } ) ;
116
+
117
+ const testAddress = 'testAddress1' ;
118
+ ( wallet as any ) . newAddresses = [ { address : testAddress , index : 0 , addressPath : "m/0'/0/0" } ] as AddressInfoObject [ ] ;
119
+
120
+ const getNewAddressesSpy = jest . spyOn ( wallet as any , 'getNewAddresses' ) . mockResolvedValue ( undefined ) ;
121
+
122
+ const newTx : WsTransaction = {
123
+ tx_id : 'tx1' ,
124
+ nonce : 0 ,
125
+ timestamp : 0 ,
126
+ signal_bits : 0 ,
127
+ version : 1 ,
128
+ weight : 1 ,
129
+ parents : [ ] ,
130
+ inputs : [ ] ,
131
+ outputs : [
132
+ {
133
+ value : 100n ,
134
+ token_data : 0 ,
135
+ script : { type : 'Buffer' , data : [ ] } ,
136
+ token : 'HTR' ,
137
+ decoded : {
138
+ type : 'P2PKH' ,
139
+ address : testAddress ,
140
+ timelock : null ,
141
+ } ,
142
+ locked : false ,
143
+ index : 0 ,
144
+ } ,
145
+ ] ,
146
+ } ;
147
+
148
+ await wallet . onNewTx ( newTx ) ;
149
+
150
+ expect ( getNewAddressesSpy ) . toHaveBeenCalled ( ) ;
151
+ } ) ;
152
+
153
+ it ( 'should not call getNewAddresses if no output address is in newAddresses' , async ( ) => {
154
+ const wallet = new HathorWalletServiceWallet ( {
155
+ requestPassword,
156
+ seed,
157
+ network,
158
+ } ) ;
159
+
160
+ ( wallet as any ) . newAddresses = [ { address : 'otherAddress' , index : 0 , addressPath : "m/0'/0/0" } ] as AddressInfoObject [ ] ;
161
+
162
+ const getNewAddressesSpy = jest . spyOn ( wallet as any , 'getNewAddresses' ) . mockResolvedValue ( undefined ) ;
163
+
164
+ const newTx : WsTransaction = {
165
+ tx_id : 'tx2' ,
166
+ nonce : 0 ,
167
+ timestamp : 0 ,
168
+ signal_bits : 0 ,
169
+ version : 1 ,
170
+ weight : 1 ,
171
+ parents : [ ] ,
172
+ inputs : [ ] ,
173
+ outputs : [
174
+ {
175
+ value : 100n ,
176
+ token_data : 0 ,
177
+ script : { type : 'Buffer' , data : [ ] } ,
178
+ token : 'HTR' ,
179
+ decoded : {
180
+ type : 'P2PKH' ,
181
+ address : 'someRandomAddress' ,
182
+ timelock : null ,
183
+ } ,
184
+ locked : false ,
185
+ index : 0 ,
186
+ } ,
187
+ ] ,
188
+ } ;
189
+
190
+ await wallet . onNewTx ( newTx ) ;
191
+
192
+ expect ( getNewAddressesSpy ) . not . toHaveBeenCalled ( ) ;
193
+ } ) ;
194
+ } ) ;
195
+
105
196
test ( 'getTxBalance' , async ( ) => {
106
197
const requestPassword = jest . fn ( ) ;
107
198
const network = new Network ( 'testnet' ) ;
0 commit comments