-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
384 lines (346 loc) · 11.9 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
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
376
377
378
379
380
381
382
383
384
const { EventEmitter } = require('events')
const stcUtil = require('@starcoin/stc-util')
const HDKey = require('@starcoin/stc-hdkey')
const OneKeyConnect = require('./lib/@onekeyfe/js-sdk').default
const { encoding, utils } = require('@starcoin/starcoin')
const log = require('loglevel')
const hdPathString = `m/44'/101010'/0'/0'`
const keyringType = 'OneKey Hardware'
const pathBase = 'm'
const MAX_INDEX = 1000
const DELAY_BETWEEN_POPUPS = 1000
const ONEKEY_CONNECT_MANIFEST = {
email: '[email protected]',
appUrl: 'https://www.onekey.so',
}
class OneKeyKeyring extends EventEmitter {
constructor(opts = {}) {
super()
this.type = keyringType
this.accounts = []
this.hdk = new HDKey()
this.page = 0
this.perPage = 5
this.unlockedAccount = 0
this.paths = {}
this.accountPublicKeys = {}
this.persistAllKeyringsInsideOneKey = opts.persistAllKeyringsInsideOneKey || undefined
this.deserialize(opts)
OneKeyConnect.manifest(ONEKEY_CONNECT_MANIFEST)
}
serialize() {
return Promise.resolve({
hdPath: this.hdPath,
accounts: this.accounts,
page: this.page,
paths: this.paths,
perPage: this.perPage,
unlockedAccount: this.unlockedAccount,
accountPublicKeys: this.accountPublicKeys,
})
}
deserialize(opts = {}) {
this.hdPath = opts.hdPath || hdPathString
this.accounts = opts.accounts || []
this.page = opts.page || 0
this.perPage = opts.perPage || 5
this.accountPublicKeys = opts.accountPublicKeys || {}
this.paths = opts.paths || {}
return Promise.resolve()
}
isUnlocked() {
return Boolean(this.hdk && this.hdk.publicKey)
}
unlock() {
if (this.isUnlocked()) {
return Promise.resolve('already unlocked')
}
return new Promise((resolve, reject) => {
try {
const path = `${ this.hdPath }/0'`
OneKeyConnect.starcoinGetPublicKey({
path,
showOnDevice: false,
}).then((response) => {
if (response.success) {
this.hdk.publicKey = Buffer.from(response.payload.publicKey, 'hex')
// this.hdk.chainCode = Buffer.from(response.payload.chainCode, 'hex')
resolve('just unlocked')
} else {
reject(new Error((response.payload && response.payload.error) || 'Unknown error'))
}
}).catch((e) => {
reject(new Error((e && e.toString()) || 'Unknown error'))
})
} catch (e) {
reject(new Error((e && e.toString()) || 'Unknown error'))
}
})
}
setAccountToUnlock(index) {
this.unlockedAccount = parseInt(index, 10)
}
addAccounts(n = 1) {
return new Promise((resolve, reject) => {
this.unlock()
.then(async (_) => {
const from = this.unlockedAccount
const to = from + n
for (let i = from; i < to; i++) {
const address = await this._addressFromIndex(this.hdPath, i)
if (!this.accounts.includes(address)) {
this.accounts.push(address)
}
this.page = 0
}
resolve(this.accounts)
})
.catch((e) => {
reject(e)
})
})
}
getFirstPage() {
this.page = 0
return this.__getPage(1)
}
getNextPage() {
return this.__getPage(1)
}
getPreviousPage() {
return this.__getPage(-1)
}
__getPage(increment) {
this.page += increment
if (this.page <= 0) {
this.page = 1
}
return new Promise((resolve, reject) => {
this.unlock()
.then(async (_) => {
const from = (this.page - 1) * this.perPage
const to = from + this.perPage
const accounts = []
const bundle = []
for (let i = from; i < to; i++) {
bundle.push({ path: `${ this.hdPath }/${ i }'`, showOnDevice: false })
}
const addresses = await this._addressFromBundle(bundle)
addresses.forEach((item) => {
accounts.push({
address: item[0],
balance: null,
index: item[1],
})
this.paths[stcUtil.toChecksumAddress(item[0])] = item[1]
})
resolve(accounts)
})
.catch((e) => {
reject(e)
})
})
}
getAccounts() {
return Promise.resolve(this.accounts.slice())
}
removeAccount(address) {
if (!this.accounts.map((a) => a.toLowerCase()).includes(address.toLowerCase())) {
throw new Error(`Address ${ address } not found in this keyring`)
}
this.accounts = this.accounts.filter((a) => a.toLowerCase() !== address.toLowerCase())
}
// tx is an instance of the starcoin_types.RawUserTransaction.
signTransaction(address, tx) {
return new Promise((resolve, reject) => {
this.unlock()
.then((status) => {
setTimeout(async (_) => {
try {
OneKeyConnect.starcoinSignTransaction({
path: await this._pathFromAddress(address),
rawTx: stcUtil.stripHexPrefix(encoding.bcsEncode(tx)),
}).then((response) => {
if (response.success) {
const signature = stcUtil.addHexPrefix(response.payload.signature)
const public_key = stcUtil.addHexPrefix(response.payload.public_key)
const signedTx = utils.tx.signTxn(public_key, signature, tx)
const addressSignedWith = stcUtil.toChecksumAddress(encoding.addressFromSCS(tx.sender))
const correctAddress = stcUtil.toChecksumAddress(address)
if (addressSignedWith !== correctAddress) {
reject(new Error('签名的 OneKey 设备与绑定的 OneKey 账户不是同一个设备,请确认后重试!'))
}
const signedTxHex = encoding.bcsEncode(signedTx)
resolve(signedTxHex)
} else {
reject(new Error((response.payload && response.payload.error) || '1.Unknown error'))
}
}).catch((e) => {
reject(new Error((e && e.toString()) || '2.Unknown error'))
})
} catch (e) {
// 上面的 this._pathFromAddress 可能会扔出错误, 没有被catch, promise 就一直在pending
reject(new Error((e && e.toString()) || '3.Unknown error'))
}
// This is necessary to avoid popup collision
// between the unlock & sign onekey popups
}, status === 'just unlocked' ? DELAY_BETWEEN_POPUPS : 0)
}).catch((e) => {
reject(new Error((e && e.toString()) || '4.Unknown error'))
})
})
}
signMessage(withAccount, data) {
return this.signPersonalMessage(withAccount, data)
}
// For personal_sign, we need to prefix the message:
signPersonalMessage(withAccount, message) {
return new Promise((resolve, reject) => {
this.unlock()
.then((status) => {
setTimeout(async (_) => {
try {
OneKeyConnect.starcoinSignMessage({
path: await this._pathFromAddress(withAccount),
message,
}).then((response) => {
if (response.success) {
resolve(response.payload)
} else {
reject(new Error((response.payload && response.payload.error) || 'Unknown error'))
}
}).catch((e) => {
log.info('Error while trying to sign a message ', e)
reject(new Error((e && e.toString()) || 'Unknown error'))
})
} catch (e) {
reject(new Error((e && e.toString()) || 'Unknown error'))
}
// This is necessary to avoid popup collision
// between the unlock & sign onekey popups
}, status === 'just unlocked' ? DELAY_BETWEEN_POPUPS : 0)
}).catch((e) => {
log.info('Error while trying to sign a message ', e)
reject(new Error((e && e.toString()) || 'Unknown error'))
})
})
}
signTypedData() {
// Waiting on onekey to enable this
return Promise.reject(new Error('Not supported on this device'))
}
exportAccount() {
return Promise.reject(new Error('Not supported on this device'))
}
forgetDevice() {
this.accounts = []
this.hdk = new HDKey()
this.page = 0
this.unlockedAccount = 0
this.paths = {}
this.accountPublicKeys = {}
}
getPublicKeyFor(address, opts = {}) {
return new Promise(async (resolve, reject) => {
try {
const checksummedAddress = stcUtil.toChecksumAddress(address)
if (this.accountPublicKeys[checksummedAddress]) {
resolve(this.accountPublicKeys[checksummedAddress])
return
}
const path = await this._pathFromAddress(address)
OneKeyConnect.starcoinGetPublicKey({
path,
showOnDevice: false,
}).then((response) => {
if (response.success) {
const publicKey = stcUtil.addHexPrefix(response.payload.publicKey)
this.accountPublicKeys[checksummedAddress] = publicKey
if (this.persistAllKeyringsInsideOneKey) {
this.persistAllKeyringsInsideOneKey()
}
resolve(publicKey)
} else {
reject(new Error((response.payload && response.payload.error) || 'Unknown error 1'))
}
}).catch((e) => {
reject(new Error((e && e.toString()) || 'Unknown error 2'))
})
} catch (e) {
reject(new Error((e && e.toString()) || 'Unknown error 3'))
}
})
}
getReceiptIdentifier(address) {
return this.getPublicKeyFor(address).then((publicKey) => encoding.publicKeyToReceiptIdentifier(publicKey))
}
/* PRIVATE METHODS */
_normalize(buf) {
return stcUtil.bufferToHex(buf).toString()
}
// eslint-disable-next-line no-shadow
_addressFromBundle(bundle) {
return new Promise((resolve, reject) => {
try {
OneKeyConnect.starcoinGetAddress({
bundle,
}).then((response) => {
if (response.success) {
const addresses = response.payload.map((item, idx) => {
const arr = item.serializedPath.split('/')
const i = arr[arr.length - 1].split("'")[0]
return [item.address, parseInt(i, 10)]
})
resolve(addresses)
} else {
reject(new Error((response.payload && response.payload.error) || 'Unknown error'))
}
}).catch((e) => {
reject(new Error((e && e.toString()) || 'Unknown error'))
})
} catch (e) {
reject(new Error((e && e.toString()) || 'Unknown error'))
}
})
}
// eslint-disable-next-line no-shadow
_addressFromIndex(pathBase, i) {
return new Promise((resolve, reject) => {
try {
const path = `${ pathBase }/${ i }'`
OneKeyConnect.starcoinGetAddress({
path,
showOnDevice: false,
}).then((response) => {
if (response.success) {
resolve(stcUtil.toChecksumAddress(response.payload.address))
} else {
reject(new Error((response.payload && response.payload.error) || 'Unknown error'))
}
}).catch((e) => {
reject(new Error((e && e.toString()) || 'Unknown error'))
})
} catch (e) {
reject(new Error((e && e.toString()) || 'Unknown error'))
}
})
}
async _pathFromAddress(address) {
const checksummedAddress = stcUtil.toChecksumAddress(address)
let index = this.paths[checksummedAddress]
if (typeof index === 'undefined') {
for (let i = 0; i < MAX_INDEX; i++) {
if (checksummedAddress === await this._addressFromIndex(this.hdPath, i)) {
index = i
break
}
}
}
if (typeof index === 'undefined') {
throw new Error('Unknown address')
}
return `${ this.hdPath }/${ index }'`
}
}
OneKeyKeyring.type = keyringType
module.exports = OneKeyKeyring