edge-currency-plugins 2.5.4 → 2.6.1

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 (74) hide show
  1. package/lib/bch.js +2 -2
  2. package/lib/bsv.js +2 -2
  3. package/lib/btc.js +2 -2
  4. package/lib/common/plugin/{makeCurrencyPlugin.js → CurrencyPlugin.js} +13 -9
  5. package/lib/common/plugin/{makeMetadata.js → Metadata.js} +4 -4
  6. package/lib/common/plugin/{pluginState.js → PluginState.js} +6 -6
  7. package/lib/common/plugin/{serverScores.js → ServerScores.js} +8 -1
  8. package/lib/common/plugin/types.js +8 -0
  9. package/lib/common/utxobased/db/{makeProcessor.js → Processor.js} +63 -29
  10. package/lib/common/utxobased/engine/{makeServerStates.js → ServerStates.js} +140 -45
  11. package/lib/common/utxobased/engine/{makeUtxoEngine.js → UtxoEngine.js} +18 -18
  12. package/lib/common/utxobased/engine/{makeUtxoEngineState.js → UtxoEngineState.js} +27 -22
  13. package/lib/common/utxobased/engine/types.js +5 -0
  14. package/lib/common/utxobased/info/bitcoin.js +6 -0
  15. package/lib/common/utxobased/info/bitcoincash.js +6 -0
  16. package/lib/common/utxobased/info/dash.js +6 -0
  17. package/lib/common/utxobased/info/digibyte.js +6 -0
  18. package/lib/common/utxobased/info/dogecoin.js +6 -0
  19. package/lib/common/utxobased/info/groestlcoin.js +6 -0
  20. package/lib/common/utxobased/info/litecoin.js +6 -0
  21. package/lib/common/utxobased/network/{BlockBook.js → Blockbook.js} +28 -24
  22. package/lib/common/utxobased/network/Socket.js +4 -4
  23. package/lib/firo.js +2 -2
  24. package/lib/ftc.js +2 -2
  25. package/lib/index.js +2 -2
  26. package/lib/ltc.js +2 -2
  27. package/lib/src/common/plugin/{makeEngineEmitter.d.ts → EngineEmitter.d.ts} +1 -1
  28. package/lib/src/common/plugin/{makeMetadata.d.ts → Metadata.d.ts} +1 -1
  29. package/lib/src/common/plugin/{pluginState.d.ts → PluginState.d.ts} +2 -2
  30. package/lib/src/common/plugin/{serverScores.d.ts → ServerScores.d.ts} +1 -1
  31. package/lib/src/common/plugin/types.d.ts +9 -2
  32. package/lib/src/common/utxobased/db/Models/ProcessorTransaction.d.ts +3 -3
  33. package/lib/src/common/utxobased/db/util/utxo.d.ts +1 -1
  34. package/lib/src/common/utxobased/engine/{makeServerStates.d.ts → ServerStates.d.ts} +8 -5
  35. package/lib/src/common/utxobased/engine/{makeUtxoEngineState.d.ts → UtxoEngineState.d.ts} +9 -9
  36. package/lib/src/common/utxobased/engine/{makeUtxoWalletTools.d.ts → UtxoWalletTools.d.ts} +2 -2
  37. package/lib/src/common/utxobased/engine/types.d.ts +4 -0
  38. package/lib/src/common/utxobased/engine/util/getOwnUtxosFromTx.d.ts +1 -1
  39. package/lib/src/common/utxobased/network/{BlockBook.d.ts → Blockbook.d.ts} +10 -10
  40. package/lib/src/common/utxobased/network/Socket.d.ts +1 -1
  41. package/lib/tbch.js +2 -2
  42. package/lib/tbtc.js +2 -2
  43. package/lib/tbtg.js +2 -2
  44. package/package.json +3 -3
  45. /package/lib/common/plugin/{makeCurrencyTools.js → CurrencyTools.js} +0 -0
  46. /package/lib/common/plugin/{makeEngineEmitter.js → EngineEmitter.js} +0 -0
  47. /package/lib/common/utxobased/db/{makeBaselets.js → Baselets.js} +0 -0
  48. /package/lib/common/utxobased/db/{makeMutex.js → Mutex.js} +0 -0
  49. /package/lib/common/utxobased/engine/{makeUtxoWalletTools.js → UtxoWalletTools.js} +0 -0
  50. /package/lib/common/utxobased/network/{MakeSocketEmitter.js → SocketEmitter.js} +0 -0
  51. /package/lib/common/utxobased/network/{BlockBookAPI.js → blockbookApi.js} +0 -0
  52. /package/lib/src/common/plugin/{makeCurrencyPlugin.d.ts → CurrencyPlugin.d.ts} +0 -0
  53. /package/lib/src/common/plugin/{makeCurrencyTools.d.ts → CurrencyTools.d.ts} +0 -0
  54. /package/lib/src/common/utxobased/db/{makeBaselets.d.ts → Baselets.d.ts} +0 -0
  55. /package/lib/src/common/utxobased/db/{makeMutex.d.ts → Mutex.d.ts} +0 -0
  56. /package/lib/src/common/utxobased/db/{makeProcessor.d.ts → Processor.d.ts} +0 -0
  57. /package/lib/src/common/utxobased/engine/{makeUtxoEngine.d.ts → UtxoEngine.d.ts} +0 -0
  58. /package/lib/src/common/utxobased/network/{MakeSocketEmitter.d.ts → SocketEmitter.d.ts} +0 -0
  59. /package/lib/src/common/utxobased/network/{BlockBookAPI.d.ts → blockbookApi.d.ts} +0 -0
  60. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/common.d.ts +0 -0
  61. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/bitcoin.d.ts +0 -0
  62. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/bitcoincash.d.ts +0 -0
  63. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/bitcoinsv.d.ts +0 -0
  64. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/digibyte.d.ts +0 -0
  65. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/feathercoin.d.ts +0 -0
  66. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/groestlcoin.d.ts +0 -0
  67. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/litecoin.d.ts +0 -0
  68. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/smartcash.d.ts +0 -0
  69. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/currencies/zcoin.d.ts +0 -0
  70. /package/lib/test/common/plugin/{currencyPlugin.fixtures → CurrencyPlugin.fixtures}/index.d.ts +0 -0
  71. /package/lib/test/common/plugin/{currencyPlugin.spec.d.ts → CurrencyPlugin.spec.d.ts} +0 -0
  72. /package/lib/test/common/plugin/{makeMetadata.spec.d.ts → Metadata.spec.d.ts} +0 -0
  73. /package/lib/test/common/plugin/{serverScores.spec.d.ts → ServerScores.spec.d.ts} +0 -0
  74. /package/lib/test/common/utxobased/network/{BlockBook.spec.d.ts → Blockbook.spec.d.ts} +0 -0
