gpt4 book ai didi

angular - 如何在 Angular 应用程序中使用 WalletConnect?

转载 作者:行者123 更新时间:2023-12-05 02:30:22 26 4
gpt4 key购买 nike

问题:我正在尝试使用 WalletConnect 在我的 Angular 应用程序中连接 MetaMask 钱包。我有以下代码来连接钱包。一切正常,直到下面代码中的这一行:await connector.createSession();

有没有人用 angular 实现 WalletConnect 并遇到这个问题?

错误:

ERROR Error: Uncaught (in promise): ReferenceError: Buffer is not defined
ReferenceError: Buffer is not defined
at typedarrayToBuffer (vendor.js:82628:15)
at arrayToBuffer (vendor.js:61701:71)
at Module.arrayToHex (vendor.js:61704:24)
at generateRandomBytes32 (vendor.js:64322:63)
at vendor.js:56407:96
at Generator.next (<anonymous>)
at asyncGeneratorStep (vendor.js:167002:24)
at _next (vendor.js:167024:9)
at ZoneDelegate.invoke (polyfills.js:4680:26)
at Object.onInvoke (vendor.js:137134:25)
at resolvePromise (polyfills.js:5521:31)
at resolvePromise (polyfills.js:5475:17)
at polyfills.js:5587:17
at ZoneDelegate.invokeTask (polyfills.js:4714:31)
at Object.onInvokeTask (vendor.js:137122:25)
at ZoneDelegate.invokeTask (polyfills.js:4713:60)
at Zone.runTask (polyfills.js:4486:47)
at drainMicroTaskQueue (polyfills.js:4890:35)
at ZoneTask.invokeTask [as invoke] (polyfills.js:4799:21)
at invokeTask (polyfills.js:5908:14)

使用 WalletConnect 文档中的代码:

// Create a connector
const connector = new WalletConnect({
bridge: "https://bridge.walletconnect.org", // Required
qrcodeModal: QRCodeModal,
});

// Subscribe to connection events
connector.on("connect", (error, payload) => {
if (error) {
throw error;
}

// Get provided accounts and chainId
const { accounts, chainId } = payload.params[0];
});

connector.on("session_update", (error, payload) => {
if (error) {
throw error;
}

// Get updated accounts and chainId
const { accounts, chainId } = payload.params[0];
});

connector.on("disconnect", (error, payload) => {
if (error) {
throw error;
}

// Delete connector
});

// Check if connection is already established
if (!connector.connected) {
// create new session
await connector.createSession();
}

最佳答案

首先,使用 npm npm install buffer 安装缓冲区,然后在 polyfills.ts 文件末尾添加此代码:

(window as any)['global'] = window;
global.Buffer = global.Buffer || require('buffer').Buffer;

关于angular - 如何在 Angular 应用程序中使用 WalletConnect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71887241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com