transactions – Is there any python library could possibly be used to decodescript in output

0
34


Resolved and save the code in right here.
https://github.com/keviny/bitcoin-block-parser

Many particulars are present in https://en.bitcoin.it/wiki/Script.

The code to decode the ScriptPubKey in transaction output:

class TxOutput:
  def __init__(self, blockchain, idx):
    self.idx = idx
    self.worth = uint8(blockchain)
    self.script_len = varint(blockchain)
    self.pubkey = blockchain.learn(self.script_len)
    self.addr = "UNKNOWN"
    self.decode_scriptpubkey(self.pubkey)

  def to_string(self):
    sb = []
    sb.append("Worth: %d" % self.worth + " Satoshi")
    sb.append("Script Len: %d" % self.script_len)
    sb.append("ScriptPubkey: %s" % self.addr)
    sb.append("Addr: %s" % self.addr)
    return sb

  def decode_scriptpubkey(self, knowledge):
    ''' https://en.bitcoin.it/wiki/Script '''
    hexstr = hashStr(knowledge)
    # Get the primary two bytes.
    # which could some downside.
    # https://www.blockchain.com/btc/tx/7bd54def72825008b4ca0f4aeff13e6be2c5fe0f23430629a9d484a1ac2a29b8
    attempt:
      op_idx = int(hexstr[0:2], 16)
    besides:
      self.sort = "EXCEPTION"
      self.addr = "UNKNOWN"
      return
    attempt:
      op_code = OPCODE_NAMES[op_idx]
    besides KeyError:
      if op_idx==65:
        self.sort = "P2PK"
        # Obsoleted pay to pubkey immediately
        # For element see: https://en.bitcoin.it/wiki/Script#Obsolete_pay-to-pubkey_transaction
        pub_key_len = op_idx
        op_code_tail = OPCODE_NAMES[int(hexstr[2 + pub_key_len * 2:2 + pub_key_len * 2 + 2], 16)]
        self.pubkey_human = "Pubkey OP_CODE: None Bytes:%s tail_op_code:%s %d" % (pub_key_len, op_code_tail, op_idx)
        self.addr = pubkey_to_address(hexstr[2:2 + pub_key_len * 2])[0]
      else:
        # Some occasions individuals will push knowledge immediately
        # e.g: https://www.blockchain.com/btc/tx/d65bb24f6289dad27f0f7e75e80e187d9b189a82dcf5a86fb1c6f8ff2b2c190f
        self.sort = "UN"
        pub_key_len = op_idx
        self.pubkey_human = "PUSH_DATA:%s" % hexstr[2:2 + pub_key_len * 2]
        self.addr = "UNKNOWN"
      return
    attempt:
      if op_code == "OP_DUP":
        self.sort = "P2PKHA"
        # P2PKHA pay to pubkey hash mode
        # For element see: https://en.bitcoin.it/wiki/Script#Standard_Transaction_to_Bitcoin_address_.28pay-to-pubkey-hash.29
        op_code2 = OPCODE_NAMES[int(hexstr[2:4], 16)]
        pub_key_len = int(hexstr[4:6], 16)
        op_code_tail2 = OPCODE_NAMES[int(hexstr[6 + pub_key_len * 2:6 + pub_key_len * 2 + 2], 16)]
        op_code_tail_last = OPCODE_NAMES[int(hexstr[6 + pub_key_len * 2 + 2:6 + pub_key_len * 2 + 4], 16)]
        self.pubkey_human = "%s %s %s %s %s" % (op_code, op_code2,  hexstr[6:6 + pub_key_len * 2], op_code_tail2, op_code_tail_last)
        self.addr = gen_addr(hexstr[6:6 + pub_key_len * 2])[0]
      elif op_code == "OP_HASH160":
        self.sort = "P2SH"
        # P2SHA pay to script hash
        # https://en.bitcoin.it/wiki/Transaction#Pay-to-Script-Hash
        pub_key_len = int(hexstr[2:4], 16)
        op_code_tail = OPCODE_NAMES[int(hexstr[4 + pub_key_len * 2:4 + pub_key_len * 2 + 2], 16)]
        hash_code = hexstr[4:4 + pub_key_len * 2]
        self.pubkey_human = "%s %s %s" % (op_code, hash_code, op_code_tail)
        self.addr = hash_code
      elif op_code == "OP_RETURN":
        self.sort = "OP_RETURN"
        pub_key_len = int(hexstr[2:4], 16)
        hash_code = hexstr[4:4 + pub_key_len * 2]
        self.pubkey_human = "OP_RETURN %s" % (hash_code)
        self.addr = hash_code
      else:  # TODO prolong for multi-signature parsing
        self.sort = "UN"
        self.pubkey_human = "Want to increase multi-signaturer parsing %x" % int(hexstr[0:2], 16) + op_code
        self.addr = "UNKNOWN"
    besides:
      self.sort = "ERROR"
      self.addr = "UNKNOWN"



Supply hyperlink

Leave a reply