Beispiel #1
0
    def _create_addresses(tx,batch,testnet=True):
        """
        testnet is TRUE always. Change this for mainnet later.
        """
        batch_size = 5
        app_log.info(f" batch size {batch_size}. Represents the amount of blocks whose outputs are being encoded to addresses.")
        for height in range(batch*batch_size, batch_size*(batch + 1)):

            result = tx.run("MATCH (b:block {height:$height}) "
                             "MATCH (x)<-[:CREATES]-(t:transaction)<-[:CONTAINS]-(b) "
                             "RETURN x.script_pubkey, x.index, t.id", height=height)

            addresses = []
            for output in result.data():
                address = None
                addr_type = None
                raw_script_pubkey = output["x.script_pubkey"]
                b_sp = bytes.fromhex(raw_script_pubkey)
                length = encode_varint(len(b_sp))
                stream = BytesIO(length+b_sp)
                
                try: 
                    script_pubkey = Script.parse(stream)
                
                    if script_pubkey.is_p2pkh_script_pubkey(): 
                        address= h160_to_p2pkh_address(script_pubkey.cmds[2], testnet)
                        addr_type = "P2PKH"
                    elif script_pubkey.is_p2sh_script_pubkey():  
                        address= h160_to_p2sh_address(script_pubkey.cmds[1], testnet)
                        addr_type = "P2SH"
                    elif script_pubkey.is_p2wpkh_script_pubkey() or script_pubkey.is_p2wsh_script_pubkey(): 
                        if testnet: address = segwit_addr.encode("tb",0,script_pubkey.cmds[1])
                        else: address = segwit_addr.encode("bc",0,script_pubkey.cmds[1]) 
                        if script_pubkey.is_p2wpkh_script_pubkey(): addr_type = "P2WPKH"
                        else: addr_type = "P2WSH"
                    elif len(script_pubkey.cmds)==2 and script_pubkey.cmds[1]==0xac:
                        try: 
                            address = script_pubkey.cmds[0].hex()
                            addr_type = "P2PK"
                        except: app_log.info(f"P2PK failed {script_pubkey.cmds[0]} from tx: {output['t.id']}")

                except: app_log.info(f"script parsing failed in tx {output['t.id']} index {output['x.index']} ")
                    
                if address is not None:
                    address_dict = {
                        "address":address,
                        "type": addr_type,
                        "tx_id":output["t.id"],
                        "index":output["x.index"]
                    }
                    addresses.append(address_dict)

            if len(addresses)>0:
                result = tx.run("FOREACH (address in $addresses | \n"
                                "MERGE (o:output {index:address.index})<-[:CREATES]-(:transaction {id:address.tx_id}) \n"
                                "MERGE (a:address {address:address.address}) SET a.type=address.type \n"
                                "MERGE (a)-[:HAS]->(o) )", addresses=addresses)

        
        return
