From 1ad2f49e9f97b43bc150fc0adb982f3439a6c0ed Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Sat, 10 Aug 2024 16:14:11 +0200 Subject: [PATCH] move backend functionality inward --- package.json | 2 ++ src/beta.ts | 85 +++++++++++++++++++++++++++++++++++++++++++++++----- yarn.lock | 4 +-- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 87b0137..9ede6ed 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@types/node": "^22.1.0", "@typescript-eslint/eslint-plugin": "^8.0.0", "@typescript-eslint/parser": "^8.0.0", + "@walletconnect/types": "^2.14.0", "dotenv": "^16.4.5", "eslint": "^9.7.0", "ethers": "^6.13.1", @@ -38,6 +39,7 @@ "typescript": "^5.5.3" }, "dependencies": { + "@walletconnect/utils": "^2.14.0", "@walletconnect/web3wallet": "^1.13.0", "elliptic": "^6.5.6", "near-api-js": "^4.0.3", diff --git a/src/beta.ts b/src/beta.ts index d031bdd..6c90d3b 100644 --- a/src/beta.ts +++ b/src/beta.ts @@ -1,6 +1,5 @@ import { Hex, - Signature, fromHex, hashMessage, hashTypedData, @@ -11,7 +10,11 @@ import { import { addSignature, populateTx, toPayload } from "./utils/transaction"; import { NearEthTxData, RecoveryData } from "./types"; import { NearEthAdapter } from "./chains/ethereum"; -import { Web3WalletTypes } from "@walletconnect/web3wallet"; +import Web3Wallet, { Web3WalletTypes } from "@walletconnect/web3wallet"; +import { buildApprovedNamespaces } from "@walletconnect/utils"; +import { SessionTypes } from "@walletconnect/types"; +import { configFromNetworkId, getNetworkId } from "./chains/near"; +import { signatureFromTxHash } from "./utils/signature"; // Interface for Ethereum transaction parameters export interface EthTransactionParams { @@ -131,7 +134,28 @@ export class Beta { constructor(adapter: NearEthAdapter) { this.adapter = adapter; } - + async approveSession( + w3Wallet: Web3Wallet, + proposal: Web3WalletTypes.SessionProposal + ): Promise { + const result = await w3Wallet.approveSession({ + id: proposal.id, + namespaces: buildApprovedNamespaces({ + proposal: proposal.params, + supportedNamespaces: { + eip155: { + chains: supportedChainIds.map((id) => `eip155:${id}`), + methods: supportedMethods, + events: supportedEvents, + accounts: supportedChainIds.map( + (id) => `eip155:${id}:${this.adapter.address}` + ), + }, + }, + }), + }); + return result; + } async handleSessionRequest( request: Partial ): Promise { @@ -158,14 +182,61 @@ export class Beta { } async respondSessionRequest( - signature: Signature, + w3Wallet: Web3Wallet, + request: Web3WalletTypes.SessionRequest, + nearTxHash: string, transaction?: Hex - ): Promise { + ): Promise { + const nearConfig = configFromNetworkId( + getNetworkId(this.adapter.nearAccountId()) + ); + const signature = await signatureFromTxHash(nearConfig.nodeUrl, nearTxHash); + let result = serializeSignature(signature); if (transaction) { const signedTx = addSignature({ transaction, signature }); // Returns relayed transaction hash (without waiting for confirmation). - return this.adapter.relaySignedTransaction(signedTx, false); + result = await this.adapter.relaySignedTransaction(signedTx, false); } - return serializeSignature(signature); + + await w3Wallet.respondSessionRequest({ + topic: request.topic, + response: { + id: request.id, + jsonrpc: "2.0", + result, + }, + }); } } + +const supportedChainIds = [1, 100, 11155111]; +const supportedMethods = [ + // "eth_accounts", + // "eth_requestAccounts", + "eth_sendRawTransaction", + "eth_sign", + "eth_signTransaction", + "eth_signTypedData", + "eth_signTypedData_v3", + "eth_signTypedData_v4", + "eth_sendTransaction", + "personal_sign", + // "wallet_switchEthereumChain", + // "wallet_addEthereumChain", + // "wallet_getPermissions", + // "wallet_requestPermissions", + // "wallet_registerOnboarding", + // "wallet_watchAsset", + // "wallet_scanQRCode", + // "wallet_sendCalls", + // "wallet_getCallsStatus", + // "wallet_showCallsStatus", + // "wallet_getCapabilities", +]; +const supportedEvents = [ + "chainChanged", + "accountsChanged", + "message", + "disconnect", + "connect", +]; diff --git a/yarn.lock b/yarn.lock index 7d2acfe..11ef35c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1678,7 +1678,7 @@ dependencies: tslib "1.14.1" -"@walletconnect/types@2.14.0": +"@walletconnect/types@2.14.0", "@walletconnect/types@^2.14.0": version "2.14.0" resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.14.0.tgz#af3d4799b8ac5d166251af12bc024276f82f9b91" integrity sha512-vevMi4jZLJ55vLuFOicQFmBBbLyb+S0sZS4IsaBdZkQflfGIq34HkN13c/KPl4Ye0aoR4/cUcUSitmGIzEQM5g== @@ -1690,7 +1690,7 @@ "@walletconnect/logger" "2.1.2" events "3.3.0" -"@walletconnect/utils@2.14.0", "@walletconnect/utils@^2.10.1": +"@walletconnect/utils@2.14.0", "@walletconnect/utils@^2.10.1", "@walletconnect/utils@^2.14.0": version "2.14.0" resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.14.0.tgz#48493ffe1e902815fda3cbd5cc5409288a066d35" integrity sha512-vRVomYQEtEAyCK2c5bzzEvtgxaGGITF8mWuIL+WYSAMyEJLY97mirP2urDucNwcUczwxUgI+no9RiNFbUHreQQ==