@psf/bch-js 4.20.28 → 4.21.0
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 +3 -3
- package/src/address.js +15 -0
- package/src/bch-js.js +2 -0
- package/src/ecash.js +47 -0
- package/src/slp/address.js +1 -1
- package/test/integration/blockchain.js +8 -0
- package/test/unit/ecash.js +56 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psf/bch-js",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.21.0",
|
|
4
4
|
"description": "The FullStack.cash JavaScript library for Bitcoin Cash and SLP Tokens",
|
|
5
5
|
"author": "Chris Troutner <chris.troutner@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"test:integration:local:abc": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/",
|
|
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
|
-
"test:integration:decatur:bchn": "export RESTURL=http://192.168.
|
|
22
|
-
"test:integration:decatur:abc": "export RESTURL=http://192.168.
|
|
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.141: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/address.js
CHANGED
|
@@ -186,6 +186,21 @@ class Address {
|
|
|
186
186
|
return cashAddress.split(':')[1]
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
+
/**
|
|
190
|
+
* @api Address.toHash160() toHash160()
|
|
191
|
+
* @apiName toHash160
|
|
192
|
+
* @apiGroup Address
|
|
193
|
+
* @apiDescription Converts any address format to hash160
|
|
194
|
+
*
|
|
195
|
+
* @apiExample Example usage:
|
|
196
|
+
* // cash address mainnet p2pkh
|
|
197
|
+
* bchjs.Address.toHash160("bitcoincash:qptnmya5wkly7xf97wm5ak23yqdsz3l2cyj7k9vyyh")
|
|
198
|
+
* // 573d93b475be4f1925f3b74ed951201b0147eac1
|
|
199
|
+
*
|
|
200
|
+
* // cash address mainnet p2sh
|
|
201
|
+
* bchjs.Address.toHash160("bitcoincash:pp7ushdxf5we8mcpaa3wqgsuqt639cu59ur5xu5fug")
|
|
202
|
+
* // 7dc85da64d1d93ef01ef62e0221c02f512e3942f
|
|
203
|
+
*/
|
|
189
204
|
// Converts any address format to hash160
|
|
190
205
|
toHash160 (address) {
|
|
191
206
|
const legacyAddress = this.toLegacyAddress(address)
|
package/src/bch-js.js
CHANGED
|
@@ -33,6 +33,7 @@ const Encryption = require('./encryption')
|
|
|
33
33
|
const Utxo = require('./utxo')
|
|
34
34
|
const Transaction = require('./transaction')
|
|
35
35
|
const DSProof = require('./dsproof')
|
|
36
|
+
const Ecash = require('./ecash')
|
|
36
37
|
|
|
37
38
|
// Indexers
|
|
38
39
|
const Ninsight = require('./ninsight')
|
|
@@ -118,6 +119,7 @@ class BCHJS {
|
|
|
118
119
|
this.Transaction = new Transaction(libConfig)
|
|
119
120
|
|
|
120
121
|
this.DSProof = new DSProof(libConfig)
|
|
122
|
+
this.eCash = new Ecash()
|
|
121
123
|
}
|
|
122
124
|
}
|
|
123
125
|
|
package/src/ecash.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Utility library for converting units with eCash.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
class eCash {
|
|
6
|
+
/**
|
|
7
|
+
* @api eCash.toSatoshi() toSatoshi()
|
|
8
|
+
* @apiName toSatoshi
|
|
9
|
+
* @apiGroup eCash
|
|
10
|
+
* @apiDescription
|
|
11
|
+
* Convert XEC units into satoshi units
|
|
12
|
+
*
|
|
13
|
+
* @apiExample Example usage:
|
|
14
|
+
* // convert 10,704.35 XEC to satoshis:
|
|
15
|
+
* bchjs.eCash.toSatoshi(10704.35)
|
|
16
|
+
* // 1070435
|
|
17
|
+
*/
|
|
18
|
+
toSatoshi (xec) {
|
|
19
|
+
if (typeof xec !== 'number') {
|
|
20
|
+
throw new Error('input must be a floating number representing XEC')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return Math.floor(xec * 100)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @api eCash.toXec() toXec()
|
|
28
|
+
* @apiName toXec
|
|
29
|
+
* @apiGroup eCash
|
|
30
|
+
* @apiDescription
|
|
31
|
+
* Convert satoshi units to XEC units
|
|
32
|
+
*
|
|
33
|
+
* @apiExample Example usage:
|
|
34
|
+
* // convert 1,070,435 satoshis to XEC:
|
|
35
|
+
* bchjs.eCash.toSatoshi(1070435)
|
|
36
|
+
* // 10704.35
|
|
37
|
+
*/
|
|
38
|
+
toXec (sats) {
|
|
39
|
+
if (typeof sats !== 'number') {
|
|
40
|
+
throw new Error('input must be a floating number representing satoshis')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return sats / 100
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = eCash
|
package/src/slp/address.js
CHANGED
|
@@ -36,7 +36,7 @@ class Address extends BCHJSAddress {
|
|
|
36
36
|
* bchjs.SLP.Address.toSLPAddress('qzm47qz5ue99y9yl4aca7jnz7dwgdenl85jkfx3znl')
|
|
37
37
|
* // simpleledger:qzm47qz5ue99y9yl4aca7jnz7dwgdenl857dzayzdp
|
|
38
38
|
*
|
|
39
|
-
* //
|
|
39
|
+
* // testnet legacy
|
|
40
40
|
* bchjs.SLP.Address.toSLPAddress('msDbtTj7kWXPpYaR7PQmMK84i66fJqQMLx')
|
|
41
41
|
* // slptest:qzq9je6pntpva3wf6scr7mlnycr54sjgeqauyclpwv
|
|
42
42
|
*
|
|
@@ -313,6 +313,14 @@ describe('#blockchain', () => {
|
|
|
313
313
|
assert.isString(result)
|
|
314
314
|
})
|
|
315
315
|
})
|
|
316
|
+
|
|
317
|
+
describe('#getBlockchainInfo', () => {
|
|
318
|
+
it('should get info about the blockchain', async () => {
|
|
319
|
+
const result = await bchjs.Blockchain.getBlockchainInfo()
|
|
320
|
+
|
|
321
|
+
console.log(`blockchain info: ${JSON.stringify(result, null, 2)}`)
|
|
322
|
+
})
|
|
323
|
+
})
|
|
316
324
|
})
|
|
317
325
|
|
|
318
326
|
function sleep (ms) {
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Unit tests for eCash library.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Global npm libraries
|
|
6
|
+
const assert = require('chai').assert
|
|
7
|
+
const Ecash = require('../../src/ecash')
|
|
8
|
+
const uut = new Ecash()
|
|
9
|
+
|
|
10
|
+
describe('#eCash', () => {
|
|
11
|
+
describe('#toSatoshi', () => {
|
|
12
|
+
it('should convert XEC to satoshis', () => {
|
|
13
|
+
const xec = 10704.35
|
|
14
|
+
|
|
15
|
+
const result = uut.toSatoshi(xec)
|
|
16
|
+
|
|
17
|
+
assert.equal(result, 1070435)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should throw an error if input is not a number', () => {
|
|
21
|
+
try {
|
|
22
|
+
uut.toSatoshi('test')
|
|
23
|
+
|
|
24
|
+
assert.fail('Unexpected code path')
|
|
25
|
+
} catch (err) {
|
|
26
|
+
assert.equal(
|
|
27
|
+
err.message,
|
|
28
|
+
'input must be a floating number representing XEC'
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('#toXec', () => {
|
|
35
|
+
it('should convert satoshis to XEC', () => {
|
|
36
|
+
const sats = 1070435
|
|
37
|
+
|
|
38
|
+
const result = uut.toXec(sats)
|
|
39
|
+
|
|
40
|
+
assert.equal(result, 10704.35)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should throw an error if input is not a number', () => {
|
|
44
|
+
try {
|
|
45
|
+
uut.toXec('test')
|
|
46
|
+
|
|
47
|
+
assert.fail('Unexpected code path')
|
|
48
|
+
} catch (err) {
|
|
49
|
+
assert.equal(
|
|
50
|
+
err.message,
|
|
51
|
+
'input must be a floating number representing satoshis'
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
})
|