Beispiel #2
0
 def address(self, testnet=False):
     print('address', self.cmds)
     if self.is_p2pkh_script_pubkey():  # p2pkh
         print('p2pkh')
         # hash160 is the 3rd cmd
         h160 = self.cmds[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif self.is_p2sh_script_pubkey():  # p2sh
         # hash160 is the 2nd cmd
         h160 = self.cmds[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
     elif self.is_p2wpkh_script_pubkey():
         witver = self.cmds[0]
         script = self.cmds[1]
         print('bech32 addr', script_to_bech32(script, witver, testnet))
         return script_to_bech32(script, witver, testnet)
     elif self.is_p2wsh_script_pubkey():
         witver = self.cmds[0]
         script = self.cmds[1]
         print('bech32 addr', script_to_bech32(script, witver, testnet))
         return script_to_bech32(script, witver, testnet)
     elif self.is_p2pk_script_pubkey():
         return 'P2PK'
     elif self.cmds[0] == 106:
         return 'OP_RETURN'
     raise ValueError('Unknown ScriptPubKey')
Beispiel #3
0
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     sig_type = self.type()
     if sig_type == 'p2pkh':
         return h160_to_p2pkh_address(self.elements[2], testnet=testnet)
     elif sig_type == 'p2sh':
         return h160_to_p2sh_address(self.elements[1], testnet=testnet)
     else:
         return None
Beispiel #4
0
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     if self.is_p2pkh_script_pubkey():  # p2pkh
         # hash160 is the 3rd instruction
         h160 = self.instructions[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif self.is_p2sh_script_pubkey():  # p2sh
         # hash160 is the 2nd instruction
         h160 = self.instructions[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
Beispiel #5
0
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     # if p2pkh
     if self.is_p2pkh_script_pubkey():  # p2pkh
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(self.hash160(), testnet)
     # if p2sh
     elif self.is_p2sh_script_pubkey():  # p2sh
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(self.hash160(), testnet)
     # raise a ValueError
     raise ValueError('Unknown ScriptPubKey')
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     sig_type = self.type()
     if sig_type == 'p2pkh':
         # hash160 is the 3rd element
         h160 = self.elements[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif sig_type == 'p2sh':
         # hash160 is the 2nd element
         h160 = self.elements[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     if self.is_p2pkh_script_pubkey():  # p2pkh
         # hash160 is the 3rd cmd
         h160 = self.cmds[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif self.is_p2sh_script_pubkey():  # p2sh
         # hash160 is the 2nd cmd
         h160 = self.cmds[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
     raise ValueError('Unknown ScriptPubKey')
Beispiel #8
0
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     sig_type = self.type()
     if sig_type == 'p2pkh':
         # hash160 is the 3rd element
         h160 = self.elements[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif sig_type == 'p2sh':
         # hash160 is the 2nd element
         h160 = self.elements[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
     elif sig_type == 'p2pk':
         return h160_to_p2pkh_address(hash160(self.elements[0]), testnet)
     elif sig_type == 'p2wpkh':
         import segwit_addr
         if self.elements[0] == b'':
             witver = 0
         else:
             assert 0
         return segwit_addr.encode("bc", witver, self.elements[1])
Beispiel #9
0
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     # HACK: just count how many items to determine what type it is
     if len(self.items) == 5:  # p2pkh
         # hash160 is the 3rd item
         h160 = self.items[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif len(self.items) == 3:  # p2sh
         # hash160 is the 2nd item
         h160 = self.items[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
Beispiel #10
0
        def encode_address(_script_pubkey, testnet=True):
            address = ""
            addr_type = ""
            length = encode_varint(len(_script_pubkey))
            stream = BytesIO(length + _script_pubkey)
            #stream = BytesIO(_script_pubkey)
            try:
                script_pubkey = Script.parse(stream)
                if script_pubkey.is_p2pkh_script_pubkey():
                    address = h160_to_p2pkh_address(script_pubkey.cmds[2],
                                                    testnet)
                    addr_type = "P2PKH"
                elif script_pubkey.is_p2sh_script_pubkey():
                    address = h160_to_p2sh_address(script_pubkey.cmds[1],
                                                   testnet)
                    addr_type = "P2SH"
                elif script_pubkey.is_p2wpkh_script_pubkey(
                ) or script_pubkey.is_p2wsh_script_pubkey():
                    if testnet:
                        address = segwit_addr.encode("tb", 0,
                                                     script_pubkey.cmds[1])
                    else:
                        address = segwit_addr.encode("bc", 0,
                                                     script_pubkey.cmds[1])
                    if script_pubkey.is_p2wpkh_script_pubkey():
                        addr_type = "P2WPKH"
                    else:
                        addr_type = "P2WSH"
                elif len(script_pubkey.cmds
                         ) == 2 and script_pubkey.cmds[1] == 0xac:
                    try:
                        address = script_pubkey.cmds[0].hex()
                        addr_type = "P2PK"
                    except:
                        app_log.info(
                            f"P2PK failed {script_pubkey.cmds[0]} from tx: {output['t.id']}"
                        )

            except:
                app_log.info(f"script parsing failed.")

            return address, addr_type
Beispiel #11
0
 def address(self, testnet=False):
     '''Returns the address corresponding to the script'''
     if self.is_p2pkh_script_pubkey():  # p2pkh
         # hash160 is the 3rd element
         h160 = self.instructions[2]
         # convert to p2pkh address using h160_to_p2pkh_address (remember testnet)
         return h160_to_p2pkh_address(h160, testnet)
     elif self.is_p2sh_script_pubkey():  # p2sh
         # hash160 is the 2nd element
         h160 = self.instructions[1]
         # convert to p2sh address using h160_to_p2sh_address (remember testnet)
         return h160_to_p2sh_address(h160, testnet)
     elif self.is_p2wpkh_script_pubkey():  # p2sh
         # hash160 is the 2nd element
         witness_program = self.raw_serialize()
         # convert to bech32 address using encode_bech32_checksum
         return encode_bech32_checksum(witness_program, testnet)
     elif self.is_p2wsh_script_pubkey():  # p2sh
         # hash160 is the 2nd element
         witness_program = self.raw_serialize()
         # convert to bech32 address using encode_bech32_checksum
         return encode_bech32_checksum(witness_program, testnet)
Beispiel #12
0
 def test_p2pkh_address(self):
     h160 = unhexlify('74d691da1574e6b3c192ecfb52cc8984ee7b6c56')
     want = '1BenRpVUFK65JFWcQSuHnJKzc4M8ZP8Eqa'
     self.assertEqual(h160_to_p2pkh_address(h160), want)
     want = 'mrAjisaT4LXL5MzE81sfcDYKU3wqWSvf9q'
     self.assertEqual(h160_to_p2pkh_address(h160, prefix=b'\x6f'), want)