Our Code for Calling a Multichain API using callAPI()
function.php file through which we are calling callAPI()
<?php
include "helper.php";
$c='abhishek';
$b='abhinilekar@gmail.com';
$a='12345';
createNewUser($c,$a,$b);
//create a new user with keys stored in the web server
function createNewUser($name,$pin,$email){
//check if this phone number is already used
//generate new wallet address and grant create/send permissions from webaddress
$uaddress=callAPI('getnewaddress');
callAPI('grantfrom',array(getWebAddress(),$uaddress,'create'));
callAPI('grantfrom',array(getWebAddress(),$uaddress,'send'));
callAPI('grantfrom',array(getWebAddress(),$uaddress,'receive'));
callAPI('grantfrom',array(getWebAddress(),$uaddress,'connect'));
//create useckey=hash of pin
$useckey=hash('sha256',$pin);
//AES encrypt name with pin
$encrypted_name=ascii2hex(AESEncrypt($useckey,$pin,$name));
//$sendname= $encrypted_name;
//AES encrypt email with pin
$encrypted_email=ascii2hex(AESEncrypt($useckey,$pin,$email));
//AES encrypt pin with pin
$encrypted_pin=ascii2hex(AESEncrypt($useckey,$pin,$pin));
/*
//grant stream level write permission to uaddress from webaddress
callAPI('grantfrom',array(getWebAddress(),$uaddress,'system.access.write'));
//write encrypted phone number to system.access stream from uaddress with key=pin.validate
callAPI('publishfrom',array($uaddress,'system.access',$pin.'.validate',$encrypted_pin));
//write wallet address to system.access stream from uaddress with key=pin.wallet
callAPI('publishfrom',array($uaddress,'system.access',$pin.'.wallet',ascii2hex($uaddress)));
//revoke stream level write permission to uaddress from webaddress
callAPI('revokefrom',array(getWebAddress(),$uaddress,'system.access.write'));
//issue to new user address */
//return $pin;
echo $name.'--->'.$encrypted_name ;
echo "<br>";
echo $email.'--->'.$encrypted_email;
echo "<br>";
echo $pin.'--->'.$encrypted_pin;
// return $encrypted_name
}
/*
function validateUserAccess($name,$pin){
$enc_uaccess=callAPI('liststreamkeyitems',array('system.access',$name.'.validate'));
$unhex_uaccess=hex2ascii($enc_uaccess['0']['data']);
//create useckey=hash of pin
$useckey=hash('sha256',$pin);
$uaccess=AESDecrypt($useckey,$pin,$unhex_uaccess);
return $uaccess;
}
function getWalletAddress($pin){
$saved_wallet=callAPI('liststreamkeyitems',array('system.access',$pin.'.wallet'));
$wallet_address=hex2ascii($saved_wallet['0']['data']);
return $wallet_address;
}
echo $encrypted_name;
echo $encrypted_email;
echo $encrypted_pin;
echo $useckey;
echo 'asefsdgsgfg';
*/
?>
helper.php having CallAPI function.
helper.php
<?php
/*
* Developer: Yogesh Ghogare,Abhishek Nilekar,Prasad Dhole and Vallabh Patil
* Project Name : User Identity Management Using Blockchain
* Objective: Helper functions for Multichain API
*/
//generic function to call Multichain API
function callAPI($method,$params=array()){
//read connection settings from config.ini
$config=parse_ini_file('config.ini');
//setup URL to Multichain Webnode
$url='http://'.$config['host'].':'.$config['port'];
//setup json input to be sent to the API
$input=json_encode(array('id'=>time(),'method'=>$method,'params'=>$params,'chain_name'=>$config['chain']));
//initiate cURL handle and setup cURL options
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$input);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$config['user'].':'.$config['pwd']);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/json','Content-Length:'.strlen($input)));
//execute cURL request
$output_json=curl_exec($ch);
if($errno=curl_errno($ch)){
echo 'cURL ERROR [Code : '.$errno.'] [Message : '.curl_strerror($errno).']';
}
//convert json output into array
$output_array=json_decode($output_json,true);
//return output
return $output_array['result'];
//close cURL handle
curl_close($ch);
}
//convert ascii string to hexadecimal dump
function ascii2hex($ascii) {
$hexadecimal = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hexadecimal.=$byte;
}
return $hexadecimal;
}
//convert hexdecimal dump to ascii string
function hex2ascii($hexadecimal){
$ascii='';
$hexadecimal=str_replace(" ", "", $hexadecimal);
for($i=0; $i<strlen($hexadecimal); $i=$i+2) {
$ascii.=chr(hexdec(substr($hexadecimal, $i, 2)));
}
return $ascii;
}
//symmetric encryption with AES-256-CBC using openssl
function AESEncrypt($secretKey,$secretIV,$plainText){
$key = $secretKey;
$iv = substr(hash('sha256', $secretIV), 0, 16);
$cipherText = base64_encode(openssl_encrypt($plainText,'AES-256-CBC', $key, 0, $iv));
return $cipherText;
}
//symmetric decryption with AES-256-CBC using openssl
function AESDecrypt($secretKey,$secretIV,$cipherText){
$key = $secretKey;
$iv = substr(hash('sha256', $secretIV), 0, 16);
$plainText = openssl_decrypt(base64_decode($cipherText),'AES-256-CBC', $key, 0, $iv);
return $plainText;
}
//get default webnode address
function getWebAddress(){
$tempcall=callAPI('getaddresses');
return $tempcall['0'];
}
?>