예제 #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
예제 #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')
예제 #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
예제 #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)
예제 #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')
예제 #6
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)
예제 #7
0
 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')
예제 #8
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)
예제 #9
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
예제 #10
0
파일: script.py 프로젝트: jimmysong/lepton
 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)
예제 #11
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])
예제 #12
0
import script
import helper
from tx import TxIn, TxOut, Tx

key_pairs = [x.split('/') for x in map(str.strip,raw.split('\n'))]
key_pairs = [(S256Point.parse(unhexlify(sec)), PrivateKey.parse(wif, compressed=False)) for sec, wif in key_pairs]
assert all(p == pk.point for p,pk in key_pairs)
pubkeys = [p for p, _ in key_pairs]

OP_n = lambda n: 0x51 + n - 1

n_required = 2
elements = [OP_n(n_required)] + [pk.sec(compressed=False) for pk in pubkeys] + [OP_n(len(pubkeys))] + [174]
redeemScript = script.Script(elements)

address=helper.h160_to_p2sh_address(helper.hash160(redeemScript.serialize()), testnet=False)
redeemScript=hexlify(redeemScript.serialize())
assert address=="3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC"
assert redeemScript=="52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353ae".encode('ascii')
print(hexlify(helper.hash160(unhexlify(redeemScript))))

prev_tx = 'd6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389'
prev_index = 0
tx_in = TxIn(unhexlify(prev_tx), prev_index, b'')

h160 = helper.decode_base58_checksum(address)[1:]
tx_out = TxOut(int(0.01*100e6), script_pubkey=helper.p2sh_script(h160))
t = Tx(version=1, tx_ins=[tx_in], tx_outs=[tx_out], locktime=0, testnet=False)
raw_transaction = hexlify(t.serialize())
assert raw_transaction == "010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d60000000000ffffffff0140420f000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e955108700000000".encode('ascii')
print(t)
예제 #13
0
 def test_p2sh_address(self):
     h160 = unhexlify('74d691da1574e6b3c192ecfb52cc8984ee7b6c56')
     want = '3CLoMMyuoDQTPRD3XYZtCvgvkadrAdvdXh'
     self.assertEqual(h160_to_p2sh_address(h160, prefix=b'\x05'), want)
     want = '2N3u1R6uwQfuobCqbCgBkpsgBxvr1tZpe7B'
     self.assertEqual(h160_to_p2sh_address(h160, prefix=b'\xc4'), want)
예제 #14
0
#redeem script as op code

dat_redeem_script_op = Script([0x52, public_key1, public_key2, 0x52, 0xae])

# we serialize it in binary

dat_redeem_script_serialized = dat_redeem_script_op.serialize()

# hash 160

dat_redeem_script_h160 = hash160(dat_redeem_script_serialized)

# we get the address

dat_redeem_script_address = h160_to_p2sh_address(dat_redeem_script_h160,
                                                 testnet=True)

# get it back in bytes

dat_target_h160 = decode_base58(dat_redeem_script_address)

# use the result to build our p2sh_script

target_script = p2sh_script(dat_target_h160)

print(target_script)
print('is p2sh: {}'.format(target_script.is_p2sh_script_pubkey()))

print(dat_redeem_script_h160)
print()
print(dat_redeem_script_address)