client/src/utils/encodings.js
// Extract an address from a raw scriptpubkey
Extract an address from a raw scriptpubkey
Extract an address from a raw scriptpubkey
function SPKToAddress (spk) {
  if (spk.startsWith('5120')) {
    return (new bech32.BitcoinAddress('bc', 1, hexToUintArray(spk.slice(4)))).encode()
  } else if (spk.startsWith('0020') || spk.startsWith('0014')) {
    return (new bech32.BitcoinAddress('bc', 0, hexToUintArray(spk.slice(4)))).encode()
  } else if (spk.startsWith('76a914')) {
    const payload = "00" + spk.slice(6, -4)
    const checksum = hash(hash(payload)).slice(0, 8)
    return binary_to_base58(hexToUintArray(payload + checksum))
  } else if (spk.startsWith('a914')) {
    const payload = "05" + spk.slice(4, -2)
    const checksum = hash(hash(payload)).slice(0, 8)
    return binary_to_base58(hexToUintArray(payload + checksum))
  } else if (spk.startsWith('6a')) {
    return 'OP_RETURN'
  } else if (spk.length == 134 && spk.startsWith('41') && spk.endsWith('ac')) {
    return 'P2PK'
  } else if (spk.length == 70 && spk.startsWith('21') && spk.endsWith('ac')) {
    return 'P2PK'
  } else if (spk.endsWith('51ae') && spk.startsWith('51')) {
    return '1-of-1 P2MS'
  } else if (spk.endsWith('52ae')) {
    if (spk.startsWith(51)) return '1-of-2 P2MS'
    if (spk.startsWith(52)) return '2-of-2 P2MS'
  } else if (spk.endsWith('53ae')) {
    if (spk.startsWith(51)) return '1-of-3 P2MS'
    if (spk.startsWith(52)) return '2-of-3 P2MS'
    if (spk.startsWith(53)) return '3-of-3 P2MS'
  }
}
function SPKToAddress ( spk ) { if ( spk . startsWith ( '5120' ) ) { return ( new bech32 . BitcoinAddress ( 'bc' , 1 , hexToUintArray ( spk . slice ( 4 ) ) ) ) . encode ( ) } else if ( spk . startsWith ( '0020' ) || spk . startsWith ( '0014' ) ) { return ( new bech32 . BitcoinAddress ( 'bc' , 0 , hexToUintArray ( spk . slice ( 4 ) ) ) ) . encode ( ) } else if ( spk . startsWith ( '76a914' ) ) { const payload = "00" + spk . slice ( 6 , - 4 ) const checksum = hash ( hash ( payload ) ) . slice ( 0 , 8 ) return binary_to_base58 ( hexToUintArray ( payload + checksum ) ) } else if ( spk . startsWith ( 'a914' ) ) { const payload = "05" + spk . slice ( 4 , - 2 ) const checksum = hash ( hash ( payload ) ) . slice ( 0 , 8 ) return binary_to_base58 ( hexToUintArray ( payload + checksum ) ) } else if ( spk . startsWith ( '6a' ) ) { return 'OP_RETURN' } else if ( spk . length == 134 && spk . startsWith ( '41' ) && spk . endsWith ( 'ac' ) ) { return 'P2PK' } else if ( spk . length == 70 && spk . startsWith ( '21' ) && spk . endsWith ( 'ac' ) ) { return 'P2PK' } else if ( spk . endsWith ( '51ae' ) && spk . startsWith ( '51' ) ) { return '1-of-1 P2MS' } else if ( spk . endsWith ( '52ae' ) ) { if ( spk . startsWith ( 51 ) ) return '1-of-2 P2MS' if ( spk . startsWith ( 52 ) ) return '2-of-2 P2MS' } else if ( spk . endsWith ( '53ae' ) ) { if ( spk . startsWith ( 51 ) ) return '1-of-3 P2MS' if ( spk . startsWith ( 52 ) ) return '2-of-3 P2MS' if ( spk . startsWith ( 53 ) ) return '3-of-3 P2MS' } }
Extract an address from a raw scriptpubkey
Extract an address from a raw scriptpubkey
// taproot // p2wsh or p2wpkh // p2pkh // p2sh // OP_RETURN // uncompressed p2pk // compressed p2pk // possible p2ms (raw multisig) // possible p2ms (raw multisig) // possible p2ms (raw multisig)