mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-service.git
synced 2026-09-22 09:12:02 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9fa44328a | ||
|
|
efaffa8e0f | ||
|
|
27a9e9f3f3 | ||
|
|
41a5071467 | ||
|
|
c6c3bb5d9c | ||
|
|
e4977a4351 | ||
|
|
fe3b45f6be | ||
|
|
d25b240112 | ||
|
|
7a89a06ddb | ||
|
|
74fb65df72 | ||
|
|
0b38247f5a | ||
|
|
473aaae307 | ||
|
|
284f4e4478 | ||
|
|
56f6dc273a | ||
|
|
4bd3387bde | ||
|
|
7e7e91460a | ||
|
|
e5f6f24570 | ||
|
|
818c85e3d4 | ||
|
|
651c4a1346 | ||
|
|
4b90ad5009 | ||
|
|
05ddf8521a | ||
|
|
88d1805685 | ||
|
|
ea104b5586 | ||
|
|
4251e2a02d | ||
|
|
5e0eb017ef | ||
|
|
86946968fe | ||
|
|
9597f1963f | ||
|
|
67d80056ff | ||
|
|
199677e4c6 | ||
|
|
86ea5c6354 | ||
|
|
fe5c573291 | ||
|
|
463d7df582 | ||
|
|
5a1ec640a4 | ||
|
|
8bb8dbcd23 | ||
|
|
ba212b0997 | ||
|
|
803496fbc3 | ||
|
|
c49ab4f58b | ||
|
|
4670bd22af | ||
|
|
b3e7c63e61 | ||
|
|
421ca359f2 | ||
|
|
4e6559ac96 | ||
|
|
1fcc92e10d | ||
|
|
f332aece98 | ||
|
|
4112b34205 | ||
|
|
30d0a62455 | ||
|
|
89f24e9940 | ||
|
|
25a75dc369 | ||
|
|
f36ce14087 | ||
|
|
df45792f47 | ||
|
|
9e71e33ccd | ||
|
|
c7b0b0c1d5 | ||
|
|
e475e5f4c3 | ||
|
|
7fff41b702 | ||
|
|
a7f7fc4e14 | ||
|
|
83ad0d8bfa | ||
|
|
ac4aaca66e | ||
|
|
0748106637 | ||
|
|
27dcc219b9 |
@@ -24,13 +24,17 @@ This repository is one step in a longer path. Here are the major milestones of t
|
||||
|
||||
## Requirements
|
||||
|
||||
- node **^14.17.0**
|
||||
- npm **^7.13.0**
|
||||
- node **^14.18.2**
|
||||
- npm **^8.3.0**
|
||||
- Docker **^20.10.8**
|
||||
- Docker Compose **^1.27.4**
|
||||
|
||||
## Installation
|
||||
|
||||
### Development Environment
|
||||
|
||||
**Note:** This software now uses an external go-ipfs IPFS node. The instructions below have not been updated to reflect this.
|
||||
|
||||
A development environment will allow you modify the code on-the-fly and contribute to the code base of this repository. [PM2](https://www.npmjs.com/package/pm2) is recommended for running this code base as an IPFS Circuit Relay.
|
||||
|
||||
- [Video: Installing ipfs-service-provider](https://youtu.be/Z0NsboIVN44)
|
||||
@@ -41,23 +45,33 @@ git clone https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-
|
||||
cd ipfs-bch-wallet-service
|
||||
./install-mongo.sh
|
||||
npm install
|
||||
cd shell-scripts
|
||||
cd shell-scripts
|
||||
./ipfs-bch-wallet-service.sh
|
||||
```
|
||||
|
||||
### Production Environment
|
||||
|
||||
The [docker](./production/docker) directory contains a Dockerfile for building a production deployment. However, there is currently [a bug](https://github.com/Permissionless-Software-Foundation/ipfs-service-provider/issues/38) preventing the Docker container from being used as a Circuit Relay.
|
||||
The [docker](./production/docker) directory contains a Dockerfile for building a production deployment.
|
||||
|
||||
```
|
||||
docker-compose build --no-cache
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
- Bring the containers up once, then take them back down with `docker-compose down`.
|
||||
- Copy the [swarm.key](./swarm.key) file to `ipfs-service-provider/production/data/go-ipfs/data/swarm.key`.
|
||||
- Bring the containers back up with `docker-compose up -d`.
|
||||
|
||||
### Operation Notes
|
||||
|
||||
- There is a memory leak in the version of js-ipfs. The app is currently configured to shut down every 8 hours to flush memory. It relies on a process manager like pm2, Docker, or systemd to restart the app after it shuts down, in order to ensure continuous operation.
|
||||
|
||||
- The PSF network operates as a private network. It does not connect or interact with the wider PSF network, relying instead on gateways to bridge the two networks, when they need to share content. This improves the performance and experience for everyone in the PSF network. To join the network, you'll need to add the [swarm.key](./swarm.key) file to the IPFS data folder.
|
||||
|
||||
- [Instructions on setting up IPFS private networks.](https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#private-networks)
|
||||
- For external installations, the swarm.key file will typically go in `~/.ipfs/swarm.key`
|
||||
- For production Docker containers, the key would go in `ipfs-service-provider/production/data/go-ipfs/data/swarm.key`
|
||||
|
||||
## Structure
|
||||
|
||||
The file layout of this repository differs from the koa-api-boilerplate. Instead, it follows the file layout of [Clean Architecture](troutsblog.com/blog/clean-architecture).
|
||||
|
||||
+5
-2
@@ -97,8 +97,11 @@ class Server {
|
||||
)
|
||||
}
|
||||
|
||||
// Attach the other IPFS controllers
|
||||
await controllers.attachControllers(app)
|
||||
// Attach the other IPFS controllers.
|
||||
// Skip if this is a test environment.
|
||||
if (config.env !== 'test') {
|
||||
await controllers.attachControllers(app)
|
||||
}
|
||||
|
||||
// ipfs-coord has a memory leak. This app shuts down after 4 hours. It
|
||||
// expects to be run by Docker or pm2, which can automatically restart
|
||||
|
||||
Generated
+422
-342
File diff suppressed because it is too large
Load Diff
+6
-4
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ipfs-bch-wallet-service",
|
||||
"version": "1.11.12",
|
||||
"version": "2.0.0",
|
||||
"description": "IPFS and REST service providing blockchain access needed by a wallet. ",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@@ -9,6 +9,7 @@
|
||||
"test:all": "export SVC_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 --recursive test/unit test/e2e/automated/",
|
||||
"test:unit": "export SVC_ENV=test && mocha --exit --timeout 15000 --recursive test/unit/",
|
||||
"test:e2e:auto": "export SVC_ENV=test && mocha --exit --timeout 30000 test/e2e/automated/",
|
||||
"test:integration": "export SVC_ENV=test && mocha --timeout 30000 --recursive test/integration/",
|
||||
"test:temp": "export SVC_ENV=test && mocha --exit --timeout 15000 -g '#rate-limit' test/unit/json-rpc/",
|
||||
"lint": "standard --env mocha --fix",
|
||||
"docs": "./node_modules/.bin/apidoc -i src/ -o docs",
|
||||
@@ -23,12 +24,12 @@
|
||||
},
|
||||
"repository": "Permissionless-Software-Foundation/ipfs-bch-wallet-service",
|
||||
"dependencies": {
|
||||
"@psf/bch-js": "4.20.28",
|
||||
"axios": "0.21.1",
|
||||
"@psf/bch-js": "5.3.0",
|
||||
"axios": "0.25.0",
|
||||
"bcryptjs": "2.4.3",
|
||||
"glob": "7.1.6",
|
||||
"ipfs": "0.60.0",
|
||||
"ipfs-coord": "6.8.5",
|
||||
"ipfs-coord": "6.8.10",
|
||||
"ipfs-http-client": "55.0.0",
|
||||
"jsonrpc-lite": "2.2.0",
|
||||
"jsonwebtoken": "8.5.1",
|
||||
@@ -64,6 +65,7 @@
|
||||
"eslint-plugin-prettier": "3.3.1",
|
||||
"eslint-plugin-standard": "4.0.0",
|
||||
"husky": "4.3.8",
|
||||
"lodash.clonedeep": "^4.5.0",
|
||||
"mocha": "8.2.1",
|
||||
"nyc": "15.1.0",
|
||||
"semantic-release": "17.4.4",
|
||||
|
||||
@@ -14,7 +14,7 @@ services:
|
||||
restart: always
|
||||
|
||||
ipfs-bch-wallet:
|
||||
image: ipfs/go-ipfs:latest
|
||||
image: ipfs/go-ipfs:v0.11.0
|
||||
container_name: ipfs-bch-wallet
|
||||
environment:
|
||||
MY_ENV_VAR: 'placeholder'
|
||||
|
||||
@@ -47,8 +47,10 @@ class Adapters {
|
||||
this.bchjs = new BCHJS({ restURL: this.config.apiServer })
|
||||
}
|
||||
|
||||
// Start the IPFS node.
|
||||
await this.ipfs.start({ bchjs: this.bchjs })
|
||||
if (this.config.env !== 'test') {
|
||||
// Start the IPFS node.
|
||||
await this.ipfs.start({ bchjs: this.bchjs })
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error in adapters/index.js/start()')
|
||||
throw err
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
const IPFSembedded = require('ipfs')
|
||||
const IPFSexternal = require('ipfs-http-client')
|
||||
const fs = require('fs')
|
||||
const http = require('http')
|
||||
|
||||
// Local libraries
|
||||
const config = require('../../../config')
|
||||
@@ -74,7 +75,8 @@ class IpfsAdapter {
|
||||
|
||||
const ipfsOptionsExternal = {
|
||||
host: this.config.ipfsHost,
|
||||
port: this.config.ipfsApiPort
|
||||
port: this.config.ipfsApiPort,
|
||||
agent: http.Agent({ keepAlive: true, maxSockets: 2000 })
|
||||
}
|
||||
|
||||
let ipfsOptions = ipfsOptionsEmbedded
|
||||
|
||||
@@ -32,7 +32,6 @@ class BCHRPC {
|
||||
this.jsonrpc = jsonrpc
|
||||
this.validators = new Validators(localConfig)
|
||||
this.rateLimit = new RateLimit()
|
||||
// this.bchjs = new BCHJS()
|
||||
this.bchjs = this.adapters.bchjs
|
||||
}
|
||||
|
||||
@@ -92,7 +91,12 @@ class BCHRPC {
|
||||
* @apiName Transactions
|
||||
* @apiGroup JSON BCH
|
||||
* @apiDescription This endpoint wraps the bchjs.Electrumx.transactions([]) function.
|
||||
* Given the 'addresses' property this endpoint returns an object with the following properties
|
||||
* There are three possible inputs:
|
||||
* - address: (required) the address to query for a transaction history
|
||||
* - sortOrder: (optional) will sort results in 'DECENDING' (default) or 'ASCENDING' order.
|
||||
* - page: (optional) will return a 'page' of 100 results. Default is 0
|
||||
*
|
||||
* Given the 'address' property this endpoint returns an object with the following properties
|
||||
*
|
||||
* - jsonrpc: "" - jsonrpc version
|
||||
* - id: "" - jsonrpc id
|
||||
@@ -101,15 +105,14 @@ class BCHRPC {
|
||||
* - receiver: "" - Receiver address
|
||||
* - value: {} - Final result value of the petition
|
||||
* - success : - Petition status
|
||||
* - transactions : [] - Transactions of the provided adresses
|
||||
* - transactions: [] - Transaction details
|
||||
* - height : - Reference to the blockchain size
|
||||
* - tx_hash: "" - Hash of the transaction
|
||||
* - address : "" - Address asociated to the transactions
|
||||
* - txs : [] - Transactions of the provided address
|
||||
* - height : - Reference to the blockchain size
|
||||
* - tx_hash: "" - Hash of the transaction
|
||||
* - address : "" - Address asociated to the transactions
|
||||
* - status: - HTTP Status Code
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* {"jsonrpc":"2.0","id":"555","method":"bch","params":{ "endpoint": "transactions", "addresses": ["bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj"]}}
|
||||
* {"jsonrpc":"2.0","id":"555","method":"bch","params":{ "endpoint": "transactions", "addresses": ["bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj"], "sortOrder": "DESCENDING", "page": 0}}
|
||||
*
|
||||
* @apiSuccessExample {json} Success-Response:
|
||||
* {
|
||||
@@ -119,17 +122,13 @@ class BCHRPC {
|
||||
* "method":"bch",
|
||||
* "reciever":"QmU86vLVbUY1UhziKB6rak7GPKRA2QHWvzNm2AjEvXNsT6",
|
||||
* "value":{
|
||||
* "address":"bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj"
|
||||
* "success":true,
|
||||
* "transactions":[
|
||||
* {
|
||||
* "transactions":[
|
||||
* {
|
||||
* "height":631219,
|
||||
* "tx_hash":"ae2daa01c8172545b5edd205ea438706bcb74e63d4084a26b9ff2a46d46dc97f"
|
||||
* }
|
||||
* ],
|
||||
* "address":"bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj"
|
||||
* }
|
||||
* "txs":[
|
||||
* {
|
||||
* "height":631219,
|
||||
* "tx_hash":"ae2daa01c8172545b5edd205ea438706bcb74e63d4084a26b9ff2a46d46dc97f"
|
||||
* }
|
||||
* ],
|
||||
* "status":200
|
||||
* }
|
||||
@@ -138,17 +137,14 @@ class BCHRPC {
|
||||
*/
|
||||
async transactions (rpcData) {
|
||||
try {
|
||||
// console.log('createUser rpcData: ', rpcData)
|
||||
// console.log('transactions rpcData: ', rpcData)
|
||||
|
||||
const addrs = rpcData.payload.params.addresses
|
||||
// const addr = rpcData.payload.params.address
|
||||
// const sortOrder = rpcData.payload.params.sortOrder
|
||||
|
||||
const data = await this.bchjs.Electrumx.transactions(addrs)
|
||||
// console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
const data = await this.useCases.bch.getTransactions(rpcData)
|
||||
|
||||
const retObj = data
|
||||
retObj.status = 200
|
||||
|
||||
return retObj
|
||||
return data
|
||||
} catch (err) {
|
||||
console.error('Error in JSON RPC BCH transactions()')
|
||||
// throw err
|
||||
@@ -331,7 +327,7 @@ class BCHRPC {
|
||||
// console.log('createUser rpcData: ', rpcData)
|
||||
|
||||
const addr = rpcData.payload.params.address
|
||||
console.log('addr: ', addr)
|
||||
// console.log('addr: ', addr)
|
||||
|
||||
const data = await this.bchjs.Utxo.get(addr)
|
||||
// console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
@@ -529,7 +525,7 @@ class BCHRPC {
|
||||
const txid = rpcData.payload.params.txid
|
||||
|
||||
const data = await this.bchjs.Transaction.get(txid.toString())
|
||||
// console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
|
||||
const retObj = data
|
||||
retObj.status = 200
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
Use Cases for interacting with the BCH blockchain.
|
||||
*/
|
||||
|
||||
class BCHUseCases {
|
||||
constructor (localConfig = {}) {
|
||||
// console.log('User localConfig: ', localConfig)
|
||||
this.adapters = localConfig.adapters
|
||||
if (!this.adapters) {
|
||||
throw new Error(
|
||||
'Instance of adapters must be passed in when instantiating BCH Use Cases library.'
|
||||
)
|
||||
}
|
||||
|
||||
// Encapsulate dependencies
|
||||
this.bchjs = this.adapters.bchjs
|
||||
}
|
||||
|
||||
// Get transaction history for an address, sorted by block height.
|
||||
async getTransactions (rpcData) {
|
||||
try {
|
||||
console.log(
|
||||
`getTransactions rpcData: ${JSON.stringify(rpcData, null, 2)}`
|
||||
)
|
||||
|
||||
// Get the list of addresses.
|
||||
const addr = rpcData.payload.params.address
|
||||
|
||||
// Default to descending sorting.
|
||||
let sortOrder = rpcData.payload.params.sortOrder
|
||||
if (!sortOrder) sortOrder = 'DESCENDING'
|
||||
// console.log('sortOrder: ', sortOrder)
|
||||
|
||||
// Default to page 1
|
||||
let page = rpcData.payload.params.page
|
||||
if (!page) page = 0
|
||||
console.log('page: ', page)
|
||||
|
||||
// Get the transaction history for the list of addresses.
|
||||
const data = await this.bchjs.Electrumx.transactions([addr])
|
||||
console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error('Could not query Fulcrum indexer.')
|
||||
}
|
||||
// console.log(`transactions: ${JSON.stringify(transactions, null, 2)}`);
|
||||
|
||||
// Sort the transactions.
|
||||
const txsArr = await this.bchjs.Electrumx.sortAllTxs(
|
||||
data.transactions[0].transactions,
|
||||
sortOrder
|
||||
)
|
||||
console.log(`txsArr: ${JSON.stringify(txsArr, null, 2)}`)
|
||||
|
||||
// Paginate the results
|
||||
const pagedResults = this.bchjs.Util.chunk100(txsArr)
|
||||
console.log(
|
||||
`pagedResults[page]: ${JSON.stringify(pagedResults[page], null, 2)}`
|
||||
)
|
||||
|
||||
const retObj = {
|
||||
address: addr,
|
||||
txs: pagedResults[page],
|
||||
status: 200,
|
||||
success: true
|
||||
}
|
||||
// console.log(`retObj: ${JSON.stringify(retObj, null, 2)}`)
|
||||
|
||||
return retObj
|
||||
} catch (err) {
|
||||
console.error('Error in JSON RPC BCH transactions(): ', err)
|
||||
// throw err
|
||||
|
||||
// Return an error response
|
||||
return {
|
||||
success: false,
|
||||
status: 422,
|
||||
message: err.message,
|
||||
endpoint: 'transactions'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BCHUseCases
|
||||
@@ -4,7 +4,9 @@
|
||||
https://troutsblog.com/blog/clean-architecture
|
||||
*/
|
||||
|
||||
// Local libraries
|
||||
const UserUseCases = require('./user')
|
||||
const BCHUseCases = require('./bch')
|
||||
|
||||
class UseCases {
|
||||
constructor (localConfig = {}) {
|
||||
@@ -17,6 +19,7 @@ class UseCases {
|
||||
|
||||
// console.log('use-cases/index.js localConfig: ', localConfig)
|
||||
this.user = new UserUseCases(localConfig)
|
||||
this.bch = new BCHUseCases(localConfig)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
/key/swarm/psk/1.0.0/
|
||||
/base16/
|
||||
bbd935b70105b03ebd0c6a3c2d2730cd22fc0d18c490ccf689b6c8a22e1bed2a
|
||||
@@ -5,10 +5,11 @@
|
||||
// Public npm libraries
|
||||
const BCHJS = require('@psf/bch-js')
|
||||
const IpfsCoord = require('ipfs-coord')
|
||||
const IPFS = require('@chris.troutner/ipfs')
|
||||
const IPFS = require('ipfs-http-client')
|
||||
const EventEmitter = require('events')
|
||||
const { v4: uid } = require('uuid')
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
const http = require('http')
|
||||
|
||||
let _this
|
||||
|
||||
@@ -88,8 +89,14 @@ class TestUtils {
|
||||
|
||||
async startIpfs () {
|
||||
try {
|
||||
const ipfsOptionsExternal = {
|
||||
host: 'localhost',
|
||||
port: 5001,
|
||||
agent: http.Agent({ keepAlive: true, maxSockets: 2000 })
|
||||
}
|
||||
|
||||
// Start the IPFS node.
|
||||
this.ipfs = await IPFS.create()
|
||||
this.ipfs = await IPFS.create(ipfsOptionsExternal)
|
||||
await this.ipfs.config.profiles.apply('server')
|
||||
|
||||
// Start ipfs-coord.
|
||||
@@ -101,7 +108,8 @@ class TestUtils {
|
||||
privateLog: this.rpcHandler, // Default to console.log
|
||||
isCircuitRelay: false,
|
||||
apiInfo: 'none',
|
||||
announceJsonLd: announceJsonLd
|
||||
announceJsonLd: announceJsonLd,
|
||||
nodeType: 'external'
|
||||
})
|
||||
|
||||
await this.ipfsCoord.start()
|
||||
@@ -214,7 +222,7 @@ class TestUtils {
|
||||
const id = uid()
|
||||
const cmd = jsonrpc.request(id, 'bch', {
|
||||
endpoint: 'transactions',
|
||||
addresses: ['bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj']
|
||||
address: 'bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj'
|
||||
})
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"uutAddr": "/ip4/127.0.0.1/tcp/5268/p2p/QmdBGB8S6sEr19gaAxJYjhRbz5ZmMiH5a1JyyMmaxcRKnY",
|
||||
"uutId": "QmdBGB8S6sEr19gaAxJYjhRbz5ZmMiH5a1JyyMmaxcRKnY"
|
||||
"uutAddr": "/ip4/88.99.188.196/tcp/4001/p2p/12D3KooWT2tDg2pHwKz84Htzh2KsfSUZbZXmoAUFnaSwUVrDsag3/p2p-circuit/ipfs/12D3KooWNuZPDb15ENgk2CYF2dByq1oK6CQ5tUpRW9CpwwNynuJY",
|
||||
"uutId": "12D3KooWNuZPDb15ENgk2CYF2dByq1oK6CQ5tUpRW9CpwwNynuJY"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
Integration tests for the BCH Use Cases library
|
||||
*/
|
||||
|
||||
// Global npm libraries
|
||||
const assert = require('chai').assert
|
||||
|
||||
// Local libraries
|
||||
const BCHUseCases = require('../../../../src/use-cases/bch')
|
||||
const Adapters = require('../../../../src/adapters')
|
||||
|
||||
describe('#BCH', () => {
|
||||
let uut
|
||||
|
||||
before(async () => {
|
||||
const adapters = new Adapters()
|
||||
await adapters.start()
|
||||
|
||||
uut = new BCHUseCases({ adapters })
|
||||
})
|
||||
|
||||
describe('#getTransactions', () => {
|
||||
it('should get transactions for an address, with default descending sorting', async () => {
|
||||
const rpcData = {
|
||||
payload: {
|
||||
params: {
|
||||
address: 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await uut.getTransactions(rpcData)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(result.success, true)
|
||||
assert.equal(result.status, 200)
|
||||
assert.isArray(result.txs)
|
||||
|
||||
// Assert descending sort order.
|
||||
assert.isAbove(result.txs[0].height, result.txs[1].height)
|
||||
})
|
||||
|
||||
it('should get transactions for an address, with explicit descending sorting', async () => {
|
||||
const rpcData = {
|
||||
payload: {
|
||||
params: {
|
||||
address: 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v',
|
||||
sortOrder: 'DESCENDING'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await uut.getTransactions(rpcData)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(result.success, true)
|
||||
assert.equal(result.status, 200)
|
||||
assert.isArray(result.txs)
|
||||
|
||||
// Assert descending sort order.
|
||||
assert.isAbove(result.txs[0].height, result.txs[1].height)
|
||||
})
|
||||
|
||||
it('should sort in ascending order', async () => {
|
||||
const rpcData = {
|
||||
payload: {
|
||||
params: {
|
||||
address: 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v',
|
||||
sortOrder: 'ASCENDING'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await uut.getTransactions(rpcData)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(result.success, true)
|
||||
assert.equal(result.status, 200)
|
||||
assert.isArray(result.txs)
|
||||
|
||||
// Assert descending sort order.
|
||||
assert.isAbove(result.txs[1].height, result.txs[0].height)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -199,11 +199,6 @@ describe('#BCHRPC', () => {
|
||||
|
||||
describe('#transactions', () => {
|
||||
it('should return data from bchjs', async () => {
|
||||
// Mock dependencies
|
||||
sandbox
|
||||
.stub(uut.bchjs.Electrumx, 'transactions')
|
||||
.resolves({ success: true })
|
||||
|
||||
// Generate the parsed data that the main router would pass to this
|
||||
// endpoint.
|
||||
const id = uid()
|
||||
@@ -224,7 +219,7 @@ describe('#BCHRPC', () => {
|
||||
it('should return an error for invalid address', async () => {
|
||||
// Force an error
|
||||
sandbox
|
||||
.stub(uut.bchjs.Electrumx, 'transactions')
|
||||
.stub(uut.useCases.bch, 'getTransactions')
|
||||
.rejects(new Error('Invalid address'))
|
||||
|
||||
// Generate the parsed data that the main router would pass to this
|
||||
@@ -442,7 +437,11 @@ describe('#BCHRPC', () => {
|
||||
describe('#pubKey', () => {
|
||||
it('should return data from bchjs', async () => {
|
||||
// Mock dependencies
|
||||
const mock = { success: true, publicKey: '033f267fec0f7eb2b27f8c2e3052b3d03b09d36b47de4082ffb638ffb334ef0eee' }
|
||||
const mock = {
|
||||
success: true,
|
||||
publicKey:
|
||||
'033f267fec0f7eb2b27f8c2e3052b3d03b09d36b47de4082ffb638ffb334ef0eee'
|
||||
}
|
||||
sandbox.stub(uut.bchjs.encryption, 'getPubKey').resolves(mock)
|
||||
|
||||
// Generate the parsed data that the main router would pass to this
|
||||
|
||||
@@ -56,7 +56,8 @@ const localdb = {
|
||||
const bchjs = {
|
||||
Electrumx: {
|
||||
transactions: () => {},
|
||||
balance: () => {}
|
||||
balance: () => {},
|
||||
sortAllTxs: () => {}
|
||||
},
|
||||
Utxo: {
|
||||
get: () => {}
|
||||
@@ -69,6 +70,9 @@ const bchjs = {
|
||||
},
|
||||
encryption: {
|
||||
getPubKey: () => {}
|
||||
},
|
||||
Util: {
|
||||
chunk100: () => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Mock data for use-cases/bch/index.js unit tests
|
||||
*/
|
||||
|
||||
const fulcrumOut01 = {
|
||||
success: true,
|
||||
transactions: [
|
||||
{
|
||||
transactions: [
|
||||
{
|
||||
height: 560430,
|
||||
tx_hash:
|
||||
'3e1f3e882be9c03897eeb197224bf87f312be556a89f4308fabeeeabcf9bc851'
|
||||
},
|
||||
{
|
||||
height: 560534,
|
||||
tx_hash:
|
||||
'4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280'
|
||||
}
|
||||
],
|
||||
address: 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fulcrumOut01
|
||||
}
|
||||
@@ -31,12 +31,22 @@ class UserUseCaseMock {
|
||||
}
|
||||
}
|
||||
|
||||
class BCH {
|
||||
async getTransactions(rpcData) {
|
||||
return {
|
||||
success: true,
|
||||
status: 200
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class UseCasesMock {
|
||||
constuctor(localConfig = {}) {
|
||||
// this.user = new UserUseCaseMock(localConfig)
|
||||
}
|
||||
|
||||
user = new UserUseCaseMock()
|
||||
bch = new BCH()
|
||||
}
|
||||
|
||||
module.exports = UseCasesMock
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
Unit tests for the BCH Use Cases
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
const clone = require('lodash.clonedeep')
|
||||
|
||||
// Local support libraries
|
||||
const BCHUseCases = require('../../../../src/use-cases/bch')
|
||||
const adapters = require('../../mocks/adapters')
|
||||
const mockDataLib = require('../../mocks/use-cases/bch/bch.use-cases.mocks.js')
|
||||
|
||||
describe('#bch-use-case', () => {
|
||||
let uut
|
||||
let sandbox
|
||||
let mockData
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
mockData = clone(mockDataLib)
|
||||
|
||||
uut = new BCHUseCases({ adapters })
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error if adapters are not passed in', () => {
|
||||
try {
|
||||
uut = new BCHUseCases()
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Instance of adapters must be passed in when instantiating BCH Use Cases library.'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getTransactions', () => {
|
||||
it('should get transactions for an address', async () => {
|
||||
// Mock dependencies
|
||||
sandbox
|
||||
.stub(uut.bchjs.Electrumx, 'transactions')
|
||||
.resolves(mockData.fulcrumOut01)
|
||||
sandbox
|
||||
.stub(uut.bchjs.Electrumx, 'sortAllTxs')
|
||||
.resolves(mockData.fulcrumOut01.transactions[0].transactions)
|
||||
sandbox
|
||||
.stub(uut.bchjs.Util, 'chunk100')
|
||||
.returns([mockData.fulcrumOut01.transactions[0].transactions])
|
||||
|
||||
const address = 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v'
|
||||
const rpcData = {
|
||||
payload: {
|
||||
params: {
|
||||
address
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await uut.getTransactions(rpcData)
|
||||
// console.log('result: ', result)
|
||||
|
||||
// Assert expected values and properties.
|
||||
assert.equal(result.address, address)
|
||||
assert.equal(result.status, 200)
|
||||
assert.equal(result.success, true)
|
||||
assert.isArray(result.txs)
|
||||
})
|
||||
|
||||
it('should return error if Fulcrum communication is not possible', async () => {
|
||||
// Force an error
|
||||
sandbox
|
||||
.stub(uut.bchjs.Electrumx, 'transactions')
|
||||
.resolves({ success: false })
|
||||
|
||||
const address = 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v'
|
||||
const rpcData = {
|
||||
payload: {
|
||||
params: {
|
||||
address
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await uut.getTransactions(rpcData)
|
||||
// console.log('result: ', result)
|
||||
|
||||
assert.equal(result.success, false)
|
||||
assert.equal(result.status, 422)
|
||||
assert.equal(result.message, 'Could not query Fulcrum indexer.')
|
||||
assert.equal(result.endpoint, 'transactions')
|
||||
})
|
||||
|
||||
it('should return error if there is an error', async () => {
|
||||
// Force an error
|
||||
sandbox
|
||||
.stub(uut.bchjs.Electrumx, 'transactions')
|
||||
.rejects(new Error('test error'))
|
||||
|
||||
const address = 'bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v'
|
||||
const rpcData = {
|
||||
payload: {
|
||||
params: {
|
||||
address
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await uut.getTransactions(rpcData)
|
||||
// console.log('result: ', result)
|
||||
|
||||
assert.equal(result.success, false)
|
||||
assert.equal(result.status, 422)
|
||||
assert.equal(result.message, 'test error')
|
||||
assert.equal(result.endpoint, 'transactions')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -7,9 +7,6 @@ const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
|
||||
// Local support libraries
|
||||
// const testUtils = require('../../utils/test-utils')
|
||||
|
||||
// Unit under test (uut)
|
||||
const UseCases = require('../../../src/use-cases')
|
||||
const adapters = require('../mocks/adapters')
|
||||
|
||||
|
||||
@@ -9,9 +9,6 @@ const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
|
||||
// Local support libraries
|
||||
// const testUtils = require('../../utils/test-utils')
|
||||
|
||||
// Unit under test (uut)
|
||||
const UserLib = require('../../../src/use-cases/user')
|
||||
const adapters = require('../mocks/adapters')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user