├── LICENSE ├── composer.json ├── config └── blockchain.php └── libraries └── Blockchain.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mehdi Bounya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mehdibo/codeigniter-blockchain", 3 | "description": "A CodeIgniter library to use the Blockchain Wallet API", 4 | "type": "library", 5 | "keywords": [ 6 | "codeigniter", "blockchain", "blockchain wallet", "blockchain api" 7 | ], 8 | "homepage": "https://github.com/mehdibo/Codeigniter-blockchain", 9 | "readme": "./README.md", 10 | "support": { 11 | "issues": "https://github.com/mehdibo/Codeigniter-blockchain/issues" 12 | }, 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Mehdi Bounya", 17 | "email": "contact.mehdi@pm.me", 18 | "role": "Developer", 19 | "homepage": "https://twitter.com/bounyamehdi" 20 | } 21 | ], 22 | "require": { 23 | "php": ">=5.6" 24 | }, 25 | "autoload": { 26 | "classmap": [ 27 | "libraries/Blockchain.php" 28 | ] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /config/blockchain.php: -------------------------------------------------------------------------------- 1 | _ci =& get_instance(); 85 | 86 | // Load config file 87 | $this->_ci->config->load('blockchain', TRUE, TRUE); 88 | 89 | $config = array( 90 | 'guid' => $this->_ci->config->item('guid', 'blockchain'), 91 | 'main_password' => $this->_ci->config->item('main_password', 'blockchain'), 92 | 'second_password' => $this->_ci->config->item('second_password', 'blockchain'), 93 | 'api_code' => $this->_ci->config->item('api_code', 'blockchain'), 94 | 'base_url' => $this->_ci->config->item('base_url', 'blockchain'), 95 | 'port' => $this->_ci->config->item('port', 'blockchain'), 96 | ); 97 | 98 | // Merge loaded configs with passed ones 99 | if (is_array($options)) { 100 | $config = array_merge($config, $options); 101 | } 102 | 103 | // Set config values 104 | $this->guid = $config['guid']; 105 | $this->main_password = $config['main_password']; 106 | $this->api_code = $config['api_code']; 107 | $this->second_password = $config['second_password']; 108 | $this->base_url = rtrim($config['base_url'], '/'); 109 | $this->port = $config['port']; 110 | 111 | log_message('info', 'Blockchain Class Initialized'); 112 | 113 | // Check if the Blockchain Wallet service is running 114 | if ($this->_exec('') === NULL) { 115 | show_error('Blockchain Wallet: Unable to connect to Blockchain Wallet Service on: '.$this->base_url.':'.$this->port.''); 116 | log_message('error', "Blockchain: Unable to connect to Blockchain Wallet Service."); 117 | } 118 | } 119 | 120 | /** 121 | * Create a new wallet 122 | * 123 | * @param string $password The new wallet's password, must be at least 10 characters. 124 | * @param string $private_key A private key to add to the wallet (optional) 125 | * @param string $email An e-mail to associate with the new wallet. (optional) 126 | * @param string $label A label to set for the wallet's first address. (optional) 127 | * 128 | * @return array API's response 129 | */ 130 | public function create_wallet($password, $private_key = NULL, $email = NULL, $label = NULL) 131 | { 132 | // Make sure the password is at least 10 chars long 133 | if(strlen($password) < 10){ 134 | return ['error' => 'Password must be at least 10 characters']; 135 | } 136 | 137 | // Prepare parameters 138 | $parameters = [ 139 | 'password' => $password, 140 | 'api_code' => $this->api_code, 141 | 'priv' => $private_key, 142 | 'label' => $label, 143 | 'email' => $email 144 | ]; 145 | 146 | // Execute 147 | return $this->_exec('api/v2/create', $parameters); 148 | } 149 | 150 | 151 | /** 152 | * Send funds 153 | * 154 | * @param string $to Recipient's Bitcoin address. 155 | * @param string $amount Amount to send in Satoshis. 156 | * @param string $from Send from a specific Bitcoin address. (optional) 157 | * @param string $fee Transaction fee value in satoshi. (Must be greater than default fee) (Optional) 158 | * 159 | * @return array API's response 160 | */ 161 | public function send($to, $amount, $from = NULL, $fee = NULL) 162 | { 163 | // Build parameters 164 | $parameters = [ 165 | 'password' => $this->main_password, 166 | 'to' => $to, 167 | 'amount' => $amount, 168 | 'second_password' => $this->second_password, 169 | 'from' => $from, 170 | 'fee' => $fee 171 | ]; 172 | 173 | // Execute 174 | return $this->_exec('merchant/'.urlencode($this->guid).'/payment', $parameters); 175 | } 176 | 177 | /** 178 | * Send funds to multiple addresses 179 | * 180 | * @param array $recipients An array of 'address' => 'amount to send in satoshis'. 181 | * @param string $from Send from a specific Bitcoin address. (optional) 182 | * @param string $fee Transaction fee value in satoshi. (Must be greater than default fee) (Optional) 183 | * 184 | * @return array API's response 185 | */ 186 | public function send_many($recipients, $from = NULL, $fee=NULL) 187 | { 188 | // Build parameters 189 | $parameters = [ 190 | 'password' => $this->main_password, 191 | 'second_password' => $this->second_password, 192 | 'recipients' => json_encode($recipients), 193 | 'from' => $from, 194 | 'fee' => $fee, 195 | ]; 196 | 197 | // Execute 198 | return $this->_exec('merchant/'.urlencode($this->guid).'/sendmany', $parameters); 199 | } 200 | 201 | /** 202 | * Get wallet's balance 203 | * 204 | * @return array API's response 205 | */ 206 | public function wallet_balance() 207 | { 208 | // Build parameters 209 | $parameters = [ 210 | 'password' => $this->main_password, 211 | ]; 212 | 213 | // Execute 214 | return $this->_exec('merchant/'.urlencode($this->guid).'/balance', $parameters); 215 | } 216 | 217 | /** 218 | * List all active addresses 219 | * 220 | * @return array API's response 221 | */ 222 | public function list_addresses() 223 | { 224 | // Build parameters 225 | $parameters = [ 226 | 'password' => $this->main_password, 227 | ]; 228 | 229 | // Execute 230 | return $this->_exec('merchant/'.urlencode($this->guid).'/list', $parameters); 231 | } 232 | 233 | /** 234 | * Get the balance of a specific address 235 | * 236 | * @param string $address Bitcoin address to lookup 237 | * 238 | * @return array API's response 239 | */ 240 | public function address_balance($address) 241 | { 242 | // Build parameters 243 | $parameters = [ 244 | 'password' => $this->main_password, 245 | 'address' => $address 246 | ]; 247 | 248 | // Execute 249 | return $this->_exec('merchant/'.urlencode($this->guid).'/address_balance', $parameters); 250 | } 251 | 252 | /** 253 | * Generate a new address 254 | * 255 | * @param string $label The new address's label. (optional) 256 | * 257 | * @return array API's response 258 | */ 259 | public function new_address($label = NULL) 260 | { 261 | // Build parameters 262 | $parameters = [ 263 | 'password' => $this->main_password, 264 | 'second_password' => $this->second_password, 265 | 'label' => $label, 266 | ]; 267 | 268 | // Execute 269 | return $this->_exec('merchant/'.urlencode($this->guid).'/new_address', $parameters); 270 | } 271 | 272 | /** 273 | * Execute an API request 274 | * 275 | * @param string $endpoint API's endpoint (the part after the base_url) 276 | * @param array $parameters Array of GET parameters 'parameter'=>'value' 277 | * 278 | * @return array API's decoded response 279 | */ 280 | private function _exec($endpoint, $parameters = NULL) 281 | { 282 | // Start building URL 283 | $url = $this->base_url; 284 | 285 | // Add port 286 | $url .= ':'.$this->port.'/'; 287 | 288 | // Add endpint 289 | $url .= trim($endpoint, '/').'/'; 290 | 291 | // Build query 292 | if(!empty($parameters)){ 293 | $url .= '?'.http_build_query($parameters); 294 | } 295 | 296 | // Get CURL resource 297 | $curl = curl_init(); 298 | // Set options 299 | curl_setopt_array($curl, array( 300 | CURLOPT_RETURNTRANSFER => TRUE, 301 | CURLOPT_URL => $url, 302 | )); 303 | 304 | // Send the request & save response 305 | $response = curl_exec($curl); 306 | 307 | // Close request to clear up some resources 308 | curl_close($curl); 309 | 310 | log_message('debug', 'Blockchain: URL executed '.$url); 311 | 312 | // Return the decoded response as an associative array 313 | return json_decode($response, TRUE); 314 | } 315 | } 316 | --------------------------------------------------------------------------------