Web3.py
Last updated
Was this helpful?
Last updated
Was this helpful?
In order to provide easy access to all the features of COTI, the Python SDK was created, which is made in a way that has an interface very similar to that of . In fact, web3 is a peer dependency of our library and all of the objects exported by coti-web3 (e.g. LocalAccount, Web3, etc.) inherit from the corresponding web3 objects and extend their functionality where needed.
While most of the existing SDKs should work out of the box, using unique COTI features like encrypting transaction inputs, requires executing the onboarding procedure and encrypting using the defined protocol.
The library is made in such a way that after replacing web3 with coti-web3
most client apps will work out of box.
Full instructions for building and testing the package locally are available in the
See the repository for code examples.
Description: Loads an account from a raw private key.
Parameters:
private_key
: The private key as a hex string, bytes
, int
, or PrivateKey
object.
user_onboard_info
(optional): An dictionary containing the information from the user's onboarding procedure
Returns: A LocalAccount
instance.
Description: Derives an account from a BIP39 mnemonic phrase.
Parameters:
mnemonic
: The mnemonic phrase.
passphrase
(optional): A passphrase for added security.
account_path
(optional): Custom HD path.
user_onboard_info
(optional): A dictionary containing the information from the user's onboarding procedure
Returns: A LocalAccount
instance.
Description: Encrypts a plaintext value for secure contract inputs.
Parameters:
plaintext_value
: A boolean, unsigned integer or string to be encrypted
contract_address
: The address of the contract that the user is interacting with
function_selector
: The four-byte function selector
private_key
: The private key used to sign the transaction and encrypted value
aes_key
: The AES key used to encrypt the value
Returns: Encrypted data object (ItBool
, ItUint
, or ItString
)
Description: Decrypts a value encrypted with encrypt_value
.
Parameters:
ciphertext
: The encrypted ciphertext to decrypt
aes_key
: The AES key used to decrypt the value
Returns: The original value (bool
, int
, or str
).
user_onboard_info
: A dictionary containing the information from the user's onboarding procedure
aes_key
: The AES key used for encryption and decryption
rsa_key_pair
: A tuple containing the RSA public and private keys
onboard_tx_hash
: The transaction hash associated with the onboarding procedure
Description: Initializes a new LocalAccount
instance with a private key and an associated AccountLocalActions
instance.
Parameters:
key
(PrivateKey): The private key for the account.
account
(AccountLocalActions): Key management and signing API.
user_onboard_info
(optional): Metadata associated with the account.
Description: Updates the onboarding metadata.
Parameters:
user_onboard_info
: Metadata dictionary containing user-specific information.
Description: Sets the AES key in the onboarding metadata.
Parameters:
aes_key
: The AES key.
Description: Sets the RSA key pair in the onboarding metadata.
Parameters:
rsa_key_pair
: A tuple of RSA public and private keys.
Description: Sets the onboarding transaction hash in the metadata.
Parameters:
tx_hash
: The transaction hash.
Description: Encrypts a plaintext value for secure smart contract interactions.
Parameters:
plaintext_value
: The value to encrypt (bool
, int
, or str
).
contract_address
: The contract's Ethereum address.
function_selector
: The function selector for the contract call.
Returns: An encrypted value (ItBool
, ItUint
, or ItString
).
Description: Decrypts an encrypted value.
Parameters:
ciphertext
: The encrypted value (CtBool
, CtUint
, or CtString
).
Returns: The decrypted value (bool
, int
, or str
).
Description: Initializes a Web3 client for the specified COTI network.
Parameters:
coti_network
(CotiNetwork
): Network configuration (e.g., testnet or mainnet).
Returns: A Web3
instance configured with the appropriate middleware.
Description: Onboards an Ethereum account by generating RSA keys, signing the public key, and interacting with the onboarding smart contract. It also retrieves and stores the AES key.
Parameters:
web3
(Web3
): A Web3 instance.
account
(LocalAccount
): The Ethereum account to onboard.
onboard_contract_address
(str
): Address of the onboarding contract.
Returns: None. Updates the LocalAccount
instance with onboarded information.
Raises:
RuntimeError
if the account has insufficient funds for the transaction.
Description: Recovers the AES key for an account using transaction receipt data from the onboarding process.
Parameters:
web3
(Web3
): A Web3 instance.
account
(LocalAccount
): The Ethereum account with onboarding metadata.
onboard_contract_address
(str
): Address of the onboarding contract.
Returns: None. Updates the LocalAccount
instance with the recovered AES key.
Description: Ensures the account has an AES key by either recovering it from the onboarding transaction or initiating the onboarding process.
Parameters:
web3
(Web3
): A Web3 instance.
account
(LocalAccount
): The Ethereum account.
onboard_contract_address
(str
): Address of the onboarding contract (optional, defaults to ACCOUNT_ONBOARD_CONTRACT_ADDRESS
).
Returns: None. Updates the LocalAccount
with AES key information.
Raises:
RuntimeError
if the account has insufficient funds.