Skip to content

Commit

Permalink
fix(bch-js): replaced slp-sdk with bch-js
Browse files Browse the repository at this point in the history
  • Loading branch information
christroutner committed Sep 29, 2019
1 parent b7535ad commit f75d9c3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 45 deletions.
62 changes: 62 additions & 0 deletions config/bchjs.js
@@ -0,0 +1,62 @@
/*
Configuration settings for bch-js and bch-api.
*/

'use strict'

// default rest API.
let RESTAPI = 'bchjs'

// Override the RESTAPI setting if envronment variable is set.
if (process.env.RESTAPI && process.env.RESTAPI !== '') { RESTAPI = process.env.RESTAPI }

// Ensure bch-js can pick up the env var.
process.env.RESTAPI = RESTAPI

const BCHJS = require('@chris.troutner/bch-js')
const BITBOX = require('slp-sdk')

const config = {}

if (RESTAPI === 'bchjs') {
config.BCHLIB = BCHJS.BitboxShim()
config.MAINNET_REST = `https://mainnet.bchjs.cash/v3/`
config.TESTNET_REST = `https://testnet.bchjs.cash/v3/`
config.RESTAPI = 'bchjs'
}

// Use BITBOX and the bitcoin.com infrastructure.
if (RESTAPI === 'bitcoin.com') {
config.BCHLIB = BITBOX
config.MAINNET_REST = `https://rest.bitcoin.com/v2/`
config.TESTNET_REST = `https://trest.bitcoin.com/v2/`
config.RESTAPI = 'bitcoin.com'
}

// Use bch-js but use it with the bitcoin.com infrastructure.
if (RESTAPI === 'rest.bitcoin.com') {
config.BCHLIB = BCHJS.BitboxShim()
config.MAINNET_REST = `https://rest.bitcoin.com/v2/`
config.TESTNET_REST = `http://157.245.141.224:3000/v2/`
config.RESTAPI = 'rest.bitcoin.com'
}

// Use bch-js with local infrastructure.
if (RESTAPI === 'local') {
config.BCHLIB = BCHJS.BitboxShim()
// config.MAINNET_REST = `http://192.168.0.36:12400/v3/`
// config.TESTNET_REST = `http://192.168.0.38:13400/v3/`
config.MAINNET_REST = `http://127.0.0.1:3000/v3/`
config.TESTNET_REST = `http://decatur.hopto.org:13400/v3/`
config.RESTAPI = 'local'
}

// Use bch-js with decatur infrastructure.
if (RESTAPI === 'decatur') {
config.BCHLIB = BCHJS.BitboxShim()
config.MAINNET_REST = `http://decatur.hopto.org:12400/v3/`
config.TESTNET_REST = `http://decatur.hopto.org:13400/v3/`
config.RESTAPI = 'decatur'
}

module.exports = config
3 changes: 2 additions & 1 deletion config/index.js
@@ -1,8 +1,9 @@
const common = require('./env/common')
const bchjs = require('./bchjs')

const env = process.env.APP_ENV || 'development'
console.log(`Starting ${env} environment`)
const config = require(`./env/${env}`)
config.env = env

