我想将我的 dapp 与 WalletConnect 连接,但我只想为我的应用程序创建一对。出于测试目的,我使用 WalletConnect demo wallet 每次创建连接时我都可以看到,新配对出现:
我只想要一对。
当用户第一次访问我的 dapp 时,我使用 UniversalProvider 连接到 WalletConnect(没有主题 - 主题尚未定义)。
import UniversalProvider from "@walletconnect/universal-provider";
const ethereumProvider = await UniversalProvider.init({
projectId: projectId
});
let requiredNamespaces = {
namespaces: {
eip155: {
methods: [
"eth_sendTransaction",
"eth_signTransaction",
"eth_sign",
"personal_sign",
"eth_signTypedData",
],
chains: [chainId],
events: ["chainChanged", "accountsChanged"],
}
},
};
const session = await ethereumProvider.connect(requiredNamespaces);
所以会话建立了所以我将会话对象和topicId存储在本地存储中:
// after this I store session object and session.topic in local storage
window.localStorage.setItem('session', JSON.stringify(session));
window.localStorage.setItem('topicId', JSON.stringify(session.topic));
现在用户决定断开会话:
await ethereumProvider.disconnect();
// I remove session, but topic is still stored
window.localStorage.removeItem('session');
现在用户决定再次连接到我的 dapp:
const topicId = JSON.parse(window.localStorage.getItem('topicId'));
let requiredNamespaces = {
namespaces: {
eip155: {
methods: [
"eth_sendTransaction",
"eth_signTransaction",
"eth_sign",
"personal_sign",
"eth_signTypedData",
],
chains: [chainId],
events: ["chainChanged", "accountsChanged"],
pairingTopic: topicId
}
},
};
const session = await ethereumProvider.connect(requiredNamespaces);
现在问题来了——新配对将被添加到钱包中,而不是使用旧配对。 我不知道这是否是处理 WalletConnect 会话和配对的正确方法 - 恕我直言,他们的文档很差。 我做错了什么吗?也许我不应该这样断开连接?这是 WalletConnect 问题吗? 感谢您的任何回答:)