@psf/bch-js 6.5.2 → 6.5.4
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.
- package/package.json +2 -2
- package/src/encryption.js +22 -0
- package/src/utxo.js +14 -0
- package/test/e2e/utxo/unsynced-indexer.js +27 -0
- package/test/unit/utxo-unit.js +4 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psf/bch-js",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.4",
|
|
4
4
|
"description": "A JavaScript library for working with Bitcoin Cash, eCash, and SLP Tokens",
|
|
5
5
|
"author": "Chris Troutner <chris.troutner@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"test:integration:local:bchn": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/",
|
|
20
20
|
"test:integration:local:testnet": "RESTURL=http://localhost:4000/v5/ mocha --timeout 30000 test/integration/chains/testnet",
|
|
21
21
|
"test:integration:decatur:bchn": "export RESTURL=http://192.168.2.129:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/",
|
|
22
|
-
"test:integration:decatur:abc": "export RESTURL=http://192.168.2.
|
|
22
|
+
"test:integration:decatur:abc": "export RESTURL=http://192.168.2.142:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/",
|
|
23
23
|
"test:integration:temp:bchn": "export RESTURL=http://157.90.174.219:3000/v5/ && mocha --timeout 30000 test/integration/",
|
|
24
24
|
"test:temp": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 -g '#Encryption' test/integration/",
|
|
25
25
|
"test:temp2": "mocha --timeout=30000 -g '#TransactionLib' test/unit/",
|
package/src/encryption.js
CHANGED
|
@@ -32,6 +32,28 @@ class Encryption {
|
|
|
32
32
|
_this = this
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* @api encryption.getPubKey() getPubKey()
|
|
37
|
+
* @apiName Encryption getPubKey()
|
|
38
|
+
* @apiGroup Encryption
|
|
39
|
+
* @apiDescription Get the public key for an address
|
|
40
|
+
* Given an address, the command will search the blockchain for a public
|
|
41
|
+
* key associated with that address. The address needs to have made at least
|
|
42
|
+
* one spend transaction, in order for its public key to be retrievable.
|
|
43
|
+
*
|
|
44
|
+
* @apiExample Example usage:
|
|
45
|
+
*(async () => {
|
|
46
|
+
* try {
|
|
47
|
+
* const addr = 'bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d'
|
|
48
|
+
* const pubkey = await bchjs.encryption.getPubKey(addr);
|
|
49
|
+
* console.log(pubkey);
|
|
50
|
+
* } catch(err) {
|
|
51
|
+
* console.error(err)
|
|
52
|
+
* }
|
|
53
|
+
*})()
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
|
|
35
57
|
// Search the blockchain for a public key associated with a BCH address.
|
|
36
58
|
async getPubKey (addr) {
|
|
37
59
|
try {
|
package/src/utxo.js
CHANGED
|
@@ -109,6 +109,11 @@ class UTXO {
|
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
// Get the sync status of the SLP indexer
|
|
113
|
+
const syncStatus = await this.psfSlpIndexer.status()
|
|
114
|
+
const slpIndexerHeight = syncStatus.status.syncedBlockHeight
|
|
115
|
+
const chainBlockHeight = syncStatus.status.chainBlockHeight
|
|
116
|
+
|
|
112
117
|
// Loop through the Fulcrum UTXOs.
|
|
113
118
|
for (let i = 0; i < utxos.length; i++) {
|
|
114
119
|
const thisUtxo = utxos[i]
|
|
@@ -152,6 +157,15 @@ class UTXO {
|
|
|
152
157
|
thisUtxo.isSlp = false
|
|
153
158
|
}
|
|
154
159
|
// console.log(`thisUtxo.isSlp: ${thisUtxo.isSlp}`)
|
|
160
|
+
|
|
161
|
+
// If the SLP indexer more than 1 block behind the full node, then
|
|
162
|
+
// move any BCH UTXOs of 600 sats or less into the null array. This
|
|
163
|
+
// protects token UTXOs from being burned accidentally.
|
|
164
|
+
if (slpIndexerHeight < chainBlockHeight - 1) {
|
|
165
|
+
if (thisUtxo.value < 601) {
|
|
166
|
+
thisUtxo.isSlp = null
|
|
167
|
+
}
|
|
168
|
+
}
|
|
155
169
|
}
|
|
156
170
|
}
|
|
157
171
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This test is used to interrogate the behavior of bch-js when a psf-slp-indexer
|
|
3
|
+
panicks and starts indexing from SLP genesis. In this corner-case, bch-js
|
|
4
|
+
should detect the indexer is out of sync, and protect any token UTXOs by
|
|
5
|
+
moving any UTXO under 600 sats into the null array.
|
|
6
|
+
|
|
7
|
+
TO RUN THIS TEST:
|
|
8
|
+
- Reset a local instance of psf-slp-indexer to sync from genesis.
|
|
9
|
+
- Start a local copy of bch-api
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const BCHJS = require('../../../src/bch-js.js')
|
|
13
|
+
const bchjs = new BCHJS({
|
|
14
|
+
restURL: 'http://localhost:3000/v5/'
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
async function startTest () {
|
|
18
|
+
try {
|
|
19
|
+
const addr = 'bitcoincash:qzkvas58zag9tjry693mflkjze2m20ps7vx7uw7z9d'
|
|
20
|
+
|
|
21
|
+
const result = await bchjs.Utxo.get(addr)
|
|
22
|
+
console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
23
|
+
} catch (err) {
|
|
24
|
+
console.error('Error in startTest(): ', err)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
startTest()
|
package/test/unit/utxo-unit.js
CHANGED
|
@@ -121,6 +121,8 @@ describe('#utxo', () => {
|
|
|
121
121
|
sandbox
|
|
122
122
|
.stub(bchjs.Utxo.psfSlpIndexer, 'tx')
|
|
123
123
|
.resolves({ txData: { isValidSlp: false } })
|
|
124
|
+
sandbox.stub(bchjs.Utxo.psfSlpIndexer, 'status')
|
|
125
|
+
.resolves({ status: { syncedBlockHeight: 600000, chainBlockHeight: 600000 } })
|
|
124
126
|
|
|
125
127
|
// Mock function to return the same input. Good enough for this test.
|
|
126
128
|
sandbox.stub(bchjs.Utxo, 'hydrateTokenData').resolves(x => x)
|
|
@@ -154,6 +156,8 @@ describe('#utxo', () => {
|
|
|
154
156
|
sandbox
|
|
155
157
|
.stub(bchjs.Utxo.psfSlpIndexer, 'tx')
|
|
156
158
|
.resolves({ txData: { isValidSlp: false } })
|
|
159
|
+
sandbox.stub(bchjs.Utxo.psfSlpIndexer, 'status')
|
|
160
|
+
.resolves({ status: { syncedBlockHeight: 600000, chainBlockHeight: 600000 } })
|
|
157
161
|
|
|
158
162
|
// Force psf-slp-indexer to return no UTXOs
|
|
159
163
|
sandbox
|