Web3.py
In order to provide easy access to all the features of COTI, the coti-web3 Python SDK was created, which is made in a way that has an interface very similar to that of web3.py. 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.
Clone, Build & Test
Full instructions for building and testing the package locally are available in the coti-web3.py GitHub repository
Installation
SDK versions < 1.0.0 are for use with the Devnet, versions >= 1.0.0 are for use with the Testnet
Usage
See the coti-python-examples repository for code examples.
Classes
Account
Description: Loads an account from a raw private key.
Parameters:
private_key
: The private key as a hex string,bytes
,int
, orPrivateKey
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 encryptedcontract_address
: The address of the contract that the user is interacting withfunction_selector
: The four-byte function selectorprivate_key
: The private key used to sign the transaction and encrypted valueaes_key
: The AES key used to encrypt the value
Returns: Encrypted data object (
ItBool
,ItUint
, orItString
)
Description: Decrypts a value encrypted with
encrypt_value
.Parameters:
ciphertext
: The encrypted ciphertext to decryptaes_key
: The AES key used to decrypt the value
Returns: The original value (
bool
,int
, orstr
).
LocalAccount
Properties
user_onboard_info
: A dictionary containing the information from the user's onboarding procedureaes_key
: The AES key used for encryption and decryptionrsa_key_pair
: A tuple containing the RSA public and private keysonboard_tx_hash
: The transaction hash associated with the onboarding procedure
Methods
Description: Initializes a new
LocalAccount
instance with a private key and an associatedAccountLocalActions
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
, orstr
).contract_address
: The contract's Ethereum address.function_selector
: The function selector for the contract call.
Returns: An encrypted value (
ItBool
,ItUint
, orItString
).
Description: Decrypts an encrypted value.
Parameters:
ciphertext
: The encrypted value (CtBool
,CtUint
, orCtString
).
Returns: The decrypted value (
bool
,int
, orstr
).
Modules
COTI
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 toACCOUNT_ONBOARD_CONTRACT_ADDRESS
).
Returns: None. Updates the
LocalAccount
with AES key information.Raises:
RuntimeError
if the account has insufficient funds.
Last updated