def is_address_valid(addr, on_testnet=False): # Check if scripthash, # pubkey or stealth address if on_testnet: if addr[0] == '2': magic_byte = 196 elif addr[0] == 'm' or addr[0] == 'n': magic_byte = 111 elif addr[0] == 'w': magic_byte = 43 else: return False else: if addr[0] == '3': magic_byte = 5 elif addr[0] == '1': magic_byte = 0 elif addr[0] == 'v': magic_byte = 42 else: return False addr_valid = True try: bin_addr = bc.b58check_to_bin(addr) if bc.bin_to_b58check(bin_addr, magic_byte) != addr: addr_valid = False except: addr_valid = False return addr_valid
def _calculate_hash(self, filename, **kwargs): """ Calculates the hash of the file and the hash of the file + metadata (passed on the keywargs) """ # hash to address with open(filename, 'rb') as f: file_hash = hashlib.md5(f.read()).hexdigest() if kwargs: data = str([unicode(value) for value in kwargs.itervalues()] + [file_hash]) else: data = file_hash address_piece_with_metadata = unicode(pybitcointools.bin_to_b58check(pybitcointools.bin_hash160(data), magicbyte=self._magicbyte)) address_piece = unicode(pybitcointools.bin_to_b58check(pybitcointools.bin_hash160(file_hash), magicbyte=self._magicbyte)) return address_piece, address_piece_with_metadata
def hash_as_address(self): data = str([ unicode(self.title), unicode(self.artist_name), unicode(self.date_created), unicode(self.bitcoin_path), unicode(self.digital_work.hash), ]) address = unicode( pybitcointools.bin_to_b58check(pybitcointools.bin_hash160(data))) return address
def parse_pay2scripthash(script): # First 2 bytes is OP_HASH160 # Second 2 bytes is num bytes to be pushed to the stack # Then comes the pubkey pubkey = script[4:-2] # P2SH addresses use the version prefix 5, which results in # Base58Check-encoded addresses that start with a 3 address = bin_to_b58check(binascii.unhexlify(pubkey), constants.PAY_TO_SCRIPT_VERSION_PREFIX) return (pubkey, address)
def _calculate_hash(self, filename, **kwargs): """ Calculates the hash of the file and the hash of the file + metadata (passed on the keywargs) """ # hash to address with open(filename, 'rb') as f: file_hash = hashlib.md5(f.read()).hexdigest() if kwargs: data = str([unicode(value) for value in kwargs.itervalues()] + [file_hash]) else: data = file_hash address_piece_with_metadata = unicode( pybitcointools.bin_to_b58check(pybitcointools.bin_hash160(data), magicbyte=self._magicbyte)) address_piece = unicode( pybitcointools.bin_to_b58check( pybitcointools.bin_hash160(file_hash), magicbyte=self._magicbyte)) return address_piece, address_piece_with_metadata
def parse_pay2pubkey_hash(script): # Extract pubkey hash from script # First 2 bytes is OP_DUP, second 2 bytes is OP_HASH160 # Then there are 2 bytes representing how much data is pushed to the stack (not important just skip them) # That's why we start at 6, last four bytes are OP_EQUALVERIFY and OP_CHECKSIG so we end at -4 pubkey = script[6:-4] # Public Key Hash is equivalent to the GAME address, # it has been hashed twice but without the Base58Check encoding, # so we need only to encode it address = bin_to_b58check(binascii.unhexlify(pubkey), constants.PAY_TO_PUBKEY_VERSION_PREFIX) return (pubkey, address)
def _get_vout_address(self, vout_data): if vout_data["scriptPubKey"]["type"] == "multisig": address = pybitcointools.bin_to_b58check( pybitcointools.hash160(vout_data["scriptPubKey"]["hex"]), self.config.MULTISIG_VERSION) elif vout_data["scriptPubKey"][ "type"] == "witness_v0_scripthash" or vout_data[ "scriptPubKey"]["type"] == "witness_v0_keyhash": address = vout_data["scriptPubKey"]["hex"] else: if not vout_data["scriptPubKey"].has_key("addresses"): #ToDo: OP_ADD and other OP_CODE may add exectuing function return "" elif len(vout_data["scriptPubKey"]["addresses"]) > 1: logging.error("error data: ", vout_data) pass address = vout_data["scriptPubKey"]["addresses"][0] return address
def hash_as_address(self): """ @return btc_address -- address that has a hash of all the info about the piece. @notes Steps: 1. Hash the data using sha256. 2. The hash is hashed again using ripemd160*. 3. The version byte (0x00 for main net) is added to the beginning. 4. The checksum, which is the first 4 bytes of sha256(sha256(versioned_hash)))*, is added to the end. 5. Finally, everything is converted to base58. * Note that when hashing is applied on hashes, the actual hash is being hashed, and not the hexadecimal representation of the hash. -The steps are from https://www.btproof.com/technical.html -Below, the steps are implemented with the help of pybitcointools. -The hash is *not* unique per edition, so that we can have a single bitcoin transaction to register > 1 pieces, all with the same hash. """ assert 'placeholder' not in self.bitcoin_path data = str([ unicode(self.title), unicode(self.artist_name), unicode(self.date_created), unicode(self.num_editions), unicode(self.bitcoin_path), unicode(self.digital_work.hash), ]) address = unicode( pybitcointools.bin_to_b58check(pybitcointools.bin_hash160(data))) return address
def hash_as_address_no_metada(self): address = unicode( pybitcointools.bin_to_b58check( pybitcointools.bin_hash160(self.digital_work.hash))) return address
seed = mnemonic.mn_decode(passPhrase.split(" ")) rootPrivKey = pbt.electrum_privkey(seed, 0, 0) rootAddress = pbt.electrum_address(seed, 0, 0) mpk = pbt.electrum_mpk(seed) # output parts = [" ".join(words[:WORDS / 2]), " ".join(words[WORDS / 2:])] for i, p in enumerate(parts): part = " ".join(p) for c in range(2): print "\n" + "-" * DASHLEN + "\n" if title: print title print "Electrum 1.x seed part %d of %d" % (i + 1, PARTS), " copy", "AB"[c] if i == 0: print "rootAddress:", rootAddress #print "mpk:", mpk print p print "\n" + "-" * DASHLEN + "\n" if 1: print "\n\n" print "Press <enter> to show verification data. <ctrl-c> to exit." raw_input() print "Only for verification against address above (do not print / do destroy):" print "privKey:", pbt.bin_to_b58check(rootPrivKey.decode("hex"), 0x80) # WIF wallet import format print "electrum seed:", passPhrase
rootPrivKey = pbt.electrum_privkey(seed, 0, 0) rootAddress = pbt.electrum_address(seed, 0, 0) mpk = pbt.electrum_mpk(seed) # output parts = [" ".join(words[:WORDS / 2]), " ".join(words[WORDS / 2:])] for i, p in enumerate(parts): part = " ".join(p) for c in range(2): print "\n" + "-" * DASHLEN + "\n" if title: print title print "Electrum 1.x seed part %d of %d" % (i + 1, PARTS), " copy", "AB"[c] if i == 0: print "rootAddress:", rootAddress #print "mpk:", mpk print p print "\n" + "-" * DASHLEN + "\n" if 1: print "\n\n" print "Press <enter> to show verification data. <ctrl-c> to exit." raw_input() print "Only for verification against address above (do not print / do destroy):" print "privKey:", pbt.bin_to_b58check(rootPrivKey.decode("hex"), 0x80) # WIF wallet import format print "electrum seed:", passPhrase