async onTransactionToScript(script, callback) {
    const scriptHash = scriptToHash(script)

    // Check if transaction for script already exists.
    const initialStatus = await this.electrumClient
      .blockchain_scripthash_subscribe(scriptHash)
      .catch(err => {
        throw new Error(`failed to subscribe: ${err}`)
      })

    // Invoke callback for the current status.
    const result = await callback(initialStatus)
    if (result) {
      // TODO: We send request directly, because `electrumjs` library doesn't
      // support `blockchain.scripthash.unsubscribe` method.
      await this.electrumClient
        .blockchain_scripthash_unsubscribe(scriptHash)
        .catch(err => {
          throw new Error(`failed to unsubscribe: ${err}`)
        })

      return result
    }

    // If callback have not resolved wait for new transaction notifications.
    return new Promise(async resolve => {
      try {
        const eventName = "blockchain.scripthash.subscribe"
        const electrumClient = this.electrumClient

        const listener = async function(/** @type {[string, string]} */ msg) {
          const receivedScriptHash = msg[0]
          const status = msg[1]

          console.log(
            `Received notification for script hash: [${receivedScriptHash}] with status: [${status}]`
          )

          if (receivedScriptHash == scriptHash) {
            const result = await callback(status)
            if (result) {
              await electrumClient.subscribe.off(eventName, listener)

              await electrumClient
                .blockchain_scripthash_unsubscribe(scriptHash)
                .catch(err => {
                  throw new Error(`failed to unsubscribe: ${err}`)
                })

              return resolve(result)
            }
          }
        }

        this.electrumClient.subscribe.on(eventName, listener)
      } catch (err) {
        throw new Error(`failed listening for notification: ${err}`)
      }
    })
  }
/**
   * Listens for transactions sent to a script until callback resolves to a
   * non-`null` value. It includes transactions in the mempool. It passes
   * the status of the transaction to the callback (see
   * {@link TransactionToScriptReceived<T>}).
   *
   * @template T
   * @param {string} script ScriptPubKey in a hexadecimal format.
   * @param {TransactionToScriptReceived<T>} callback An async callback
   *        function called when an existing transaction for the script is found
   *        or a new transaction is sent to the script. If the transaction
   *        returns non-`null`, the returned value is also returned from
   *        `onTransactionToScript` and monitoring of the specified script is
   *        discontinued.
   * @return {Promise<T>} Value resolved by the callback.
   */
Listens for transactions sent to a script until callback resolves to a
non-`null` value. It includes transactions in the mempool. It passes
the status of the transaction to the callback (see
TransactionToScriptReceived).

@template T
@param {string} script ScriptPubKey in a hexadecimal format.
@param {TransactionToScriptReceived} callback An async callback
function called when an existing transaction for the script is found
or a new transaction is sent to the script. If the transaction
returns non-`null`, the returned value is also returned from
`onTransactionToScript` and monitoring of the specified script is
discontinued.
@return {Promise} Value resolved by the callback.
Listens for transactions sent to a script until callback resolves to a non - ` null ` value . It includes transactions in the mempool . It passes the status of the transaction to the callback ( see TransactionToScriptReceived ) . @template T @param { string } script ScriptPubKey in a hexadecimal format . @param { TransactionToScriptReceived } callback An async callback function called when an existing transaction for the script is found or a new transaction is sent to the script . If the transaction returns non - ` null ` the returned value is also returned from ` onTransactionToScript ` and monitoring of the specified script is discontinued . @return { Promise } Value resolved by the callback .
async onTransactionToScript(script, callback) {
    const scriptHash = scriptToHash(script)
    const initialStatus = await this.electrumClient
      .blockchain_scripthash_subscribe(scriptHash)
      .catch(err => {
        throw new Error(`failed to subscribe: ${err}`)
      })
    const result = await callback(initialStatus)
    if (result) {
      await this.electrumClient
        .blockchain_scripthash_unsubscribe(scriptHash)
        .catch(err => {
          throw new Error(`failed to unsubscribe: ${err}`)
        })
      return result
    }
    return new Promise(async resolve => {
      try {
        const eventName = "blockchain.scripthash.subscribe"
        const electrumClient = this.electrumClient
        const listener = async function( msg) {
          const receivedScriptHash = msg[0]
          const status = msg[1]
          console.log(
            `Received notification for script hash: [${receivedScriptHash}] with status: [${status}]`
          )
          if (receivedScriptHash == scriptHash) {
            const result = await callback(status)
            if (result) {
              await electrumClient.subscribe.off(eventName, listener)
              await electrumClient
                .blockchain_scripthash_unsubscribe(scriptHash)
                .catch(err => {
                  throw new Error(`failed to unsubscribe: ${err}`)
                })
              return resolve(result)
            }
          }
        }
        this.electrumClient.subscribe.on(eventName, listener)
      } catch (err) {
        throw new Error(`failed listening for notification: ${err}`)
      }
    })
  }
async onTransactionToScript ( script , callback ) { const scriptHash = scriptToHash ( script ) const initialStatus = await this . electrumClient . blockchain_scripthash_subscribe ( scriptHash ) . catch ( err => { throw new Error ( ` ${ err } ` ) } ) const result = await callback ( initialStatus ) if ( result ) { await this . electrumClient . blockchain_scripthash_unsubscribe ( scriptHash ) . catch ( err => { throw new Error ( ` ${ err } ` ) } ) return result } return new Promise ( async resolve => { try { const eventName = "blockchain.scripthash.subscribe" const electrumClient = this . electrumClient const listener = async function ( msg ) { const receivedScriptHash = msg [ 0 ] const status = msg [ 1 ] console . log ( ` ${ receivedScriptHash } ${ status } ` ) if ( receivedScriptHash == scriptHash ) { const result = await callback ( status ) if ( result ) { await electrumClient . subscribe . off ( eventName , listener ) await electrumClient . blockchain_scripthash_unsubscribe ( scriptHash ) . catch ( err => { throw new Error ( ` ${ err } ` ) } ) return resolve ( result ) } } } this . electrumClient . subscribe . on ( eventName , listener ) } catch ( err ) { throw new Error ( ` ${ err } ` ) } } ) }
Listens for transactions sent to a script until callback resolves to a
 non-`null` value.
Listens for transactions sent to a script until callback resolves to a non - ` null ` value .
// Check if transaction for script already exists. // Invoke callback for the current status. // TODO: We send request directly, because `electrumjs` library doesn't // support `blockchain.scripthash.unsubscribe` method. // If callback have not resolved wait for new transaction notifications. /** @type {[string, string]} */