Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions aptos_sdk/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,48 @@ def __eq__(self, other: object) -> bool:
self.account_address == other.account_address and self.private_key == other.private_key
)

@staticmethod
def generate() -> Account:
@classmethod
def generate(cls) -> Account:
"""Generate a new Ed25519 account with a random private key.

:returns: A new Account with a freshly generated Ed25519 key pair.
"""
private_key = ed25519.PrivateKey.random()
account_address = AccountAddress.from_key(private_key.public_key())
return Account(account_address, private_key)
return cls(account_address, private_key)

@staticmethod
def generate_secp256k1_ecdsa() -> Account:
@classmethod
def generate_secp256k1_ecdsa(cls) -> Account:
"""Generate a new Secp256k1 ECDSA account with a random private key.

:returns: A new Account with a freshly generated Secp256k1 key pair.
"""
private_key = secp256k1_ecdsa.PrivateKey.random()
public_key = asymmetric_crypto_wrapper.PublicKey(private_key.public_key())
account_address = AccountAddress.from_key(public_key)
return Account(account_address, private_key)
return cls(account_address, private_key)

@staticmethod
def load_key(key: str) -> Account:
@classmethod
def load_key(cls, key: str) -> Account:
"""Create an Account from an Ed25519 private key hex string.

:param key: Hex-encoded private key string.
:returns: An Account derived from the given private key.
"""
private_key = ed25519.PrivateKey.from_str(key)
account_address = AccountAddress.from_key(private_key.public_key())
return Account(account_address, private_key)
return cls(account_address, private_key)

