@psf/bch-js 6.4.0 → 6.4.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@psf/bch-js",
3
- "version": "6.4.0",
3
+ "version": "6.4.3",
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": [
package/src/address.js CHANGED
@@ -133,7 +133,7 @@ class Address {
133
133
  * @api Address.toEcashAddress() toEcashAddress()
134
134
  * @apiName toEcashAddress
135
135
  * @apiGroup Address
136
- * @apiDescription Convert legacy to cashAddress format
136
+ * @apiDescription Convert legacy to eCash (XEC) format
137
137
  *
138
138
  * @apiExample Example usage:
139
139
  * // mainnet
@@ -158,6 +158,35 @@ class Address {
158
158
  return ecashAddress.split(':')[1]
159
159
  }
160
160
 
161
+ /**
162
+ * @api Address.toEtokenAddress() toEtokenAddress()
163
+ * @apiName toEtokenAddress
164
+ * @apiGroup Address
165
+ * @apiDescription Convert legacy to eToken (XEC) format
166
+ *
167
+ * @apiExample Example usage:
168
+ * // mainnet
169
+ * bchjs.Address.toEtokenAddress('bitcoincash:qq50d800hgunr8u4trz3uuppspk3mds0dy9978plt2')
170
+ * // etoken:qq50d800hgunr8u4trz3uuppspk3mds0dyug2v69da
171
+ *
172
+ * // mainnet no prefix
173
+ * bchjs.Address.toEtokenAddress('bitcoincash:qq50d800hgunr8u4trz3uuppspk3mds0dy9978plt2', false)
174
+ * // qq50d800hgunr8u4trz3uuppspk3mds0dyug2v69da
175
+ *
176
+ */
177
+ toEtokenAddress (address, prefix = true) {
178
+ const decoded = this._decode(address)
179
+
180
+ const etokenAddress = cashaddr.encode(
181
+ 'etoken',
182
+ decoded.type,
183
+ decoded.hash
184
+ )
185
+
186
+ if (prefix) return etokenAddress
187
+ return etokenAddress.split(':')[1]
188
+ }
189
+
161
190
  /**
162
191
  * @api Address.ecashtoCashAddress() ecashtoCashAddress()
163
192
  * @apiName ecashtoCashAddress
package/src/electrumx.js CHANGED
@@ -7,6 +7,7 @@ const axios = require('axios')
7
7
 
8
8
  // Local libraries.
9
9
  const Blockchain = require('./blockchain')
10
+ // const Address = require('./address')
10
11
 
11
12
  // let _this
12
13
 
@@ -32,7 +33,9 @@ class ElectrumX {
32
33
  }
33
34
  }
34
35
 
36
+ // Encapsulate dependencies
35
37
  this.blockchain = new Blockchain(config)
38
+ // this.address = new Address(config)
36
39
 
37
40
  // _this = this
38
41
  }
@@ -104,6 +107,8 @@ class ElectrumX {
104
107
  */
105
108
  async utxo (address) {
106
109
  try {
110
+ // console.log(`electrumx.js/utxo() restURL: ${this.restURL}`)
111
+
107
112
  // Handle single address.
108
113
  if (typeof address === 'string') {
109
114
  const response = await axios.get(
package/src/utxo.js CHANGED
@@ -82,7 +82,10 @@ class UTXO {
82
82
  }
83
83
 
84
84
  // Ensure the address is a BCH address.
85
- const addr = this.slp.Address.toCashAddress(address)
85
+ let addr = address
86
+ if (!addr.includes('ecash')) {
87
+ addr = this.slp.Address.toCashAddress(address)
88
+ }
86
89
 
87
90
  // Get the UTXOs associated with the address.
88
91
  const utxoData = await this.electrumx.utxo(addr)
@@ -209,7 +212,7 @@ class UTXO {
209
212
 
210
213
  return outObj
211
214
  } catch (err) {
212
- console.error('Error in bchjs.Utxo.get()')
215
+ console.error('Error in bchjs.Utxo.get(): ', err)
213
216
 
214
217
  if (err.error) throw new Error(err.error)
215
218
  throw err
@@ -0,0 +1,38 @@
1
+ /*
2
+ Integration tests for the psf-slp-indexer.js library, specific to the eCash
3
+ blockchain.
4
+ */
5
+
6
+ // Global npm libraries
7
+ const assert = require('chai').assert
8
+
9
+ // Local libraries
10
+ const BCHJS = require('../../../../src/bch-js')
11
+ let bchjs
12
+
13
+ describe('#psf-slp-indexer', () => {
14
+ beforeEach(async () => {
15
+ // Introduce a delay so that the BVT doesn't trip the rate limits.
16
+ if (process.env.IS_USING_FREE_TIER) await sleep(3000)
17
+
18
+ bchjs = new BCHJS()
19
+ })
20
+
21
+ describe('#balance', () => {
22
+ it('should get token balance for an ecash address', async () => {
23
+ const addr = 'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09'
24
+
25
+ const result = await bchjs.PsfSlpIndexer.balance(addr)
26
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
27
+
28
+ assert.property(result.balance, 'utxos')
29
+ assert.property(result.balance, 'txs')
30
+ assert.property(result.balance, 'balances')
31
+ })
32
+ })
33
+ })
34
+
35
+ // Promise-based sleep function
36
+ function sleep (ms) {
37
+ return new Promise(resolve => setTimeout(resolve, ms))
38
+ }
@@ -2,10 +2,10 @@
2
2
  Integration tests for the utxo.js library.
3
3
  */
4
4
 
5
- // const assert = require('chai').assert
5
+ const assert = require('chai').assert
6
6
 
7
- // const BCHJS = require('../../../../src/bch-js')
8
- // const bchjs = new BCHJS()
7
+ const BCHJS = require('../../../../src/bch-js')
8
+ const bchjs = new BCHJS()
9
9
 
10
10
  describe('#UTXO', () => {
11
11
  beforeEach(async () => {
@@ -14,25 +14,23 @@ describe('#UTXO', () => {
14
14
  if (process.env.IS_USING_FREE_TIER) await sleep(1500)
15
15
  })
16
16
 
17
- /*
18
17
  describe('#get', () => {
19
18
  it('should get hydrated and filtered UTXOs for an address', async () => {
20
- // const addr = 'bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9'
21
- const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
19
+ const addr = 'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09'
20
+ // const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
22
21
 
23
22
  const result = await bchjs.Utxo.get(addr)
24
23
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
25
24
 
26
- assert.isArray(result)
27
- assert.property(result[0], 'address')
28
- assert.property(result[0], 'bchUtxos')
29
- assert.property(result[0], 'nullUtxos')
30
- assert.property(result[0], 'slpUtxos')
31
- assert.isArray(result[0].bchUtxos)
32
- assert.isArray(result[0].nullUtxos)
25
+ // assert.isArray(result)
26
+ assert.property(result, 'address')
27
+ assert.property(result, 'bchUtxos')
28
+ assert.property(result, 'nullUtxos')
29
+ assert.property(result, 'slpUtxos')
30
+ assert.isArray(result.bchUtxos)
31
+ assert.isArray(result.nullUtxos)
33
32
  })
34
33
  })
35
- */
36
34
  })
37
35
 
38
36
  function sleep (ms) {
@@ -1,9 +1,12 @@
1
1
  /*
2
- Integration tests for the psf-slp-indexer.js library
2
+ Integration tests for the psf-slp-indexer.js library, specific to the BCH
3
+ blockchain.
3
4
  */
4
5
 
6
+ // Global npm libraries
5
7
  const assert = require('chai').assert
6
8
 
9
+ // Local libraries
7
10
  const BCHJS = require('../../../../src/bch-js')
8
11
  let bchjs
9
12
 
@@ -128,7 +131,7 @@ describe('#psf-slp-indexer', () => {
128
131
  describe('#getTokenData', () => {
129
132
  it('should get token data', async () => {
130
133
  const tokenId =
131
- 'f055256b938f1ecfa270459d6f12c7c8c82b66d3263c03d5074445a2b1a498a3'
134
+ 'd9aafa7acb514c597caf440ae268b5e4e955f2687e05f044cdf8fd9550d9a27b'
132
135
 
133
136
  // bchjs.PsfSlpIndexer.restURL = 'http://localhost:3000/v5/'
134
137
  const result = await bchjs.PsfSlpIndexer.getTokenData(tokenId)
@@ -935,4 +935,44 @@ describe('#address.js', () => {
935
935
  })
936
936
  })
937
937
  })
938
+
939
+ describe('#toEcashAddress', () => {
940
+ it('should convert a BCH address to an eCash address', () => {
941
+ const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
942
+
943
+ const result = bchjs.Address.toEcashAddress(cashAddr)
944
+ // console.log('result: ', result)
945
+
946
+ assert.equal(result, 'ecash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s8f6wu02p')
947
+ })
948
+
949
+ it('should convert a BCH address to an eCash address without a prefix', () => {
950
+ const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
951
+
952
+ const result = bchjs.Address.toEcashAddress(cashAddr, false)
953
+ // console.log('result: ', result)
954
+
955
+ assert.equal(result, 'qr24z7q6s26d9wr078lr2pxsmxg22cyn9s8f6wu02p')
956
+ })
957
+ })
958
+
959
+ describe('#toEtokenAddress', () => {
960
+ it('should convert a BCH address to an eToken address', () => {
961
+ const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
962
+
963
+ const result = bchjs.Address.toEtokenAddress(cashAddr)
964
+ // console.log('result: ', result)
965
+
966
+ assert.equal(result, 'etoken:qr24z7q6s26d9wr078lr2pxsmxg22cyn9sfhnv2gwk')
967
+ })
968
+
969
+ it('should convert a BCH address to an eToken address without a prefix', () => {
970
+ const cashAddr = 'bitcoincash:qr24z7q6s26d9wr078lr2pxsmxg22cyn9s7yw984vk'
971
+
972
+ const result = bchjs.Address.toEtokenAddress(cashAddr, false)
973
+ // console.log('result: ', result)
974
+
975
+ assert.equal(result, 'qr24z7q6s26d9wr078lr2pxsmxg22cyn9sfhnv2gwk')
976
+ })
977
+ })
938
978
  })