module.exports = Object.assign({}, common, config)
module.exports = Object.assign({}, bchjs, common, config)
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -35,7 +35,7 @@
},
"repository": "Permissionless-Software-Foundation/token-liquidity",
"dependencies": {
"@chris.troutner/bch-js": "^1.4.22",
"@chris.troutner/bch-js": "^1.4.24",
"apidoc": "^0.17.6",
"bcryptjs": "^2.4.3",
"bitbox-sdk": "^8.8.0",
Expand Down
41 changes: 12 additions & 29 deletions src/lib/slp.js
Expand Up @@ -5,8 +5,6 @@

'use strict'

const rp = require('request-promise')

// Used for debugging and iterrogating JS objects.
const util = require('util')
util.inspect.defaultOptions = { depth: 5 }
Expand All @@ -19,23 +17,17 @@ const tlUtils = new TLUtils()
// Winston logger
const wlogger = require('../utils/logging')

let SLPSDK = require('slp-sdk')
let slpsdk, REST_URL
if (config.NETWORK === `testnet`) {
REST_URL = `https://trest.bitcoin.com/v2/`
slpsdk = new SLPSDK({ restURL: REST_URL })
} else {
REST_URL = `https://rest.bitcoin.com/v2/`
slpsdk = new SLPSDK({ restURL: REST_URL })
}
// Mainnet by default
let slpsdk = new config.BCHLIB({ restURL: config.MAINNET_REST })

class SLP {
constructor () {
this.slpsdk = slpsdk
}
// Determine if this is a testnet wallet or a mainnet wallet.
if (config.NETWORK === 'testnet') {
slpsdk = new config.BCHLIB({ restURL: config.TESTNET_REST })
}

hello () {
console.log(`Hello world!`)
this.slpsdk = slpsdk
}

// Get the token balance of an address.
Expand All @@ -44,7 +36,7 @@ class SLP {
wlogger.silly(`Enter slp.getTokenBalance()`)
// console.log(`addr: ${addr}`)

const result = await this.slpsdk.Utils.balancesForAddress(addr)
const result = await this.slpsdk.Util.balancesForAddress(addr)
wlogger.debug(`token balance: `, result)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

Expand Down Expand Up @@ -72,17 +64,8 @@ class SLP {
// Return false if the tx is not a valid SLP transaction.
if (!txValid[0].valid) return false

const options = {
method: 'GET',
uri: `${REST_URL}slp/txDetails/${txid}`,
// resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json'
}
}

const result = await rp(options)
// const result = await rp(options)
const result = this.slpsdk.Util.txDetails(txid)
// console.log(`txDetails: ${util.inspect(result)}`)

return result
Expand Down Expand Up @@ -124,15 +107,15 @@ class SLP {
// Generate TX hex for sending tokens to an address. Returns a config object
// ready to be broadcast to the BCH network with the SLP SDK TokenType1.send()
// method.
createTokenTx (addr, qty) {
async createTokenTx (addr, qty) {
try {
// Open the wallet controlling the tokens
const walletInfo = tlUtils.openWallet()

const mnemonic = walletInfo.mnemonic

// root seed buffer
const rootSeed = slpsdk.Mnemonic.toSeed(mnemonic)
const rootSeed = await slpsdk.Mnemonic.toSeed(mnemonic)

// master HDNode
let masterHDNode
Expand Down
25 changes: 14 additions & 11 deletions test/unit/a03-slp.js
Expand Up @@ -17,12 +17,14 @@ util.inspect.defaultOptions = { depth: 1 }
const slpMockData = require('./mocks/slp')
// const slpMock = require('slp-sdk-mock')

const config = require('../../config')

// Determine if this is a Unit or Integration test
// If not specified, default to unit test.
if (!process.env.APP_ENV) process.env.APP_ENV = 'test'
if (!process.env.TEST_ENV) process.env.TEST_ENV = 'unit'

const REST_URL = `https://trest.bitcoin.com/v2/`
// const REST_URL = `https://trest.bitcoin.com/v2/`

describe('#slp', () => {
let slp
Expand Down Expand Up @@ -55,7 +57,7 @@ describe('#slp', () => {
it('should get token balance', async () => {
// If unit test, use the mocking library instead of live calls.
if (process.env.TEST_ENV === 'unit') {
sandbox.stub(slp.slpsdk.Utils, 'balancesForAddress').resolves([
sandbox.stub(slp.slpsdk.Util, 'balancesForAddress').resolves([
{
tokenId:
'435fdbf1beeacbbf4ac0f7acccbee0b98d2a6a8b9a9c52af562d4029f9192e92',
Expand All @@ -77,8 +79,8 @@ describe('#slp', () => {

it(`should throw an error for an invalid address`, async () => {
if (process.env.TEST_ENV === 'unit') {
// slp.slpsdk.Utils.balancesForAddress = sandbox.throws()
sandbox.stub(slp.slpsdk.Utils, 'balancesForAddress').throws({
// slp.slpsdk.Util.balancesForAddress = sandbox.throws()
sandbox.stub(slp.slpsdk.Util, 'balancesForAddress').throws({
error:
'Invalid BCH address. Double check your address is valid: slptest:qz4qnxcxwvmacgye8wlakhz0835x0w3vtvxu67aaaa'
})
Expand All @@ -104,7 +106,7 @@ describe('#slp', () => {
it('should return 0 on address with zero balance', async () => {
if (process.env.TEST_ENV === 'unit') {
sandbox
.stub(slp.slpsdk.Utils, 'balancesForAddress')
.stub(slp.slpsdk.Util, 'balancesForAddress')
.resolves('No balance for this address')
}

Expand Down Expand Up @@ -149,9 +151,10 @@ describe('#slp', () => {
}

// Mock the http call to rest.
nock(REST_URL)
.get(uri => uri.includes('/'))
.reply(200, testData)
// nock(config.TESTNET_REST)
// .get(uri => uri.includes('/'))
// .reply(200, testData)
sandbox.stub(slp.slpsdk.Util, 'txDetails').resolves(testData)
}

const txid =
Expand Down Expand Up @@ -189,7 +192,7 @@ describe('#slp', () => {
}

// Mock the http call to rest.
nock(REST_URL)
nock(config.TESTNET_REST)
.get(uri => uri.includes('/'))
.reply(200, testData)
}
Expand Down Expand Up @@ -256,11 +259,11 @@ describe('#slp', () => {
})

describe('#createTokenTx', () => {
it('should generate a SLP config object', () => {
it('should generate a SLP config object', async () => {
const addr = 'bchtest:qpwa35xq0q0cnmdu0rwzkct369hddzsqpsme94qqh2'
const qty = 1

const result = slp.createTokenTx(addr, qty)
const result = await slp.createTokenTx(addr, qty)
// console.log(`result: ${util.inspect(result)}`)

assert.hasAllKeys(result, [
Expand Down

0 comments on commit f75d9c3

Please sign in to comment.