@bitpay-labs/bitcore-client 11.11.5 → 11.11.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/package.json +13 -8
  2. package/ts_build/src/wallet.d.ts +3 -3
  3. package/.mocharc.json +0 -6
  4. package/LICENSE +0 -21
  5. package/mcr.config.js +0 -16
  6. package/src/client.ts +0 -275
  7. package/src/encryption.ts +0 -191
  8. package/src/index.ts +0 -21
  9. package/src/storage/level.ts +0 -171
  10. package/src/storage/mongo.ts +0 -191
  11. package/src/storage/textFile.ts +0 -327
  12. package/src/storage.ts +0 -271
  13. package/src/stream-util.ts +0 -80
  14. package/src/types/index.d.ts +0 -2
  15. package/src/types/storage.d.ts +0 -1
  16. package/src/types/wallet.d.ts +0 -36
  17. package/src/utils.ts +0 -35
  18. package/src/wallet.ts +0 -1221
  19. package/test/unit/data/ethMigrationTestWallet.fixture.ts +0 -68
  20. package/test/unit/wallet.arc.test.ts +0 -10
  21. package/test/unit/wallet.test.ts +0 -1177
  22. package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.d.ts +0 -51
  23. package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.d.ts.map +0 -1
  24. package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.js +0 -68
  25. package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.js.map +0 -1
  26. package/ts_build/test/unit/wallet.arc.test.d.ts +0 -2
  27. package/ts_build/test/unit/wallet.arc.test.d.ts.map +0 -1
  28. package/ts_build/test/unit/wallet.arc.test.js +0 -12
  29. package/ts_build/test/unit/wallet.arc.test.js.map +0 -1
  30. package/ts_build/test/unit/wallet.test.d.ts +0 -2
  31. package/ts_build/test/unit/wallet.test.d.ts.map +0 -1
  32. package/ts_build/test/unit/wallet.test.js +0 -1037
  33. package/ts_build/test/unit/wallet.test.js.map +0 -1
  34. package/tsconfig.json +0 -40
  35. package/tsconfig.prod.json +0 -6