package/lib/bch.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
- var _makeCurrencyPlugin = require('./common/plugin/makeCurrencyPlugin');
3
+ var _CurrencyPlugin = require('./common/plugin/CurrencyPlugin');
4
4
  var _bitcoincash = require('./common/utxobased/info/bitcoincash');
5
5
 
6
6
  const plugin = (options) =>
7
- _makeCurrencyPlugin.makeCurrencyPlugin.call(void 0, options, _bitcoincash.info)
7
+ _CurrencyPlugin.makeCurrencyPlugin.call(void 0, options, _bitcoincash.info)
8
8
 
9
9
  if (typeof window !== 'undefined') {
10
10
  _optionalChain([window, 'access', _ => _.addEdgeCorePlugins, 'optionalCall', _2 => _2({
package/lib/bsv.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
- var _makeCurrencyPlugin = require('./common/plugin/makeCurrencyPlugin');
3
+ var _CurrencyPlugin = require('./common/plugin/CurrencyPlugin');
4
4
  var _bitcoinsv = require('./common/utxobased/info/bitcoinsv');
5
5
 
6
6
  const plugin = (options) =>
7
- _makeCurrencyPlugin.makeCurrencyPlugin.call(void 0, options, _bitcoinsv.info)
7
+ _CurrencyPlugin.makeCurrencyPlugin.call(void 0, options, _bitcoinsv.info)
8
8
 
9
9
  if (typeof window !== 'undefined') {
10
10
  _optionalChain([window, 'access', _ => _.addEdgeCorePlugins, 'optionalCall', _2 => _2({
package/lib/btc.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
- var _makeCurrencyPlugin = require('./common/plugin/makeCurrencyPlugin');
3
+ var _CurrencyPlugin = require('./common/plugin/CurrencyPlugin');
4
4
  var _bitcoin = require('./common/utxobased/info/bitcoin');
5
5
 
6
6
  const plugin = (options) =>
7
- _makeCurrencyPlugin.makeCurrencyPlugin.call(void 0, options, _bitcoin.info)
7
+ _CurrencyPlugin.makeCurrencyPlugin.call(void 0, options, _bitcoin.info)
8
8
 
9
9
  if (typeof window !== 'undefined') {
10
10
  _optionalChain([window, 'access', _ => _.addEdgeCorePlugins, 'optionalCall', _2 => _2({
@@ -7,11 +7,14 @@
7
7
 
8
8
 
9
9
 
10
- var _makeUtxoEngine = require('../utxobased/engine/makeUtxoEngine');
10
+
11
+
12
+
11
13
  var _types3 = require('../utxobased/engine/types');
12
- var _makeCurrencyTools = require('./makeCurrencyTools');
13
- var _makeEngineEmitter = require('./makeEngineEmitter');
14
- var _pluginState = require('./pluginState');
14
+ var _UtxoEngine = require('../utxobased/engine/UtxoEngine');
15
+ var _CurrencyTools = require('./CurrencyTools');
16
+ var _EngineEmitter = require('./EngineEmitter');
17
+ var _PluginState = require('./PluginState');
15
18
 
16
19
 
17
20
  function makeCurrencyPlugin(
@@ -19,10 +22,10 @@ var _pluginState = require('./pluginState');
19
22
  pluginInfo
20
23
  ) {
21
24
  const { currencyInfo } = pluginInfo
22
- const { io, log, pluginDisklet } = pluginOptions
23
- const currencyTools = _makeCurrencyTools.makeCurrencyTools.call(void 0, io, pluginInfo)
25
+ const { io, log, pluginDisklet, initOptions } = pluginOptions
26
+ const currencyTools = _CurrencyTools.makeCurrencyTools.call(void 0, io, pluginInfo)
24
27
  const { defaultSettings, pluginId, currencyCode } = currencyInfo
25
- const pluginState = _pluginState.makePluginState.call(void 0, {
28
+ const pluginState = _PluginState.makePluginState.call(void 0, {
26
29
  io,
27
30
  currencyCode,
28
31
  pluginId,
@@ -37,12 +40,13 @@ var _pluginState = require('./pluginState');
37
40
  walletInfo,
38
41
  engineOptions
39
42
  ) {
40
- const emitter = _makeEngineEmitter.makeEngineEmitter.call(void 0, engineOptions.callbacks)
43
+ const emitter = _EngineEmitter.makeEngineEmitter.call(void 0, engineOptions.callbacks)
41
44
  const engineConfig = {
42
45
  walletInfo,
43
46
  pluginInfo,
44
47
  pluginDisklet,
45
48
  currencyTools,
49
+ initOptions: _types3.asUtxoInitOptions.call(void 0, initOptions),
46
50
  io,
47
51
  options: {
48
52
  ...pluginOptions,
@@ -51,7 +55,7 @@ var _pluginState = require('./pluginState');
51
55
  },
52
56
  pluginState
53
57
  }
54
- const engine = await _makeUtxoEngine.makeUtxoEngine.call(void 0, engineConfig)
58
+ const engine = await _UtxoEngine.makeUtxoEngine.call(void 0, engineConfig)
55
59
 
56
60
  return engine
57
61
  },
@@ -4,7 +4,7 @@
4
4
  var _memlet = require('memlet');
5
5
 
6
6
  var _awaitlock = require('../utxobased/engine/await-lock'); var _awaitlock2 = _interopRequireDefault(_awaitlock);
7
- var _makeEngineEmitter = require('./makeEngineEmitter');
7
+ var _EngineEmitter = require('./EngineEmitter');
8
8
  var _types3 = require('./types');
9
9
  var _utils = require('./utils');
10
10
 
@@ -47,14 +47,14 @@ const metadataPath = `metadata.json`
47
47
  cache.balance = cumulativeBalance
48
48
  await setMetadata(cache)
49
49
  emitter.emit(
50
- _makeEngineEmitter.EngineEvent.WALLET_BALANCE_CHANGED,
50
+ _EngineEmitter.EngineEvent.WALLET_BALANCE_CHANGED,
51
51
  currencyCode,
52
52
  cumulativeBalance
53
53
  )
54
54
  }
55
55
 
56
56
  emitter.on(
57
- _makeEngineEmitter.EngineEvent.ADDRESS_BALANCE_CHANGED,
57
+ _EngineEmitter.EngineEvent.ADDRESS_BALANCE_CHANGED,
58
58
  async (
59
59
  currencyCode,
60
60
  addressBalanceChanges
@@ -79,7 +79,7 @@ const metadataPath = `metadata.json`
79
79
  )
80
80
 
81
81
  emitter.on(
82
- _makeEngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED,
82
+ _EngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED,
83
83
  async (_uri, height) => {
84
84
  if (height > cache.lastSeenBlockHeight) {
85
85
  cache.lastSeenBlockHeight = height
@@ -12,12 +12,12 @@ var _memlet = require('memlet');
12
12
 
13
13
 
14
14
 
15
- var _serverScores = require('./serverScores');
15
+ var _ServerScores = require('./ServerScores');
16
16
 
17
17
  // Info server endpoint to getting ServerListInfo data
18
- const serverListInfoUrl = 'https://info1.edge.app/v1/blockBook/'
19
- // The filename for ServerInfoCache data (see serverScores.ts)
20
- // Perhaps this should be in serverScores.ts file, but that'll take some refactoring
18
+ const serverListInfoUrl = 'https://info1.edge.app/v1/blockbook/'
19
+ // The filename for ServerInfoCache data (see ServerScores.ts)
20
+ // Perhaps this should be in ServerScores.ts file, but that'll take some refactoring
21
21
  const SERVER_CACHE_FILE = 'serverCache.json'
22
22
 
23
23
  // ServerListInfo data structure from info server and saved to disk
@@ -108,7 +108,7 @@ const asServerListInfo = _cleaners.asObject.call(void 0, _cleaners.asEither.call
108
108
  }
109
109
  }
110
110
 
111
- const serverScores = new (0, _serverScores.ServerScores)({
111
+ const serverScores = new (0, _ServerScores.ServerScores)({
112
112
  log,
113
113
  onDirtyServer
114
114
  })
@@ -179,7 +179,7 @@ const asServerListInfo = _cleaners.asObject.call(void 0, _cleaners.asEither.call
179
179
 
180
180
  async load() {
181
181
  try {
182
- serverCache = _serverScores.asServerCache.call(void 0, await memlet.getJson(SERVER_CACHE_FILE))
182
+ serverCache = _ServerScores.asServerCache.call(void 0, await memlet.getJson(SERVER_CACHE_FILE))
183
183
  } catch (e) {
184
184
  log(`${pluginId}: Failed to load server cache: ${JSON.stringify(e)}`)
185
185
  }
@@ -236,7 +236,14 @@ const RE_ADDED_SERVER_SCORE = -10
236
236
  const filter = (server) => {
237
237
  for (const pattern of includePatterns) {
238
238
  // make sure that the server URL starts with the required pattern
239
- if (server.serverUrl.indexOf(pattern) === 0) return true
239
+ if (
240
+ typeof pattern === 'string' &&
241
+ server.serverUrl.indexOf(pattern) === 0
242
+ )
243
+ return true
244
+ // Or make sure that the server URL matches regex
245
+ if (pattern instanceof RegExp && pattern.test(server.serverUrl))
246
+ return true
240
247
  }
241
248
  return false
242
249
  }
@@ -29,6 +29,7 @@ var _types3 = require('../utxobased/db/types');
29
29
 
30
30
 
31
31
 
32
+
32
33
  const asCurrencyFormat = _cleaners.asValue.call(void 0, 'bip32', 'bip44', 'bip49', 'bip84'); exports.asCurrencyFormat = asCurrencyFormat
33
34
 
34
35
  // Path up to changeIndex
@@ -144,6 +145,12 @@ var _types3 = require('../utxobased/db/types');
144
145
 
145
146
 
146
147
 
148
+
149
+
150
+
151
+
152
+
153
+
147
154
 
148
155
 
149
156
 
@@ -205,6 +212,7 @@ var _types3 = require('../utxobased/db/types');
205
212
 
206
213
 
207
214
 
215
+
208
216
  const asLocalWalletMetadata = _cleaners.asObject.call(void 0, {
209
217
  balance: _cleaners.asString,
210
218
  // scriptPubkey -> balance
@@ -6,7 +6,7 @@ var _memlet = require('memlet');
6
6
 
7
7
  var _unixTime = require('../../../util/unixTime');
8
8
 
9
- var _makeBaselets = require('./makeBaselets');
9
+ var _Baselets = require('./Baselets');
10
10
  var _baselet3 = require('./Models/baselet');
11
11
 
12
12
 
@@ -113,7 +113,7 @@ const BASELET_DIR = 'tables'
113
113
  ) {
114
114
  const disklet = _disklet.navigateDisklet.call(void 0, config.disklet, BASELET_DIR)
115
115
  let memlet = _memlet.makeMemlet.call(void 0, disklet)
116
- let baselets = await _makeBaselets.makeBaselets.call(void 0, { storage: memlet }).catch(async error => {
116
+ let baselets = await _Baselets.makeBaselets.call(void 0, { storage: memlet }).catch(async error => {
117
117
  await memlet.delete('.')
118
118
  throw error
119
119
  })
@@ -153,16 +153,21 @@ const BASELET_DIR = 'tables'
153
153
  await _baselet.clearMemletCache.call(void 0, )
154
154
  await disklet.delete('.')
155
155
  memlet = _memlet.makeMemlet.call(void 0, disklet)
156
- baselets = await _makeBaselets.makeBaselets.call(void 0, { storage: memlet })
156
+ baselets = await _Baselets.makeBaselets.call(void 0, { storage: memlet })
157
157
  },
158
158
 
159
159
  async dumpData() {
160
- const allBases = Object.values(baselets.all)
160
+
161
+ const allBases = Object.values(
162
+ baselets.all
163
+ )
161
164
  return await Promise.all(
162
- allBases.map(async base => ({
163
- databaseName: base.databaseName,
164
- data: (await base.dumpData(''))
165
- }))
165
+ allBases.map(async base => {
166
+ return {
167
+ databaseName: base.databaseName,
168
+ data: (await base.dumpData())
169
+ }
170
+ })
166
171
  )
167
172
  },
168
173
 
@@ -399,6 +404,15 @@ const BASELET_DIR = 'tables'
399
404
 
400
405
  async saveAddress(address) {
401
406
  await baselets.address(async tables => {
407
+ // This variable is used to update the scriptPubkeyByPath table.
408
+ // The path table must be written after the address table because the
409
+ // path table is an index of the address table.
410
+ // This variable acts as a catch for that update to be done after the
411
+ // address table is written.
412
+ let indexTableUpdate
413
+
414
+
415
+
402
416
  const [existingAddress] = await tables.addressByScriptPubkey.query('', [
403
417
  address.scriptPubkey
404
418
  ])
@@ -415,14 +429,14 @@ const BASELET_DIR = 'tables'
415
429
  'Attempted to save address with an existing path, but different script pubkey'
416
430
  )
417
431
 
418
- await tables.scriptPubkeyByPath.insert(
419
- _baselet3.addressPathToPrefix.call(void 0, address.path),
420
- address.path.addressIndex,
421
- address.scriptPubkey
422
- )
432
+ indexTableUpdate = {
433
+ path: address.path,
434
+ scriptPubkey: address.scriptPubkey
435
+ }
423
436
 
424
- // check if this address is used and if so, whether it has a higher last used index
425
- if (address.used || (_nullishCoalesce(_optionalChain([existingAddress, 'optionalAccess', _2 => _2.used]), () => ( false)))) {
437
+ // check if this address is used and if so, whether it has a higher
438
+ // last used index
439
+ if (address.used || _optionalChain([existingAddress, 'optionalAccess', _2 => _2.used]) === true) {
426
440
  let [lastUsed] = await tables.lastUsedByFormatPath.query('', [
427
441
  _baselet3.addressPathToPrefix.call(void 0, address.path)
428
442
  ])
@@ -438,6 +452,7 @@ const BASELET_DIR = 'tables'
438
452
  }
439
453
  }
440
454
 
455
+ // Update routine:
441
456
  if (existingAddress != null) {
442
457
  // Only update the lastQueriedBlockHeight on the address if one was given and is greater than the existing value
443
458
  if (
@@ -463,16 +478,21 @@ const BASELET_DIR = 'tables'
463
478
  existingAddress.balance = address.balance
464
479
  }
465
480
 
466
- // Only update the path field if one was given and currently does not have one
467
- // NOTE: Addresses can be stored in the db without a path due to the `EdgeCurrencyEngine.addGapLimitAddresses` function
468
- // Once an address path is known, it should never be updated
481
+ /*
482
+ Only update the path field if one was given and the existing address
483
+ currently does not have one. We never update paths for addresses, only
484
+ insert paths when they're not present.
485
+
486
+ NOTE: Addresses can be stored in the db without a path due to the
487
+ `EdgeCurrencyEngine.addGapLimitAddresses` function. Once an address
488
+ path is known, it should never be updated
489
+ */
469
490
  if (address.path != null && existingAddress.path == null) {
470
491
  existingAddress.path = address.path
471
- await tables.scriptPubkeyByPath.insert(
472
- _baselet3.addressPathToPrefix.call(void 0, address.path),
473
- address.path.addressIndex,
474
- address.scriptPubkey
475
- )
492
+ indexTableUpdate = {
493
+ path: existingAddress.path,
494
+ scriptPubkey: existingAddress.scriptPubkey
495
+ }
476
496
  }
477
497
 
478
498
  // Only update the used flag if one was given and is true
@@ -495,18 +515,32 @@ const BASELET_DIR = 'tables'
495
515
  )
496
516
  }
497
517
  }
518
+
519
+ // Update the address table:
498
520
  await tables.addressByScriptPubkey.insert(
499
521
  '',
500
522
  existingAddress.scriptPubkey,
501
523
  existingAddress
502
524
  )
503
- return
504
525
  }
505
- await tables.addressByScriptPubkey.insert(
506
- '',
507
- address.scriptPubkey,
508
- address
509
- )
526
+
527
+ // Insert routine:
528
+ if (existingAddress == null) {
529
+ await tables.addressByScriptPubkey.insert(
530
+ '',
531
+ address.scriptPubkey,
532
+ address
533
+ )
534
+ }
535
+
536
+ // Insert the path into the index table:
537
+ if (indexTableUpdate != null) {
538
+ await tables.scriptPubkeyByPath.insert(
539
+ _baselet3.addressPathToPrefix.call(void 0, indexTableUpdate.path),
540
+ indexTableUpdate.path.addressIndex,
541
+ indexTableUpdate.scriptPubkey
542
+ )
543
+ }
510
544
  })
511
545
  },
512
546
 
@@ -1,16 +1,20 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
  var _urijs = require('uri-js');
3
3
 
4
- var _makeEngineEmitter = require('../../plugin/makeEngineEmitter');
4
+ var _EngineEmitter = require('../../plugin/EngineEmitter');
5
5
 
6
6
 
7
7
  var _utils = require('../../plugin/utils');
8
8
 
9
- var _BlockBook = require('../network/BlockBook');
9
+ var _Blockbook = require('../network/Blockbook');
10
10
 
11
+
12
+
13
+
14
+ var _blockbookApi = require('../network/blockbookApi');
11
15
  var _Deferred = require('../network/Deferred'); var _Deferred2 = _interopRequireDefault(_Deferred);
12
- var _MakeSocketEmitter = require('../network/MakeSocketEmitter');
13
16
 
17
+ var _SocketEmitter = require('../network/SocketEmitter');
14
18
  var _socketQueue = require('../network/socketQueue');
15
19
  var _constants = require('./constants');
16
20
 
@@ -53,12 +57,24 @@ var _constants = require('./constants');
53
57
 
54
58
 
55
59
 
60
+
61
+
62
+
56
63
 
57
64
 
58
65
 
59
66
 
60
67
  function makeServerStates(config) {
61
- const { engineEmitter, log, pluginInfo, pluginState, walletInfo } = config
68
+ const {
69
+ engineEmitter,
70
+ initOptions,
71
+ io,
72
+ log,
73
+ pluginInfo,
74
+ pluginState,
75
+ walletInfo
76
+ } = config
77
+ const { serverConfigs = [] } = pluginInfo.engineInfo
62
78
  log('Making server states')
63
79
 
64
80
  const serverStatesCache = {}
@@ -68,15 +84,15 @@ var _constants = require('./constants');
68
84
  let reconnectTimer = setTimeout(() => {
69
85
  return
70
86
  }, 0)
71
- const socketEmitter = new (0, _MakeSocketEmitter.SocketEmitter)()
87
+ const socketEmitter = new (0, _SocketEmitter.SocketEmitter)()
72
88
 
73
89
  // set server specific event emitters
74
- socketEmitter.on(_MakeSocketEmitter.SocketEvent.CONNECTION_OPEN, (uri) => {
90
+ socketEmitter.on(_SocketEmitter.SocketEvent.CONNECTION_OPEN, (uri) => {
75
91
  reconnectCounter = 0
76
92
  log(`${uri} ** Connected **`)
77
93
  })
78
94
  socketEmitter.on(
79
- _MakeSocketEmitter.SocketEvent.CONNECTION_CLOSE,
95
+ _SocketEmitter.SocketEvent.CONNECTION_CLOSE,
80
96
  (uri, error) => {
81
97
  _utils.removeItem.call(void 0, serverStatesCache, uri)
82
98
 
@@ -90,7 +106,7 @@ var _constants = require('./constants');
90
106
  }
91
107
  )
92
108
  socketEmitter.on(
93
- _MakeSocketEmitter.SocketEvent.CONNECTION_TIMER,
109
+ _SocketEmitter.SocketEvent.CONNECTION_TIMER,
94
110
  (uri, queryDate) => {
95
111
  const queryTime = Date.now() - queryDate
96
112
  log(`${uri} returned healthCheck in ${queryTime}ms`)
@@ -98,7 +114,7 @@ var _constants = require('./constants');
98
114
  }
99
115
  )
100
116
  engineEmitter.on(
101
- _makeEngineEmitter.EngineEvent.NEW_ADDRESS_TRANSACTION,
117
+ _EngineEmitter.EngineEvent.NEW_ADDRESS_TRANSACTION,
102
118
  (uri, newTx) => {
103
119
  log(
104
120
  `${uri} received received new transaction with id ${newTx.tx.txid} to address ${newTx.address}`
@@ -110,7 +126,7 @@ var _constants = require('./constants');
110
126
  }
111
127
  )
112
128
  engineEmitter.on(
113
- _makeEngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED,
129
+ _EngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED,
114
130
  (uri, blockHeight) => {
115
131
  log(`${uri} block height changed to ${blockHeight}`)
116
132
  serverStatesCache[uri].blockHeight = blockHeight
@@ -208,9 +224,9 @@ var _constants = require('./constants');
208
224
  }
209
225
 
210
226
  // Make new Blockbook instance
211
- const blockbook = _BlockBook.makeBlockBook.call(void 0, {
212
- wsAddress: uri,
213
- socketEmitter,
227
+ const blockbook = _Blockbook.makeBlockbook.call(void 0, {
228
+ asAddress: pluginInfo.engineInfo.asBlockbookAddress,
229
+ connectionUri: uri,
214
230
  engineEmitter,
215
231
  log,
216
232
  onQueueSpaceCB: async (
@@ -229,8 +245,8 @@ var _constants = require('./constants');
229
245
  }
230
246
  return task
231
247
  },
232
- walletId: walletInfo.id,
233
- asAddress: pluginInfo.engineInfo.asBlockbookAddress
248
+ socketEmitter,
249
+ walletId: walletInfo.id
234
250
  })
235
251
 
236
252
  // Make new ServerStates instance
@@ -249,7 +265,7 @@ var _constants = require('./constants');
249
265
  serverStatesCache[uri].blockHeight = blockHeight
250
266
 
251
267
  // Emit initial BLOCK_HEIGHT_CHANGED event
252
- engineEmitter.emit(_makeEngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED, uri, blockHeight)
268
+ engineEmitter.emit(_EngineEmitter.EngineEvent.BLOCK_HEIGHT_CHANGED, uri, blockHeight)
253
269
 
254
270
  // Increment server score using response time
255
271
  const responseTime = Date.now() - startTime
@@ -304,38 +320,117 @@ var _constants = require('./constants');
304
320
 
305
321
  const broadcastTx = async (transaction) => {
306
322
  return await new Promise((resolve, reject) => {
307
- const uris = Object.keys(serverStatesCache).filter(uri => {
308
- const { blockbook } = serverStatesCache[uri]
309
- if (blockbook == null) return false
310
- return blockbook.isConnected
311
- })
312
- if (uris == null || uris.length < 1) {
313
- reject(
314
- new Error('No available connections\nCheck your internet signal')
315
- )
316
- }
317
323
  let resolved = false
318
324
  let bad = 0
319
- for (const uri of uris) {
320
- const { blockbook } = serverStatesCache[uri]
321
- if (blockbook == null) continue
322
- blockbook
323
- .broadcastTx(transaction)
324
- .then(response => {
325
- if (!resolved) {
326
- resolved = true
327
- resolve(response.result)
328
- }
329
- })
330
- .catch((e) => {
331
- if (++bad === uris.length) {
332
- const msg = e != null ? `With error ${e.message}` : ''
333
- log.error(
334
- `broadcastTx fail: ${JSON.stringify(transaction)}\n${msg}`
335
- )
336
- reject(e)
337
- }
325
+
326
+ const wsUris = Object.keys(serverStatesCache).filter(
327
+ uri => serverStatesCache[uri].blockbook != null
328
+ )
329
+
330
+ // If there are no blockbook instances, reject the promise
331
+ if (wsUris.length < 1) {
332
+ reject(new Error('Unexpected error. Missing WebSocket connections.'))
333
+ // Exit early if there are blockbook instances
334
+ return
335
+ }
336
+
337
+ // Determine if there are any connected blockbook instances
338
+ const isAnyBlockbookConnected = wsUris.some(
339
+ uri => serverStatesCache[uri].blockbook.isConnected
340
+ )
341
+
342
+ if (isAnyBlockbookConnected) {
343
+ for (const uri of wsUris) {
344
+ const { blockbook } = serverStatesCache[uri]
345
+ if (blockbook == null) continue
346
+ blockbook
347
+ .broadcastTx(transaction)
348
+ .then(response => {
349
+ if (!resolved) {
350
+ resolved = true
351
+ resolve(response.result)
352
+ }
353
+ })
354
+ .catch((e) => {
355
+ if (++bad === wsUris.length) {
356
+ const msg = e != null ? `With error ${e.message}` : ''
357
+ log.error(
358
+ `broadcastTx fail: ${JSON.stringify(transaction)}\n${msg}`
359
+ )
360
+ reject(e)
361
+ }
362
+ })
363
+ }
364
+ }
365
+
366
+ // Broadcast through any HTTP URI that may be configured, only if no
367
+ // blockbook instances are connected.
368
+ if (!isAnyBlockbookConnected) {
369
+ // This is for the future when we want to HTTP servers from the user
370
+ // settings:
371
+ // const httpUris = pluginState.getLocalServers(Infinity, [
372
+ // /^http(?:s)?:/i
373
+ // ])
374
+
375
+ const { nowNodeApiKey } = initOptions
376
+ const nowNodeUris = serverConfigs
377
+ .filter(config => config.type === 'blockbook-nownode')
378
+ .map(config => config.uris)
379
+ .flat(1)
380
+
381
+ // If there are no HTTP servers, reject the promise
382
+ if (nowNodeUris.length < 1) {
383
+ // If no HTTP servers are available, and we had no connected blockbook
384
+ // instances, reject the promise with a message indicating no
385
+ // available connections. It's clear we have some connection instances
386
+ // if we gotten to this point, but we just don't have any of those
387
+ // instances connected at this time.
388
+ reject(
389
+ new Error('No available connections. Check your internet signal.')
390
+ )
391
+ return
392
+ }
393
+
394
+ // If there is no key for the NowNode servers:
395
+ if (nowNodeApiKey == null) {
396
+ reject(new Error('Missing connection key for fallback servers.'))
397
+ return
398
+ }
399
+
400
+ for (const uri of nowNodeUris) {
401
+ // HTTP Fallback
402
+ io.fetchCors(`${uri}/api/v2/sendtx/`, {
403
+ method: 'POST',
404
+ headers: {
405
+ 'api-key': nowNodeApiKey
406
+ },
407
+ body: transaction.signedTx
338
408
  })
409
+ .then(async response => {
410
+ if (!response.ok) {
411
+ throw new Error(
412
+ `Failed to broadcast transaction via Blockbook: HTTP ${response.status}`
413
+ )
414
+ }
415
+ const json = await response.json()
416
+ return _blockbookApi.asBlockbookResponse.call(void 0, (0, _blockbookApi.asBroadcastTxResponse))(json)
417
+ })
418
+ .then(response => {
419
+ if (!resolved) {
420
+ resolved = true
421
+ resolve(response.result)
422
+ }
423
+ })
424
+ .catch((e) => {
425
+ if (++bad === nowNodeUris.length) {
426
+ const msg = e != null ? `With error ${e.message}` : ''
427
+ log.error(
428
+ `broadcastTx fail: ${JSON.stringify(transaction)}\n${msg}`
429
+ )
430
+ reject(e)
431
+ }
432
+ })
433
+ }
339
434
  }
340
435
  })
341
436
  }