Last active
November 17, 2024 20:06
-
-
Save bitlogik/89b41bb60443c041704f82bcd9b43901 to your computer and use it in GitHub Desktop.
pyWalletConnect basic demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import platform | |
import signal | |
from logging import basicConfig, DEBUG | |
from threading import Event | |
from pywalletconnect.client import WCClient | |
# Enable for debug output | |
basicConfig(level=DEBUG) | |
# pyWalletConnect Basic Dapp demo, using the Ethereum Goerli network | |
# WC v1 at https://example.walletconnect.org/ | |
# WC v2 at https://react-app.walletconnect.com/ | |
def WCCLIdemo(): | |
print(" ") | |
print(" pyWalletConnect minimal demo - Goerli chain") | |
uri = input("Paste a Dapp WC URI (v1/v2) >") | |
# Seed mnemonic | |
# chat network pigeon used gadget wrestle pizza invite stuff rose tree kingdom | |
wallet_address = "0xC17B46133c4589C0a01Bec34FA3515fC8b021f5C" | |
wallet_chain_id = 5 # Goerli | |
# Required for v2 | |
WCClient.set_project_id("XXYOURWCPROJECTIDXX") | |
# Optional | |
WCClient.set_origin("https://thisgist.net") | |
wclient = WCClient.from_wc_uri(uri) | |
print("Connecting to the Dapp ...") | |
session_data = wclient.open_session() | |
# Waiting for user accept the Dapp request | |
user_ok = input( | |
f"WalletConnect pairing request from {session_data[2]['name']}. Approve? [y/N]>" | |
) | |
if user_ok.lower() != "y": | |
print("User denied the pairing.") | |
wclient.reject_session_request(session_data[0]) | |
return | |
print("Accepted, continue connecting with the Dapp ...") | |
wclient.reply_session_request(session_data[0], wallet_chain_id, wallet_address) | |
print("Connected.") | |
print(" To quit : Hit CTRL+C, or disconnect from Dapp.") | |
print("Now waiting for dapp messages ...") | |
while not evt_quit.is_set(): | |
# Better to use a timer to call this loop in a function | |
# get_message return : (id, method, params) or (None, "", []) | |
id_request, method, parameters = wclient.get_message() | |
if id_request is not None: | |
# Detect quit | |
# v1 disconnect | |
if (method == "wc_sessionUpdate" and parameters[0]["approved"] == False): | |
print("User disconnects from Dapp (WC v1).") | |
break | |
# v2 disconnect | |
if method == "wc_sessionDelete" and parameters.get("message"): | |
print("User disconnects from Dapp (WC v2).") | |
print("Reason :", parameters["message"]) | |
break | |
if method == "wc_sessionRequest" or method == "wc_sessionPayload": | |
print("\n <---- Received WalletConnect wallet query :") | |
# Read if v2 and convert to v1 format | |
if parameters.get("request"): | |
method = parameters["request"].get("method") | |
parameters = parameters["request"].get("params") | |
print("Request received :", method, parameters) | |
if "personal_sign" == method: | |
print("PERSONAL_SIGN request") | |
approve_ask = input(f"Approve {parameters} ?\n (y/N) >") | |
if approve_ask.lower() == 'y': | |
result = "12345678" # Dummy data, could be : process_sign(parameters) | |
wclient.reply(id_request, result) | |
else: | |
wclient.reject(id_request) | |
# elif method == ... | |
# do that ... | |
evt_quit.wait(2) | |
wclient.close() | |
print("WC disconnected.") | |
def quit_handler(signo, _frame): | |
print("Demo interrupted.") | |
evt_quit.set() | |
evt_quit = Event() | |
if __name__ == "__main__": | |
signal.signal(signal.SIGTERM, quit_handler) | |
if platform.system() == "Linux": | |
signal.signal(signal.SIGHUP, quit_handler) | |
signal.signal(signal.SIGINT, quit_handler) | |
signal.signal(signal.SIGBREAK, quit_handler) | |
WCCLIdemo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment