Skip to content

Commit 546d7cb

Browse files
committed
moving some bitcoin methods to API
1 parent 11dc44e commit 546d7cb

File tree

4 files changed

+158
-58
lines changed

4 files changed

+158
-58
lines changed

package-lock.json

Lines changed: 102 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"babel-plugin-transform-class-properties": "^6.24.1",
3131
"babel-plugin-transform-object-rest-spread": "^6.26.0",
3232
"babel-preset-env": "^1.6.1",
33+
"blockchain.info": "^2.12.1",
3334
"bluebird": "^3.5.3",
3435
"body-parser": "^1.18.2",
3536
"compression": "^1.7.3",

src/routes.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {dateId} from "./util/dates";
2222
import ReflinkService from "./services/ReflinkService";
2323
import config from "./util/config";
2424
import * as ecc from "eosjs-ecc";
25+
import BitcoinService from "./services/BitcoinService";
2526

2627
const bucket = couchbase('scatter');
2728

@@ -241,33 +242,6 @@ routes.post('/apps', async (req, res) => {
241242

242243
routes.post('/create_eos', async (req, res) => {
243244
returnResult({error:"makeaccounts is no longer valid."}, req, res);
244-
// const defaultError = {error:'There was an error creating the account. Please try again later.'};
245-
// const {transaction_id, signature, keys, account_name} = req.body;
246-
//
247-
// if(!keys.hasOwnProperty('active') || !keys.hasOwnProperty('owner') || !keys.active.length || !keys.owner.length){
248-
// return returnResult({error:'Invalid keys'}, req, res);
249-
// }
250-
//
251-
// const minimumCost = await AccountService.getAccountMinimumCost();
252-
// if(!minimumCost) return returnResult(defaultError, req, res);
253-
//
254-
// const transactionStatus = await TransactionService.eos(transaction_id, minimumCost, PAYMENT_ACCOUNTS.EOS.NEW_ACCOUNT);
255-
// if(!transactionStatus || transactionStatus.hasOwnProperty('error')) return returnResult(transactionStatus.hasOwnProperty('error')
256-
// ? {error:transactionStatus.error}
257-
// : {error:'The transaction could not be verified.'}, req, res);
258-
//
259-
//
260-
// const [quantity, memo] = transactionStatus;
261-
//
262-
// const leftForResources = parseFloat(quantity - minimumCost).toFixed(4);
263-
// if(!leftForResources || leftForResources <= 0) return returnResult({error:'There was not enough EOS left for resources.'}, req, res);
264-
//
265-
// if(memo !== keys.active) return returnResult({error:'The signature for account creation did not match the key from the exchange memo'}, req, res);
266-
//
267-
// const created = await AccountService.createEosAccount(account_name, keys, leftForResources, transaction_id, signature);
268-
// if(!created) return returnResult(defaultError, req, res);
269-
//
270-
// returnResult({created}, req, res);
271245
});
272246

273247
routes.post('/create_bridge', async (req, res) => {
@@ -306,6 +280,30 @@ routes.get('/machine/:id', async (req, res) => {
306280

307281

308282

283+
/************************************************/
284+
/* */
285+
/* BITCOIN HELPERS */
286+
/* */
287+
/************************************************/
288+
289+
290+
routes.get('/btc/balance/:address', async (req, res) => {
291+
const address = req.params.address;
292+
returnResult(await BitcoinService.getBalance(address), req, res);
293+
});
294+
295+
routes.get('/btc/unspent/:address', async (req, res) => {
296+
const address = req.params.address;
297+
returnResult(await BitcoinService.getUnspent(address), req, res);
298+
});
299+
300+
routes.post('/btc/pushtx', async (req, res) => {
301+
const signed = req.body.signed;
302+
returnResult(await BitcoinService.pushTransaction(signed), req, res);
303+
});
304+
305+
306+
309307
routes.all('*', (req, res) => res.sendStatus(403));
310308

311309
export default routes;

src/services/BitcoinService.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { blockexplorer, pushtx } from 'blockchain.info';
2+
3+
const SELECTED_CHAIN = 0;
4+
5+
const explorer = blockexplorer.usingNetwork(SELECTED_CHAIN);
6+
7+
8+
export default class BitcoinService {
9+
10+
static async getBalance(address){
11+
return explorer.getBalance(address);
12+
}
13+
14+
static async getUnspent(address){
15+
return explorer.getUnspentOutputs(address);
16+
}
17+
18+
static pushTransaction(signedTransaction){
19+
console.log('signed', signedTransaction);
20+
// Blockchain.info uses a very old version of lodash via the request-promise library.
21+
// This handles some prototype pollution vectors.
22+
if(signedTransaction.toString().indexOf('constructor') > -1) return null;
23+
24+
return pushtx.usingNetwork(SELECTED_CHAIN).pushtx(signedTransaction).catch(error => {
25+
if(error.indexOf('Error #-26: dust')) return 'The amount you are trying to send is too low. Bitcoin considers this "dust".';
26+
return error;
27+
});
28+
}
29+
30+
}

0 commit comments

Comments
 (0)