def decrypt_with_ont_id_in_gcm(nonce, mac_tag, encode_g_tilde, cipher_text, hdr: bytes, ont_id_acct: Account): if not isinstance(ont_id_acct, Account): return b'' private_key_bytes = ont_id_acct.serialize_private_key() plain_text = ECIES.decrypt_with_gcm_mode(nonce, mac_tag, cipher_text, private_key_bytes, hdr, encode_g_tilde) return plain_text
def decrypt_with_ont_id_in_cbc(aes_iv: bytes, encode_g_tilde: bytes, cipher_text: bytes, ont_id_acct: Account): if not isinstance(ont_id_acct, Account): return b'' private_key_bytes = ont_id_acct.serialize_private_key() plain_text = ECIES.decrypt_with_cbc_mode(cipher_text, private_key_bytes, aes_iv, encode_g_tilde) return plain_text
def encrypt_with_ont_id_in_cbc(plain_text: bytes, ont_id_acct: Account): if not isinstance(ont_id_acct, Account): return b'' private_key_bytes = ont_id_acct.serialize_private_key() public_key_bytes = ECDSA.ec_get_public_key_by_private_key( private_key_bytes) aes_iv, encode_g_tilde, cipher_text = ECIES.encrypt_with_cbc_mode( plain_text, public_key_bytes) return aes_iv, encode_g_tilde, cipher_text
def encrypt_with_ont_id_in_gcm(plain_text: bytes, hdr: bytes, ont_id_acct: Account): if not isinstance(ont_id_acct, Account): return b'' private_key_bytes = ont_id_acct.serialize_private_key() public_key_bytes = ECDSA.ec_get_public_key_by_private_key( private_key_bytes) nonce, mac_tag, encode_g_tilde, cipher_text = ECIES.encrypt_with_gcm_mode( plain_text, hdr, public_key_bytes) return nonce, mac_tag, encode_g_tilde, cipher_text
def test_serialize_private_key(self): hex_private_key = "523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f" hex_public_key = "03036c12be3726eb283d078dff481175e96224f0b0c632c7a37e10eb40fe6be889" base58_addr = "ANH5bHrrt111XwNEnuPZj6u95Dd6u7G4D6" wif = b'KyyZpJYXRfW8CXxH2B6FRq5AJsyTH7PjPACgBht4xRjstz4mxkeJ' account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) hex_serialize_private_key = account.serialize_private_key().hex() self.assertEqual(hex_private_key, hex_serialize_private_key) self.assertEqual(account.serialize_public_key().hex(), hex_public_key) self.assertEqual(account.export_wif(), wif) self.assertEqual(account.get_address_base58(), base58_addr)
def __create_account(self, label: str, pwd: str, salt: bytes, priv_key: bytes, account_flag: bool): account = Account(priv_key, self.scheme) # initialization if self.scheme == SignatureScheme.SHA256withECDSA: acct = AccountData() else: raise ValueError("scheme type is error") # set key if pwd != None: acct.key = account.export_gcm_encrypted_private_key( pwd, salt, Scrypt().get_n()) pwd = None else: acct.key = account.serialize_private_key().hex() acct.address = account.get_address_base58() # set label if label == None or label == "": label = str(uuid.uuid4())[0:8] if account_flag: for index in range(len(self.wallet_in_mem.accounts)): if acct.address == self.wallet_in_mem.accounts[index].address: raise ValueError("wallet account exists") if len(self.wallet_in_mem.accounts) == 0: acct.isDefault = True self.wallet_in_mem.defaultAccountAddress = acct.address acct.label = label acct.salt = base64.b64encode(salt).decode() acct.publicKey = account.serialize_public_key().hex() self.wallet_in_mem.accounts.append(acct) else: for index in range(len(self.wallet_in_mem.identities)): if self.wallet_in_mem.identities[ index].ontid == did_ont + acct.address: raise ValueError("wallet identity exists") idt = Identity() idt.ontid = did_ont + acct.address idt.label = label if len(self.wallet_in_mem.identities) == 0: idt.isDefault = True self.wallet_in_mem.defaultOntid = idt.ontid ctl = Control(id="keys-1", key=acct.key, salt=base64.b64encode(salt).decode(), address=acct.address, public_key=account.serialize_public_key().hex()) idt.controls.append(ctl) self.wallet_in_mem.identities.append(idt) return account
def test_serialize_private_key(self): hex_private_key = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) hex_serialize_private_key = account.serialize_private_key().hex() self.assertEqual(hex_private_key, hex_serialize_private_key)
import base64 import unittest from ontology.utils import util from ontology.account.account import Account from ontology.wallet.wallet import WalletData from ontology.wallet.account import AccountData from ontology.crypto.signature_scheme import SignatureScheme from ontology.common.address import Address private_key = "8b6bb2bebb27f3e2c24cc8a3febb413c1ef98bd2481e2292ada6d90f9a5f5ec9" if __name__ == '__main__': account = Account(private_key) publickey = account.serialize_public_key() wif = account.export_wif() privatekey = account.get_privatekey_from_wif(wif) print("public key is ", publickey, type(publickey)) print("private key is ", privatekey) print("...", account.serialize_private_key()) address = Address(publickey) addr = address.b58decode() print("address is ", addr)