### Install pyWalletConnect Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Commands to install the library via pip. ```bash python3 -m pip install pyWalletConnect ``` ```bash python3 -m pip install . ``` -------------------------------- ### WCClient.open_session Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Starts a session. ```APIDOC ## WCClient.open_session() ### Description Starts a WalletConnect session by waiting for the session request message. Must be called after client creation. ### Response - **Returns** (tuple) - (message RPCid, chainIDsList, peerMeta data object). ``` -------------------------------- ### Initialize and Connect WalletConnect Session Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Demonstrates initializing the WCClient, setting metadata, and handling session requests from a Dapp. ```python from pywalletconnect import WCClient, WCClientInvalidOption # Input the wc URI string_uri = input("Input the WalletConnect URI : ") WCClient.set_wallet_metadata(WALLET_METADATA) # Optional, else identify pyWalletConnect as the wallet WCClient.set_project_id(WALLETCONNECT_PROJECT_ID) # Required for v2 WCClient.set_origin(WALLETCONNECT_ORIGIN_DOMAIN) # Optional for v2 try: wallet_dapp = WCClient.from_wc_uri(string_uri) except WCClientInvalidOption as exc: # In case error in the wc URI provided if hasattr(wallet_dapp, "wc_client"): wallet_dapp.close() raise InvalidOption(exc) # Wait for the sessionRequest info # Can throw WCClientException "sessionRequest timeout" req_id, chain_ids, request_info = wallet_dapp.open_session() if str(account.chainID) not in chain_ids: # Chain id mismatch wallet_dapp.close() raise InvalidOption("Chain ID from Dapp is not the same as the wallet.") # Display to the user request details provided by the Dapp. user_ok = input(f"WalletConnect link request from : {request_info['name']}. Approve? [y/N]") if user_ok.lower() == "y": # User approved wallet_dapp.reply_session_request(req_id, account.chainID, account.address) # Now the session with the Dapp is opened <...> else: # User rejected wclient.reject_session_request(req_id) wallet_dapp.close() raise UserInteration("user rejected the dapp connection request.") ``` -------------------------------- ### WCClient.from_wc_uri Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Constructor to create a client from a URI. ```APIDOC ## WCClient.from_wc_uri(wc_uri_str) ### Description Creates a WalletConnect wallet client from a v1 or v2 EIP1328 URI. ### Parameters - **wc_uri_str** (string) - Required - The full EIP1328 URI provided by the Dapp. ``` -------------------------------- ### WCClient.reply_session_request Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Approves a session request. ```APIDOC ## WCClient.reply_session_request(msg_id, chain_id, account_address) ### Description Sends a session approval message to the Dapp. ### Parameters - **msg_id** (string/int) - Required - The RPC ID of the session request. - **chain_id** (int/string) - Required - The blockchain ID. - **account_address** (string) - Required - The wallet account address. ``` -------------------------------- ### WCClient.reply Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Sends an RPC response. ```APIDOC ## WCClient.reply(req_id, result_str) ### Description Sends an RPC result response to the webapp. ### Parameters - **req_id** (string/int) - Required - The JSON-RPC ID of the request. - **result_str** (string) - Required - The result data to return. ``` -------------------------------- ### WCClient.set_wallet_namespace Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Sets the supported chain collection namespace for v2. ```APIDOC ## WCClient.set_wallet_namespace(wallet_namespace) ### Description Sets the wallet namespace, representing the supported chain collection. Defaults to 'eip155'. ### Parameters - **wallet_namespace** (string) - Required - The namespace identifier. ``` -------------------------------- ### WCClient.reject Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Rejects a request. ```APIDOC ## WCClient.reject(req_id, error_code=5002) ### Description Informs the webapp that the request was rejected by the user. ### Parameters - **req_id** (string/int) - Required - The JSON-RPC ID of the request. - **error_code** (int) - Optional - The rejection code (default 5002). ``` -------------------------------- ### WCClient.get_message Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Pops a message from the internal queue. ```APIDOC ## WCClient.get_message() ### Description Retrieves an RPC call message from the internal FIFO queue. This method also triggers auto-reconnection logic. ### Response - **Returns** (tuple) - (RPCid, method, params) or (None, "", []) if no data is available. ``` -------------------------------- ### WCClient.set_project_id Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Sets the WalletConnect project ID. ```APIDOC ## WCClient.set_project_id(project_id) ### Description Sets the WalletConnect project ID, mandatory for v2 connections using the official central bridge relay. ### Parameters - **project_id** (string) - Required - The project ID string. ``` -------------------------------- ### Implement WalletConnect message processing and transaction handling Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Defines functions for processing eth_sendTransaction requests and a message polling loop to handle various WalletConnect RPC methods. ```python def process_sendtransaction(call_id, tx): # Processing the RPC query eth_sendTransaction # Collect the user approval about the tx query < Accept (tx) ? > if approved : # Build and sign the provided transaction <...> # Broadcast the tx # Provide the transaction id as result return "0x..." # Tx id def watch_messages(): # Watch for messages received. # For WalletConnect calls reading. # Read all the message requests received from the dapp. # Then dispatch to the wallet service handlers. # get_message gives (id, method, params) or (None, "", []) wc_message = wallet_dapp.get_message() # Loop in the waiting messages pool, until depleted while wc_message[0] is not None: # Read a WalletConnect call message available id_request = wc_message[0] method = wc_message[1] parameters = wc_message[2] if method == "wc_sessionRequest" or method == "wc_sessionPayload": # Read if v2 and convert to v1 format if parameters.get("request"): method = parameters["request"].get("method") parameters = parameters["request"].get("params") if "wc_sessionUpdate" == method: if parameters[0].get("approved") is False: raise Exception("Disconnected by the Dapp.") # v2 disconnect if "wc_sessionDelete" == method: raise Exception("Disconnected by the Dapp.") # Dispatch query processing elif "eth_signTypedData" == method: result = process_signtypeddata(id_request, parameters[1]) wallet_dapp.reply(call_id, result) elif "eth_sendTransaction" == method: approve_ask = input("Approve (y/N)?: ").lower() if approve_ask == 'y': result = process_sendtransaction(id_request, parameters[0]) wallet_dapp.reply(call_id, result) else: wallet_dapp.reply_error(call_id, "User rejected request.", 4001) elif "eth_sign" == method: approve_ask = input("Approve (y/N)?: ").lower() if approve_ask == 'y': result = process_signtransaction(parameters[1]) wallet_dapp.reply(call_id, result) else: wallet_dapp.reject(call_id) <...> # Next loop wc_message = wallet_dapp.get_message() # GUI timer repeated or threading daemon # Will call watch_messages every 4 seconds apptimer = Timer(4000) # Call watch_messages when expires periodically apptimer.notify = watch_messages ``` -------------------------------- ### WCClient.set_wallet_metadata Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Sets the wallet metadata object for v2 connections. ```APIDOC ## WCClient.set_wallet_metadata(wallet_metadata) ### Description Sets the wallet metadata as an object for v2 connections. If not provided, default pyWalletConnect metadata is used. ### Parameters - **wallet_metadata** (object) - Required - The metadata object following WalletConnect standard format. ``` -------------------------------- ### WCClient.reject_session_request Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Rejects a session request. ```APIDOC ## WCClient.reject_session_request(req_id) ### Description Sends a session rejection message to the Dapp. ### Parameters - **req_id** (string/int) - Required - The RPC ID of the session request. ``` -------------------------------- ### WCClient.close Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Closes the session and connection. ```APIDOC ## WCClient.close() ### Description Sends a session close message and terminates the underlying WebSocket connection. ``` -------------------------------- ### WCClient.reply_error Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Sends an RPC error response. ```APIDOC ## WCClient.reply_error(req_id, message, error_code) ### Description Sends an RPC error response to the webapp. ### Parameters - **req_id** (string/int) - Required - The JSON-RPC ID of the request. - **message** (string) - Required - A short description of the error. - **error_code** (int) - Required - The error code. ``` -------------------------------- ### WCClient.get_relay_url Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Retrieves the relay URL. ```APIDOC ## WCClient.get_relay_url() ### Description Returns the address of the WebSocket relay bridge. ``` -------------------------------- ### WCClient.set_origin Source: https://github.com/antonio-fr/pywalletconnect/blob/master/README.md Sets the origin domain for the websocket connection. ```APIDOC ## WCClient.set_origin(origin_domain) ### Description Sets the origin of the first HTTP query for the websocket. Only applicable for v2. ### Parameters - **origin_domain** (string) - Required - The origin domain string. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.