generated from MetaMask/template-snap-monorepo
/
index.ts
219 lines (193 loc) · 6.54 KB
/
index.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
import { OnRpcRequestHandler } from '@metamask/snaps-types';
import { heading, text } from '@metamask/snaps-ui';
import { Account, ExternalAccount, IdentitySnapParams } from './interfaces';
import { getAccountInfo } from './rpc/account/getAccountInfo';
import { getAvailableDIDMethods } from './rpc/did/getAvailableDIDMethods';
import { getCurrentDIDMethod } from './rpc/did/getCurrentDIDMethod';
import { resolveDID } from './rpc/did/resolveDID';
import { switchDIDMethod } from './rpc/did/switchDIDMethod';
import { configureGoogleAccount } from './rpc/gdrive/configureGoogleAccount';
import { createNewHederaAccount } from './rpc/hedera/createNewHederaAccount';
import { togglePopups } from './rpc/snap/togglePopups';
import { createVC } from './rpc/vc/createVC';
import { createVP } from './rpc/vc/createVP';
import { deleteAllVCs } from './rpc/vc/deleteAllVCs';
import { getSupportedProofFormats } from './rpc/vc/getSupportedProofFormats';
import { getVCs } from './rpc/vc/getVCs';
import { removeVC } from './rpc/vc/removeVC';
import { saveVC } from './rpc/vc/saveVC';
import { syncGoogleVCs } from './rpc/vc/syncGoogleVCs';
import { verifyVC } from './rpc/vc/verifyVC';
import { verifyVP } from './rpc/vc/verifyVP';
import { getCurrentAccount } from './snap/account';
import { generateCommonPanel } from './snap/dialog';
import { getSnapStateUnchecked } from './snap/state';
import { CreateNewHederaAccountRequestParams } from './types/params';
import { init } from './utils/init';
import {
isExternalAccountFlagSet,
isValidConfigueGoogleRequest,
isValidCreateNewHederaAccountParams,
isValidCreateVCRequest,
isValidCreateVPRequest,
isValidDeleteAllVCsRequest,
isValidGetVCsRequest,
isValidMetamaskAccountParams,
isValidRemoveVCRequest,
isValidResolveDIDRequest,
isValidSaveVCRequest,
isValidSwitchMethodRequest,
isValidVerifyVCRequest,
isValidVerifyVPRequest,
} from './utils/params';
/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
*
* @param args - The request handler args as object.
* @param args.request - A validated JSON-RPC request object.
* @param args.origin - Origin of the request.
* @returns `null` if the request succeeded.
* @throws If the request method is not valid for this snap.
*/
export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
request,
}) => {
console.log('Request:', JSON.stringify(request, null, 4));
console.log('Origin:', origin);
console.log('-------------------------------------------------------------');
console.log(
'request.params=========',
JSON.stringify(request.params, null, 4),
);
let state = await getSnapStateUnchecked(snap);
if (state === null) {
state = await init(snap);
}
console.log('state:', JSON.stringify(state, null, 4));
let isExternalAccount: boolean;
let extraData: unknown;
if (isExternalAccountFlagSet(request.params)) {
isExternalAccount = true;
extraData = (request.params as ExternalAccount).externalAccount.data;
} else {
isExternalAccount = false;
isValidMetamaskAccountParams(request.params);
}
const account: Account = await getCurrentAccount(
state,
request.params,
isExternalAccount,
);
account.extraData = extraData;
console.log(`Current account: ${JSON.stringify(account, null, 4)}`);
const identitySnapParams: IdentitySnapParams = {
origin,
snap,
state,
metamask: ethereum,
account,
};
switch (request.method) {
case 'hello': {
return snap.request({
method: 'snap_dialog',
params: {
/*
Type can be one of the following:
- 'alert': for displaying information.
- 'confirmation': for accepting or rejecting some action.
- 'prompt': for inputting some information.
*/
type: 'alert',
content: await generateCommonPanel(origin, [
heading('Hello from Identify Snap!'),
text('This custom alert is just for display purposes.'),
]),
},
});
}
case 'togglePopups': {
return await togglePopups(identitySnapParams);
}
case 'getAccountInfo': {
return await getAccountInfo(identitySnapParams);
}
case 'createNewHederaAccount': {
isValidCreateNewHederaAccountParams(request.params);
return await createNewHederaAccount(
identitySnapParams,
request.params as CreateNewHederaAccountRequestParams,
);
}
case 'resolveDID': {
isValidResolveDIDRequest(request.params);
return await resolveDID(identitySnapParams, request.params.did);
}
case 'getVCs': {
isValidGetVCsRequest(request.params);
return await getVCs(identitySnapParams, request.params);
}
case 'saveVC': {
isValidSaveVCRequest(request.params);
return await saveVC(identitySnapParams, request.params);
}
case 'createVC': {
isValidCreateVCRequest(request.params);
return await createVC(identitySnapParams, request.params);
}
case 'verifyVC': {
isValidVerifyVCRequest(request.params);
return await verifyVC(
identitySnapParams,
request.params.verifiableCredential,
);
}
case 'removeVC': {
isValidRemoveVCRequest(request.params);
return await removeVC(identitySnapParams, request.params);
}
case 'deleteAllVCs': {
isValidDeleteAllVCsRequest(request.params);
return await deleteAllVCs(identitySnapParams, request.params);
}
case 'createVP': {
isValidCreateVPRequest(request.params);
return await createVP(identitySnapParams, request.params);
}
case 'verifyVP': {
isValidVerifyVPRequest(request.params);
return await verifyVP(
identitySnapParams,
request.params.verifiablePresentation,
);
}
case 'getAvailableMethods': {
return getAvailableDIDMethods();
}
case 'getCurrentDIDMethod': {
return getCurrentDIDMethod(identitySnapParams);
}
case 'switchDIDMethod': {
isValidSwitchMethodRequest(request.params);
return await switchDIDMethod(
identitySnapParams,
request.params.didMethod,
);
}
case 'getSupportedProofFormats': {
return getSupportedProofFormats();
}
case 'configureGoogleAccount': {
isValidConfigueGoogleRequest(request.params);
return await configureGoogleAccount(identitySnapParams, request.params);
}
case 'syncGoogleVCs': {
return await syncGoogleVCs(identitySnapParams);
}
default: {
console.error('Method not found');
throw new Error('Method not found');
}
}
};