mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-consumer.git
synced 2026-09-21 16:52:03 -07:00
fix(bch): Moved bch use-cases to adapters
This commit is contained in:
@@ -8,23 +8,23 @@ const { v4: uid } = require('uuid')
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
|
||||
// Local libraries
|
||||
const { wlogger } = require('../adapters/wlogger')
|
||||
const { wlogger } = require('../wlogger')
|
||||
|
||||
let _this
|
||||
|
||||
class BchUseCases {
|
||||
class BchAdapter {
|
||||
constructor (localConfig = {}) {
|
||||
// console.log('User localConfig: ', localConfig)
|
||||
this.adapters = localConfig.adapters
|
||||
if (!this.adapters) {
|
||||
// console.log('BCH localConfig: ', localConfig)
|
||||
this.ipfs = localConfig.ipfs
|
||||
if (!this.ipfs) {
|
||||
throw new Error(
|
||||
'Instance of adapters must be passed in when instantiating BCH Use Cases library.'
|
||||
'An instance of IPFS must be passed when instantiating the BCH Adapter library.'
|
||||
)
|
||||
}
|
||||
this.eventEmitter = localConfig.eventEmitter
|
||||
if (!this.eventEmitter) {
|
||||
throw new Error(
|
||||
'An instance of an EventEmitter must be passed when instantiating the BCH Use Cases library.'
|
||||
'An instance of an EventEmitter must be passed when instantiating the adapters.'
|
||||
)
|
||||
}
|
||||
|
||||
@@ -61,12 +61,12 @@ class BchUseCases {
|
||||
async getStatus () {
|
||||
try {
|
||||
console.log(
|
||||
'this.adapters.ipfs.ipfsCoordAdapter.state: ',
|
||||
this.adapters.ipfs.ipfsCoordAdapter.state
|
||||
'this.ipfs.ipfsCoordAdapter.state: ',
|
||||
this.ipfs.ipfsCoordAdapter.state
|
||||
)
|
||||
|
||||
const status = {
|
||||
state: this.adapters.ipfs.ipfsCoordAdapter.state
|
||||
state: this.ipfs.ipfsCoordAdapter.state
|
||||
}
|
||||
|
||||
return status
|
||||
@@ -84,7 +84,8 @@ class BchUseCases {
|
||||
|
||||
// Throw an error if this IPFS node has not yet made a connection to a
|
||||
// wallet service provider.
|
||||
const selectedProvider = this.adapters.ipfs.ipfsCoordAdapter.state.selectedServiceProvider
|
||||
const selectedProvider =
|
||||
this.ipfs.ipfsCoordAdapter.state.selectedServiceProvider
|
||||
if (!selectedProvider) {
|
||||
throw new Error('No BCH Wallet Service provider available yet.')
|
||||
}
|
||||
@@ -103,8 +104,8 @@ class BchUseCases {
|
||||
// console.log('cmdStr: ', cmdStr)
|
||||
|
||||
// Send the RPC command to selected wallet service.
|
||||
const thisNode = this.adapters.ipfs.ipfsCoordAdapter.ipfsCoord.thisNode
|
||||
await this.adapters.ipfs.ipfsCoordAdapter.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
const thisNode = this.ipfs.ipfsCoordAdapter.ipfsCoord.thisNode
|
||||
await this.ipfs.ipfsCoordAdapter.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
selectedProvider,
|
||||
cmdStr,
|
||||
thisNode
|
||||
@@ -157,7 +158,7 @@ class BchUseCases {
|
||||
|
||||
// Wait between loops.
|
||||
// await this.sleep(1000)
|
||||
await this.adapters.ipfs.ipfsCoordAdapter.bchjs.Util.sleep(2000)
|
||||
await this.ipfs.ipfsCoordAdapter.bchjs.Util.sleep(2000)
|
||||
|
||||
cnt++
|
||||
|
||||
@@ -173,4 +174,4 @@ class BchUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BchUseCases
|
||||
module.exports = BchAdapter
|
||||
@@ -16,6 +16,7 @@ const Nodemailer = require('./nodemailer')
|
||||
// const { wlogger } = require('./wlogger')
|
||||
const JSONFiles = require('./json-files')
|
||||
const FullStackJWT = require('./fullstack-jwt')
|
||||
const BCH = require('./bch')
|
||||
|
||||
const config = require('../../config')
|
||||
|
||||
@@ -31,6 +32,7 @@ class Adapters {
|
||||
|
||||
// Encapsulate dependencies
|
||||
this.ipfs = new IPFSAdapter(localConfig)
|
||||
localConfig.ipfs = this.ipfs
|
||||
this.localdb = new LocalDB()
|
||||
this.logapi = new LogsAPI()
|
||||
this.passport = new Passport()
|
||||
@@ -38,6 +40,7 @@ class Adapters {
|
||||
this.jsonFiles = new JSONFiles()
|
||||
this.bchjs = new BCHJS()
|
||||
this.config = config
|
||||
this.bch = new BCH(localConfig)
|
||||
|
||||
// Get a valid JWT API key and instance bch-js.
|
||||
this.fullStackJwt = new FullStackJWT(config)
|
||||
|
||||
@@ -41,7 +41,7 @@ class BchRESTControllerLib {
|
||||
*/
|
||||
async getStatus (ctx) {
|
||||
try {
|
||||
const status = await this.useCases.bch.getStatus()
|
||||
const status = await this.adapters.bch.getStatus()
|
||||
|
||||
ctx.body = { status }
|
||||
} catch (err) {
|
||||
@@ -100,7 +100,7 @@ class BchRESTControllerLib {
|
||||
try {
|
||||
const addrs = ctx.request.body.addresses
|
||||
|
||||
const data = await this.useCases.bch.getBalances(addrs)
|
||||
const data = await this.adapters.bch.getBalances(addrs)
|
||||
// console.log(`data: ${JSON.stringify(data, null, 2)}`)
|
||||
|
||||
ctx.body = data
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
const UserUseCases = require('./user')
|
||||
const BchUseCases = require('./bch')
|
||||
|
||||
class UseCases {
|
||||
constructor (localConfig = {}) {
|
||||
@@ -24,7 +23,6 @@ class UseCases {
|
||||
|
||||
// console.log('use-cases/index.js localConfig: ', localConfig)
|
||||
this.user = new UserUseCases(localConfig)
|
||||
this.bch = new BchUseCases(localConfig)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ const cloneDeep = require('lodash.clonedeep')
|
||||
// const testUtils = require('../../utils/test-utils')
|
||||
|
||||
// Unit under test (uut)
|
||||
const BchUseCases = require('../../../src/use-cases/bch')
|
||||
const BchAdapter = require('../../../src/adapters/bch')
|
||||
const adapters = require('../mocks/adapters')
|
||||
const eventEmitter = new EventEmitter()
|
||||
const mockDataLib = require('../mocks/use-cases/rest-api-mocks')
|
||||
@@ -34,43 +34,45 @@ describe('#bch-use-case', () => {
|
||||
|
||||
mockData = cloneDeep(mockDataLib)
|
||||
|
||||
uut = new BchUseCases({ adapters, eventEmitter })
|
||||
const ipfs = adapters.ipfs
|
||||
uut = new BchAdapter({ ipfs, eventEmitter })
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error if adapters are not passed in', () => {
|
||||
it('should throw an error if ipfs is not passed in', () => {
|
||||
try {
|
||||
uut = new BchUseCases()
|
||||
uut = new BchAdapter()
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Instance of adapters must be passed in when instantiating BCH Use Cases library.'
|
||||
'An instance of IPFS must be passed when instantiating the BCH Adapter library.'
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw an error if adapters are not passed in', () => {
|
||||
try {
|
||||
uut = new BchUseCases({ adapters })
|
||||
const ipfs = {}
|
||||
uut = new BchAdapter({ ipfs })
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'An instance of an EventEmitter must be passed when instantiating the BCH Use Cases library.'
|
||||
'An instance of an EventEmitter must be passed when instantiating the adapters.'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('#waitForRPCResponse', () => {
|
||||
it('should should resolve when data is received', async () => {
|
||||
it('should resolve when data is received', async () => {
|
||||
// Mock dependencies
|
||||
uut.adapters.ipfs.ipfsCoordAdapter.bchjs = {
|
||||
uut.ipfs.ipfsCoordAdapter.bchjs = {
|
||||
Util: {
|
||||
sleep: () => {}
|
||||
}
|
||||
@@ -92,7 +94,7 @@ describe('#bch-use-case', () => {
|
||||
it('should catch and throw an error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
uut.adapters.ipfs.ipfsCoordAdapter.bchjs = {
|
||||
uut.ipfs.ipfsCoordAdapter.bchjs = {
|
||||
Util: {
|
||||
sleep: () => {
|
||||
throw new Error('test error')
|
||||
@@ -136,7 +138,7 @@ describe('#bch-use-case', () => {
|
||||
describe('#getBalances', () => {
|
||||
it('should get the balance of an address', async () => {
|
||||
// Force connection to a wallet service
|
||||
uut.adapters.ipfs.ipfsCoordAdapter.state = {
|
||||
uut.ipfs.ipfsCoordAdapter.state = {
|
||||
selectedServiceProvider: 'abc123'
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ describe('#BCH-REST-Controller', () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox
|
||||
.stub(uut.useCases.bch, 'getStatus')
|
||||
.stub(uut.adapters.bch, 'getStatus')
|
||||
.rejects(new Error('test error'))
|
||||
|
||||
await uut.getStatus(ctx)
|
||||
@@ -93,7 +93,7 @@ describe('#BCH-REST-Controller', () => {
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
sandbox.stub(uut.useCases.bch, 'getStatus').resolves('bch')
|
||||
sandbox.stub(uut.adapters.bch, 'getStatus').resolves('bch')
|
||||
|
||||
await uut.getStatus(ctx)
|
||||
|
||||
@@ -110,7 +110,7 @@ describe('#BCH-REST-Controller', () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox
|
||||
.stub(uut.useCases.bch, 'getBalances')
|
||||
.stub(uut.adapters.bch, 'getBalances')
|
||||
.rejects(new Error('test error'))
|
||||
|
||||
ctx.request.body = {
|
||||
@@ -128,7 +128,7 @@ describe('#BCH-REST-Controller', () => {
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
sandbox.stub(uut.useCases.bch, 'getBalances').resolves({ status: 200 })
|
||||
sandbox.stub(uut.adapters.bch, 'getBalances').resolves({ status: 200 })
|
||||
|
||||
ctx.request.body = {
|
||||
addresses: 'blah'
|
||||
|
||||
@@ -54,4 +54,23 @@ const localdb = {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { ipfs, localdb }
|
||||
class BchUseCaseMock {
|
||||
rpcHandler () {
|
||||
return {}
|
||||
}
|
||||
|
||||
async getStatus () {
|
||||
return {}
|
||||
}
|
||||
|
||||
async getBalances () {
|
||||
return {}
|
||||
}
|
||||
|
||||
async waitForRPCResponse () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
const bch = new BchUseCaseMock()
|
||||
|
||||
module.exports = { ipfs, localdb, bch }
|
||||
|
||||
Reference in New Issue
Block a user