# metamask_component.py
import os
import json
import streamlit as st
import streamlit.components.v1 as components
# Define the MetaMask component HTML
def get_metamask_component_html():
"""
Creates an HTML component that can interact with MetaMask browser extension.
"""
return """
"""
def metamask_connector():
"""
Component that renders the MetaMask connector UI and returns connection info.
Returns:
dict: Connection information if connected, None otherwise
"""
# Generate a unique key for the component
component_key = "metamask_connector"
# Create a placeholder for the component if it doesn't exist
if component_key not in st.session_state:
st.session_state[component_key] = {
"connected": False,
"address": None,
"network_name": None,
"network_id": None
}
# Render the HTML component
components.html(
get_metamask_component_html(),
height=200,
scrolling=False
)
# JavaScript callback to update session state
st.markdown("""
""", unsafe_allow_html=True)
# For demonstration - since we can't get real callbacks in this version,
# let's add some manual controls for testing
col1, col2 = st.columns(2)
with col1:
if st.button("Simulate Connection"):
st.session_state[component_key] = {
"connected": True,
"address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"network_name": "Ethereum Mainnet",
"network_id": "1"
}
st.rerun() # Use st.rerun() instead of st.experimental_rerun()
with col2:
if st.button("Simulate Disconnection"):
st.session_state[component_key] = {
"connected": False,
"address": None,
"network_name": None,
"network_id": None
}
st.rerun() # Use st.rerun() instead of st.experimental_rerun()
return st.session_state[component_key]