client/src/utils/search.js
// ***does not validate that a given address/txid/block is valid***
does not validate that a given address/txid/block is valid
does not validate that a given address / txid / block is valid
function matchQuery (query) {
  if (!query || !query.length) return
  const q = query.toLowerCase()
  const asInt = parseInt(q)
  if (!isNaN(asInt) && asInt >= 0 && `${asInt}` === q) {
    return {
      query: 'blockheight',
      label: 'block height',
      height: asInt,
      value: asInt
    }
  }
  if (/^0{8}[a-f0-9]{56}$/.test(q)) {
    return {
      query: 'blockhash',
      label: 'block hash',
      hash: query,
      value: query,
    }
  }
  if (/^[0-9]+:[a-f0-9]{64}$/.test(q)) {
    const parts = q.split(':')
    return {
      query: 'input',
      label: 'transaction input',
      txid: parts[1],
      output: parts[0],
      value: q
    }
  }
  if (/^[a-f0-9]{64}:[0-9]+$/.test(q)) {
    const parts = q.split(':')
    return {
      query: 'output',
      label: 'transaction output',
      txid: parts[0],
      output: parts[1],
      value: q
    }
  }
  if (/^[a-f0-9]{64}$/.test(q)) {
    return {
      query: 'txid',
      label: 'transaction',
      txid: q,
      value: q
    }
  }
  if ((q.length >= 26 && q.length <= 34) || q.length === 42 || q.length === 62) {
    if (/^[13]\w{24,33}$/.test(q)) {
      let addressType
      if (q[0] === '1') addressType = 'p2pkh'
      else if (q[0] === '3') addressType = 'p2sh'
      else return null
      return {
        query: 'address',
        label: 'address',
        encoding: 'base58',
        addressType,
        address: query,
        value: query,
        scriptPubKey: addressToSPK(query)
      }
    }
    if (/^bc1\w{39}(\w{20})?$/.test(q)) {
      let addressType
      if (q.startsWith('bc1q')) {
        if (q.length === 42) addressType = 'p2wpkh'
        else if (q.length === 62) addressType = 'p2wsh'
        else return null
      } else if (q.startsWith('bc1p') && q.length === 62) {
        addressType = 'p2tr'
      } else return null
      return {
        query: 'address',
        label: 'address',
        encoding: 'bech32',
        addressType,
        address: query,
        value: query,
        scriptPubKey: addressToSPK(query)
      }
    }
  }
  return null
}
function matchQuery ( query ) { if ( ! query || ! query . length ) return const q = query . toLowerCase ( ) const asInt = parseInt ( q ) if ( ! isNaN ( asInt ) && asInt >= 0 && ` ${ asInt } ` === q ) { return { query : 'blockheight' , label : 'block height' , height : asInt , value : asInt } } if ( / ^0{8}[a-f0-9]{56}$ / . test ( q ) ) { return { query : 'blockhash' , label : 'block hash' , hash : query , value : query , } } if ( / ^[0-9]+:[a-f0-9]{64}$ / . test ( q ) ) { const parts = q . split ( ':' ) return { query : 'input' , label : 'transaction input' , txid : parts [ 1 ] , output : parts [ 0 ] , value : q } } if ( / ^[a-f0-9]{64}:[0-9]+$ / . test ( q ) ) { const parts = q . split ( ':' ) return { query : 'output' , label : 'transaction output' , txid : parts [ 0 ] , output : parts [ 1 ] , value : q } } if ( / ^[a-f0-9]{64}$ / . test ( q ) ) { return { query : 'txid' , label : 'transaction' , txid : q , value : q } } if ( ( q . length >= 26 && q . length <= 34 ) || q . length === 42 || q . length === 62 ) { if ( / ^[13]\w{24,33}$ / . test ( q ) ) { let addressType if ( q [ 0 ] === '1' ) addressType = 'p2pkh' else if ( q [ 0 ] === '3' ) addressType = 'p2sh' else return null return { query : 'address' , label : 'address' , encoding : 'base58' , addressType , address : query , value : query , scriptPubKey : addressToSPK ( query ) } } if ( / ^bc1\w{39}(\w{20})?$ / . test ( q ) ) { let addressType if ( q . startsWith ( 'bc1q' ) ) { if ( q . length === 42 ) addressType = 'p2wpkh' else if ( q . length === 62 ) addressType = 'p2wsh' else return null } else if ( q . startsWith ( 'bc1p' ) && q . length === 62 ) { addressType = 'p2tr' } else return null return { query : 'address' , label : 'address' , encoding : 'bech32' , addressType , address : query , value : query , scriptPubKey : addressToSPK ( query ) } } } return null }
does not validate that a given address/txid/block is valid
does not validate that a given address / txid / block is valid
// Looks like a block height? // Remember to update the bounds in // Looks like a block hash? // Looks like a transaction input? // Looks like a transaction output? // Looks like a transaction id? // Looks like an address // Looks like a legacy address // Looks like a bech32 address