@@ -137,16 +137,25 @@ async function testWithdraw(
137137 const stats = await memberClient . getDataUnion ( dataUnion . getAddress ( ) ) . getMemberStats ( memberWallet . address )
138138 log ( `Stats: ${ JSON . stringify ( stats ) } ` )
139139
140+ // "bridge-sponsored mainnet withdraw" case
141+ if ( ! options . payForTransport && options . waitUntilTransportIsComplete ) {
142+ bridgeWhitelist . push ( memberWallet . address )
143+ }
144+
140145 // test setup done, do the withdraw
141146 const balanceBefore = await getBalance ( memberWallet )
142147 log ( `Balance before: ${ balanceBefore } . Withdrawing tokens...` )
143-
144148 let ret = await withdraw ( dataUnion . getAddress ( ) , memberClient , memberWallet , adminClient )
149+
150+ // "other-sponsored mainnet withdraw" case
145151 if ( typeof ret === 'string' ) {
146152 log ( `Transporting message "${ ret } "` )
147153 ret = await dataUnion . transportMessage ( String ( ret ) )
148154 }
149155 log ( `Tokens withdrawn, return value: ${ JSON . stringify ( ret ) } ` )
156+
157+ // "skip waiting" or "without checking the recipient account" case
158+ // we need to wait nevertheless, to be able to assert that balance in fact changed
150159 if ( ! options . waitUntilTransportIsComplete ) {
151160 log ( `Waiting until balance changes from ${ balanceBefore . toString ( ) } ` )
152161 await until ( async ( ) => getBalance ( memberWallet ) . then ( ( b ) => ! b . eq ( balanceBefore ) ) )
@@ -164,6 +173,28 @@ async function testWithdraw(
164173 expect ( balanceIncrease . toString ( ) ) . toBe ( amount . toString ( ) )
165174}
166175
176+ log ( 'Starting the simulated bridge-sponsored signature transport process' )
177+ // event UserRequestForSignature(bytes32 indexed messageId, bytes encodedData)
178+ const signatureRequestEventSignature = '0x520d2afde79cbd5db58755ac9480f81bc658e5c517fcae7365a3d832590b0183'
179+ const sidechainAmbAddress = '0xaFA0dc5Ad21796C9106a36D68f69aAD69994BB64'
180+ const bridgeWhitelist : string [ ] = [ ]
181+ providerSidechain . on ( {
182+ address : sidechainAmbAddress ,
183+ topics : [ signatureRequestEventSignature ]
184+ } , async ( event ) => {
185+ log ( `Observed signature request for message id=${ event . topics [ 1 ] } ` ) // messageId is indexed so it's in topics...
186+ const message = defaultAbiCoder . decode ( [ 'bytes' ] , event . data ) [ 0 ] // ...only encodedData is in data
187+ const recipient = '0x' + message . slice ( 200 , 240 )
188+ if ( bridgeWhitelist . find ( ( address ) => address . toLowerCase ( ) === recipient ) ) {
189+ log ( `Recipient ${ recipient } not whitelisted, ignoring` )
190+ return
191+ }
192+ const hash = keccak256 ( message )
193+ const adminClient = new StreamrClient ( config . clientOptions )
194+ await adminClient . getDataUnion ( '0x0000000000000000000000000000000000000000' ) . transportMessage ( hash , 100 , 120000 )
195+ log ( `Transported message hash=${ hash } ` )
196+ } )
197+
167198describe ( 'DataUnion withdraw' , ( ) => {
168199 const balanceClient = createClient ( )
169200
@@ -189,33 +220,6 @@ describe('DataUnion withdraw', () => {
189220 return sendToMainnet ? balanceClient . getTokenBalance ( wallet . address ) : balanceClient . getSidechainTokenBalance ( wallet . address )
190221 }
191222
192- // emulate the bridge-sponsored withdrawals
193- beforeAll ( ( ) => {
194- if ( ! payForTransport && waitUntilTransportIsComplete ) {
195- log ( 'Starting the simulated bridge-sponsored signature transport process' )
196- // event UserRequestForSignature(bytes32 indexed messageId, bytes encodedData)
197- const signatureRequestEventSignature = '0x520d2afde79cbd5db58755ac9480f81bc658e5c517fcae7365a3d832590b0183'
198- const sidechainAmbAddress = '0xaFA0dc5Ad21796C9106a36D68f69aAD69994BB64'
199- providerSidechain . on ( {
200- address : sidechainAmbAddress ,
201- topics : [ signatureRequestEventSignature ]
202- } , async ( event ) => {
203- log ( `Observed signature request for message id=${ event . topics [ 1 ] } ` ) // messageId is indexed so it's in topics...
204- const message = defaultAbiCoder . decode ( [ 'bytes' ] , event . data ) [ 0 ] // ...only encodedData is in data
205- const hash = keccak256 ( message )
206- const adminClient = new StreamrClient ( config . clientOptions )
207- await adminClient . getDataUnion ( '0x0000000000000000000000000000000000000000' ) . transportMessage ( hash )
208- log ( `Transported message hash=${ hash } ` )
209- } )
210- }
211- } )
212- afterAll ( ( ) => {
213- if ( ! payForTransport && waitUntilTransportIsComplete ) {
214- log ( 'Stopping the simulated bridge-sponsored signature transport process' )
215- providerSidechain . removeAllListeners ( )
216- }
217- } )
218-
219223 describe ( 'by member' , ( ) => {
220224
221225 it ( 'to itself' , ( ) => {
0 commit comments