|
| 1 | +import * as dotenv from 'dotenv'; |
| 2 | +import { generateModularSDKInstance } from '../helpers/sdk-helper'; |
| 3 | +import { Address, createPublicClient, Hex, http } from 'viem'; |
| 4 | +import { sepolia } from 'viem/chains'; |
| 5 | + |
| 6 | +dotenv.config(); |
| 7 | + |
| 8 | +// tsx examples/basics/get-address.ts |
| 9 | +async function main() { |
| 10 | + const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9'; |
| 11 | + |
| 12 | + // initializating sdk... |
| 13 | + const modularSdk = generateModularSDKInstance( |
| 14 | + process.env.WALLET_PRIVATE_KEY as string, |
| 15 | + Number(process.env.CHAIN_ID), bundlerApiKey); |
| 16 | + |
| 17 | + // get EtherspotWallet address... |
| 18 | + const address: string = await modularSdk.getCounterFactualAddress(); |
| 19 | + console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`); |
| 20 | + |
| 21 | + const typedData = { |
| 22 | + domain: { |
| 23 | + name: 'EtherspotModular', |
| 24 | + version: '1.0.0', |
| 25 | + chainId: 11155111, |
| 26 | + verifyingContract: `0x${address.slice(2)}` as Address, |
| 27 | + }, |
| 28 | + types: { |
| 29 | + Person: [ |
| 30 | + { name: 'name', type: 'string' }, |
| 31 | + { name: 'wallet', type: 'address' }, |
| 32 | + ], |
| 33 | + Mail: [ |
| 34 | + { name: 'from', type: 'Person' }, |
| 35 | + { name: 'to', type: 'Person' }, |
| 36 | + { name: 'contents', type: 'string' }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + primaryType: 'Mail', |
| 40 | + message: { |
| 41 | + from: { |
| 42 | + name: 'Cow', |
| 43 | + wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', |
| 44 | + }, |
| 45 | + to: { |
| 46 | + name: 'Bob', |
| 47 | + wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', |
| 48 | + }, |
| 49 | + contents: 'Hello, Bob!', |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + const signTypedData = await modularSdk.signTypedData(typedData); |
| 54 | + const sign = await modularSdk.signMessage({message: 'Hello'}) |
| 55 | + |
| 56 | + console.log('signature: ', sign, signTypedData); |
| 57 | + |
| 58 | + const publicClient = createPublicClient({ |
| 59 | + chain: sepolia, |
| 60 | + transport: http(`https://testnet-rpc.etherspot.io/v2/11155111?api-key=${bundlerApiKey}`), |
| 61 | + }) |
| 62 | + |
| 63 | + console.log(await publicClient.verifyMessage({ |
| 64 | + address: address as Address, |
| 65 | + signature: sign as Hex, |
| 66 | + message: 'Hello' |
| 67 | + })) |
| 68 | + |
| 69 | + console.log(await publicClient.verifyTypedData({ |
| 70 | + address: address as Address, |
| 71 | + domain: typedData.domain, |
| 72 | + types: typedData.types, |
| 73 | + primaryType: 'Mail', |
| 74 | + message: typedData.message, |
| 75 | + signature: signTypedData as Hex, |
| 76 | + })) |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +main() |
| 81 | + .catch(console.error) |
| 82 | + .finally(() => process.exit()); |
0 commit comments