It is a project for Bytom Chrome extension JS SDK https://bytom.github.io/Bytom-JS-SDK
リビジョン | d280896c0bc3be49ed5083c79a2f9a84a6272547 (tree) |
---|---|
日時 | 2020-09-09 15:40:47 |
作者 | Zhiting Lin <zlin035@uott...> |
コミッター | Zhiting Lin |
update error code
@@ -1,47 +1,8 @@ | ||
1 | -import {getDB} from '../db/db'; | |
2 | -import {createAccount, createAccountReceiver, signTransaction} from '../wasm/func'; | |
3 | -import {handleApiError, handleAxiosError} from '../utils/http'; | |
4 | - | |
5 | - | |
6 | 1 | function accountsSDK(bytom){ |
7 | 2 | this.http = bytom.serverHttp; |
8 | 3 | this.bytom = bytom; |
9 | 4 | } |
10 | 5 | |
11 | -/** | |
12 | - * List of the account. | |
13 | - * | |
14 | - * @returns {Promise} List of Accounts | |
15 | - */ | |
16 | -accountsSDK.prototype.listAccountUseServer = function() { | |
17 | - let net = 'mainnet'; | |
18 | - // let net = 'testnet'; | |
19 | - let retPromise = new Promise((resolve, reject) => { | |
20 | - getDB().then(db => { | |
21 | - let transaction = db.transaction(['accounts-server'], 'readonly'); | |
22 | - let objectStore = transaction.objectStore('accounts-server').index('net'); | |
23 | - let keyRange = IDBKeyRange.only(net); | |
24 | - let oc = objectStore.openCursor(keyRange); | |
25 | - let ret = []; | |
26 | - | |
27 | - oc.onsuccess = function (event) { | |
28 | - var cursor = event.target.result; | |
29 | - if (cursor) { | |
30 | - ret.push(cursor.value); | |
31 | - cursor.continue(); | |
32 | - } else { | |
33 | - resolve(ret); | |
34 | - } | |
35 | - }; | |
36 | - oc.onerror = function(e){ | |
37 | - reject(e); | |
38 | - }; | |
39 | - }).catch(error => { | |
40 | - reject(error); | |
41 | - }); | |
42 | - }); | |
43 | - return retPromise; | |
44 | -}; | |
45 | 6 | |
46 | 7 | |
47 | 8 | /** |
@@ -55,43 +16,6 @@ accountsSDK.prototype.listAddressUseServer = function(address) { | ||
55 | 16 | let net = this.bytom.net; |
56 | 17 | return this.http.request(`account/address?address=${address}`, '', net, 'GET'); |
57 | 18 | }; |
58 | -/** | |
59 | - * Create a new address for a wallet. | |
60 | - * | |
61 | - * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmaccountnew-address | |
62 | - * @param {String} guid unique id for each wallet | |
63 | - * @param {String} label alias for the address to be created | |
64 | - * @returns {Promise} | |
65 | - */ | |
66 | -accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) { | |
67 | - let net = this.bytom.net; | |
68 | - let retPromise = new Promise((resolve, reject) => { | |
69 | - let pm = {guid: guid}; | |
70 | - if (label) { | |
71 | - pm.label = label; | |
72 | - } | |
73 | - this.http.request('account/new-address', pm, net).then(resp => { | |
74 | - let dbData = resp; | |
75 | - dbData.guid = guid; | |
76 | - dbData.net = net; | |
77 | - getDB().then(db => { | |
78 | - let transaction = db.transaction(['addresses-server'], 'readwrite'); | |
79 | - let objectStore = transaction.objectStore('addresses-server'); | |
80 | - delete dbData.rootXPub; | |
81 | - let request = objectStore.add(dbData); | |
82 | - request.onsuccess = function() { | |
83 | - resolve(dbData); | |
84 | - }; | |
85 | - request.onerror = function() { | |
86 | - reject(request.error); | |
87 | - }; | |
88 | - }); | |
89 | - }).catch(error => { | |
90 | - reject(handleAxiosError(error)); | |
91 | - }); | |
92 | - }); | |
93 | - return retPromise; | |
94 | -}; | |
95 | 19 | |
96 | 20 | /** |
97 | 21 | * Create a wallet using a public key. Each wallet is identified by a guid. (by server) |
@@ -114,72 +38,6 @@ accountsSDK.prototype.createNewAccount = function(rootXPub, label) { | ||
114 | 38 | |
115 | 39 | /** |
116 | 40 | * Create a wallet using a public key. Each wallet is identified by a guid. (by server) |
117 | - * | |
118 | - * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints | |
119 | - * @param {String} rootXPub | |
120 | - * @param {String} alias alias for the account | |
121 | - * @param {String} label alias for the first address | |
122 | - * @returns {Promise} | |
123 | - */ | |
124 | -accountsSDK.prototype.createAccountUseServer = function(rootXPub, alias, label) { | |
125 | - let net = this.bytom.net; | |
126 | - let that = this; | |
127 | - let retPromise = new Promise((resolve, reject) => { | |
128 | - getDB().then(db => { | |
129 | - let getRequest = db.transaction(['accounts-server'], 'readonly') | |
130 | - .objectStore('accounts-server') | |
131 | - .index('alias') | |
132 | - .get(alias); | |
133 | - getRequest.onsuccess = function(e) { | |
134 | - if (e.target.result) { | |
135 | - reject(new Error('duplicate account alias')); | |
136 | - return; | |
137 | - } | |
138 | - let pm = {pubkey: rootXPub}; | |
139 | - if (label) { | |
140 | - pm.label = label; | |
141 | - } | |
142 | - that.http.request('account/create', pm, net).then(resp => { | |
143 | - let dbData = resp; | |
144 | - dbData.rootXPub = rootXPub; | |
145 | - dbData.alias = alias; | |
146 | - dbData.net = net; | |
147 | - getDB().then(db => { | |
148 | - let transaction = db.transaction(['accounts-server'], 'readwrite'); | |
149 | - let objectStore = transaction.objectStore('accounts-server'); | |
150 | - let request = objectStore.add(dbData); | |
151 | - request.onsuccess = function() { | |
152 | - let transaction = db.transaction(['addresses-server'], 'readwrite'); | |
153 | - let objectStore = transaction.objectStore('addresses-server'); | |
154 | - delete dbData.rootXPub; | |
155 | - let request = objectStore.add(dbData); | |
156 | - request.onsuccess = function() { | |
157 | - resolve(dbData); | |
158 | - }; | |
159 | - request.onerror = function() { | |
160 | - reject(request.error); | |
161 | - }; | |
162 | - }; | |
163 | - request.onerror = function() { | |
164 | - reject(request.error); | |
165 | - }; | |
166 | - }); | |
167 | - }).catch(error => { | |
168 | - reject(handleAxiosError(error)); | |
169 | - }); | |
170 | - }; | |
171 | - getRequest.onerror = function() { | |
172 | - reject(getRequest.error); | |
173 | - }; | |
174 | - }).catch(err => { | |
175 | - reject(err); | |
176 | - }); | |
177 | - }); | |
178 | - return retPromise; | |
179 | -}; | |
180 | - | |
181 | -/** | |
182 | - * Create a wallet using a public key. Each wallet is identified by a guid. (by server) | |
183 | 41 | * |
184 | 42 | * @param {String} guid |
185 | 43 | * @param {String} coin type of coin |
@@ -195,205 +53,5 @@ accountsSDK.prototype.copyAccountUseServer = function(guid, coin) { | ||
195 | 53 | return this.http.request('account/copy', pm, net); |
196 | 54 | }; |
197 | 55 | |
198 | -/** | |
199 | - * list a wallet using a guid. Each wallet is identified by a guid. (by server) | |
200 | - * | |
201 | - * @param {String} guid | |
202 | - * @param {String} coin type of coin | |
203 | - * @returns {Promise} | |
204 | - */ | |
205 | -accountsSDK.prototype.listVaporAccountUseServer = function(guid) { | |
206 | - let net = this.bytom.net; | |
207 | - let that = this; | |
208 | - let retPromise = new Promise((resolve, reject) => { | |
209 | - that.http.request('account/addresses', {address:address}, net).then(resp => { | |
210 | - getDB().then(db => { | |
211 | - let objectStore = db.transaction(['accounts-server'], 'readwrite').objectStore('accounts-server'); | |
212 | - let index = objectStore.index('guid'); | |
213 | - let keyRange = IDBKeyRange.only(guid); | |
214 | - let getRequest = index.openCursor(keyRange); | |
215 | - | |
216 | - getRequest.onsuccess = function(e) { | |
217 | - const cursor = e.target.result; | |
218 | - if(cursor){ | |
219 | - const accountObject = cursor.value; | |
220 | - | |
221 | - accountObject.vpAddress = resp[0].address; | |
222 | - const request = cursor.update(accountObject); | |
223 | - request.onsuccess = function () { | |
224 | - resolve(accountObject); | |
225 | - }; | |
226 | - request.onerror = function () { | |
227 | - reject(request.error); | |
228 | - }; | |
229 | - } | |
230 | - }; | |
231 | - getRequest.onerror = function() { | |
232 | - reject(getRequest.error); | |
233 | - }; | |
234 | - }).catch(err => { | |
235 | - throw (err); | |
236 | - }); | |
237 | - }).catch(err => { | |
238 | - reject(err); | |
239 | - }); | |
240 | - }); | |
241 | - return retPromise; | |
242 | -}; | |
243 | - | |
244 | -/** | |
245 | - * create account | |
246 | - * | |
247 | - * @param {String} alias | |
248 | - * @param {Int} quorum | |
249 | - * @param {String} rootXPub | |
250 | - * @returns {Promise} | |
251 | - */ | |
252 | -accountsSDK.prototype.createAccount = function(alias, quorum, rootXPub) { | |
253 | - let retPromise = new Promise((resolve, reject) => { | |
254 | - getDB().then(db => { | |
255 | - let transaction = db.transaction(['accounts'], 'readwrite'); | |
256 | - let objectStore = transaction.objectStore('accounts'); | |
257 | - let request = objectStore.add({ | |
258 | - alias:alias, | |
259 | - }); | |
260 | - request.onsuccess = function () { | |
261 | - let data = {alias:alias, quorum:quorum, rootXPub:rootXPub, nextIndex:request.result}; | |
262 | - createAccount(data).then(res => { | |
263 | - let jsonData = JSON.parse(res.data); | |
264 | - let putTransaction = db.transaction(['accounts'], 'readwrite'); | |
265 | - let putObjectStore = putTransaction.objectStore('accounts'); | |
266 | - let putRequest = putObjectStore.put(jsonData, request.result); | |
267 | - putRequest.onsuccess = function() { | |
268 | - resolve(jsonData); | |
269 | - }; | |
270 | - putRequest.onerror = function() { | |
271 | - reject(putRequest.error); | |
272 | - }; | |
273 | - }).catch(error => { | |
274 | - reject(error); | |
275 | - }); | |
276 | - }; | |
277 | - request.onerror = function() { | |
278 | - reject(request.error); | |
279 | - }; | |
280 | - }).catch(error => { | |
281 | - reject(error); | |
282 | - }); | |
283 | - }); | |
284 | - return retPromise; | |
285 | -}; | |
286 | - | |
287 | -/** | |
288 | - * create account address | |
289 | - * | |
290 | - * @param {Object} account createAccount return account Object val | |
291 | - * @param {Int} nextIndex | |
292 | - * @returns {Promise} | |
293 | - */ | |
294 | -accountsSDK.prototype.createAccountReceiver = function(account) { | |
295 | - let retPromise = new Promise((resolve, reject) => { | |
296 | - getDB().then(db => { | |
297 | - let transaction = db.transaction(['addresses'], 'readwrite'); | |
298 | - let objectStore = transaction.objectStore('addresses'); | |
299 | - let request = objectStore.add({ | |
300 | - account_id:account.id, | |
301 | - account_alias:account.alias, | |
302 | - }); | |
303 | - request.onsuccess = function() { | |
304 | - let data = {account:JSON.stringify(account), nextIndex: request.result}; | |
305 | - createAccountReceiver(data).then(res => { | |
306 | - let jsonData = JSON.parse(res.data); | |
307 | - let jsonDB = JSON.parse(res.db); | |
308 | - let putTransaction = db.transaction(['addresses'], 'readwrite'); | |
309 | - let putObjectStore = putTransaction.objectStore('addresses'); | |
310 | - let putRequest = null; | |
311 | - for (let key in jsonDB) { | |
312 | - if(!jsonDB.hasOwnProperty(key)) continue; | |
313 | - let putData = JSON.parse(jsonDB[key]); | |
314 | - putRequest = putObjectStore.put(putData, request.result); | |
315 | - } | |
316 | - putRequest.onsuccess = function() { | |
317 | - resolve(jsonData); | |
318 | - }; | |
319 | - putRequest.onerror = function() { | |
320 | - reject(putRequest.error); | |
321 | - }; | |
322 | - }).catch(error => { | |
323 | - reject(error); | |
324 | - }); | |
325 | - }; | |
326 | - request.onerror = function() { | |
327 | - reject(request.error); | |
328 | - }; | |
329 | - }).catch(error => { | |
330 | - reject(error); | |
331 | - }); | |
332 | - }); | |
333 | - return retPromise; | |
334 | -}; | |
335 | - | |
336 | -/** | |
337 | - * create account address | |
338 | - * | |
339 | - * @param {Object} account createAccount return account Object val | |
340 | - * @param {Int} nextIndex | |
341 | - * @returns {Promise} | |
342 | - */ | |
343 | -accountsSDK.prototype.getAccountXpubById = function(guid) { | |
344 | - let retPromise = new Promise((resolve, reject) => { | |
345 | - getDB().then(db => { | |
346 | - let getRequest = db.transaction(['accounts-server'], 'readonly') | |
347 | - .objectStore('accounts-server') | |
348 | - .index('guid') | |
349 | - .get(guid); | |
350 | - getRequest.onsuccess = function(e) { | |
351 | - if (!e.target.result) { | |
352 | - reject(new Error('not found guid')); | |
353 | - return; | |
354 | - } | |
355 | - resolve(e.target.result.rootXPub); | |
356 | - }; | |
357 | - getRequest.onerror = function() { | |
358 | - reject(getRequest.error); | |
359 | - }; | |
360 | - }).catch(error => { | |
361 | - reject(error); | |
362 | - }); | |
363 | - }); | |
364 | - return retPromise; | |
365 | -}; | |
366 | - | |
367 | - | |
368 | -/** | |
369 | - * create account address | |
370 | - * | |
371 | - * @param {Object} account createAccount return account Object val | |
372 | - * @param {Int} nextIndex | |
373 | - * @returns {Promise} | |
374 | - */ | |
375 | -accountsSDK.prototype.getAccountXpubByAddress = function(guid) { | |
376 | - let retPromise = new Promise((resolve, reject) => { | |
377 | - getDB().then(db => { | |
378 | - let getRequest = db.transaction(['accounts-server'], 'readonly') | |
379 | - .objectStore('accounts-server') | |
380 | - .index('guid') | |
381 | - .get(guid); | |
382 | - getRequest.onsuccess = function(e) { | |
383 | - if (!e.target.result) { | |
384 | - reject(new Error('not found guid')); | |
385 | - return; | |
386 | - } | |
387 | - resolve(e.target.result.rootXPub); | |
388 | - }; | |
389 | - getRequest.onerror = function() { | |
390 | - reject(getRequest.error); | |
391 | - }; | |
392 | - }).catch(error => { | |
393 | - reject(error); | |
394 | - }); | |
395 | - }); | |
396 | - return retPromise; | |
397 | -}; | |
398 | 56 | |
399 | 57 | export default accountsSDK; |
\ No newline at end of file |
@@ -1,123 +1,15 @@ | ||
1 | -import { createKey ,resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func'; | |
2 | -import {getDB} from '../db/db'; | |
3 | 1 | import {createkey, isValidMnemonic} from '../utils/key/createKey'; |
4 | -import {encryptKey, decryptKey} from '../utils/key/keystore'; | |
5 | -import { restoreFromKeyStore } from '../utils/account'; | |
2 | +import { decryptKey} from '../utils/key/keystore'; | |
6 | 3 | import { camelize } from '../utils/utils'; |
7 | 4 | import CryptoJS from 'crypto-js'; |
8 | 5 | import {signMessage as signMJs} from '../utils/transaction/signMessage'; |
9 | -import transactionSDK from './transaction'; | |
6 | +import Error from '../utils/error'; | |
10 | 7 | |
11 | 8 | |
12 | 9 | function keysSDK() { |
13 | 10 | } |
14 | 11 | |
15 | 12 | /** |
16 | - * reset key password | |
17 | - * | |
18 | - * @param {String}} rootXPub | |
19 | - * @param {String} oldPassword | |
20 | - * @param {String} newPassword | |
21 | - */ | |
22 | -keysSDK.prototype.resetKeyPassword = function(rootXPub, oldPassword, newPassword) { | |
23 | - let retPromise = new Promise((resolve, reject) => { | |
24 | - let data = {rootXPub: rootXPub, oldPassword:oldPassword, newPassword:newPassword}; | |
25 | - resetKeyPassword(data).then(res => { | |
26 | - getDB().then(db => { | |
27 | - let objectStore = db.transaction(['keys'], 'readwrite').objectStore('keys'); | |
28 | - let index = objectStore.index('xpub'); | |
29 | - let keyRange = IDBKeyRange.only(rootXPub); | |
30 | - let getRequest = index.openCursor(keyRange); | |
31 | - getRequest.onsuccess = function (event) { | |
32 | - const cursor = event.target.result; | |
33 | - if(cursor && cursor.value.xpub === rootXPub) { | |
34 | - const updateData = cursor.value; | |
35 | - updateData.key = res.data; | |
36 | - const request = cursor.update(updateData); | |
37 | - request.onsuccess = function() { | |
38 | - resolve(true); | |
39 | - }; | |
40 | - request.onerror = function() { | |
41 | - reject(new Error('db update error')); | |
42 | - }; | |
43 | - } else { | |
44 | - reject(new Error('db update error: not found by rootXPub')); | |
45 | - } | |
46 | - }; | |
47 | - getRequest.onerror = function () { | |
48 | - reject(new Error('db get error')); | |
49 | - }; | |
50 | - }).catch(error => { | |
51 | - reject(error); | |
52 | - }); | |
53 | - }).catch(error => { | |
54 | - reject(error); | |
55 | - }); | |
56 | - }); | |
57 | - return retPromise; | |
58 | -}; | |
59 | - | |
60 | -/** | |
61 | - * get key by XPub | |
62 | - * | |
63 | - * @param {String} xpub | |
64 | - */ | |
65 | -keysSDK.prototype.getKeyByXPub = function(xpub) { | |
66 | - let retPromise = new Promise((resolve, reject) => { | |
67 | - getDB().then(db => { | |
68 | - let getRequest = db.transaction(['keys'], 'readonly') | |
69 | - .objectStore('keys') | |
70 | - .index('xpub') | |
71 | - .get(xpub); | |
72 | - getRequest.onsuccess = function(e) { | |
73 | - if(e.target.result) { | |
74 | - resolve(e.target.result.key); | |
75 | - } else { | |
76 | - reject(new Error('not found by XPub')); | |
77 | - } | |
78 | - }; | |
79 | - getRequest.onerror = function() { | |
80 | - reject(new Error('db get error')); | |
81 | - }; | |
82 | - }).catch(error => { | |
83 | - reject(error); | |
84 | - }); | |
85 | - }); | |
86 | - return retPromise; | |
87 | -}; | |
88 | - | |
89 | -/** | |
90 | - * List key | |
91 | - * | |
92 | - * @returns {Promise} | |
93 | - */ | |
94 | -keysSDK.prototype.list = function() { | |
95 | - let retPromise = new Promise((resolve, reject) => { | |
96 | - getDB().then(db => { | |
97 | - let transaction = db.transaction(['keys'], 'readonly'); | |
98 | - let objectStore = transaction.objectStore('keys'); | |
99 | - let oc = objectStore.openCursor(); | |
100 | - let ret = []; | |
101 | - oc.onsuccess = function (event) { | |
102 | - var cursor = event.target.result; | |
103 | - if (cursor) { | |
104 | - ret.push({alias: cursor.value.alias, xpub: cursor.value.xpub}); | |
105 | - cursor.continue(); | |
106 | - } else { | |
107 | - resolve(ret); | |
108 | - } | |
109 | - }; | |
110 | - oc.onerror = function(e){ | |
111 | - reject(e); | |
112 | - }; | |
113 | - }).catch(err => { | |
114 | - reject(err); | |
115 | - }); | |
116 | - }); | |
117 | - return retPromise; | |
118 | -}; | |
119 | - | |
120 | -/** | |
121 | 13 | * Create a new key. |
122 | 14 | * |
123 | 15 | * @param {String} alias - User specified, unique identifier. |
@@ -231,7 +123,7 @@ keysSDK.prototype.isValidKeystore = function( keystore ) { | ||
231 | 123 | |
232 | 124 | if(keys){ |
233 | 125 | if(keys.length>1){ |
234 | - throw 'do not support multiple keystore imported.'; | |
126 | + throw new Error('do not support multiple keystore imported.', 'BTM3004'); | |
235 | 127 | } |
236 | 128 | else if(keys.length === 1){ |
237 | 129 | key = keys[0]; |
@@ -268,100 +160,6 @@ keysSDK.prototype.verifyPassword = function(keystore, password) { | ||
268 | 160 | }; |
269 | 161 | |
270 | 162 | /** |
271 | - * Create a new key. | |
272 | - * | |
273 | - * @param {String} alias - User specified, unique identifier. | |
274 | - * @param {String} password - User specified, key password. | |
275 | - */ | |
276 | -keysSDK.prototype.create = function(alias, password) { | |
277 | - var normalizedAlias = alias.toLowerCase().trim(); | |
278 | - let retPromise = new Promise((resolve, reject) => { | |
279 | - getDB().then(db => { | |
280 | - let getRequest = db.transaction(['keys'], 'readonly') | |
281 | - .objectStore('keys') | |
282 | - .index('alias') | |
283 | - .get(normalizedAlias); | |
284 | - getRequest.onsuccess = function (e) { | |
285 | - if (e.target.result) { | |
286 | - reject(new Error('key alias already exists')); | |
287 | - return; | |
288 | - } | |
289 | - let data = {}; | |
290 | - data.alias = normalizedAlias; | |
291 | - data.auth = password; | |
292 | - createKey(data).then((res) => { | |
293 | - let jsonData = JSON.parse(res.data); | |
294 | - let dbData = { | |
295 | - key:res.data, | |
296 | - xpub:jsonData.xpub, | |
297 | - alias:alias, | |
298 | - }; | |
299 | - let request = db.transaction(['keys'], 'readwrite') | |
300 | - .objectStore('keys') | |
301 | - .add(dbData); | |
302 | - request.onsuccess = function () { | |
303 | - resolve({xpub:jsonData.xpub, alias: alias}); | |
304 | - }; | |
305 | - request.onerror = function () { | |
306 | - reject(new Error('db insert error')); | |
307 | - }; | |
308 | - }).catch(error => { | |
309 | - reject(error); | |
310 | - }); | |
311 | - }; | |
312 | - getRequest.onerror = function () { | |
313 | - reject(new Error('db get error')); | |
314 | - }; | |
315 | - }).catch(error => { | |
316 | - reject(error); | |
317 | - }); | |
318 | - }); | |
319 | - return retPromise; | |
320 | -}; | |
321 | - | |
322 | - | |
323 | -/** | |
324 | - * Create a new key. | |
325 | - * | |
326 | - * @param {String} xpub - xpub. | |
327 | - */ | |
328 | -keysSDK.prototype.createPubkey = function(xpub) { | |
329 | - let retPromise = new Promise((resolve, reject) => { | |
330 | - let data = {}; | |
331 | - data.xpub = xpub; | |
332 | - data.seed = 1; | |
333 | - createPubkey(data).then((res) => { | |
334 | - let jsonData = JSON.parse(res.data); | |
335 | - resolve(jsonData); | |
336 | - }).catch(error => { | |
337 | - reject(error); | |
338 | - }); | |
339 | - }); | |
340 | - return retPromise; | |
341 | -}; | |
342 | - | |
343 | -/** | |
344 | - * Sign Message. | |
345 | - * | |
346 | - * @param {String} message - message. | |
347 | - * @param {String} password - password. | |
348 | - * @param {Object} address - address. | |
349 | - */ | |
350 | -keysSDK.prototype.signMessage = function(message, password, keystore) { | |
351 | - let data = {}; | |
352 | - data.message = message; | |
353 | - data.password = password; | |
354 | - data.key = keystore; | |
355 | - return signMessage(data).then((res) => { | |
356 | - let jsonData = JSON.parse(res.data); | |
357 | - return (jsonData); | |
358 | - }).catch(error => { | |
359 | - throw (error); | |
360 | - }); | |
361 | - | |
362 | -}; | |
363 | - | |
364 | -/** | |
365 | 163 | * Sign Message. |
366 | 164 | * |
367 | 165 | * @param {String} message - message. |
@@ -1,8 +1,6 @@ | ||
1 | -import {convertArgument, signTransaction} from '../wasm/func'; | |
2 | -import { handleApiError, handleAxiosError } from '../utils/http'; | |
3 | -import { getDB } from '../db/db'; | |
4 | 1 | import {signTransaction as signJs} from '../utils/transaction/signTransaction'; |
5 | 2 | import { camelize } from '../utils/utils'; |
3 | +import {convertArgument} from '../utils/convertArguement'; | |
6 | 4 | |
7 | 5 | |
8 | 6 | function transactionSDK(bytom) { |
@@ -209,57 +207,6 @@ transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, f | ||
209 | 207 | return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net); |
210 | 208 | }; |
211 | 209 | |
212 | -/** | |
213 | - * sign transaction | |
214 | - * @param {String} guid | |
215 | - * @param {String} transaction | |
216 | - * @param {String} password | |
217 | - * @returns {Object} signed data | |
218 | - */ | |
219 | -transactionSDK.prototype.signTransaction = function(guid, transaction, password, getKeyByXPub) { | |
220 | - let bytom = this.bytom; | |
221 | - let retPromise = new Promise((resolve, reject) => { | |
222 | - getDB().then(db => { | |
223 | - let getRequest = db.transaction(['accounts-server'], 'readonly') | |
224 | - .objectStore('accounts-server') | |
225 | - .index('guid') | |
226 | - .get(guid); | |
227 | - getRequest.onsuccess = function(e) { | |
228 | - if (!e.target.result) { | |
229 | - reject(new Error('not found guid')); | |
230 | - return; | |
231 | - } | |
232 | - const res = getKeyByXPub(e.target.result.rootXPub)[0]; | |
233 | - if(!res){ | |
234 | - reject('not found xpub'); | |
235 | - }else{ | |
236 | - const key = res.key; | |
237 | - let pm = {transaction: transaction, password: password, key: key}; | |
238 | - signTransaction(pm).then(res => { | |
239 | - resolve(JSON.parse(res.data)); | |
240 | - }).catch(err => { | |
241 | - reject(err); | |
242 | - }); | |
243 | - } | |
244 | - }; | |
245 | - getRequest.onerror = function() { | |
246 | - reject(getRequest.error); | |
247 | - }; | |
248 | - }).catch(error => { | |
249 | - reject(error); | |
250 | - }); | |
251 | - }); | |
252 | - return retPromise; | |
253 | -}; | |
254 | - | |
255 | -transactionSDK.prototype._signTransaction = function( transaction, password, key) { | |
256 | - let pm = {transaction: transaction, password: password, key: key}; | |
257 | - return signTransaction(pm).then(res => { | |
258 | - return JSON.parse(res.data); | |
259 | - }).catch(err => { | |
260 | - throw (err); | |
261 | - }); | |
262 | -}; | |
263 | 210 | |
264 | 211 | transactionSDK.prototype._signTransactionJs = function( transaction, password, key) { |
265 | 212 | let tx = camelize(JSON.parse(transaction)); |
@@ -292,14 +239,16 @@ transactionSDK.prototype.convertArgument = function(type, value) { | ||
292 | 239 | let data = {}; |
293 | 240 | data.type = type; |
294 | 241 | data.raw_data = JSON.stringify({value}); |
295 | - convertArgument(data).then((res) => { | |
296 | - let jsonData = JSON.parse(res.data); | |
297 | - resolve(jsonData); | |
298 | - }).catch(error => { | |
242 | + try{ | |
243 | + const result = convertArgument(data).data | |
244 | + resolve(result); | |
245 | + } | |
246 | + catch(error){ | |
299 | 247 | reject(error); |
300 | - }); | |
248 | + } | |
301 | 249 | }); |
302 | 250 | return retPromise; |
303 | 251 | }; |
304 | 252 | |
253 | + | |
305 | 254 | export default transactionSDK; |
\ No newline at end of file |
@@ -1,58 +1,10 @@ | ||
1 | -import {initDB, getDB} from '../db/db'; | |
2 | 1 | import { restoreFromKeyStore } from '../utils/account'; |
3 | -import accountsSDK from './accounts'; | |
4 | 2 | |
5 | 3 | function walletSDK(bytom) { |
6 | 4 | this.http = bytom.serverHttp; |
7 | 5 | this.bytom = bytom; |
8 | 6 | } |
9 | 7 | |
10 | -let backupDBList = ['keys', 'accounts-server']; | |
11 | - | |
12 | -/** | |
13 | - * backup wallet. | |
14 | - */ | |
15 | -walletSDK.prototype.backup = function() { | |
16 | - let retPromise = new Promise((resolve, reject) => { | |
17 | - initDB().then(() =>{ | |
18 | - let walletImage = { | |
19 | - key_images:{ xkeys:[] }, | |
20 | - account_image:{ assets:[] }, | |
21 | - asset_image:{ slices:[] } | |
22 | - }; | |
23 | - | |
24 | - | |
25 | - let promiseList = []; | |
26 | - for(let dbName of backupDBList){ | |
27 | - promiseList.push(backupDB(dbName)); | |
28 | - } | |
29 | - | |
30 | - Promise.all([promiseList]).then(([keyData, AccountData]) =>{ | |
31 | - walletImage['key_images']['xkeys'] = keyData.map(a => JSON.parse(a.key)); | |
32 | - walletImage['account_image']['assets'] = AccountData.map(a => { | |
33 | - return { | |
34 | - 'account':{ | |
35 | - 'type':'account', | |
36 | - 'xpubs':[ | |
37 | - a.rootXPub | |
38 | - ], | |
39 | - 'quorum':1, | |
40 | - 'id':'byone-', | |
41 | - 'alias':a.alias, | |
42 | - 'keyIndex':1 | |
43 | - },'contractIndex':1 | |
44 | - }; | |
45 | - }); | |
46 | - | |
47 | - resolve(JSON.stringify(walletImage)); | |
48 | - }).catch(error => { | |
49 | - reject(error); | |
50 | - }); | |
51 | - }); | |
52 | - }); | |
53 | - return retPromise; | |
54 | -}; | |
55 | - | |
56 | 8 | walletSDK.prototype.list = function(pubkey) { |
57 | 9 | let net = this.bytom.net; |
58 | 10 | let pm = {pubkey}; |
@@ -87,62 +39,4 @@ walletSDK.prototype.restore = function(keystore, password) { | ||
87 | 39 | |
88 | 40 | }; |
89 | 41 | |
90 | -function backupDB(dbname) { | |
91 | - let ret = new Promise((resolve, reject) => { | |
92 | - getDB().then(db => { | |
93 | - | |
94 | - let transaction = db.transaction([dbname], 'readonly'); | |
95 | - let objectStore = transaction.objectStore(dbname); | |
96 | - let oc = objectStore.openCursor(); | |
97 | - let data = []; | |
98 | - oc.onsuccess = function (event) { | |
99 | - var cursor = event.target.result; | |
100 | - if (cursor) { | |
101 | - data.push(cursor.value); | |
102 | - cursor.continue(); | |
103 | - } else { | |
104 | - resolve(data); | |
105 | - } | |
106 | - }; | |
107 | - oc.onerror = function(e){ | |
108 | - reject(e); | |
109 | - }; | |
110 | - }).catch(err => { | |
111 | - reject(err); | |
112 | - }); | |
113 | - }); | |
114 | - return ret; | |
115 | -} | |
116 | - | |
117 | -function restoreDB(dbname, data) { | |
118 | - let ret = new Promise((resolve, reject) => { | |
119 | - getDB().then(db => { | |
120 | - let index = 0; | |
121 | - let errList = []; | |
122 | - batchAdd(); | |
123 | - | |
124 | - function batchAdd() { | |
125 | - if (index >= data.length) { | |
126 | - let r = {name: dbname, err: errList}; | |
127 | - resolve(r); | |
128 | - return; | |
129 | - } | |
130 | - let transaction = db.transaction([dbname], 'readwrite'); | |
131 | - let objectStore = transaction.objectStore(dbname); | |
132 | - let req = objectStore.add(data[index]); | |
133 | - req.onsuccess = batchAdd; | |
134 | - req.onerror = function() { | |
135 | - // if error continue add | |
136 | - errList.push(req.error); | |
137 | - batchAdd(); | |
138 | - }; | |
139 | - index++; | |
140 | - } | |
141 | - }).catch(err => { | |
142 | - reject(err); | |
143 | - }); | |
144 | - }); | |
145 | - return ret; | |
146 | -} | |
147 | - | |
148 | 42 | export default walletSDK; |
\ No newline at end of file |
@@ -1,7 +1,8 @@ | ||
1 | 1 | import { hexEncode } from './hex.js'; |
2 | 2 | import bech32 from 'bech32'; |
3 | +import Error from './error'; | |
3 | 4 | |
4 | -export function convertArguements(obj){ | |
5 | +export function convertArgument(obj){ | |
5 | 6 | const type = obj.type; |
6 | 7 | const value = obj.raw_data.value; |
7 | 8 | let convertValue; |
@@ -22,7 +23,7 @@ export function convertArguements(obj){ | ||
22 | 23 | convertValue = convertAddress(value); |
23 | 24 | break; |
24 | 25 | default: |
25 | - throw 'Invalid data type.'; | |
26 | + throw new Error('Invalid data type.', 'BTM3101'); | |
26 | 27 | } |
27 | 28 | return { data: convertValue }; |
28 | 29 | } |
@@ -33,7 +34,7 @@ function convertBoolean(bool) { | ||
33 | 34 | }else if(bool === false){ |
34 | 35 | return ''; |
35 | 36 | }else { |
36 | - throw 'Invalid Boolean argument'; | |
37 | + throw new Error('Invalid Boolean argument','BTM3102'); | |
37 | 38 | } |
38 | 39 | } |
39 | 40 |
@@ -54,7 +55,7 @@ function convertData(data){ | ||
54 | 55 | if(/^[-+]?[0-9A-Fa-f]+\.?[0-9A-Fa-f]*?$/i.test(data)){ |
55 | 56 | return data; |
56 | 57 | }else{ |
57 | - throw 'Invalid hex data'; | |
58 | + throw new Error('Invalid hex data','BTM3103'); | |
58 | 59 | } |
59 | 60 | } |
60 | 61 |
@@ -0,0 +1,7 @@ | ||
1 | +export default class Error { | |
2 | + | |
3 | + constructor( _message, _code) { | |
4 | + this.message = _message; | |
5 | + this.code = _code; | |
6 | + } | |
7 | +} | |
\ No newline at end of file |
@@ -1,7 +1,6 @@ | ||
1 | 1 | let createHmac = require('create-hmac'); |
2 | 2 | let ED25519 =require('../ed25519'); |
3 | 3 | let Curve25519 = new ED25519.Curve; |
4 | -const ExpandedPrivateKeySize = 64 | |
5 | 4 | const nacl = require('../nacl'); |
6 | 5 | // If r is nil, crypto/rand.Reader is used. |
7 | 6 |
@@ -1,5 +1,6 @@ | ||
1 | 1 | const bip39 = require('bip39'); |
2 | 2 | const utils = require('./utils'); |
3 | +import Error from '../error' | |
3 | 4 | |
4 | 5 | const WORDLISTS = { |
5 | 6 | en: require('bip39/src/wordlists/english.json'), |
@@ -35,13 +36,13 @@ function isValidMnemonic(mnemonic, language){ | ||
35 | 36 | // mnemonic length = (entropy length + checksum length)/11 |
36 | 37 | let mnemArray = mnemonic.trim().split(' '); |
37 | 38 | if (mnemArray.length != ((EntropyLength+EntropyLength/32)/11 )){ |
38 | - throw 'mnemonic length error'; | |
39 | + throw new Error('mnemonic length error', 'BTM3005'); | |
39 | 40 | } |
40 | 41 | |
41 | 42 | // Pre validate that the mnemonic is well formed and only contains words that |
42 | 43 | // are present in the word list |
43 | 44 | if (!bip39.validateMnemonic(mnemonic, WORDLISTS[language])) { |
44 | - throw 'mnemonic is invalid'; | |
45 | + throw new Error('mnemonic is invalid', 'BTM3006'); | |
45 | 46 | } |
46 | 47 | } |
47 | 48 |
@@ -3,6 +3,7 @@ let scrypt = require('scrypt-js'); | ||
3 | 3 | import {XPrv} from './chainkd'; |
4 | 4 | let sha3_256 = require('js-sha3').sha3_256; |
5 | 5 | import _ from 'lodash'; |
6 | +import Error from '../error'; | |
6 | 7 | |
7 | 8 | const scryptDKLen = 32; |
8 | 9 | const scryptR = 8; |
@@ -64,7 +65,7 @@ function aesCTRXOR(key, inText, iv) { | ||
64 | 65 | |
65 | 66 | function decryptKey(v3Keystore, password){ |
66 | 67 | if (!_.isString(password)) { |
67 | - throw new Error('No password given.'); | |
68 | + throw new Error('No password given.', 'BTM3003'); | |
68 | 69 | } |
69 | 70 | |
70 | 71 | let k = (_.isObject(v3Keystore)) ? v3Keystore : JSON.parse(v3Keystore); |
@@ -92,19 +93,19 @@ function decrypt(json, password) { | ||
92 | 93 | kdfparams = json.crypto.kdfparams; |
93 | 94 | |
94 | 95 | if (kdfparams.prf !== 'hmac-sha256') { |
95 | - throw new Error('Unsupported parameters to PBKDF2'); | |
96 | + throw new Error('Unsupported parameters to PBKDF2', 'BTM3002'); | |
96 | 97 | } |
97 | 98 | |
98 | 99 | derivedKey = cryp.pbkdf2Sync(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.c, kdfparams.dklen, 'sha3256'); |
99 | 100 | } else { |
100 | - throw new Error('Unsupported key derivation scheme'); | |
101 | + throw new Error('Unsupported key derivation scheme' , 'BTM3001'); | |
101 | 102 | } |
102 | 103 | |
103 | 104 | var ciphertext = Buffer.from(json.crypto.ciphertext, 'hex'); |
104 | 105 | |
105 | 106 | var mac = sha3_256(Buffer.concat([Buffer(derivedKey.slice(16, 32)), ciphertext])); |
106 | 107 | if (mac !== json.crypto.mac) { |
107 | - throw new Error('Key derivation failed - possibly wrong password'); | |
108 | + throw new Error('Key derivation failed - possibly wrong password', 'BTM3000'); | |
108 | 109 | } |
109 | 110 | |
110 | 111 | var decipher = cryp.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), Buffer.from(json.crypto.cipherparams.iv, 'hex')); |
@@ -2,7 +2,7 @@ import {SignData} from './signTransaction'; | ||
2 | 2 | |
3 | 3 | function signMessage(message, password, keyJSON) { |
4 | 4 | if (!(message) || !(password) || !(keyJSON) ){ |
5 | - throw('Input args are empty'); | |
5 | + throw('Input args are empty', 'BTM3100'); | |
6 | 6 | } |
7 | 7 | |
8 | 8 | let signData = SignData(keyJSON, null, Buffer.from(message, 'utf8'), password); |
@@ -4,7 +4,7 @@ import {XPrv} from '../key/chainkd'; | ||
4 | 4 | |
5 | 5 | function signTransaction(transaction, password, keyJSON) { |
6 | 6 | if (!(transaction) || !(password) || !(keyJSON) ){ |
7 | - throw('Input args are empty'); | |
7 | + throw('Input args are empty', 'BTM3100'); | |
8 | 8 | } |
9 | 9 | let tx = transaction; |
10 | 10 | let signRet = []; |