@staticmethod
def load(path: str) -> Account:
@classmethod
def load(cls, path: str) -> Account:
"""Load an Account from a JSON file containing ``account_address`` and ``private_key``.

:param path: Path to the JSON file.
:returns: The deserialized Account.
"""
with open(path) as file:
data = json.load(file)
return Account(
return cls(
AccountAddress.from_str_relaxed(data["account_address"]),
ed25519.PrivateKey.from_str(data["private_key"]),
)
Expand Down
54 changes: 27 additions & 27 deletions aptos_sdk/account_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def is_special(self):
"""
return all(b == 0 for b in self.address[:-1]) and self.address[-1] < 0b10000

@staticmethod
def from_str(address: str) -> AccountAddress:
@classmethod
def from_str(cls, address: str) -> AccountAddress:
"""
NOTE: This function has strict parsing behavior. For relaxed behavior, please use
`from_string_relaxed` function.
Expand Down Expand Up @@ -117,7 +117,7 @@ def from_str(address: str) -> AccountAddress:
if not address.startswith("0x"):
raise RuntimeError("Hex string must start with a leading 0x.")

out = AccountAddress.from_str_relaxed(address)
out = cls.from_str_relaxed(address)

# Check if the address is in LONG form. If it is not, this is only allowed for
# special addresses, in which case we check it is in proper SHORT form.
Expand All @@ -144,8 +144,8 @@ def from_str(address: str) -> AccountAddress:

return out

@staticmethod
def from_str_relaxed(address: str) -> AccountAddress:
@classmethod
def from_str_relaxed(cls, address: str) -> AccountAddress:
"""
NOTE: This function has relaxed parsing behavior. For strict behavior, please use
the `from_string` function. Where possible, use `from_string` rather than this
Expand Down Expand Up @@ -194,10 +194,10 @@ def from_str_relaxed(address: str) -> AccountAddress:
pad = "0" * (AccountAddress.LENGTH * 2 - len(addr))
addr = pad + addr

return AccountAddress(bytes.fromhex(addr))
return cls(bytes.fromhex(addr))

@staticmethod
def from_key(key: asymmetric_crypto.PublicKey) -> AccountAddress:
@classmethod
def from_key(cls, key: asymmetric_crypto.PublicKey) -> AccountAddress:
hasher = hashlib.sha3_256()
hasher.update(key.to_crypto_bytes())

Expand All @@ -212,49 +212,49 @@ def from_key(key: asymmetric_crypto.PublicKey) -> AccountAddress:
else:
raise InvalidKeyError("Unsupported asymmetric_crypto.PublicKey key type.")

return AccountAddress(hasher.digest())
return cls(hasher.digest())

@staticmethod
def for_resource_account(creator: AccountAddress, seed: bytes) -> AccountAddress:
@classmethod
def for_resource_account(cls, creator: AccountAddress, seed: bytes) -> AccountAddress:
hasher = hashlib.sha3_256()
hasher.update(creator.address)
hasher.update(seed)
hasher.update(AuthKeyScheme.DeriveResourceAccountAddress)
return AccountAddress(hasher.digest())
return cls(hasher.digest())

@staticmethod
def for_guid_object(creator: AccountAddress, creation_num: int) -> AccountAddress:
@classmethod
def for_guid_object(cls, creator: AccountAddress, creation_num: int) -> AccountAddress:
hasher = hashlib.sha3_256()
serializer = Serializer()
serializer.u64(creation_num)
hasher.update(serializer.output())
hasher.update(creator.address)
hasher.update(AuthKeyScheme.DeriveObjectAddressFromGuid)
return AccountAddress(hasher.digest())
return cls(hasher.digest())

@staticmethod
def for_named_object(creator: AccountAddress, seed: bytes) -> AccountAddress:
@classmethod
def for_named_object(cls, creator: AccountAddress, seed: bytes) -> AccountAddress:
hasher = hashlib.sha3_256()
hasher.update(creator.address)
hasher.update(seed)
hasher.update(AuthKeyScheme.DeriveObjectAddressFromSeed)
return AccountAddress(hasher.digest())
return cls(hasher.digest())

@staticmethod
@classmethod
def for_named_token(
creator: AccountAddress, collection_name: str, token_name: str
cls, creator: AccountAddress, collection_name: str, token_name: str
) -> AccountAddress:
collection_bytes = collection_name.encode()
token_bytes = token_name.encode()
return AccountAddress.for_named_object(creator, collection_bytes + b"::" + token_bytes)
return cls.for_named_object(creator, collection_bytes + b"::" + token_bytes)

@staticmethod
def for_named_collection(creator: AccountAddress, collection_name: str) -> AccountAddress:
return AccountAddress.for_named_object(creator, collection_name.encode())
@classmethod
def for_named_collection(cls, creator: AccountAddress, collection_name: str) -> AccountAddress:
return cls.for_named_object(creator, collection_name.encode())

@staticmethod
def deserialize(deserializer: Deserializer) -> AccountAddress:
return AccountAddress(deserializer.fixed_bytes(AccountAddress.LENGTH))
@classmethod
def deserialize(cls, deserializer: Deserializer) -> AccountAddress:
return cls(deserializer.fixed_bytes(AccountAddress.LENGTH))

def serialize(self, serializer: Serializer):
serializer.fixed_bytes(self.address)
Expand Down
108 changes: 54 additions & 54 deletions aptos_sdk/aptos_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def __init__(self, allow_ungated_transfer, owner):
self.allow_ungated_transfer = allow_ungated_transfer
self.owner = owner

@staticmethod
def parse(resource: dict[str, Any]) -> Object:
return Object(
@classmethod
def parse(cls, resource: dict[str, Any]) -> Object:
return cls(
resource["allow_ungated_transfer"],
AccountAddress.from_str_relaxed(resource["owner"]),
)
Expand All @@ -51,9 +51,9 @@ def __init__(self, creator, description, name, uri):
def __str__(self) -> str:
return f"AccountAddress[creator: {self.creator}, description: {self.description}, name: {self.name}, ur: {self.uri}]"

@staticmethod
def parse(resource: dict[str, Any]) -> Collection:
return Collection(
@classmethod
def parse(cls, resource: dict[str, Any]) -> Collection:
return cls(
AccountAddress.from_str_relaxed(resource["creator"]),
resource["description"],
resource["name"],
Expand All @@ -76,9 +76,9 @@ def __init__(self, numerator, denominator, payee_address):
def __str__(self) -> str:
return f"Royalty[numerator: {self.numerator}, denominator: {self.denominator}, payee_address: {self.payee_address}]"

@staticmethod
def parse(resource: dict[str, Any]) -> Royalty:
return Royalty(
@classmethod
def parse(cls, resource: dict[str, Any]) -> Royalty:
return cls(
resource["numerator"],
resource["denominator"],
AccountAddress.from_str_relaxed(resource["payee_address"]),
Expand Down Expand Up @@ -111,9 +111,9 @@ def __init__(
def __str__(self) -> str:
return f"Token[collection: {self.collection}, index: {self.index}, description: {self.description}, name: {self.name}, uri: {self.uri}]"

@staticmethod
def parse(resource: dict[str, Any]):
return Token(
@classmethod
def parse(cls, resource: dict[str, Any]):
return cls(
AccountAddress.from_str_relaxed(resource["collection"]["inner"]),
int(resource["index"]),
resource["description"],
Expand Down Expand Up @@ -190,67 +190,67 @@ def to_transaction_arguments(self) -> List[TransactionArgument]:
TransactionArgument(self.serialize_value(), Serializer.to_bytes),
]

@staticmethod
def parse(name: str, property_type: int, value: bytes) -> Property:
@classmethod
def parse(cls, name: str, property_type: int, value: bytes) -> Property:
deserializer = Deserializer(value)

if property_type == Property.BOOL:
return Property(name, "bool", deserializer.bool())
return cls(name, "bool", deserializer.bool())
elif property_type == Property.U8:
return Property(name, "u8", deserializer.u8())
return cls(name, "u8", deserializer.u8())
elif property_type == Property.U16:
return Property(name, "u16", deserializer.u16())
return cls(name, "u16", deserializer.u16())
elif property_type == Property.U32:
return Property(name, "u32", deserializer.u32())
return cls(name, "u32", deserializer.u32())
elif property_type == Property.U64:
return Property(name, "u64", deserializer.u64())
return cls(name, "u64", deserializer.u64())
elif property_type == Property.U128:
return Property(name, "u128", deserializer.u128())
return cls(name, "u128", deserializer.u128())
elif property_type == Property.U256:
return Property(name, "u256", deserializer.u256())
return cls(name, "u256", deserializer.u256())
elif property_type == Property.ADDRESS:
return Property(name, "address", AccountAddress.deserialize(deserializer))
return cls(name, "address", AccountAddress.deserialize(deserializer))
elif property_type == Property.STRING:
return Property(name, "0x1::string::String", deserializer.str())
return cls(name, "0x1::string::String", deserializer.str())
elif property_type == Property.BYTE_VECTOR:
return Property(name, "vector<u8>", deserializer.to_bytes())
return cls(name, "vector<u8>", deserializer.to_bytes())
raise InvalidPropertyType(property_type)

@staticmethod
def bool(name: str, value: bool) -> Property:
return Property(name, "bool", value)
@classmethod
def bool(cls, name: str, value: bool) -> Property:
return cls(name, "bool", value)

@staticmethod
def u8(name: str, value: int) -> Property:
return Property(name, "u8", value)
@classmethod
def u8(cls, name: str, value: int) -> Property:
return cls(name, "u8", value)

@staticmethod
def u16(name: str, value: int) -> Property:
return Property(name, "u16", value)
@classmethod
def u16(cls, name: str, value: int) -> Property:
return cls(name, "u16", value)

@staticmethod
def u32(name: str, value: int) -> Property:
return Property(name, "u32", value)
@classmethod
def u32(cls, name: str, value: int) -> Property:
return cls(name, "u32", value)

@staticmethod
def u64(name: str, value: int) -> Property:
return Property(name, "u64", value)
@classmethod
def u64(cls, name: str, value: int) -> Property:
return cls(name, "u64", value)

@staticmethod
def u128(name: str, value: int) -> Property:
return Property(name, "u128", value)
@classmethod
def u128(cls, name: str, value: int) -> Property:
return cls(name, "u128", value)

@staticmethod
def u256(name: str, value: int) -> Property:
return Property(name, "u256", value)
@classmethod
def u256(cls, name: str, value: int) -> Property:
return cls(name, "u256", value)

@staticmethod
def string(name: str, value: str) -> Property:
return Property(name, "0x1::string::String", value)
@classmethod
def string(cls, name: str, value: str) -> Property:
return cls(name, "0x1::string::String", value)

@staticmethod
def bytes(name: str, value: bytes) -> Property:
return Property(name, "vector<u8>", value)
@classmethod
def bytes(cls, name: str, value: bytes) -> Property:
return cls(name, "vector<u8>", value)


class PropertyMap:
Expand Down Expand Up @@ -282,8 +282,8 @@ def to_tuple(self) -> Tuple[List[str], List[str], List[bytes]]:

return (names, types, values)

@staticmethod
def parse(resource: dict[str, Any]) -> PropertyMap:
@classmethod
def parse(cls, resource: dict[str, Any]) -> PropertyMap:
props = resource["inner"]["data"]
properties = []
for prop in props:
Expand All @@ -295,7 +295,7 @@ def parse(resource: dict[str, Any]) -> PropertyMap:
)
)

return PropertyMap(properties)
return cls(properties)


class ReadObject:
Expand Down
Loading
Loading