def big_map_query(self, path): """ Construct a query for big_map_get request :param path: BigMap key, string, int, or hex-string (since Babylon you can have more than one BigMap at arbitrary position) :param storage: :return: dict """ key = basename(path) big_map_path = dirname(path) big_map_id = self.big_map_id(join('/', big_map_path)) if big_map_path else None key_prim, _, _ = self._locate_big_map(big_map_id) encoded_key = encode_literal(key, key_prim) if big_map_id: query = dict( big_map_id=big_map_id, script_expr=base58_encode(blake2b_32(encoded_key.encode()), b'expr') ) else: query = dict( key=encoded_key, type={'prim': key_prim} ) return query
def secret_key(self, passphrase=None, ed25519_seed=True): """ Creates base58 encoded private key representation :param passphrase: encryption phrase for the private key :param ed25519_seed: encode seed rather than full key for ed25519 curve (True by default) :return: the secret key associated with this key, if available """ if not self.secret_exponent: raise ValueError("Secret key not known.") if self.curve == b'ed' and ed25519_seed: key = pysodium.crypto_sign_sk_to_seed(self.secret_exponent) else: key = self.secret_exponent if passphrase: if not ed25519_seed: raise NotImplementedError salt = pysodium.randombytes(8) encryption_key = hashlib.pbkdf2_hmac( hash_name="sha512", password=scrub_input(passphrase), salt=salt, iterations=32768, dklen=32) encrypted_sk = pysodium.crypto_secretbox(msg=key, nonce=b'\000' * 24, k=encryption_key) key = salt + encrypted_sk # we have to combine salt and encrypted key in order to decrypt later prefix = self.curve + b'esk' else: prefix = self.curve + b'sk' return base58_encode(key, prefix).decode()
def hash(self): """ Calculate the Base58 encoded operation group hash. :return: str """ hash_digest = blake2b_32(self.binary_payload()).digest() return base58_encode(hash_digest, b'o').decode()
def sign(self, message, generic=False): """ Sign a raw sequence of bytes :param message: sequence of bytes, raw format or hexadecimal notation :param generic: do not specify elliptic curve if set to True :return: signature in base58 encoding """ message = scrub_input(message) if not self.is_secret: raise ValueError("Cannot sign without a secret key.") # Ed25519 if self.curve == b"ed": digest = pysodium.crypto_generichash(message) signature = pysodium.crypto_sign_detached(digest, self._secret_key) # Secp256k1 elif self.curve == b"sp": pk = secp256k1.PrivateKey(self._secret_key) signature = pk.ecdsa_serialize_compact( pk.ecdsa_sign(message, digest=blake2b_32)) # P256 elif self.curve == b"p2": r, s = sign(msg=message, d=bytes_to_int(self._secret_key), hashfunc=blake2b_32) signature = int_to_bytes(r) + int_to_bytes(s) else: assert False if generic: prefix = b'sig' else: prefix = self.curve + b'sig' return base58_encode(signature, prefix).decode()
def public_key_hash(self): """ Creates base58 encoded public key hash for this key. :return: the public key hash for this key """ pkh = blake2b(data=self.public_point, digest_size=20).digest() prefix = {b'ed': b'tz1', b'sp': b'tz2', b'p2': b'tz3'}[self.curve] return base58_encode(pkh, prefix).decode()
def run(self): """ Simulate operation without signature checks. :return: RPC response """ return self.shell.head.helpers.scripts.run_operation.post({ 'operation': { 'branch': self.branch, 'contents': self.contents, 'signature': base58_encode(b'0' * 64, b'sig').decode() }, 'chain_id': self.chain_id })
def get_key_hash(val_expr, type_expr, bin_path='') -> str: """ Get Big_map key hash from key and its type :param val_expr: key expression (Micheline expression) :param type_expr: type expression (can be key type or type of the whole storage) :param bin_path: binary path to the key (if storage type is passed to the prev argument) :returns: Base58 encoded key hash "expr..." """ for idx in bin_path: assert isinstance(type_expr, dict), f'type expression contains dict nodes only' type_expr = type_expr['args'][int(idx)] data = blake2b_32(pack(val_expr, type_expr)).digest() return base58_encode(data, b'expr').decode()
def decode_literal(node, prim): core_type, value = next(iter(node.items())) if prim in ['int', 'nat']: return int(value) if prim == 'timestamp': if core_type == 'int': return pendulum.from_timestamp(int(value)) else: return pendulum.parse(value) if prim == 'mutez': return Decimal(value) / 10**6 if prim == 'bool': return value == 'True' if prim == 'address' and core_type == 'bytes': prefix = { '0000': b'tz1', '0001': b'tz2', '0002': b'tz3' } # TODO: check it's ttr return base58_encode(bytes.fromhex(value[4:]), prefix[value[:4]]).decode() return value
def calculate_hash(self): hash_digest = blake2b_32(self.raw()).digest() return base58_encode(hash_digest, b'B').decode()
def test_b58_decode_encode(self, string, prefix): data = base58_decode(string) result = base58_encode(data, prefix) self.assertEqual(string, result)
def get_fresh_address(self): nonce = b'\x00' * 32 + self.index.to_bytes(4, 'big') nonce_hash = blake2b(data=nonce, digest_size=20).digest() res = base58_encode(nonce_hash, b'KT1').decode() self.index += 1 return res
def calculate_hash(self): hash_digest = blake2b_32(proto_to_bytes(self())).digest() return base58_encode(hash_digest, b'P').decode()
def public_key(self): """ Creates base58 encoded public key representation :return: the public key associated with the private key """ return base58_encode(self.public_point, self.curve + b'pk').decode()
def hash(self): hash_digest = blake2b_32(proto_to_bytes(self._proto)).digest() return base58_encode(hash_digest, b'P').decode()
def calculate_hash(self): hash_digest = blake2b_32(self.signed_bytes()).digest() return base58_encode(hash_digest, b'o').decode()
def get_key_hash(val_expr, type_expr): data = blake2b_32(pack(val_expr, type_expr)).digest() return base58_encode(data, b'expr').decode()