Skip to content

Instantly share code, notes, and snippets.

@metashx
Last active April 7, 2024 10:06
Show Gist options
  • Save metashx/caf12c00e29a2f8391540f05535a12fb to your computer and use it in GitHub Desktop.
Save metashx/caf12c00e29a2f8391540f05535a12fb to your computer and use it in GitHub Desktop.
/*
This script is designed to allow smart accounts to log into websites that require a
signature. It will automatically (without prompting the user) accept a WalletConnect
session for the provided URI and sign the incoming personal_sign request to pass the
log in step on https://app.liquifi.finance/. It should realistically work on any
website that requires a signature to log in.
Dependencies:
1. Copy the package.json into a folder alongside this script.
2. Run `npm install` to install deps.
3. Populate the constants
- RPC_URL,
- PRIVATE KEY (of the EOA that is a valid signer for the smart account),
- WC_PROJECT_ID (free from https://cloud.walletconnect.com/),
- ERC6551_WALLET (the address of the smart account),
- ERC6551_SIGNER (the address of the EOA used above for PRIVATE_KEY)
4. Go to the website you want to log in to. Select WalletConnect as your wallet.
Copy the URI when the QR code is shown.
5. Set the value of WC_URI below as the uri you copied.
6. Run the script using `npm run connect`
If you get "timed out" errors your WalletConnect URI may have expired so just
refresh the website and try connecting again using a new URI.
*/
import { Core } from "@walletconnect/core"
import { Web3Wallet } from "@walletconnect/web3wallet"
import { buildApprovedNamespaces } from "@walletconnect/utils"
import { ethers } from "ethers"
const RPC_URL=""
const PRIVATE_KEY=""
const WC_PROJECT_ID=""
const ERC6551_WALLET=""
const WC_URI=""
const provider = new ethers.JsonRpcProvider(RPC_URL)
const signer = new ethers.Wallet(PRIVATE_KEY, provider)
const metadata = {
name: 'erc-6551-walletconnect',
description: 'connect erc-6551 wallet to ui using walletconnect',
url: 'www.walletconnect.com',
icons: [],
}
const core = new Core({
projectId: WC_PROJECT_ID,
logger: 'debug',
})
const web3wallet = await Web3Wallet.init({ core, metadata })
web3wallet.on('session_proposal', async proposal => {
const approvedNamespaces = buildApprovedNamespaces({
proposal: proposal.params,
supportedNamespaces: {
eip155: {
chains: ['eip155:11155111'],
methods: ['eth_sendTransaction', 'personal_sign'],
events: ['accountsChanged', 'chainChanged'],
accounts: [
`eip155:11155111:${ERC6551_WALLET}`,
]
}
}
})
await web3wallet.approveSession({
id: proposal.id,
namespaces: approvedNamespaces
})
})
web3wallet.on('session_request', async event => {
console.log('event', event)
const { topic, params, id } = event
const { request } = params
const requestParamsMessage = request.params[0]
console.log('requestParamsMessage', requestParamsMessage)
const signedMessage = await signer.signMessage(ethers.getBytes(requestParamsMessage))
console.log('signed', signedMessage)
const response = { id, result: signedMessage, jsonrpc: '2.0' }
await web3wallet.respondSessionRequest({ topic, response })
})
await web3wallet.core.pairing.pair({
uri: WC_URI,
activatePairing: true
})
{
"dependencies": {
"@tokenbound/sdk": "^0.5.1",
"@walletconnect/web3wallet": "^1.10.3",
"ethers": "^6.11.1"
},
"scripts": {
"connect": "node erc-6551-walletconnect.js"
},
"type": "module"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment