This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_account_manager_node_script.js
130 lines (98 loc) · 4.8 KB
/
example_account_manager_node_script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as ethers from "ethers";
import * as xmtp from "@xmtp/xmtp-js";
import * as config from "./config.js";
import * as findRpcUtil from "./findRpcUtil.js";
import * as readline from 'node:readline/promises';
import * as chainlist from 'eth-chainlist'; //or https://www.npmjs.com/package/evm-chains //import * as evnChains from 'evm-chains';
import { stdin as input, stdout as output } from 'node:process';
import { delay as delay, getKnownRPC } from "./utils.js";
const rl = readline.createInterface({ input, output });
console.log("The projects goal is to enable flexible and gas efficient remote controling of EOA wallets")
console.log("The account manager does not know the private keys of the wallets but the server knows he is authorized to manage them.\n")
//await rl.question('Enter for Next...');
console.log("Account manager="+config.XMTP_ACCOUNT_MANAGER_PUB);
console.log("Remote Wallet server listening on="+config.XMTP_LISTENER_PUB);
console.log("Wallet under managment (one of many)="+config.EXAMPLE_WALLET_UNDER_REMOTE_MANAGMENT);
//await rl.question('Enter for Next...');
const x_accountmanager = new ethers.Wallet(config.XMTP_ACCOUNT_MANAGER_PRIVATEKEY )
const x_client = await xmtp.Client.create(x_accountmanager, { env: "dev" });
let can_message_remote_wallet_server= await x_client.canMessage(config.XMTP_LISTENER_PUB);
const conversation = await x_client.conversations.newConversation(config.XMTP_LISTENER_PUB);
console.log("");
console.log("");
const transfer_eth_tx_1=`{
"method":"sign_eth",
"body": {
"from":"0xBDF05016BccbBCEC7c93e5208001a8D02eb1981d",
"to":"0x602179f3f1dAE93Ee1A80D898552b823E1E11beE",
"value":"100000000000000",
"gasLimit":"0xa2f3",
"maxFeePerGas":"0xb7263fdc",
"max_priority_fee_per_gas":"0x59682f00",
"chainId":"11155111"
}
}`
const transfer_eth_tx_1_js=JSON.parse(transfer_eth_tx_1).body;
console.log("Let the remote wallet transfer some eth");
console.log(transfer_eth_tx_1);
await conversation.send(transfer_eth_tx_1);
const timestamp_1= new Date();
await delay(2000);
console.log("loading new messages...")
const messages = await conversation.messages({startTime:timestamp_1});
let related_response = JSON.parse(messages.filter(m=>{ try { let mjs=JSON.parse(m.content); return mjs.method==="reply_sign" }catch{ return false} } ).slice(-1)[0].content);
//let related_response1_js=JSON.parse(related_response.content);
let singedTx_1 = related_response.signed;
console.log("\n Server replies with signed transcation= "+singedTx_1);
console.log("\n\nLets send that to the blockchain from our account manager local machine \n");
let provider= await getKnownRPC(Number(transfer_eth_tx_1_js.chainId));
const res_tx1_to_blockchain= await provider.sendTransaction(singedTx_1)
console.log("https://sepolia.etherscan.io/tx/"+res_tx1_to_blockchain.hash);
console.log("\n We can also ask the server to directly send the transaction to the blockchain");
const transfer_eth_tx_2=`{
"method":"sign_and_send_eth",
"body": {
"from":"0xBDF05016BccbBCEC7c93e5208001a8D02eb1981d",
"to":"0x602179f3f1dAE93Ee1A80D898552b823E1E11beE",
"value":"200000000000000",
"gasLimit":"0xa2f3",
"maxFeePerGas":"0xb7263fdc",
"max_priority_fee_per_gas":"0x59682f00",
"chainId":"11155111"
}
}`
console.log(transfer_eth_tx_2);
await conversation.send(transfer_eth_tx_2);
const timestamp_2= new Date();
await delay(3000);
console.log("loading new messages...")
const messages2 = await conversation.messages({startTime:timestamp_2});
let related_response2 = JSON.parse(messages2.filter(m=>{ try { let mjs=JSON.parse(m.content); return ( mjs.hasOwnProperty('tx_hash_onchain' )) }catch{ return false} } ).slice(-1)[0].content);
console.log("Server replied with:"+related_response2);
console.log("https://sepolia.etherscan.io/tx/"+related_response2.
tx_hash_onchain);
let wrap_eth_tx=`{
"method":"sign_and_send_eth",
"body": {
"from":"0xBDF05016BccbBCEC7c93e5208001a8D02eb1981d",
"to":"0x7b79995e5f793a07bc00c21412e50ecae098e7f9",
"value":"200000000000000",
"data":"0xd0e30db0",
"gasLimit":"0xa2f3",
"maxFeePerGas":"0xb7263fdc",
"max_priority_fee_per_gas":"0x59682f00",
"chainId":"eip155:11155111"
}
}`
console.log("Contracts of course also work:\n"+wrap_eth_tx);
let next_timestamp;
let next_messages;
let next_resp;
await conversation.send(wrap_eth_tx);
next_timestamp= new Date();
await delay(3000);
next_messages = await conversation.messages({startTime:next_timestamp});
next_resp = JSON.parse(next_messages.filter(m=>{ try { let mjs=JSON.parse(m.content); return ( mjs.hasOwnProperty('tx_hash_onchain' )) }catch{ return false} } ).slice(-1)[0].content);
console.log("https://sepolia.etherscan.io/tx/"+next_resp.
tx_hash_onchain);
//Show admin commands in chat;