ain.utils package
Submodules
Module contents
- ain.utils.addHexPrefix(input: str) str
Adds the hex prefix(‘0x’) to the given string if it does not hex prefixed.
- Parameters:
input (str) – The string that you want to add prefix.
- Returns:
The hex prefixed string.
- Return type:
str
- ain.utils.areSameAddresses(address1: str, address2: str) bool
Checks if the two addresses are the same.
- Parameters:
address1 (str) – The first address.
address2 (str) – The second address.
- Returns:
- True, if the two addresses are the same.
False, if not.
- Return type:
bool
- ain.utils.bytesToHex(input: bytes) str
Converts the given bytes into the hex prefixed hex string.
- Parameters:
input (bytes) – The bytes that you want to convert into hex.
- Returns:
The hex converted bytes.
- Return type:
str
- ain.utils.countDecimals(value: float) int
Counts the given number’s decimals.
- Parameters:
value (float) – The number.
- Returns:
The decimal count.
- Return type:
int
- ain.utils.decryptWithPrivateKey(privateKey: bytes | str, encrypted: ECIESEncrypted) str
Decrypts encrypted data with privateKey.
- Parameters:
privateKey (Union[bytes, str]) – The private key.
encrypted (ECIESEncrypted) – The ECIES encrypted object.
- Returns:
The decrypted message.
- Return type:
str
- ain.utils.ecRecoverPub(msgHash: bytes, signature: ECDSASignature, chainId: int = 0) bytes
Recovers the public key from ECDSA signature.
- Parameters:
msgHash (bytes) – The hash of the message.
signature (ECDSASignature) – The object of the ECDSA signature.
chainId (int) – The chain ID of the provider. Defaults to 0.
- Returns:
The recovered public key.
- Return type:
str
- ain.utils.ecSignHash(msgHash: bytes, privateKey: bytes, chainId: int = 0) ECDSASignature
Returns the ECDSA signature of a message hash.
- Parameters:
msgHash (bytes) – The message hash.
privateKey (bytes) – The private key for the signature.
chainId (int) – The chain ID of the provider. Defaults to 0.
- Returns:
The object of the ECDSA signature.
- Return type:
- ain.utils.ecSignMessage(message: Any, privateKey: bytes, chainId: int = 0) str
Signs a message with a private key and returns a string signature.
- Parameters:
message – The message that you want to sign. Note that message should be convertible with
toBytes()
. If message is unsupported type, raises TypeError.privateKey (bytes) – The private key for the signature.
chainId (int) – The chain ID of the provider. Defaults to 0.
- Returns:
The hex prefixed string signature.
- Return type:
str
- ain.utils.ecSignTransaction(txData: TransactionBody, privateKey: bytes, chainId: int = 0) str
Signs a transaction body with a private key and returns a string signature.
- Parameters:
txData (TransactionBody) – The transaction that you want to sign.
privateKey (bytes) – The private key for the signature.
chainId (int) – The chain ID of the provider. Defaults to 0.
- Returns:
The hex prefixed string signature.
- Return type:
str
- ain.utils.ecVerifySig(data: Any, signature: str, address: str, chainId: int = 0) bool
Verifies if the signature is valid.
- Parameters:
data – The message or transaction that you want to verify.
signature (str) – The hex prefixed string signature.
address (str) – The address of the signature.
chainId (int) – The chain ID of the provider. Defaults to 0.
- Returns:
- True, if the given signature is a valid signature.
False, if not.
- Return type:
bool
- ain.utils.encodeVarInt(number: int) bytes
Encodes the number as bitcoin variable length integer
- Parameters:
number (int) – A number that you want to encode.
- Returns:
The encoded number as bitcoin variable length integer.
- Return type:
bytes
- ain.utils.encryptWithPublicKey(publicKey: bytes | str, message: str) ECIESEncrypted
Encrypts message with publicKey.
- Parameters:
publicKey (Union[bytes, str]) – The public key. When the type is str, it must be hex prefixed.
message (str) – The message.
- Returns:
The object of the encrypted message.
- Return type:
- ain.utils.generateMnemonic() str
Generates the random english mnemonic.
- Returns:
The randomly generated english mnemonic.
- Return type:
str
- ain.utils.getTimestamp() int
Gets the current unix timestamp.
- Returns:
The current unix timestamp.
- Return type:
int
- ain.utils.hashMessage(message: Any) bytes
Creates the bitcoin’s varint encoding of Keccak-256 hash of the message, prefixed with the header AINetwork Signed Message:.
- Parameters:
message – The message that you want to create hash. Note that message should be convertible with
toBytes()
. If message is unsupported type, raises TypeError.- Returns:
The bitcoin’s varint encoding of Keccak-256 hash of the message.
- Return type:
bytes
- ain.utils.hashTransaction(transaction: TransactionBody | str) bytes
Creates the Keccak-256 hash of the transaction body.
- Parameters:
transaction (Union[TransactionBody, str]) – The transaction that you want to create hash.
- Returns:
The Keccak hash of the transaction.
- Return type:
bytes
- ain.utils.isHexPrefixed(input: str) bool
Checks if the given string is prefixed with 0x.
- Parameters:
input (str) – The string that you want to check.
- Returns:
- True, if the input is hex prefixed.
False, if not.
- Return type:
bool
- ain.utils.isHexString(input: str) bool
Checks if the given string is a hex string.
- Parameters:
input (str) – The string that you want to check.
- Returns:
- True, if the input is hex string.
False, if not.
- Return type:
bool
- ain.utils.isValidAddress(address: str) bool
Checks if the given string is a valid address.
- Parameters:
address (str) – The address that you want to check.
- Returns:
- True, if the address is valid address.
False, if not.
- Return type:
bool
- ain.utils.isValidPrivate(privateKey: bytes) bool
Checks whether the privateKey is a valid private key (follows the rules of the curve secp256k1).
- Parameters:
privateKey (bytes) – The private key of the AIN blockchain account that you want to check.
- Returns:
- True, if the given key is a valid private key.
False, if not.
- Return type:
bool
- ain.utils.isValidPublic(publicKey: bytes, isSEC1: bool = False) bool
Checks whether the publicKey is a valid public key (follows the rules of the curve secp256k1 and meets the AIN requirements).
- Parameters:
publicKey (bytes) – The public key of the AIN blockchain account that you want to check.
isSEC1 (bool) – If True, public key should be SEC1 encoded one. Defaults to False.
- Returns:
- True, if the given key is a valid public key.
False, if not.
- Return type:
bool
- ain.utils.keccak(input: Any, bits: int = 256) bytes
Creates the Keccak hash of the input.
- Parameters:
input – The input that you want to create hash. Note that input will be converted with toBytes method. If input is unsupported type, raises TypeError.
bits (int) – The size of the hash, in (224, 456, 384, 512). Defaults to 256.
- Returns:
The Keccak hash of the input.
- Return type:
bytes
- ain.utils.mnemonicToPrivatekey(mnemonic: str, index: int = 0, chain: str = 'AIN') bytes
Returns an private key with the given mnemonic.
- Parameters:
mnemonic (str) – The mnemonic of account.
index (int) – The index of account. Defaults to 0.
chain (str) – The chain to use the derivation path of. Defaults to “AIN”.
- Returns:
The private key with the given mnemonic.
- Return type:
bytes
- ain.utils.padToEven(input: str) str
Pads the leading zero into the given string to have an even length.
- Parameters:
input (str) – The string that you want to pad.
- Returns:
- The leading zero padded string, if input is odd lengthed.
input itself, if it is even lengthed.
- Return type:
str
- ain.utils.privateToAddress(privateKey: bytes) str
Returns the checksummed AIN blockchain address of the given private key.
- Parameters:
privateKey (bytes) – The private key of an AIN blockchain account.
- Returns:
The checksummed AIN blockchain address of the given private key.
- Return type:
str
- ain.utils.privateToPublic(privateKey: bytes) bytes
Returns the public key of a given private key.
- Parameters:
privateKey (bytes) – The private key of the AIN blockchain account.
- Returns:
The public key of a given private key.
- Return type:
bytes
- ain.utils.pubToAddress(publicKey: bytes | str, isSEC1: bool = False) bytes
Returns the AIN blockchain address of the given public key, which is the lower 160 bits of the Keccak-256 hash of the public key.
- Parameters:
publicKey (Union[bytes, str]) – The public key of the AIN blockchain account or SEC1 encoded public key. When the type is str, it must be hex prefixed.
isSEC1 (bool) – If True, public key should be SEC1 encoded one. Defaults to False.
- Returns:
The AIN blockchain address of the given public key.
- Return type:
bytes
- ain.utils.replaceFloat(matchObj)
Generates a replacement string for the given Match object.
- Parameters:
matchObj (Any) – The Match object.
- Returns:
The replacement string.
- Return type:
str
- ain.utils.stripHexPrefix(input: str) str
Strips 0x from the given string, if that is hex prefixed.
- Parameters:
input (str) – The string that you want to strip.
- Returns:
- The hex prefix stripped string, if input is hex prefixed.
input itself, if it not hex prefixed.
- Return type:
str
- ain.utils.toBytes(input: Any) bytes
Attempts to convert a input into a bytes, This function is equivalent to toBuffer from @ainblockchain/ain-util.
- Parameters:
input – The input that you want to convert. It supports bytes, list(of ints), string, int, and None. If input is unsupported type, raises TypeError.
- Returns:
The converted bytes.
- Return type:
bytes
- ain.utils.toChecksumAddress(address: str) str
Returns a checksummed address.
- Parameters:
address (str) – The address that you want to convert. If address isn’t valid, raises ValueError.
- Returns:
The checksummed address.
- Return type:
str
- ain.utils.toJsLikeFloats(serialized: str) str
Reformats float numbers to JavaScript-like formats.
- Parameters:
serialized (str) – The string to be reformatted.
- Returns:
The reformatted string.
- Return type:
str
- ain.utils.toJsonString(obj: Any) str
Serializes the given object to a JSON string.
- Parameters:
obj (Any) – The given object to serialize.
- Returns:
The result of the serialization.
- Return type:
str