def __init__(self, arg, password="******"): self._password = password self._valid = False self._password_double_hash = keytool.sha256_hash( str.encode(self._password), 2) self._password_thrice_hash = keytool.sha256_hash( str.encode(self._password), 3) self._accounts_data_list = list() self._sub_accounts_list = list() if isinstance(arg, Account): self._main_account = arg self._iv = keytool.create_urandom(16) self._master_key = keytool.create_urandom(32) self._master_key_encrypted = keytool.aes_encrypt( self._master_key, self._password_double_hash, self._iv) self._accounts_data_list.append( self._create_account_data(self._main_account, self.MAIN_ACCOUNT)) elif isinstance(arg, str): self._store_dict = util.read_config_file(arg) if not self._check_password(): return if "IV" not in self._store_dict.keys(): return self._iv = bytes.fromhex(self._get_iv()) self._master_key_encrypted = bytes.fromhex( self._get_master_key_encrypted()) self._master_key = self._get_master_key() self._accounts_data_list = self._get_accounts_data() self._main_account = self._get_account(0) self._sub_accounts_list = self._get_sub_accounts() self._valid = True
def gen_keystore_dat(self, password: str): iv_bytes = keytool.create_urandom(16) master_key_bytes = keytool.create_urandom(32) password_double_hash = keytool.sha256_hash(str.encode(password), 2) password_thrice_hash = keytool.sha256_hash(str.encode(password), 3) master_key_encrypted_bytes = keytool.aes_encrypt(master_key_bytes, password_double_hash, iv_bytes) private_key_encrypted_bytes = keytool.encrypt_private_key( master_key_bytes, bytearray(self.private_key), self.ecc_public_key, iv_bytes ) store = { "Version": "1.0.0", "PasswordHash": password_thrice_hash.hex(), "IV": iv_bytes.hex(), "MasterKey": master_key_encrypted_bytes.hex(), "Account": [ { "Address": self.address, "ProgramHash": self.program_hash.hex(), "RedeemScript": self.sign_script.hex(), "PrivateKeyEncrypted": private_key_encrypted_bytes.hex(), "Type": "main-account" } ] } return store
def hash(self): if self.tx_hash is not "": return self.tx_hash r = self.serialize_unsigned() tx_hash_str = keytool.sha256_hash(r, 2).hex() self.tx_hash = tx_hash_str return tx_hash_str
def _check_password(self): ret = keytool.sha256_hash(str.encode(self._password), 3).hex() == self._get_password_hash() if not ret: Logger.error("Invalid password!") return ret
def get_master_key(self): password_twice_hash = keytool.sha256_hash(str.encode(self.password), 2) encrypt_master_key = self.get_encrypt_master_key() if encrypt_master_key is None: Logger.error("encrypt master key is None") return None master_key = keytool.aes_decrypt(bytes.fromhex(encrypt_master_key), password_twice_hash, self.iv) return master_key
def _script_to_program_hash(self): # temp = SHA256.new(self.sign_script) # md = RIPEMD160.new(data=temp.digest()) # data = md.digest() temp = keytool.sha256_hash(self.sign_script, 1) data = keytool.ripemd160_hash(temp, 1) sign_type = self.sign_script[len(self.sign_script) - 1] program_hash = None if sign_type == 0xac: program_hash = bytes([33]) + data if sign_type == 0xae: program_hash = bytes([18]) + data return program_hash
#!/usr/bin/env python # -*- coding:utf-8 -*- # date: 2019/6/28 4:10 PM # author: liteng from src.core.wallet import keytool if __name__ == '__main__': with open("./datas/code.avm", "rb") as f: content = f.read() print(content) print(content.hex()) con_hash = keytool.sha256_hash(content, 1) con_160 = keytool.ripemd160_hash(con_hash, 1) print("before: ", con_160.hex()) con_160 = con_160[::-1] print(con_160.hex())
def gen_hash(self): r = b"" r = self.serialize(r, 0) return keytool.sha256_hash(r, 2)