x402-bch-axios 1.0.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/.editorconfig ADDED
@@ -0,0 +1,30 @@
1
+ # http://editorconfig.org
2
+
3
+ # A special property that should be specified at the top of the file outside of
4
+ # any sections. Set to true to stop .editor config file search on current file
5
+ root = true
6
+
7
+ [*]
8
+ # Indentation style
9
+ # Possible values - tab, space
10
+ indent_style = space
11
+
12
+ # Indentation size in single-spaced characters
13
+ # Possible values - an integer, tab
14
+ indent_size = 2
15
+
16
+ # Line ending file format
17
+ # Possible values - lf, crlf, cr
18
+ end_of_line = lf
19
+
20
+ # File character encoding
21
+ # Possible values - latin1, utf-8, utf-16be, utf-16le
22
+ charset = utf-8
23
+
24
+ # Denotes whether to trim whitespace at the end of lines
25
+ # Possible values - true, false
26
+ trim_trailing_whitespace = true
27
+
28
+ # Denotes whether file should end with a newline
29
+ # Possible values - true, false
30
+ insert_final_newline = true
package/.eslintrc.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "standard",
3
+ "env": {
4
+ "node": true,
5
+ "mocha": true
6
+ },
7
+ "parserOptions": {
8
+ "ecmaVersion": 8
9
+ }
10
+ }
package/.on-save.json ADDED
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "srcDir": "",
4
+ "destDir": "",
5
+ "files": "**/*.js",
6
+ "command": "npm run lint"
7
+ }
8
+ ]
package/.travis.yml ADDED
@@ -0,0 +1,33 @@
1
+ # This is a node.js v8+ JavaScript project
2
+ language: node_js
3
+ node_js:
4
+ - "10"
5
+
6
+ # Build on Ubuntu Trusty (14.04)
7
+ # https://docs.travis-ci.com/user/reference/trusty/#javascript-and-nodejs-images
8
+ dist: xenial
9
+ sudo: required
10
+
11
+ # Use Docker
12
+ services:
13
+ - docker
14
+
15
+ before_install:
16
+ #- npm install -g mocha
17
+
18
+ # https://github.com/greenkeeperio/greenkeeper-lockfile/issues/156
19
+ #install: case $TRAVIS_BRANCH in greenkeeper*) npm i;; *) npm ci;; esac;
20
+ install:
21
+ - npm install
22
+
23
+ script: "npm run test"
24
+
25
+ # Send coverage data to Coveralls
26
+ after_success:
27
+ - npm run coverage
28
+
29
+ deploy:
30
+ provider: script
31
+ skip_cleanup: true
32
+ script:
33
+ - npx semantic-release
package/LICENSE.md ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2025 Chris Troutner <chris.troutner@gmail.com>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # x402-bch-axios
2
+
3
+ This is a placeholder for what will become the x402-bch-axios library.
4
+
5
+ # Licence
6
+
7
+ [MIT](LICENSE.md)
package/apidoc.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "sampleUrl": null
3
+ }
package/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /*
2
+ An npm JavaScript library for front end web apps. Implements a minimal
3
+ Bitcoin Cash wallet.
4
+ */
5
+
6
+ /* eslint-disable no-async-promise-executor */
7
+
8
+ // Global npm libraries
9
+ import BCHJS from '@psf/bch-js'
10
+
11
+ // Local libraries
12
+ import Util from './lib/util.js'
13
+ const util = new Util()
14
+
15
+ class BoilerplateLib {
16
+ constructor () {
17
+ // Encapsulate dependencies
18
+ this.bchjs = new BCHJS()
19
+ this.util = util
20
+ }
21
+ }
22
+
23
+ export default BoilerplateLib
package/lib/util.js ADDED
@@ -0,0 +1,46 @@
1
+ /*
2
+ An example of a typical utility library. Things to notice:
3
+ - This library is exported as a Class.
4
+ - External dependencies are embedded into the class 'this' object: this.bchjs
5
+ */
6
+
7
+ 'use strict'
8
+
9
+ // Global npm libraries
10
+ import BCHJS from '@psf/bch-js'
11
+
12
+ class UtilLib {
13
+ constructor () {
14
+ // Encapsulate dependencies
15
+ this.bchjs = new BCHJS()
16
+
17
+ // Bind 'this' object to all class methods
18
+ this.getBchData = this.getBchData.bind(this)
19
+ }
20
+
21
+ async getBchData (addr) {
22
+ try {
23
+ // Validate Input
24
+ if (typeof addr !== 'string') throw new Error('Address must be a string')
25
+
26
+ const balance = await this.bchjs.Electrumx.balance(addr)
27
+
28
+ const utxos = await this.bchjs.Electrumx.utxo(addr)
29
+
30
+ const bchData = {
31
+ balance: balance.balance,
32
+ utxos: utxos.utxos
33
+ }
34
+ // console.log(`bchData: ${JSON.stringify(bchData, null, 2)}`)
35
+
36
+ return bchData
37
+ } catch (err) {
38
+ // Optional log to indicate the source of the error. This would normally
39
+ // be written with a logging app like Winston.
40
+ console.log('Error in util.js/getBalance()')
41
+ throw err
42
+ }
43
+ }
44
+ }
45
+
46
+ export default UtilLib
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "x402-bch-axios",
3
+ "version": "1.0.0",
4
+ "description": "Axios wrapper for x402 payment protocol with Bitcoin Cash (BCH) support.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "start": "node index.js",
9
+ "test": "npm run lint && TEST=unit c8 mocha test/unit/",
10
+ "test:integration": "mocha --timeout 25000 test/integration/",
11
+ "lint": "standard --env mocha --fix",
12
+ "docs": "./node_modules/.bin/apidoc -i src/ -o docs",
13
+ "coverage": "c8 --reporter=html mocha test/unit/ --exit"
14
+ },
15
+ "keywords": [
16
+ "bitcoin",
17
+ "bitcoin cash",
18
+ "x402",
19
+ "javascript",
20
+ "cryptocurrency"
21
+ ],
22
+ "author": "Chris Troutner <chris.troutner@gmail.com>",
23
+ "license": "MIT",
24
+ "apidoc": {
25
+ "title": "x402-bch-axios",
26
+ "url": "localhost:5000"
27
+ },
28
+ "repository": "x402-bch/x402-bch-axios",
29
+ "dependencies": {
30
+ "@psf/bch-js": "6.8.3",
31
+ "apidoc": "1.2.0"
32
+ },
33
+ "devDependencies": {
34
+ "c8": "10.1.3",
35
+ "chai": "6.2.0",
36
+ "husky": "9.1.7",
37
+ "lodash.clonedeep": "4.5.0",
38
+ "mocha": "11.7.5",
39
+ "semantic-release": "25.0.2",
40
+ "sinon": "21.0.0",
41
+ "standard": "17.1.2"
42
+ },
43
+ "release": {
44
+ "publish": [
45
+ {
46
+ "path": "@semantic-release/npm",
47
+ "npmPublish": true
48
+ }
49
+ ]
50
+ },
51
+ "husky": {
52
+ "hooks": {
53
+ "pre-commit": "npm run lint"
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ Integration tests for the util.js utility library.
3
+ */
4
+
5
+ // npm libraries
6
+ import chai from 'chai'
7
+
8
+ // Unit under test
9
+ import UtilLib from '../../lib/util.js'
10
+
11
+ // Locally global variables.
12
+ const assert = chai.assert
13
+ const uut = new UtilLib()
14
+
15
+ describe('#util.js', () => {
16
+ describe('#getBchData', () => {
17
+ it('should get BCH data on an address', async () => {
18
+ const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
19
+
20
+ const bchData = await uut.getBchData(addr)
21
+
22
+ // Assert that top-level properties exist.
23
+ assert.property(bchData, 'balance')
24
+ assert.property(bchData, 'utxos')
25
+
26
+ // Assert essential UTXOs properties exist.
27
+ assert.isArray(bchData.utxos)
28
+ assert.property(bchData.utxos[0], 'tx_pos')
29
+ assert.property(bchData.utxos[0], 'tx_hash')
30
+ assert.property(bchData.utxos[0], 'value')
31
+ })
32
+ })
33
+ })
@@ -0,0 +1,30 @@
1
+ /*
2
+ A mocking library for util.js unit tests.
3
+ A mocking library contains data to use in place of the data that would come
4
+ from an external dependency.
5
+ */
6
+
7
+ 'use strict'
8
+
9
+ const mockBalance = {
10
+ success: true,
11
+ balance: {
12
+ confirmed: 1000,
13
+ unconfirmed: 0
14
+ }
15
+ }
16
+
17
+ const mockUtxos = {
18
+ success: true,
19
+ utxos: [
20
+ {
21
+ height: 601861,
22
+ tx_hash:
23
+ '6181c669614fa18039a19b23eb06806bfece1f7514ab457c3bb82a40fe171a6d',
24
+ tx_pos: 0,
25
+ value: 1000
26
+ }
27
+ ]
28
+ }
29
+
30
+ export default { mockBalance, mockUtxos }
@@ -0,0 +1,69 @@
1
+ /*
2
+ Unit tests for the util.js utility library.
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+ import cloneDeep from 'lodash.clonedeep'
9
+
10
+ // Mocking data libraries.
11
+ import mockDataLib from './mocks/util-mocks.js'
12
+
13
+ // Unit under test
14
+ import UtilLib from '../../lib/util.js'
15
+
16
+ describe('#util.js', () => {
17
+ let sandbox
18
+ let mockData
19
+ let uut
20
+
21
+ beforeEach(() => {
22
+ // Restore the sandbox before each test.
23
+ sandbox = sinon.createSandbox()
24
+
25
+ // Clone the mock data.
26
+ mockData = cloneDeep(mockDataLib)
27
+
28
+ uut = new UtilLib()
29
+ })
30
+
31
+ afterEach(() => sandbox.restore())
32
+
33
+ describe('#getBchData', () => {
34
+ it('should throw error if address is not a string', async () => {
35
+ try {
36
+ const addr = 1234
37
+
38
+ await uut.getBchData(addr)
39
+
40
+ assert.equal(true, false, 'unexpected result')
41
+ } catch (err) {
42
+ assert.include(err.message, 'Address must be a string')
43
+ }
44
+ })
45
+
46
+ it('should get BCH data on an address', async () => {
47
+ // Mock external dependencies.
48
+ sandbox
49
+ .stub(uut.bchjs.Electrumx, 'balance')
50
+ .resolves(mockData.mockBalance)
51
+ sandbox.stub(uut.bchjs.Electrumx, 'utxo').resolves(mockData.mockUtxos)
52
+
53
+ const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
54
+
55
+ const bchData = await uut.getBchData(addr)
56
+ // console.log(`bchData: ${JSON.stringify(bchData, null, 2)}`)
57
+
58
+ // Assert that top-level properties exist.
59
+ assert.property(bchData, 'balance')
60
+ assert.property(bchData, 'utxos')
61
+
62
+ // Assert essential UTXOs properties exist.
63
+ assert.isArray(bchData.utxos)
64
+ assert.property(bchData.utxos[0], 'tx_pos')
65
+ assert.property(bchData.utxos[0], 'tx_hash')
66
+ assert.property(bchData.utxos[0], 'value')
67
+ })
68
+ })
69
+ })