-
Notifications
You must be signed in to change notification settings - Fork 621
/
index.js
114 lines (97 loc) · 2.52 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
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
import WalletConnect from 'walletconnect'
export default class WalletConnectSubprovider {
constructor(opts) {
const bridgeUrl = opts.bridgeUrl || null
if (!bridgeUrl || typeof bridgeUrl !== 'string') {
throw new Error('Missing or Invalid bridgeUrl field')
}
const dappName = opts.dappName || null
if (!dappName || typeof dappName !== 'string') {
throw new Error('Missing or Invalid dappName field')
}
this.webConnector = new WalletConnect(opts)
this.initSession()
}
set isWalletConnect(value) {
return
}
get isWalletConnect() {
return true
}
set isConnected(value) {
return
}
get isConnected() {
return this.webConnector.isConnected
}
set uri(value) {
return
}
get uri() {
return this.webConnector.uri
}
set accounts(value) {
return
}
get accounts() {
return this.webConnector.accounts
}
setEngine(engine) {
const self = this
self.engine = engine
engine.on('block', function(block) {
self.currentBlock = block
})
}
emitPayload(payload, cb) {
const self = this
const _payload = this.webConnector.formatPayload(payload)
self.engine.sendAsync(_payload, cb)
}
async initSession() {
const session = await this.webConnector.initSession()
return session
}
async getAccounts() {
const accounts = await this.walletconnect.getAccounts()
return accounts
}
async listenSessionStatus() {
const result = await this.webConnector.listenSessionStatus()
return result
}
handleRequest(payload, next, end) {
this.provider.sendAsync(payload, function(err, response) {
if (err) return end(err)
if (response.error) return end(new Error(response.error.message))
end(null, response.result)
})
const supportedMethods = [
'eth_accounts',
'eth_signTransaction',
'eth_sendTransaction',
'eth_sendRawTransaction',
'eth_sign',
'eth_signTypedData',
'personal_sign'
]
if (this.webConnector.isConnected) {
if (payload.method === 'eth_accounts') {
this.getAccounts()
.then(accounts => {
end(null, accounts)
})
.catch(err => end(err))
} else if (supportedMethods.includes(payload.method)) {
this.webConnector
.createCall(payload)
.then(result => end(null, result))
.catch(err => end(err))
} else {
next(payload)
}
} else {
throw new Error('WalletConnect connection is not established')
}
}
}