-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
225 lines (203 loc) · 5.76 KB
/
test.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import {
createWalletClient,
http,
verifyMessage,
verifyTypedData,
parseEther,
} from "viem";
import { NearViemAccountFactory } from "./lib/near-viem";
import { Account, KeyPair, connect, keyStores } from "near-api-js";
import { sepolia } from "viem/chains";
// get arguments from command line
const args = process.argv.slice(2);
/**
* Test cases:
* 1 = create a near Viem Account
* 2 = create a Viem Wallet Client and send a transaction
* 3 = create a near Viem Account and sign Typed Data
* 4 = create a near Viem Account and sign message
* 5 = create a near Viem Account and
*/
if (args.length === 0) {
console.log("\nUsage: node main.js <test case number>\n");
console.log(" Test cases:");
console.log(" 1 = create a Near wallet");
console.log(" 2 = create a Near wallet and sign message");
console.log(" 3 = create a Near wallet and sign typed data");
console.log(" 4 = create a Near wallet and send a transaction");
console.log(" 5 = create a Near wallet and send Raw Transaction ");
process.exit(0);
}
// const TEST_CASE = 1;
const TEST_CASE = parseInt(args[0]);
const testCaseMap = {
1: testNearWallet,
2: testNearWalletAndSignMessage,
3: testNearWalletAndSignTypedData,
4: testNearWalletAndSendTransaction,
5: testNearWalletSendRawTransaction,
};
const TESTNET_CONFIG = {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};
interface NearConfig {
networkId: string;
nodeUrl: string;
}
/**
* Loads Near Account from process.env.
* Defaults to TESTNET_CONFIG if no network configuration is provided
* @param network {NearConfig} network settings
* @returns {Account}
*/
const nearAccountFromEnv = async (
network: NearConfig = TESTNET_CONFIG
): Promise<Account> => {
const keyPair = KeyPair.fromString(
"ed25519:21G5n2oCE2oS88m9XEPVtDWqC5y4trc5r66Djq4ZJTDxt8nf1f53SnUUokXFVWAHsYdc9cvK89eqSZmoWJtDT5vH"
);
return nearAccountFromKeyPair({
keyPair,
accountId: "nearviem.testnet",
network,
});
};
/**
* Loads Near Account from provided keyPair and accountId
* Defaults to TESTNET_CONFIG
* @param keyPair {KeyPair}
* @param accountId {string}
* @param network {NearConfig} network settings
* @returns {Account}
*/
const nearAccountFromKeyPair = async (config: {
keyPair: KeyPair;
accountId: string;
network?: NearConfig;
}): Promise<Account> => {
const keyStore = new keyStores.InMemoryKeyStore();
await keyStore.setKey("testnet", config.accountId, config.keyPair);
const near = await connect({
...(config.network || TESTNET_CONFIG),
keyStore,
});
const account = await near.account(config.accountId);
return account;
};
async function testNearWallet() {
const account = await createNearViemAccount();
console.log("account", account.address);
}
async function testNearWalletAndSignMessage() {
const account = await createNearViemAccount();
const sig = await account.signMessage({ message: "Hello World" });
const valid = await verifyMessage({
address: account.address,
message: "Hello World",
signature: sig,
});
console.log("valid", valid);
}
async function testNearWalletAndSignTypedData() {
// message
const message = {
from: {
name: "Cow",
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
to: {
name: "Bob",
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
contents: "Hello, Bob!",
} as const;
// domain
const domain = {
name: "Ether Mail",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
} as const;
// The named list of all type definitions
const types = {
Person: [
{ name: "name", type: "string" },
{ name: "wallet", type: "address" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "string" },
],
} as const;
const account = await createNearViemAccount();
const signature = await account.signTypedData({
domain: domain,
types: types,
primaryType: "Mail",
message: message,
});
const valid = await verifyTypedData({
address: account.address,
domain,
types,
primaryType: "Mail",
message,
signature: signature,
});
console.log("valid", valid);
}
async function testNearWalletAndSendTransaction() {
const account = await createNearViemAccount();
const walletClient = createWalletClient({
account: account,
transport: http('https://ethereum-sepolia-rpc.publicnode.com'),
chain: sepolia,
});
const hash = await walletClient.sendTransaction({
account,
to: account.address,
value: parseEther("0.0001"),
chain: walletClient.chain,
kzg: undefined,
});
// check hash at sepolia scan
console.log("hash", hash);
}
async function testNearWalletSendRawTransaction() {
const account = await createNearViemAccount();
const walletClient = createWalletClient({
account: account,
transport: http('https://ethereum-sepolia-rpc.publicnode.com'),
chain: sepolia,
});
// const kzg = setupKzg(cKzg, mainnetTrustedSetupPath);
const request = await walletClient.prepareTransactionRequest({
account,
to: account.address,
value: parseEther("0.0001"),
chain: walletClient.chain,
kzg: undefined,
});
const signature = await walletClient.signTransaction(request);
const hash = await walletClient.sendRawTransaction({
serializedTransaction: signature,
});
// check hash at sepolia scan
console.log("hash", hash);
}
/**
* ========== STARTS HERE ==========
* Main function to create a wallet
*/
(async () => {
try {
await testCaseMap[TEST_CASE]();
} catch (e) {}
})();
async function createNearViemAccount() {
const account = await nearAccountFromEnv();
const viemAccount = await NearViemAccountFactory(account);
return viemAccount;
}