def validate(self): if len(self.request.address) == 111: return self.validate_extended(checksum_algo='blake256') try: decoded_address = base58check.b58decode(self.request.address) except ValueError: return False # decoded address has to be 26 bytes long if len(decoded_address) != 26: return False # original one has to start with D,T,S or R if not self.request.address.startswith((b'D', b'T', b'S', b'R')): return False expected_checksum = decoded_address[-4:] version_bytes = int.from_bytes(decoded_address[:2], byteorder='big') if self.network == '': return False checksum = blake256.blake_hash( blake256.blake_hash(decoded_address[:-4]))[:4] # double blake256 checksum needs to be equal with the expected checksum if checksum != expected_checksum: return False return True
def checksum(input): """ A checksum Returns: bytes: A 4-byte checksum. """ return blake_hash(blake_hash(input))[:4]
def checksum(b): """ A checksum. Args: b (byte-like): Bytes to obtain a checksum for. Returns: bytes: A 4-byte checksum. """ return blake_hash(blake_hash(b))[:4]
def validate_extended(self, checksum_algo='sha256'): if len(self.request.address) != 111: return False if self.network == '': return False # strip leading "zeros" (the "1" digit with base58) base58_stripped = self.request.address.decode('utf-8').lstrip("1") # convert base58 to decimal int_rep = 0 for base58_digit in base58_stripped: int_rep *= 58 try: int_rep += self.base58_digit_to_dec[base58_digit] except KeyError: # not a valid base58 digit -> invalid address return False # encode it to base64 hex_rep = "{:X}".format(int_rep) # if the length is odd, add leading zero (needed for b16decode) if len(hex_rep) % 2 == 1: hex_rep = "0" + hex_rep # decode it into a binary string, padded with zeros # 72 bytes (extended key size) + 4 bytes (prefix version bytes) all_bytes = base64.b16decode(hex_rep).rjust(82, b"\0") # count leading zeros zero_count = next(zeros for zeros, byte in enumerate(all_bytes) if byte != 0) # compare it with the number of leading zeros lstripped at the beginning if len(self.request.address.decode('utf-8')) - len( base58_stripped) != zero_count: return False if checksum_algo == 'blake256': checksum = blake256.blake_hash(blake256.blake_hash( all_bytes[:-4]))[:4] elif checksum_algo == 'sha256': checksum = sha256(sha256(all_bytes[:-4]).digest()).digest()[:4] else: return False # checking if the checksum is valid if checksum != all_bytes[-4:]: return False return True
def main(): h = ByteArray( "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320" ) print(h) print(blake_hash(h.bytes()).hex()) print(crypto.hash160(h.bytes()).hex())
def hashH(b): """ The BLAKE256 hash as a ByteArray. Returns: ByteArray: The hash. """ return ByteArray(blake_hash(b), length=BLAKE256_SIZE)
def hash160(b): """ A RIPEMD160 hash of the blake256 hash of the input. Returns: ByteArray: A 20-byte hash. """ h = hashlib.new("ripemd160") h.update(blake_hash(b)) return ByteArray(h.digest())
def hashH(b): """ The BLAKE256 hash as a ByteArray. Args: b (byte-like): The thing to hash. Returns: ByteArray: The hash. """ return ByteArray(blake_hash(b), length=BLAKE256_SIZE)
def hash(self): """ Hash returns the BLAKE256 hash of the filter. Returns: ByteArray: The hash. """ # Empty filters have a hash of all zeroes. nData = self.filterNData() if len(nData) == 0: return ByteArray(length=32) return ByteArray(blake_hash(nData.bytes()))
def blake256d(data): from blake256.blake256 import blake_hash return blake_hash(blake_hash(data))
def H(m): return blake_hash(m)
def create_commitment(pub): # Commitment is defined as the first 16 bytes of the hash of COMMIT + pubkey return blake_hash(b"COMMIT" + pub)[0:16]