def grs_addr_base58_to_pubkeyhash(address, as_hex=False): """ Convert Base58 encoded address to public key hash (Groestlcoin) >>> addr_base58_to_pubkeyhash('142Zp9WZn9Fh4MV8F3H5Dv4Rbg7Ja1sPWZ', as_hex=True) '21342f229392d7c9ed82c932916cee6517fbc9a2' :param address: Crypto currency address in base-58 format :type address: str, bytes :param as_hex: Output as hexstring :type as_hex: bool :return bytes, str: Public Key Hash """ try: address = change_base(address, 58, 256, 25) except EncodingError as err: raise EncodingError("Invalid address %s: %s" % (address, err)) check = address[-4:] pkh = address[:-4] #x = to_bytes(pkh, 'utf8') checksum = groestlcoin_hash.getHash(pkh, len(pkh))[0:4] assert (check == checksum), "Invalid GRS address, checksum incorrect" if as_hex: return change_base(pkh, 256, 16)[2:] else: return pkh[1:]
def to_string(self) -> str: """ Serialize the ExtendedKey as a Base58 check encoded xpub string :return: Base58 check encoded xpub """ data = self.serialize() checksum = groestlcoin_hash.getHash(data, len(data))[0:4] return base58.encode(data + checksum)
def groestlHash(data): """Groestl-512 compound hash.""" try: import groestlcoin_hash except ImportError: t = 'Groestlcoin requires the groestlcoin_hash package ("pip install groestlcoin_hash").' print(t) raise ImportError(t) return bytes_as_revhex(groestlcoin_hash.getHash(data, len(data)))
def b58grsdecode_check(v): '''Decode and verify the checksum of a Base58 GRS encoded string''' result = b58decode(v) result, check = result[:-4], result[-4:] digest = groestlcoin_hash.getHash(result, len(result)) if check != digest[:4]: raise ValueError("Invalid checksum") return result
def is_valid_wif(wif: str, chain: str) -> bool: """ Check if wif is valid""" try: if chain == "grs" or chain == "grs_testnet": result = base58grs.b58decode(wif) #print(result) result, check = result[:-4], result[-4:] # DOUBLE GROESTL-512 hash digest = groestlcoin_hash.getHash(result, len(result)) else: result = base58.b58decode(wif) #print(result) result, check = result[:-4], result[-4:] # DOUBLE sha256 hash digest = hashlib.sha256(hashlib.sha256(result).digest()).digest() return check == digest[:4] except ValueError: return False
def grs_encode_wif(private_key: Union[str, bytes], version: bytes, compressed_wif: bool = False) -> bytes: """ Encode wif format private key """ private_key = scrub_input(private_key) # prepended version byte to private key private_key_with_version = version + private_key if compressed_wif: private_key_with_version = version + private_key + b'\x01' # perform DOUBLE GROESTL-512 hash on the mainnet_private_key hash_bytes = groestlcoin_hash.getHash(private_key_with_version, len(private_key_with_version)) # create a checksum using the first 4 bytes of the GROESTL-512 hash # append the 4 checksum bytes to the mainnet_private_key checksum = hash_bytes[:4] # print('checksum', checksum.hex()) hash_bytes = private_key_with_version + checksum # print('hash', hash_bytes.hex()) # convert private_key_with_version + checksum into base58 encoded string return base58grs.b58encode(hash_bytes)
def groestlHash(x: bytes) -> bytes: x = to_bytes(x, 'utf8') return groestlcoin_hash.getHash(x, len(x))
def groestl(s: bytes) -> bytes: return groestlcoin_hash.getHash(s, len(s))
def parse_multisig( script: bytes, tx_xpubs: Dict[bytes, KeyOriginInfo], psbt_scope: Union[PartiallySignedInput, PartiallySignedOutput] ) -> Tuple[bool, Optional[messages.MultisigRedeemScriptType]]: # at least OP_M pub OP_N OP_CHECKMULTISIG if len(script) < 37: return (False, None) # Get m m = script[0] - 80 if m < 1 or m > 15: return (False, None) # Get pubkeys and build HDNodePathType pubkeys = [] offset = 1 while True: pubkey_len = script[offset] if pubkey_len != 33: break offset += 1 key = script[offset:offset + 33] offset += 33 hd_node = messages.HDNodeType( depth=0, fingerprint=0, child_num=0, chain_code= b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', public_key=key) pubkeys.append(messages.HDNodePathType(node=hd_node, address_n=[])) # Check things at the end n = script[offset] - 80 if n != len(pubkeys): return (False, None) offset += 1 op_cms = script[offset] if op_cms != 174: return (False, None) # check if we know corresponding xpubs from global scope for pub in pubkeys: if pub.node.public_key in psbt_scope.hd_keypaths: derivation = psbt_scope.hd_keypaths[pub.node.public_key] for xpub in tx_xpubs: hd = ExtendedKey.deserialize( base58.encode( xpub + groestlcoin_hash.getHash(xpub, len(xpub))[:4])) origin = tx_xpubs[xpub] # check fingerprint and derivation if (origin.fingerprint == derivation.fingerprint) and ( origin.path == derivation.path[:len(origin.path)]): # all good - populate node and break pub.address_n = list(derivation.path[len(origin.path):]) pub.node = messages.HDNodeType(depth=hd.depth, fingerprint=int.from_bytes( hd.parent_fingerprint, 'big'), child_num=hd.child_num, chain_code=hd.chaincode, public_key=hd.pubkey) break # Build MultisigRedeemScriptType and return it multisig = messages.MultisigRedeemScriptType(m=m, signatures=[b''] * n, pubkeys=pubkeys) return (True, multisig)
def calc_hdr_hash(blk_hdr): return groestlcoin_hash.getHash(blk_hdr, len(blk_hdr))
def groestl(secret): return groestlcoin_hash.getHash(secret, len(secret))
def groestlHash(x): if type(x) is unicode: x=x.encode('utf-8') return groestlcoin_hash.getHash(x, len(x))
def grs_base58_cksum(inputs: bytes) -> bytes: """ Computes base 58 four bytes check sum for grs """ return groestlcoin_hash.getHash(inputs, len(inputs))[0:4]
def groestlHash(data): return groestlcoin_hash.getHash(data, len(data))
def generate_hashes_from_block(data_block, net): sha256_hash = hashlib.sha256(hashlib.sha256(data_block).digest()).digest()[::-1] header_hash = groestlcoin_hash.getHash(data_block, len(data_block))[::-1] return sha256_hash, header_hash
def groestlHash(data): """Groestl hash returning bytes.""" return groestlcoin_hash.getHash(data, len(data))
def GroestlHash(x): return groestlcoin_hash.getHash(x, len(x))
import groestlcoin_hash data = b'\x00' digest = groestlcoin_hash.getHash(data, len(data)) assert digest == b'\xe8r\x10\xb1\xe1\xd6t\xe6%?\x85\x82\x05\x9a;\xeaQa\xce\xf1\xe5\xb3-]\x06\xc5\xc3\x15P\xf9\xef*' print("OK")
def groestl_hash(x): '''Simple wrapper of groestl hash.''' return groestlcoin_hash.getHash(x, len(x))
def bin_groestl(s): bytes_to_hash = from_string_to_bytes(s) return groestlcoin_hash.getHash(bytes_to_hash, len(bytes_to_hash))
def grshash(data): import groestlcoin_hash return groestlcoin_hash.getHash(data, len(data))
PUBKEY_ADDRESS = 0 SCRIPT_ADDRESS = 5 def rev_hex(s): return s.decode('hex')[::-1].encode('hex') def int_to_hex(i, length=1): s = hex(i)[2:].rstrip('L') s = "0"*(2*length - len(s)) + s return rev_hex(s) singleSha256 = lambda x: hashlib.sha256(x).digest() Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest() groestlHash = lambda x: groestlcoin_hash.getHash(x, len(x)) hash_encode = lambda x: x[::-1].encode('hex') hash_decode = lambda x: x.decode('hex')[::-1] def header_to_string(res): pbh = res.get('prev_block_hash') if pbh is None: pbh = '0'*64 return int_to_hex(res.get('version'), 4) \ + rev_hex(pbh) \
def groestlhash(data): return getHash(data, len(data))
SCRIPT_ADDRESS = 5 def rev_hex(s): return s.decode('hex')[::-1].encode('hex') def int_to_hex(i, length=1): s = hex(i)[2:].rstrip('L') s = "0" * (2 * length - len(s)) + s return rev_hex(s) singleSha256 = lambda x: hashlib.sha256(x).digest() Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest() groestlHash = lambda x: groestlcoin_hash.getHash(x, len(x)) hash_encode = lambda x: x[::-1].encode('hex') hash_decode = lambda x: x.decode('hex')[::-1] def header_to_string(res): pbh = res.get('prev_block_hash') if pbh is None: pbh = '0' * 64 return int_to_hex(res.get('version'), 4) \ + rev_hex(pbh) \ + rev_hex(res.get('merkle_root')) \ + int_to_hex(int(res.get('timestamp')), 4) \
def b58grsencode_check(v): '''Encode a string using Base58 GRS with a 4 character checksum''' digest = groestlcoin_hash.getHash(v, len(v)) return b58encode(v + digest[:4])
def generate_hashes_from_block(data_block, ): return groestlcoin_hash.getHash(data_block, len(data_block))[::-1]
def new(self, data): self.data = data if isinstance(data, bytes) else data.encode("utf-8") self.digest_bits = len(data) self.hash = groestlcoin_hash.getHash(self.data, self.digest_bits) return self