mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-23 01:32:03 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
553d6fd4b0 | ||
|
|
ab98bf05da | ||
|
|
6806b4a197 | ||
|
|
4ae74aed59 | ||
|
|
98e1a5b40f | ||
|
|
279c9b7bea | ||
|
|
56be614980 | ||
|
|
584037ebdd | ||
|
|
44230b73f7 | ||
|
|
b7f1eba6bd | ||
|
|
35bc3025d6 | ||
|
|
6f6531428a | ||
|
|
cb5e962167 | ||
|
|
33da626f78 | ||
|
|
e9766630ed | ||
|
|
9b6adbf4ec | ||
|
|
5f09447da4 |
@@ -1,6 +1,5 @@
|
|||||||
# bch-api
|
# bch-api
|
||||||
|
|
||||||
[](https://greenkeeper.io/)
|
|
||||||
|
|
||||||
[](https://travis-ci.org/christroutner/bch-api)
|
[](https://travis-ci.org/christroutner/bch-api)
|
||||||
|
|
||||||
@@ -16,9 +15,8 @@ of [BITBOX SDK](https://github.com/Bitcoin-com/bitbox-sdk).
|
|||||||
Both bch-api and bch-js are part of the
|
Both bch-api and bch-js are part of the
|
||||||
[full stack of BCH software](https://troutsblog.com/research/bitcoin-cash/how-to-bch-full-stack-developer).
|
[full stack of BCH software](https://troutsblog.com/research/bitcoin-cash/how-to-bch-full-stack-developer).
|
||||||
|
|
||||||
- [API Documentation](https://api.bchjs.cash/docs/)
|
- [API Documentation](https://fullstack.cash/documentation)
|
||||||
- [BCH Example Applications](https://github.com/Permissionless-Software-Foundation/bch-js-examples)
|
- [Example Code](https://fullstack.cash/examples)
|
||||||
- [slp-cli-wallet](https://github.com/christroutner/slp-cli-wallet): a hacker-friendly, command-line BCH and SLP token wallet using this REST API.
|
|
||||||
|
|
||||||
Have questions? Need help? Join our community support
|
Have questions? Need help? Join our community support
|
||||||
[Telegram channel](https://t.me/bch_js_toolkit)
|
[Telegram channel](https://t.me/bch_js_toolkit)
|
||||||
|
|||||||
@@ -11,5 +11,7 @@ export DATA="{\"ref\":\"$DEPLOY_SECRET\"}"
|
|||||||
|
|
||||||
curl -X POST http://fullstack.cash:9000/hooks/bch-api-mainnet -H "Content-Type: application/json" -d $DATA
|
curl -X POST http://fullstack.cash:9000/hooks/bch-api-mainnet -H "Content-Type: application/json" -d $DATA
|
||||||
curl -X POST http://fullstack.cash:9000/hooks/bch-api-testnet -H "Content-Type: application/json" -d $DATA
|
curl -X POST http://fullstack.cash:9000/hooks/bch-api-testnet -H "Content-Type: application/json" -d $DATA
|
||||||
|
curl -X POST http://$FREE_SERVER:9000/hooks/bch-api-testnet -H "Content-Type: application/json" -d $DATA
|
||||||
|
curl -X POST http://$FREE_SERVER:9000/hooks/bch-api-mainnet -H "Content-Type: application/json" -d $DATA
|
||||||
|
|
||||||
echo "...Finished deploying to production."
|
echo "...Finished deploying to production."
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ USER safeuser
|
|||||||
|
|
||||||
# Clone the repository
|
# Clone the repository
|
||||||
WORKDIR /home/safeuser
|
WORKDIR /home/safeuser
|
||||||
RUN git clone https://github.com/christroutner/bch-api
|
RUN git clone https://github.com/Permissionless-Software-Foundation/bch-api
|
||||||
WORKDIR /home/safeuser/bch-api
|
WORKDIR /home/safeuser/bch-api
|
||||||
RUN npm install
|
RUN npm install --silent
|
||||||
|
|
||||||
# Generate documentation
|
# Generate documentation
|
||||||
RUN npm run docs
|
RUN npm run docs
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ USER safeuser
|
|||||||
|
|
||||||
# Clone the repository
|
# Clone the repository
|
||||||
WORKDIR /home/safeuser
|
WORKDIR /home/safeuser
|
||||||
RUN git clone https://github.com/christroutner/bch-api
|
RUN git clone https://github.com/Permissionless-Software-Foundation/bch-api
|
||||||
WORKDIR /home/safeuser/bch-api
|
WORKDIR /home/safeuser/bch-api
|
||||||
RUN npm install
|
RUN npm install --silent
|
||||||
|
|
||||||
# Generate documentation
|
# Generate documentation
|
||||||
RUN npm run docs
|
RUN npm run docs
|
||||||
|
|||||||
@@ -116,13 +116,20 @@ class RateLimits {
|
|||||||
let pointsToConsume = _this.calcPoints(decoded)
|
let pointsToConsume = _this.calcPoints(decoded)
|
||||||
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
||||||
|
|
||||||
|
// Retrieve the origin.
|
||||||
|
let origin = req.get('origin')
|
||||||
|
if (origin === undefined && key.indexOf('10.0.0.5') > -1) {
|
||||||
|
origin = 'slp-api'
|
||||||
|
}
|
||||||
|
wlogger.info(`origin: ${origin}`)
|
||||||
|
|
||||||
// If the request originates from one of the approved wallet apps, then
|
// If the request originates from one of the approved wallet apps, then
|
||||||
// apply paid-access rate limits.
|
// apply paid-access rate limits.
|
||||||
const origin = req.get('origin')
|
|
||||||
if (
|
if (
|
||||||
origin &&
|
origin &&
|
||||||
(origin.toString().indexOf('wallet.fullstack.cash') > -1 ||
|
(origin.toString().indexOf('wallet.fullstack.cash') > -1 ||
|
||||||
origin.toString().indexOf('sandbox.fullstack.cash') > -1)
|
origin.toString().indexOf('sandbox.fullstack.cash') > -1 ||
|
||||||
|
origin === 'slp-api')
|
||||||
) {
|
) {
|
||||||
pointsToConsume = 1
|
pointsToConsume = 1
|
||||||
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
||||||
|
|||||||
+14
-14
@@ -23,24 +23,24 @@
|
|||||||
target="_blank"
|
target="_blank"
|
||||||
>REST API</a
|
>REST API</a
|
||||||
>, allowing modern applications to interact with the Bitcoin Cash (BCH)
|
>, allowing modern applications to interact with the Bitcoin Cash (BCH)
|
||||||
blockchain. New to the BCH blockchain? Start by reading more about
|
blockchain. New to the BCH blockchain? Check out these
|
||||||
<a
|
<a
|
||||||
href="https://troutsblog.com/research/bitcoin-cash/how-to-bch-full-stack-developer"
|
href="https://fullstack.cash/documentation"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
>building full stack applications on BCH</a
|
>educational resources</a
|
||||||
>.
|
>.
|
||||||
</p><br />
|
</p><br />
|
||||||
<p>
|
<p>
|
||||||
A REST API can work with any programming language, but bch-api is intended
|
A REST API can work with any programming language, but bch-api is intended
|
||||||
to be paired with the
|
to be paired with the
|
||||||
<a
|
<a
|
||||||
href="https://www.npmjs.com/package/@chris.troutner/bch-js"
|
href="https://www.npmjs.com/package/@psf/bch-js"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
>npm bch-js</a
|
>npm bch-js</a
|
||||||
>
|
>
|
||||||
JavaScript library.
|
JavaScript library.
|
||||||
<a
|
<a
|
||||||
href="https://github.com/Permissionless-Software-Foundation/bch-js-examples"
|
href="https://fullstack.cash/examples"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
>Here is example code</a
|
>Here is example code</a
|
||||||
>
|
>
|
||||||
@@ -53,7 +53,7 @@
|
|||||||
</p><br />
|
</p><br />
|
||||||
<p>
|
<p>
|
||||||
While you'll be able to run through the examples without one, you should
|
While you'll be able to run through the examples without one, you should
|
||||||
<a href="https://account.bchjs.cash/" target="_blank"
|
<a href="https://fullstack.cash/login" target="_blank"
|
||||||
>sign-up for a free API key</a
|
>sign-up for a free API key</a
|
||||||
>
|
>
|
||||||
in order to unlock higher rate limits.
|
in order to unlock higher rate limits.
|
||||||
@@ -66,7 +66,7 @@
|
|||||||
<h3>bch-api</h3>
|
<h3>bch-api</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<a href="https://account.bchjs.cash" target="_blank">
|
<a href="https://fullstack.cash/login" target="_blank">
|
||||||
Get an API key to increase rate limits
|
Get an API key to increase rate limits
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
@@ -80,14 +80,14 @@
|
|||||||
<br />
|
<br />
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="https://tapi.bchjs.cash">
|
<a href="https://tapi.fullstack.cash">
|
||||||
testnet REST API
|
testnet REST API
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="https://github.com/christroutner/bch-api" target="_blank">
|
<a href="https://github.com/Permissionless-Software-Foundation/bch-api" target="_blank">
|
||||||
bch-api GitHub repository
|
bch-api GitHub repository
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
@@ -114,14 +114,14 @@
|
|||||||
</li>
|
</li>
|
||||||
<br />
|
<br />
|
||||||
<li>
|
<li>
|
||||||
<a href="https://bchjs.cash/" target="_blank">
|
<a href="https://fullstack.cash/cashstrap" target="_blank">
|
||||||
Run your own infrastructure
|
Run your own infrastructure
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="https://github.com/bitcoinbook/bitcoinbook#chapters" target="_blank">
|
<a href="https://fullstack.cash/documentation" target="_blank">
|
||||||
Mastering Bitcoin (great free reference)
|
Mastering Bitcoin (great free reference)
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
@@ -135,7 +135,7 @@
|
|||||||
<br />
|
<br />
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="https://developer.bitcoin.com/faucets/bch/" target="_blank">
|
<a href="https://fullstack.cash/faucet/" target="_blank">
|
||||||
Testnet Faucet
|
Testnet Faucet
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
@@ -148,7 +148,7 @@
|
|||||||
<h3>bch-js</h3>
|
<h3>bch-js</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<a href="https://www.npmjs.com/package/@chris.troutner/bch-js"
|
<a href="https://www.npmjs.com/package/@psf/bch-js"
|
||||||
target="_blank">
|
target="_blank">
|
||||||
npm library
|
npm library
|
||||||
</a>
|
</a>
|
||||||
@@ -163,7 +163,7 @@
|
|||||||
<br />
|
<br />
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="https://github.com/christroutner/bch-js" target="_blank">
|
<a href="https://github.com/Permissionless-Software-Foundation/bch-js" target="_blank">
|
||||||
GitHub code repository
|
GitHub code repository
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -64,10 +64,6 @@ class Slp {
|
|||||||
_this.router.get('/balancesForAddress/:address', _this.balancesForAddress)
|
_this.router.get('/balancesForAddress/:address', _this.balancesForAddress)
|
||||||
_this.router.post('/balancesForAddress', _this.balancesForAddressBulk)
|
_this.router.post('/balancesForAddress', _this.balancesForAddressBulk)
|
||||||
_this.router.get('/balancesForToken/:tokenId', _this.balancesForTokenSingle)
|
_this.router.get('/balancesForToken/:tokenId', _this.balancesForTokenSingle)
|
||||||
_this.router.get(
|
|
||||||
'/balance/:address/:tokenId',
|
|
||||||
_this.balancesForAddressByTokenID
|
|
||||||
)
|
|
||||||
_this.router.get('/convert/:address', _this.convertAddressSingle)
|
_this.router.get('/convert/:address', _this.convertAddressSingle)
|
||||||
_this.router.post('/convert', _this.convertAddressBulk)
|
_this.router.post('/convert', _this.convertAddressBulk)
|
||||||
_this.router.post('/validateTxid', _this.validateBulk)
|
_this.router.post('/validateTxid', _this.validateBulk)
|
||||||
@@ -830,170 +826,6 @@ class Slp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @api {get} /slp/balance/{address}/{TokenId} List single slp token balance for address.
|
|
||||||
* @apiName List single slp token balance for address.
|
|
||||||
* @apiGroup SLP
|
|
||||||
* @apiDescription Returns List single slp token balance for address.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @apiExample Example usage:
|
|
||||||
* curl -X GET "https://api.fullstack.cash/v3/slp/balance/simpleledger:qz9tzs6d5097ejpg279rg0rnlhz546q4fsnck9wh5m/1cda254d0a995c713b7955298ed246822bee487458cd9747a91d9e81d9d28125" -H "accept:application/json"
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
// Retrieve token balances for a single token class, for a single address.
|
|
||||||
async balancesForAddressByTokenID (req, res, next) {
|
|
||||||
try {
|
|
||||||
// Validate input data.
|
|
||||||
const address = req.params.address
|
|
||||||
if (!address || address === '') {
|
|
||||||
res.status(400)
|
|
||||||
return res.json({ error: 'address can not be empty' })
|
|
||||||
}
|
|
||||||
|
|
||||||
const tokenId = req.params.tokenId
|
|
||||||
if (!tokenId || tokenId === '') {
|
|
||||||
res.status(400)
|
|
||||||
return res.json({ error: 'tokenId can not be empty' })
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the input is a valid BCH address.
|
|
||||||
try {
|
|
||||||
_this.bchjs.SLP.Address.toCashAddress(address)
|
|
||||||
} catch (err) {
|
|
||||||
res.status(400)
|
|
||||||
return res.json({
|
|
||||||
error: `Invalid BCH address. Double check your address is valid: ${address}`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent a common user error. Ensure they are using the correct network address.
|
|
||||||
const cashAddr = _this.bchjs.SLP.Address.toCashAddress(address)
|
|
||||||
const networkIsValid = _this.routeUtils.validateNetwork(cashAddr)
|
|
||||||
if (!networkIsValid) {
|
|
||||||
res.status(400)
|
|
||||||
return res.json({
|
|
||||||
error:
|
|
||||||
'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert input to an simpleledger: address.
|
|
||||||
const slpAddr = _this.bchjs.SLP.Address.toSlpAddress(req.params.address)
|
|
||||||
|
|
||||||
const query = {
|
|
||||||
v: 3,
|
|
||||||
q: {
|
|
||||||
db: ['g'],
|
|
||||||
aggregate: [
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
'graphTxn.outputs': {
|
|
||||||
$elemMatch: {
|
|
||||||
address: slpAddr,
|
|
||||||
status: 'UNSPENT',
|
|
||||||
slpAmount: { $gte: 0 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$unwind: '$graphTxn.outputs'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
'graphTxn.outputs.address': slpAddr,
|
|
||||||
'graphTxn.outputs.status': 'UNSPENT',
|
|
||||||
'graphTxn.outputs.slpAmount': { $gte: 0 }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
amount: '$graphTxn.outputs.slpAmount',
|
|
||||||
address: '$graphTxn.outputs.address',
|
|
||||||
txid: '$graphTxn.txid',
|
|
||||||
vout: '$graphTxn.outputs.vout',
|
|
||||||
tokenId: '$tokenDetails.tokenIdHex'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: '$tokenId',
|
|
||||||
balanceString: {
|
|
||||||
$sum: '$amount'
|
|
||||||
},
|
|
||||||
slpAddress: {
|
|
||||||
$first: '$address'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
limit: 10000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = _this.generateCredentials()
|
|
||||||
|
|
||||||
const s = JSON.stringify(query)
|
|
||||||
const b64 = Buffer.from(s).toString('base64')
|
|
||||||
const url = `${process.env.SLPDB_URL}q/${b64}`
|
|
||||||
|
|
||||||
// Get data from SLPDB.
|
|
||||||
const opt = {
|
|
||||||
method: 'get',
|
|
||||||
baseURL: url,
|
|
||||||
headers: options.headers,
|
|
||||||
timeout: options.timeout
|
|
||||||
}
|
|
||||||
const tokenRes = await _this.axios.request(opt)
|
|
||||||
console.log(`tokenRes.data: ${JSON.stringify(tokenRes.data, null, 2)}`)
|
|
||||||
|
|
||||||
let resVal = {
|
|
||||||
cashAddress: _this.bchjs.SLP.Address.toCashAddress(slpAddr),
|
|
||||||
legacyAddress: _this.bchjs.SLP.Address.toLegacyAddress(slpAddr),
|
|
||||||
slpAddress: slpAddr,
|
|
||||||
tokenId: tokenId,
|
|
||||||
balance: 0,
|
|
||||||
balanceString: '0'
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tokenRes.data.g.length > 0) {
|
|
||||||
tokenRes.data.g.forEach(async (token) => {
|
|
||||||
if (token._id === tokenId) {
|
|
||||||
resVal = {
|
|
||||||
cashAddress: _this.bchjs.SLP.Address.toCashAddress(slpAddr),
|
|
||||||
legacyAddress: _this.bchjs.SLP.Address.toLegacyAddress(slpAddr),
|
|
||||||
slpAddress: slpAddr,
|
|
||||||
tokenId: token._id,
|
|
||||||
balance: parseFloat(token.balanceString),
|
|
||||||
balanceString: token.balanceString
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
resVal = {
|
|
||||||
cashAddress: _this.bchjs.SLP.Address.toCashAddress(slpAddr),
|
|
||||||
legacyAddress: _this.bchjs.SLP.Address.toLegacyAddress(slpAddr),
|
|
||||||
slpAddress: slpAddr,
|
|
||||||
tokenId: tokenId,
|
|
||||||
balance: 0,
|
|
||||||
balanceString: '0'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200)
|
|
||||||
return res.json(resVal)
|
|
||||||
} catch (err) {
|
|
||||||
wlogger.error('Error in slp.ts/balancesForAddressByTokenID().', err)
|
|
||||||
|
|
||||||
return _this.errorHandler(err, res)
|
|
||||||
// return res.json({
|
|
||||||
// error: `Error in /balance/:address/:tokenId: ${err.message}`
|
|
||||||
// })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {get} /slp/convert/{address} Convert address to slpAddr, cashAddr and legacy.
|
* @api {get} /slp/convert/{address} Convert address to slpAddr, cashAddr and legacy.
|
||||||
* @apiName Convert address to slpAddr, cashAddr and legacy.
|
* @apiName Convert address to slpAddr, cashAddr and legacy.
|
||||||
|
|||||||
Reference in New Issue
Block a user