|
| 1 | +# Copyright © Aptos Foundation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +from . import asymmetric_crypto, asymmetric_crypto_wrapper, ed25519, secp256k1_ecdsa |
| 11 | +from .account_address import AccountAddress |
| 12 | +from .authenticator import AccountAuthenticator |
| 13 | +from .bcs import Serializer |
| 14 | +from .transactions import RawTransactionInternal |
| 15 | + |
| 16 | + |
| 17 | +class Account: |
| 18 | + """Represents an account as well as the private, public key-pair for the Aptos blockchain.""" |
| 19 | + |
| 20 | + account_address: AccountAddress |
| 21 | + private_key: asymmetric_crypto.PrivateKey |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, account_address: AccountAddress, private_key: asymmetric_crypto.PrivateKey |
| 25 | + ): |
| 26 | + self.account_address = account_address |
| 27 | + self.private_key = private_key |
| 28 | + |
| 29 | + def __eq__(self, other: object) -> bool: |
| 30 | + if not isinstance(other, Account): |
| 31 | + return NotImplemented |
| 32 | + return ( |
| 33 | + self.account_address == other.account_address |
| 34 | + and self.private_key == other.private_key |
| 35 | + ) |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def generate() -> Account: |
| 39 | + private_key = ed25519.PrivateKey.random() |
| 40 | + account_address = AccountAddress.from_key(private_key.public_key()) |
| 41 | + return Account(account_address, private_key) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def generate_secp256k1_ecdsa() -> Account: |
| 45 | + private_key = secp256k1_ecdsa.PrivateKey.random() |
| 46 | + public_key = asymmetric_crypto_wrapper.PublicKey(private_key.public_key()) |
| 47 | + account_address = AccountAddress.from_key(public_key) |
| 48 | + return Account(account_address, private_key) |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def load_key(key: str) -> Account: |
| 52 | + private_key = ed25519.PrivateKey.from_str(key) |
| 53 | + account_address = AccountAddress.from_key(private_key.public_key()) |
| 54 | + return Account(account_address, private_key) |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def load(path: str) -> Account: |
| 58 | + with open(path) as file: |
| 59 | + data = json.load(file) |
| 60 | + return Account( |
| 61 | + AccountAddress.from_str_relaxed(data["account_address"]), |
| 62 | + ed25519.PrivateKey.from_str(data["private_key"]), |
| 63 | + ) |
| 64 | + |
| 65 | + def store(self, path: str): |
| 66 | + data = { |
| 67 | + "account_address": str(self.account_address), |
| 68 | + "private_key": str(self.private_key), |
| 69 | + } |
| 70 | + with open(path, "w") as file: |
| 71 | + json.dump(data, file) |
| 72 | + |
| 73 | + def address(self) -> AccountAddress: |
| 74 | + """Returns the address associated with the given account""" |
| 75 | + |
| 76 | + return self.account_address |
| 77 | + |
| 78 | + def auth_key(self) -> str: |
| 79 | + """Returns the auth_key for the associated account""" |
| 80 | + return str(AccountAddress.from_key(self.private_key.public_key())) |
| 81 | + |
| 82 | + def sign(self, data: bytes) -> asymmetric_crypto.Signature: |
| 83 | + return self.private_key.sign(data) |
| 84 | + |
| 85 | + def sign_simulated_transaction( |
| 86 | + self, transaction: RawTransactionInternal |
| 87 | + ) -> AccountAuthenticator: |
| 88 | + return transaction.sign_simulated(self.private_key.public_key()) |
| 89 | + |
| 90 | + def sign_transaction( |
| 91 | + self, transaction: RawTransactionInternal |
| 92 | + ) -> AccountAuthenticator: |
| 93 | + return transaction.sign(self.private_key) |
| 94 | + |
| 95 | + def public_key(self) -> asymmetric_crypto.PublicKey: |
| 96 | + """Returns the public key for the associated account""" |
| 97 | + |
| 98 | + return self.private_key.public_key() |
| 99 | + |
| 100 | + |
| 101 | +class RotationProofChallenge: |
| 102 | + type_info_account_address: AccountAddress = AccountAddress.from_str("0x1") |
| 103 | + type_info_module_name: str = "account" |
| 104 | + type_info_struct_name: str = "RotationProofChallenge" |
| 105 | + sequence_number: int |
| 106 | + originator: AccountAddress |
| 107 | + current_auth_key: AccountAddress |
| 108 | + new_public_key: asymmetric_crypto.PublicKey |
| 109 | + |
| 110 | + def __init__( |
| 111 | + self, |
| 112 | + sequence_number: int, |
| 113 | + originator: AccountAddress, |
| 114 | + current_auth_key: AccountAddress, |
| 115 | + new_public_key: asymmetric_crypto.PublicKey, |
| 116 | + ): |
| 117 | + self.sequence_number = sequence_number |
| 118 | + self.originator = originator |
| 119 | + self.current_auth_key = current_auth_key |
| 120 | + self.new_public_key = new_public_key |
| 121 | + |
| 122 | + def serialize(self, serializer: Serializer): |
| 123 | + self.type_info_account_address.serialize(serializer) |
| 124 | + serializer.str(self.type_info_module_name) |
| 125 | + serializer.str(self.type_info_struct_name) |
| 126 | + serializer.u64(self.sequence_number) |
| 127 | + self.originator.serialize(serializer) |
| 128 | + self.current_auth_key.serialize(serializer) |
| 129 | + serializer.struct(self.new_public_key) |
| 130 | + |
| 131 | + |
| 132 | +class Test(unittest.TestCase): |
| 133 | + def test_load_and_store(self): |
| 134 | + (file, path) = tempfile.mkstemp() |
| 135 | + start = Account.generate() |
| 136 | + start.store(path) |
| 137 | + load = Account.load(path) |
| 138 | + |
| 139 | + self.assertEqual(start, load) |
| 140 | + # Auth key and Account address should be the same at start |
| 141 | + self.assertEqual(str(start.address()), start.auth_key()) |
| 142 | + |
| 143 | + def test_key(self): |
| 144 | + message = b"test message" |
| 145 | + account = Account.generate() |
| 146 | + signature = account.sign(message) |
| 147 | + self.assertTrue(account.public_key().verify(message, signature)) |
| 148 | + |
| 149 | + def test_rotation_proof_challenge(self): |
| 150 | + # Create originating account from private key. |
| 151 | + originating_account = Account.load_key( |
| 152 | + "005120c5882b0d492b3d2dc60a8a4510ec2051825413878453137305ba2d644b" |
| 153 | + ) |
| 154 | + # Create target account from private key. |
| 155 | + target_account = Account.load_key( |
| 156 | + "19d409c191b1787d5b832d780316b83f6ee219677fafbd4c0f69fee12fdcdcee" |
| 157 | + ) |
| 158 | + # Construct rotation proof challenge. |
| 159 | + rotation_proof_challenge = RotationProofChallenge( |
| 160 | + sequence_number=1234, |
| 161 | + originator=originating_account.address(), |
| 162 | + current_auth_key=originating_account.address(), |
| 163 | + new_public_key=target_account.public_key(), |
| 164 | + ) |
| 165 | + # Serialize transaction. |
| 166 | + serializer = Serializer() |
| 167 | + rotation_proof_challenge.serialize(serializer) |
| 168 | + rotation_proof_challenge_bcs = serializer.output().hex() |
| 169 | + # Compare against expected bytes. |
| 170 | + expected_bytes = ( |
| 171 | + "0000000000000000000000000000000000000000000000000000000000000001" |
| 172 | + "076163636f756e7416526f746174696f6e50726f6f664368616c6c656e6765d2" |
| 173 | + "0400000000000015b67a673979c7c5dfc8d9c9f94d02da35062a19dd9d218087" |
| 174 | + "bd9076589219c615b67a673979c7c5dfc8d9c9f94d02da35062a19dd9d218087" |
| 175 | + "bd9076589219c620a1f942a3c46e2a4cd9552c0f95d529f8e3b60bcd44408637" |
| 176 | + "9ace35e4458b9f22" |
| 177 | + ) |
| 178 | + self.assertEqual(rotation_proof_challenge_bcs, expected_bytes) |
0 commit comments