package/src/storage.ts DELETED
@@ -1,271 +0,0 @@
1
- import 'source-map-support/register';
2
- import { PassThrough } from 'stream';
3
- import { BitcoreLib } from '@bitpay-labs/crypto-wallet-core';
4
- import { Encryption } from './encryption';
5
- import { Level } from './storage/level';
6
- import { Mongo } from './storage/mongo';
7
- import { TextFile } from './storage/textFile';
8
- import { StorageType } from './types/storage';
9
- import { IWallet, KeyImport } from './types/wallet';
10
-
11
- function tryParse(json: string) {
12
- try {
13
- return JSON.parse(json);
14
- } catch {
15
- return json;
16
- }
17
- }
18
-
19
- export class Storage {
20
- path: string;
21
- db: Array<Mongo | Level | TextFile>;
22
- url?: string;
23
- errorIfExists?: boolean;
24
- createIfMissing: boolean;
25
- storageType: Mongo | Level | TextFile;
26
-
27
- constructor(params: { path?: string; createIfMissing: boolean; errorIfExists: boolean; storageType?: StorageType }) {
28
- const { path, createIfMissing, errorIfExists, storageType } = params;
29
- if (storageType && !['Mongo', 'Level', 'TextFile'].includes(storageType)) {
30
- throw new Error('Storage Type passed in must be Mongo, Level, or TextFile');
31
- }
32
- this.path = path;
33
- this.createIfMissing = createIfMissing;
34
- this.errorIfExists = errorIfExists;
35
- const dbMap = {
36
- Mongo,
37
- Level,
38
- TextFile
39
- };
40
- this.db = [];
41
- if (dbMap[storageType]) {
42
- this.db.push(new dbMap[storageType]({ createIfMissing, errorIfExists, path }));
43
- this.storageType = this.db[0];
44
- } else {
45
- for (const DbType of Object.values(dbMap)) {
46
- this.db.push(new DbType({ createIfMissing, errorIfExists, path }));
47
- }
48
- }
49
- }
50
-
51
- async verifyDbs(dbs: Storage['db']) {
52
- for await (const db of dbs) {
53
- if (typeof (db as any).testConnection === 'function') {
54
- // test mongo connection
55
- if (!(await (db as any).testConnection())) {
56
- // remove from dbs
57
- dbs.splice(dbs.indexOf(db), 1);
58
- }
59
- }
60
- }
61
- return dbs;
62
- }
63
-
64
- async close() {
65
- (this.storageType as Mongo)?.close?.();
66
- }
67
-
68
- async loadWallet(params: { name: string }): Promise<void | IWallet>
69
- async loadWallet(params: { name: string; raw: true }): Promise<void | string>
70
- async loadWallet(params: { name: string; raw: false }): Promise<void | IWallet>
71
- async loadWallet(params: { name: string; raw?: boolean }): Promise<void | IWallet | string> {
72
- const { name, raw } = params;
73
- let wallet: string | void;
74
- for (const db of await this.verifyDbs(this.db)) {
75
- try {
76
- wallet = await db.loadWallet({ name });
77
- if (wallet) {
78
- this.storageType = db;
79
- break;
80
- }
81
- } catch { /* ignore */ }
82
- }
83
- if (!wallet) {
84
- return;
85
- }
86
- return raw ? wallet : JSON.parse(wallet) as IWallet;
87
- }
88
-
89
- async deleteWallet(params: { name: string }) {
90
- const { name } = params;
91
- for (const db of await this.verifyDbs(this.db)) {
92
- try {
93
- await db.deleteWallet({ name });
94
- } catch (e) {
95
- console.log(e);
96
- }
97
- }
98
- }
99
-
100
- async listWallets() {
101
- let passThrough = new PassThrough();
102
- const dbs = await this.verifyDbs(this.db);
103
- for (const db of dbs) {
104
- const listWalletStream = await db.listWallets();
105
- passThrough = listWalletStream.pipe(passThrough, { end: false });
106
- listWalletStream.once('end', () => --dbs.length === 0 && passThrough.end());
107
- }
108
- return passThrough;
109
- }
110
-
111
- async listKeys() {
112
- let passThrough = new PassThrough();
113
- const dbs = await this.verifyDbs(this.db);
114
- for (const db of dbs) {
115
- const listWalletStream = await db.listKeys();
116
- passThrough = listWalletStream.pipe(passThrough, { end: false });
117
- listWalletStream.once('end', () => --dbs.length === 0 && passThrough.end());
118
- }
119
- return passThrough;
120
- }
121
-
122
- async saveWallet(params) {
123
- const { wallet } = params;
124
- return this.storageType.saveWallet({ wallet });
125
- }
126
-
127
- /** @deprecated - Use getStoredKey */
128
- async getKey(params: {
129
- address: string;
130
- name: string;
131
- encryptionKey: string;
132
- keepAlive: boolean;
133
- open: boolean;
134
- }): Promise<KeyImport> {
135
- const { address, name, encryptionKey, keepAlive, open } = params;
136
- const payload = await this.storageType.getKey({ name, address, keepAlive, open });
137
- const json = JSON.parse(payload) || payload;
138
- const { encKey, pubKey } = json;
139
- if (encryptionKey && pubKey) {
140
- const decrypted = Encryption.decryptPrivateKey(encKey, pubKey, encryptionKey);
141
- return JSON.parse(decrypted);
142
- } else {
143
- return json;
144
- }
145
- }
146
-
147
- /** @deprecated - Use getStoredKeys */
148
- async getKeys(params: { addresses: string[]; name: string; encryptionKey: string }): Promise<Array<KeyImport>> {
149
- const { addresses, name, encryptionKey } = params;
150
- const keys = new Array<KeyImport>();
151
- let keepAlive = true;
152
- let open = true;
153
- for (const address of addresses) {
154
- if (address === addresses[addresses.length - 1]) {
155
- keepAlive = false;
156
- }
157
- try {
158
- const key = await this.getKey({
159
- name,
160
- address,
161
- encryptionKey,
162
- keepAlive,
163
- open
164
- });
165
- keys.push(key);
166
- } catch (err) {
167
- console.error(err);
168
- }
169
- open = false;
170
- }
171
- return keys;
172
- }
173
-
174
- /** @deprecated - Use addKeysSafe */
175
- async addKeys(params: { name: string; keys: KeyImport[]; encryptionKey: string }) {
176
- const { name, keys, encryptionKey } = params;
177
- let open = true;
178
- for (const key of keys) {
179
- const { path } = key;
180
- const pubKey = key.pubKey || new BitcoreLib.PrivateKey(key.privKey).publicKey.toString();
181
- let payload = {};
182
- if (pubKey && key.privKey && encryptionKey) {
183
- const toEncrypt = JSON.stringify(key);
184
- const encKey = Encryption.encryptPrivateKey(toEncrypt, pubKey, encryptionKey);
185
- payload = { encKey, pubKey, path };
186
- }
187
- const toStore = JSON.stringify(payload);
188
- let keepAlive = true;
189
- if (key === keys[keys.length - 1]) {
190
- keepAlive = false;
191
- }
192
- await this.storageType.addKeys({ name, key, toStore, keepAlive, open });
193
- open = false;
194
- }
195
- }
196
-
197
- async getAddress(params: { name: string; address: string }) {
198
- const { name, address } = params;
199
- return this.storageType.getAddress({ name, address, keepAlive: true, open: true });
200
- }
201
-
202
- async getAddresses(params: { name: string; limit?: number; skip?: number }) {
203
- const { name, limit, skip } = params;
204
- return this.storageType.getAddresses({ name, limit, skip });
205
- }
206
-
207
- async addKeysSafe(params: { name: string; keys: KeyImport[] }) {
208
- const { name, keys } = params;
209
- let i = 0;
210
- for (const key of keys) {
211
- const { path } = key;
212
- const pubKey = key.pubKey;
213
- // key.privKey is encrypted - cannot be directly used to retrieve pubKey if required
214
- if (!pubKey) {
215
- throw new Error(`pubKey is undefined for ${name}. Keys not added to storage`);
216
- }
217
- let payload = {};
218
- if (pubKey) {
219
- payload = { key: JSON.stringify(key), pubKey, path };
220
- }
221
- const toStore = JSON.stringify(payload);
222
- // open on first, close on last
223
- await this.storageType.addKeys({ name, key, toStore, open: i === 0, keepAlive: i < keys.length - 1 });
224
- ++i;
225
- }
226
- }
227
-
228
- async getStoredKeys(params: { addresses: string[]; name: string }): Promise<Array<any>> {
229
- const { addresses, name } = params;
230
- const keys = new Array<any>();
231
- let i = 0;
232
- for (const address of addresses) {
233
- try {
234
- const key = await this.getStoredKey({
235
- name,
236
- address,
237
- open: i === 0, // open on first
238
- keepAlive: i < addresses.length - 1, // close on last
239
- });
240
- if (key) {
241
- keys.push(key);
242
- } else {
243
- console.warn(`Key not found for address ${address}`);
244
- }
245
- } catch (err) {
246
- // don't continue from catch - i must be incremented
247
- console.error(err);
248
- }
249
- ++i;
250
- }
251
- return keys;
252
- }
253
-
254
- private async getStoredKey(params: {
255
- address: string;
256
- name: string;
257
- keepAlive: boolean;
258
- open: boolean;
259
- }): Promise<any> {
260
- const { address, name, keepAlive, open } = params;
261
- const payload = await this.storageType.getKey({ name, address, keepAlive, open });
262
- // Note: payload could be undefined if the server has an address that local storage did not save
263
- const json = tryParse(payload) || payload;
264
- const { key } = json || {}; // pubKey available - not needed
265
- if (key) {
266
- return JSON.parse(key);
267
- } else {
268
- return json;
269
- }
270
- }
271
- }
@@ -1,80 +0,0 @@
1
- import { Transform } from 'stream';
2
-
3
- export class ParseApiStream extends Transform {
4
- constructor() {
5
- super({ objectMode: true });
6
- }
7
-
8
- _write(data, _encoding, cb) {
9
- const stringDatas = data.toString().split('\n');
10
- for (const stringData of stringDatas) {
11
- const normalized = stringData.endsWith(',') ? stringData.slice(0, stringData.length - 1) : stringData;
12
- if (normalized.includes('{') && normalized.includes('}')) {
13
- this.push(JSON.parse(normalized));
14
- }
15
- }
16
- cb();
17
- }
18
- }
19
-
20
- function signTxStream(wallet: any, keys: object, utxosPassedIn: object, passphrase: string) {
21
- return new Transform({
22
- objectMode: true,
23
- async transform(chunk, encoding, callback) {
24
- const rawTransaction = chunk.rawTransaction;
25
- const utxos = utxosPassedIn || chunk.utxos;
26
- const signedTx = await wallet.signTx({ tx: rawTransaction, utxos, keys, passphrase });
27
- chunk.signedRawTransaction = signedTx;
28
- return callback(null, chunk);
29
- }
30
- });
31
- }
32
-
33
- function objectModeToJsonlBuffer() {
34
- return new Transform({
35
- writableObjectMode: true,
36
- readableObjectMode: false,
37
- transform(chunk, encoding, callback) {
38
- if (typeof chunk !== 'object') {
39
- return callback(new Error(`invalid data not in form of object: ${chunk}`));
40
- }
41
- let jsonl;
42
- try {
43
- jsonl = JSON.stringify(chunk).concat('\n');
44
- callback(null, jsonl);
45
- } catch (e) {
46
- return callback(e);
47
- }
48
- }
49
- });
50
- }
51
-
52
- function jsonlBufferToObjectMode() {
53
- return new Transform({
54
- writableObjectMode: false,
55
- readableObjectMode: true,
56
- transform(chunk, encoding, callback) {
57
- const buffer = chunk;
58
- const lineArray = buffer.toString().split('\n');
59
- while (lineArray.length > 1) {
60
- try {
61
- const data = lineArray.shift();
62
- if (data === '') {
63
- continue;
64
- }
65
- const obj = JSON.parse(data);
66
- this.push(obj);
67
- } catch (e) {
68
- return callback(e);
69
- }
70
- }
71
- callback();
72
- }
73
- });
74
- }
75
-
76
- export const StreamUtil = {
77
- signTxStream,
78
- objectModeToJsonlBuffer,
79
- jsonlBufferToObjectMode
80
- };
@@ -1,2 +0,0 @@
1
- export type * as StorageTypes from './storage';
2
- export type * as WalletTypes from './wallet';
@@ -1 +0,0 @@
1
- export type StorageType = 'Mongo' | 'Level' | 'TextFile';
@@ -1,36 +0,0 @@
1
- import { StorageType } from './storage';
2
-
3
- export interface IWallet {
4
- name: string;
5
- baseUrl: string;
6
- chain: string;
7
- network: string;
8
- path: string;
9
- phrase?: string;
10
- xpriv?: string;
11
- password: string;
12
- storageType: StorageType;
13
- addressIndex?: number;
14
- tokens: Array<any>;
15
- lite: boolean;
16
- addressType: string;
17
- addressZero: string;
18
- }
19
-
20
- export interface KeyImport {
21
- address: string;
22
- privKey?: string;
23
- pubKey?: string;
24
- path?: string;
25
- }
26
-
27
- export interface BumpTxFeeType {
28
- txid?: string;
29
- rawTx?: string;
30
- changeIdx?: number;
31
- feeRate?: number;
32
- feeTarget?: number;
33
- feePriority?: number;
34
- noRbf?: boolean;
35
- isSweep?: boolean;
36
- }
package/src/utils.ts DELETED
@@ -1,35 +0,0 @@
1
- import https from 'https';
2
-
3
- class Utils {
4
-
5
- sleep(ms): Promise<void> {
6
- return new Promise(r => setTimeout(r, ms));
7
- }
8
-
9
- async getCurrencies(test?: boolean): Promise<any[]> {
10
- const baseUrl = process.env.BITPAY_BASEURL || `https://${test ? 'test.' : ''}bitpay.com`;
11
- const data = await new Promise<string>((resolve, reject) => {
12
- https.get(`${baseUrl}/currencies`, res => {
13
- if (res.statusCode !== 200) {
14
- reject(new Error('Request Failed'));
15
- }
16
- let data = '';
17
- res.on('data', (chunk) => {
18
- data += chunk;
19
- });
20
- res.on('end', () => resolve(data.toString()));
21
- });
22
- });
23
- return JSON.parse(data).data;
24
- }
25
-
26
- async getCurrencyObj(chain: string, contractAddress: string, test?: boolean) {
27
- const currencies = await this.getCurrencies(test);
28
- if (contractAddress) {
29
- return currencies.find(c => c.chain === chain && c.contractAddress === contractAddress);
30
- }
31
- return currencies.find(c => c.chain === chain && c.native);
32
- }
33
- }
34
-
35
- export const utils = new Utils();