-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyj-provider.js
375 lines (333 loc) · 10.1 KB
/
yj-provider.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
'use strict';
if ("undefined" == typeof ttb)
{
var ttb = {};
ttb.getDeviceOS = function()
{
if (!window.hasOwnProperty('__deviceOS'))
{
if (window.hasOwnProperty('flutter_inappwebview')) window.__deviceOS = 'flutter';
else window.__deviceOS = 'plain';
}
return window.__deviceOS;
};
ttb.call_webview_object = function(param)
{
let deviceOS = ttb.getDeviceOS();
if ('flutter' == deviceOS)
{
return window.flutter_inappwebview.callHandler('ttbActionRequest', JSON.stringify(param));
}
else /* PC */
{
if (param.hasOwnProperty('onCallback') && window.hasOwnProperty(param.onCallback))
{
return window[param.onCallback](param);
}
}
};
ttb.is_object = (v) => typeof v === "object" && v !== null;
ttb.is_string = (v) => typeof v === 'string' || v instanceof String;
ttb.is_function = (v) => typeof v === 'function';
ttb.is_array = (v) => Array.isArray(v);
ttb.is_null = (v) => v === null;
ttb.os = ttb.getDeviceOS();
ttb.active_chain_id = '0x1';
/* fake 용 */
class HttpProvider
{
get hasSubscriptions (){return false;}
connect (){}
disconnect (){}
isConnected (){return true;}
subscribe (type, method, ...params){throw new Error('Unimplemented');}
unsubscribe (type, method, id){throw new Error('Unimplemented');}
clone () {throw new Error('Unimplemented');}
}
ttb.provider = new HttpProvider();
ttb.event = {};
ttb.connected_chain_id = {};
ttb.message_handler = {};
ttb.provider.networkVersion = null;
ttb.provider.selectedAddress = null;
/*
By DApp - consumer(dapp)에서 이벤트 등록 할때
window.ethereum.on('disconnect', err => {err.code, err.message.....});
*/
ttb.provider.on = (name, callback_func) =>
{
/* bucket_handler registered_handlers 객체에 push
disconnect, connect, account.... */
ttb.message_handler[name] = callback_func;
};
/*
이 메소드는 end-point에서 이벤트 발생시 호출
*/
ttb.event.notify = (name, param) =>
{
/*
End-point 호출: ttb.event.notify('connect', {'chainId': nnn })
*/
if ('connect' == name)
{
ttb.connected_chain_id[param.chainId] = param.chainId;
ttb.active_chain_id = param.chainId;
/* Legacy 지원 */
ttb.provider.chainId = param.chainId;
if (ttb.message_handler[name]) ttb.message_handler[name](param);
}
/*
End-point 호출: ttb.event.notify('disconnect', {'chainId': nnn })
*/
if ('disconnect' == name)
{
if (ttb.connected_chain_id.hasOwnProperty(param.chainId))
{
delete ttb.connected_chain_id[param.chainId];
ttb.active_chain_id = null;
/* Legacy 지원 */
ttb.provider.chainId = null;
}
if (0 == Object.keys(ttb.connected_chain_id).length)
{
/* 모든 연결이 끊겼을때만 dapp에 알려야함 */
let err = new Error('disconnect');
err.code = 4900;
if (ttb.message_handler[name]) ttb.message_handler[name](err);
/* ******************* Legacy Events support ******************************* */
if (ttb.message_handler['close']) ttb.message_handler['close'](err);
}
}
/*
End-point 호출: ttb.event.notify('accountsChanged', {'accounts': string[] })
*/
if ('accountsChanged' == name)
{
if (param.hasOwnProperty('accounts') && ttb.is_array(param.accounts))
{
if (ttb.message_handler[name]) ttb.message_handler[name](param.accounts);
}
else
{
throw new Error('accountsChanged param missing');
}
}
/*
End-point 호출: ttb.event.notify('chainChanged', {'chainId': nnn })
*/
if ('chainChanged' == name)
{
if (param.hasOwnProperty('chainId'))
{
ttb.active_chain_id = param.chainId;
/* Legacy 지원 */
ttb.provider.chainId = param.chainId;
if (ttb.message_handler[name]) ttb.message_handler[name](ttb.active_chain_id);
/* ******************* Legacy Events support ******************************* */
if (ttb.message_handler['chainIdChanged']) ttb.message_handler['chainIdChanged'](err);
if (ttb.message_handler['networkChanged']) ttb.message_handler['networkChanged'](err);
}
else
{
console.log('accountsChanged param missing');
throw new Error('accountsChanged param missing');
}
}
}
ttb.return_RPCerror_message = (num) =>
{
if(4001 == num)
return "User Rejected the Request";
if(4200 == num)
return 'Unsupported method';
if(999 == num)
return "Unexpected Error while executing";
if (-32603 == num)
return "";
if(null == num)
return "Cannot connect to Endpoint(wallet)";
}
/* Promise를 리턴해야 하며, consumer(dapp)에서 rpc 요청을 대신한다. */
ttb.provider.request = (payload) =>
{
let promise = new Promise(async (resolve_func, reject_func) =>
{
if (!payload.hasOwnProperty('params')) payload.params = [];
if (!payload.hasOwnProperty('method')) return;
let bundle = {
'actionType': 'walletProvider',
'action': payload.method,
'params': payload.params
};
let result_json_string = await ttb.call_webview_object(bundle);
try
{
var oJson = JSON.parse(result_json_string);
}
catch(error)
{
let err = new ProviderRpcError(ttb.return_RPCerror_message());
err.code = -32603;
throw err;
}
/* 성공시 code 를 200 이전에 사용하던 방식에 따라 */
if (200 == oJson.code)
{
let data = oJson.body;
if(payload.method == 'eth_requestAccounts') ttb.provider.selectedAddress = data.result;
resolve_func(data.result);
}
else
{
oJson.message = ttb.return_RPCerror_message(oJson.code);
let err = new ProviderRpcError(oJson.message);
err.code = oJson.code;
/* should throw error and return error object */
if ('function' == typeof reject_func) reject_func(err);
else throw err;
}
});
return promise;
};
class ProviderRpcError extends Error
{
constructor(...params)
{
super(...params);
this.code = -1;
this.data = null;
}
}
class ProviderMessage
{
constructor(type, data)
{
this._type = type;
this._data = data;
}
get type()
{
return this._type;
}
get data()
{
return this._data;
}
}
/* 나중에 다시 검토 */
class EthSubscription extends ProviderMessage
{
constructor(type, data)
{
super('eth_subscription', data);
}
}
ttb.provider.isMetaMask = true;/* true */
window.ethereum = ttb.provider;
window.ethereum.event = ttb.event;
window.web3 = {__isMetaMaskShim__ : true};
window.web3.currentProvider = ttb.provider;
/* ******************* Experimental API ******************************* */
/* ttb지갑의 경우는 무조건 unlocked 상태에서만 dapp 브라우저를 이용할수 있슴 */
ttb.provider._metamask = {};
ttb.provider._metamask.isUnlocked = async () => await true;
/* ******************* Legacy DEPRECATED Properties support ******************************* */
/* ttb.active_chain_id 와 같음 */
ttb.provider.chainId = ttb.active_chain_id;
/* ttb.provider getnetworkVersion */
ttb.provider.networkVersion = '1';
ttb.provider.selectedAddress = null;
/* ******************* Legacy DEPRECATED method support ******************************* */
ttb.provider.enable = async () =>
{
/*
legacy API를 이용하는 dapp은 항상 이 메소드를 먼저 호출 할 것이기에
필요한 properties를 여기서 호출 설정해 놓아야함
*/
/*let accounts = await ttb.provider.request({ method: 'eth_requestAccounts' });*/
ttb.provider.selectedAddress = await ttb.provider.request({ method: 'eth_accounts' });
ttb.provider.networkVersion = await ttb.provider.request({ method: 'net_version' });
/* 처음 연결시 notify가 발생한다면 아래 2라인 불필요 */
ttb.active_chain_id = await ttb.provider.request({ method: 'eth_chainId' });
ttb.provider.chainId = ttb.active_chain_id;
/*return accounts;*/
};
ttb.provider.isConnected = () => !ttb.is_null(ttb.active_chain_id);
/* Alias request */
ttb.provider.sendAsync = async (...params) =>
{
/* payload가 object가 아닐 경우 처리하지 않는다. */
if (!ttb.is_object(params[0]))
{
let err = new ProviderRpcError(`Cannot create property 'jsonrpc' on ${typeof payload} '${payload}'`);
throw err;
}
let bundle = {'actionType':'walletProvider'};
(params[0]?.params) ? bundle.params = params[0].params : bundle.params = [];
if(params[0]?.method) bundle.action = params[0].method;
console.log(params[0].method);
let result = await ttb.call_webview_object(bundle);
let oJson = JSON.parse(result);
if(ttb.is_function(params[1]))
{
if(oJson.code == 200)
{
params[1](null, oJson.body);
}
else
{
oJson.message = ttb.return_RPCerror_message(oJson.code);
let err = new ProviderRpcError(oJson.message);
err.code = oJson.code;
throw err;
}
}
else
return;
};
/* 3가지 호출 방법 처리 async*/
ttb.provider.send = async function(...params)
{
let bundle = {'actionType':'walletProvider'};
let hasCallback = 0;
if (params.length > 1 && ttb.is_object(params[0]) && ttb.is_function(params[1]))
{
/* void */
/* params 필수적? */
if(params[0]?.method)
{
bundle.action = params[0].method;
hasCallback = 1;
}
}
else if (params.length > 0 && ttb.is_string(params[0]))
{
if(ttb.is_function(params[1])) throw Error('The Maroo Ethereum provider does not support synchronous methods without a callback parameter');
/* return Promise<JsonRpcResponse> */
bundle.action = params[0];
if(ttb.is_array(params[1])) bundle.params = params[1];
}
else if (params.length > 0 && ttb.is_object(params[0]))
{
/* return unknown */
if(params[0]?.method) bundle.action = params[0].method;
}
(params[0]?.params && bundle?.params) ? bundle.params = params[0].params : bundle.params = [];
let oJson = JSON.parse(await ttb.call_webview_object(bundle));
/*oJson = oJson.body;*/
/* error속성을 가지고 있을 건지 아니면 code != 200으로 판단할 건지 에러 판별에 대한 논의 필요 */
if (hasCallback == 1)
{
if(oJson.code != 200) {
params[1](oJson.body, oJson.body);
oJson.message = ttb.return_RPCerror_message(oJson.code);
let err = new ProviderRpcError(oJson.message);
err.code = oJson.code;
throw err;
}else params[1](null, oJson.body);
}
else
return Promise.resolve(oJson.body);
};
console.log('Maroo provider injected');
}