Skip to content

Commit

Permalink
move backend functionality inward
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Aug 10, 2024
1 parent ed42e81 commit 1ad2f49
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
85 changes: 78 additions & 7 deletions src/beta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Hex,
Signature,
fromHex,
hashMessage,
hashTypedData,
Expand All @@ -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 {
Expand Down Expand Up @@ -131,7 +134,28 @@ export class Beta {
constructor(adapter: NearEthAdapter) {
this.adapter = adapter;
}

async approveSession(
w3Wallet: Web3Wallet,
proposal: Web3WalletTypes.SessionProposal
): Promise<SessionTypes.Struct> {
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<Web3WalletTypes.SessionRequest>
): Promise<NearEthTxData> {
Expand All @@ -158,14 +182,61 @@ export class Beta {
}

async respondSessionRequest(
signature: Signature,
w3Wallet: Web3Wallet,
request: Web3WalletTypes.SessionRequest,
nearTxHash: string,
transaction?: Hex
): Promise<Hex> {
): Promise<void> {
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",
];
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@
dependencies:
tslib "1.14.1"

"@walletconnect/[email protected]":
"@walletconnect/[email protected]", "@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==
Expand All @@ -1690,7 +1690,7 @@
"@walletconnect/logger" "2.1.2"
events "3.3.0"

"@walletconnect/[email protected]", "@walletconnect/utils@^2.10.1":
"@walletconnect/[email protected]", "@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==
Expand Down

0 comments on commit 1ad2f49

Please sign in to comment.