-
Notifications
You must be signed in to change notification settings - Fork 617
/
index.js
151 lines (132 loc) · 4.13 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
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
import WalletConnect from '@walletconnect/browser'
import WalletConnectQRCodeModal from '@walletconnect/qrcode-modal'
import HookedWalletSubprovider from 'web3-provider-engine/subproviders/hooked-wallet'
export default class WalletConnectSubprovider extends HookedWalletSubprovider {
constructor (opts) {
super({
getAccounts: async cb => {
try {
const walletConnector = await this.getWalletConnector()
const accounts = walletConnector.accounts
if (accounts && accounts.length) {
cb(null, accounts)
} else {
cb(new Error('Failed to get accounts'))
}
} catch (error) {
cb(error)
}
},
processMessage: async (msgParams, cb) => {
try {
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signMessage(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processPersonalMessage: async (msgParams, cb) => {
try {
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signPersonalMessage(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processSignTransaction: async (txParams, cb) => {
try {
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signTransaction(txParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processTransaction: async (txParams, cb) => {
try {
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.sendTransaction(txParams)
cb(null, result)
} catch (error) {
cb(error)
}
},
processTypedMessage: async (msgParams, cb) => {
try {
const walletConnector = await this.getWalletConnector()
const result = await walletConnector.signTypedData(msgParams)
cb(null, result)
} catch (error) {
cb(error)
}
}
})
this.qrcode = typeof opts.qrcode === 'undefined' || opts.qrcode !== false
const bridge = opts.bridge || null
if (!bridge || typeof bridge !== 'string') {
throw new Error('Missing or Invalid bridge field')
}
this._walletConnector = new WalletConnect({ bridge })
this.isConnecting = false
this.connectCallbacks = []
}
set isWalletConnect (value) {}
get isWalletConnect () {
return true
}
set connected (value) {}
get connected () {
return this._walletConnector.connected
}
set uri (value) {}
get uri () {
return this._walletConnector.uri
}
set accounts (value) {}
get accounts () {
return this._walletConnector.accounts
}
onConnect (callback) {
this.connectCallbacks.push(callback)
}
triggerConnect (result) {
if (this.connectCallbacks && this.connectCallbacks.length) {
this.connectCallbacks.forEach(callback => callback(result))
}
}
getWalletConnector () {
return new Promise((resolve, reject) => {
const walletConnector = this._walletConnector
if (this.isConnecting) {
this.onConnect(_walletConnector => resolve(_walletConnector))
} else if (!walletConnector.connected) {
this.isConnecting = true
walletConnector
.createSession()
.then(() => {
if (this.qrcode) {
WalletConnectQRCodeModal.open(walletConnector.uri, () => {
reject(new Error('User closed WalletConnect modal'))
})
}
walletConnector.on('connect', () => {
if (this.qrcode) {
WalletConnectQRCodeModal.close()
}
this.isConnecting = false
this.triggerConnect(walletConnector)
resolve(walletConnector)
})
})
.catch(error => {
this.isConnecting = false
reject(error)
})
} else {
resolve(walletConnector)
}
})
}
}