-
Notifications
You must be signed in to change notification settings - Fork 542
/
index.js
46 lines (39 loc) · 1.29 KB
/
index.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
/**
* This example will use its app key as a signing key, and sign anything it is
* asked to.
*/
const ethers = require('ethers');
const provider = new ethers.providers.Web3Provider(ethereum);
/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
*
* @param {object} args - The request handler args as object.
* @param {JsonRpcRequest<unknown[] | Record<string, unknown>>} args.request - A
* validated JSON-RPC request object.
* @returns {unknown} The response, based on the request method.
* @throws If the request method is not valid for this snap.
*/
module.exports.onRpcRequest = async ({ request }) => {
console.log('received request', request);
const privKey = await snaps.request({
method: 'snap_getAppKey',
});
console.log(`privKey is ${privKey}`);
const ethWallet = new ethers.Wallet(privKey, provider);
console.dir(ethWallet);
switch (request.method) {
case 'address':
return ethWallet.address;
case 'signMessage': {
const message = request.params[0];
console.log('trying to sign message', message);
return ethWallet.signMessage(message);
}
case 'sign': {
const transaction = request.params[0];
return ethWallet.sign(transaction);
}
default:
throw new Error('Method not found.');
}
};