├── .gitignore ├── README.md ├── changelog.txt ├── lib └── api.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | index.js -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bitcoin-node-api 2 | 3 | Bitcoin-Node-Api is an Express middleware plugin that easily exposes a URL structure for interfacing with a bitcoind Bitcoin wallet. 4 | 5 | NB: The middleware is experimental at present. Certain JSON-RPC methods are not supported yet and/or experimental. These are methods with more complex parameters that do not fit easily into a query string: 6 | 7 | - addmultisigaddress 8 | - createmultisig 9 | - createrawtransaction 10 | - getaddednodeinfo 11 | - lockunspent 12 | - sendmany 13 | - signrawtransaction 14 | - submitblock 15 | 16 | These methods will be added in the future. If there any other problems with the other methods, please report the bugs. 17 | 18 | ## Install 19 | 20 | ```javascript 21 | npm install bitcoin-node-api 22 | ``` 23 | 24 | ## How to use 25 | 26 | ### Node.js 27 | 28 | ```javascript 29 | var bitcoinapi = require('bitcoin-node-api'); 30 | var express = require('express'); 31 | var app = express(); 32 | 33 | //Username and password relate to those set in the bitcoin.conf file 34 | 35 | var wallet = { 36 | host: 'localhost', 37 | port: 8332, 38 | user: 'username', 39 | pass: 'password' 40 | }; 41 | 42 | bitcoinapi.setWalletDetails(wallet); 43 | bitcoinapi.setAccess('default-safe'); //Access control 44 | app.use('/bitcoin/api', bitcoinapi.app); //Bind the middleware to any chosen url 45 | 46 | app.listen(3000); 47 | ``` 48 | 49 | ### Client/Browser 50 | 51 | Just add the method name after the binded url. 52 | 53 | * http://localhost:5000/URL/METHOD 54 | 55 | For example: 56 | 57 | * http://localhost:5000/bitcoin/api/getinfo 58 | 59 | This returns data exactly as would be expected from the JSON-RPC api. 60 | 61 | ```javascript 62 | { 63 | "version": 80300, 64 | "protocolversion": 70001, 65 | "walletversion": 60000, 66 | "balance": 4.3222, 67 | "blocks": 245645, 68 | "timeoffset": -2, 69 | "connections": 8, 70 | "proxy": "", 71 | "difficulty": 21335329.113983, 72 | "testnet": false, 73 | "keypoololdest": 1368414896, 74 | "keypoolsize": 101, 75 | "paytxfee": 0.0001, 76 | "unlocked_until": 0, 77 | "errors": "" 78 | } 79 | ``` 80 | 81 | Parameters are sent via a query string: 82 | 83 | * http://localhost:3000/bitcoin/api/gettransaction?txid=d6c7e35ff9c9623208c22ee37a118ad523ae6c2d137d10053739cb03dbac62e0 84 | 85 | ```javascript 86 | { 87 | "amount": 0.002, 88 | "confirmations": 1321, 89 | "blockhash": "000000000000009d0a2bb76c81dd185d1f6c28256037baef7b3345b7a7e958c7", 90 | "blockindex": 150, 91 | "blocktime": 1372728756, 92 | "txid": "d6c7e35ff9c9623208c22ee37a118ad523ae6c2d137d10053739cb03dbac62e0", 93 | "time": 1372728436, 94 | "timereceived": 1372728436, 95 | "details": [ 96 | { 97 | "account": "localtest", 98 | "address": "1Htev475XRVYfenku7ZWXGPSun15ESynCq", 99 | "category": "receive", 100 | "amount": 0.002 101 | } 102 | ] 103 | } 104 | ``` 105 | 106 | Consult the [API call list](https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list) for parameter information. 107 | 108 | 109 | ## Access Control 110 | 111 | ### .setWalletPassphrase(passphrase); 112 | 113 | If you have encrypted your wallet.dat you need to set the passphrase before attaching the middleware. 114 | ```javascript 115 | bitcoinapi.setWalletDetails(wallet); 116 | bitcoinapi.setWalletPassphrase(passphrase); 117 | app.use('/bitcoin/api', bitcoinapi.app); 118 | ``` 119 | 120 | ### .setAccces(type, accesslist); 121 | 122 | The .setAccess method controls the access to the urls. By default all commands are accessible. The method takes two parameters: type (string) and accesslist (array). To restrict access there are two ways to do this: 123 | 124 | #### 'only' 125 | 126 | The 'only' type only exposes the methods given by an array of methods as the accesslist parameter. 127 | 128 | ```javascript 129 | //Only allow the getinfo method 130 | bitcoinapi.setAccess('only', ['getinfo']); 131 | ``` 132 | 133 | #### 'restrict' 134 | 135 | The 'restrict' type prevents methods from being accessed. 136 | 137 | ```javascript 138 | bitcoinapi.setAccess('restrict', ['dumpprivkey', 'sendmany']); 139 | ``` 140 | 141 | ### Access Profiles 142 | 143 | Bitcoin-Node-Api has predefined access profiles to make it easy to set up. 144 | 145 | #### 'default-safe' 146 | 147 | It prevents 'dumpprivkey' and 'walletpassphrasechange' being accessed. This prevents potential theft. Also removes the 'stop' command to prevent someone from stopping the server. 148 | 149 | ```javascript 150 | bitcoinapi.setAccess('default-safe'); 151 | ``` 152 | 153 | #### 'read-only' 154 | 155 | This profile only exposes methods that show information. No methods that can send/alter the wallet are exposed. 156 | 157 | ```javascript 158 | bitcoinapi.setAccess('read-only'); 159 | ``` 160 | 161 | ## Projects 162 | 163 | Bitcoin-Node-Api is used in the following projects: 164 | 165 | * [Min.io](http://min.io) 166 | 167 | If you use Bitcoin-Node-Api in your projects submit a pull request to the readme with a link or send me an email: niel@delarouviere.com 168 | 169 | # Licence 170 | 171 | Copyright (C) 2013 Niel de la Rouviere 172 | 173 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 174 | 175 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 176 | 177 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 178 | -------------------------------------------------------------------------------- /changelog.txt: -------------------------------------------------------------------------------- 1 | v0.1.0 - First commit 2 | v0.1.1 - Small changes to sending json, added 'read-only' profile. -------------------------------------------------------------------------------- /lib/api.js: -------------------------------------------------------------------------------- 1 | var bitcoin = require('bitcoin'); 2 | var express = require('express'); 3 | 4 | module.exports = function(){ 5 | 6 | function express_app(){ 7 | var app = express(); 8 | 9 | app.get('*', hasAccess, function(req, res){ 10 | var method = req.path.substring(1,req.path.length); 11 | 12 | if('undefined' != typeof requires_passphrase[method]){ 13 | if(wallet_passphrase) client.walletPassphrase(wallet_passphrase, 10); 14 | else res.send('A wallet passphrase is needed and has not been set.'); 15 | } 16 | 17 | var query_parameters = req.query; 18 | var params = []; 19 | 20 | for(var parameter in query_parameters){ 21 | if(query_parameters.hasOwnProperty(parameter)){ 22 | var param = query_parameters[parameter]; 23 | if(!isNaN(param)){ 24 | param = parseFloat(param); 25 | } 26 | params.push(param); 27 | } 28 | } 29 | 30 | var command = []; 31 | if(method == "sendmany"){ 32 | command = specialApiCase('sendmany'); 33 | } 34 | else{ 35 | command = [{ 36 | method: method, 37 | params: params 38 | }]; 39 | } 40 | 41 | client.cmd(command, function(err, response){ 42 | if(err){console.log(err); res.send("There was an error. Check your console.");} 43 | else{ 44 | if(typeof response === 'object'){ 45 | res.json(response); 46 | } 47 | else{ 48 | res.send(""+response); 49 | } 50 | } 51 | }); 52 | }); 53 | 54 | function hasAccess(req, res, next){ 55 | if(accesslist.type == 'all'){ 56 | return next(); 57 | } 58 | 59 | var method = req.path.substring(1,req.path.length); 60 | if('undefined' == typeof accesslist[method]){ 61 | if(accesslist.type == 'only') res.end('This method is restricted.'); 62 | else return next(); 63 | } 64 | else{ 65 | if(accesslist[method] == true){ 66 | return next(); 67 | } 68 | else res.end('This method is restricted.'); 69 | } 70 | } 71 | 72 | function specialApiCase(method_name){ 73 | var params = []; 74 | if(method_name == 'sendmany'){ 75 | var after_account = false; 76 | var before_min_conf = true; 77 | var address_info = {}; 78 | for(var parameter in query_parameters){ 79 | if(query_parameters.hasOwnProperty(parameter)){ 80 | if(parameter == 'minconf'){ 81 | before_min_conf = false; 82 | params.push(address_info); 83 | } 84 | var param = query_parameters[parameter]; 85 | if(!isNaN(param)){ 86 | param = parseFloat(param); 87 | } 88 | if(after_account && before_min_conf){ 89 | address_info[parameter] = param; 90 | } 91 | else{ 92 | params.push(param); 93 | } 94 | if(parameter == 'account') after_account = true; 95 | } 96 | } 97 | if(before_min_conf){ 98 | params.push(address_info); 99 | } 100 | } 101 | 102 | return [{ 103 | method: method_name, 104 | params: params 105 | }]; 106 | } 107 | 108 | return app; 109 | }; 110 | 111 | var accesslist = {}; 112 | accesslist.type = 'all'; 113 | var client = {}; 114 | var wallet_passphrase = null; 115 | var requires_passphrase = { 116 | 'dumpprivkey': true, 117 | 'importprivkey': true, 118 | 'keypoolrefill': true, 119 | 'sendfrom': true, 120 | 'sendmany': true, 121 | 'sendtoaddress': true, 122 | 'signmessage': true, 123 | 'signrawtransaction': true 124 | }; 125 | 126 | function setAccess(type, access_list){ 127 | //Reset// 128 | accesslist = {}; 129 | accesslist.type = type; 130 | 131 | if(type == "only"){ 132 | var i=0; 133 | for(; i