Skip to content
Permalink
Browse files

Chainy service added

  • Loading branch information...
ArtemKo7v committed Sep 9, 2016
1 parent 3bd72df commit be3d2efc54312abdb1f639518e4174f9c8744145
Showing with 295 additions and 156 deletions.
  1. +3 −1 .gitignore
  2. +92 −83 app/classes/AmiLabs/Chainy/TX.php
  3. +12 −32 app/controllers/indexController.php
  4. +130 −0 bin/service.js
  5. +5 −40 cfg/sample.chainy.php
  6. +53 −0 cfg/sample.service.js
@@ -3,4 +3,6 @@ composer.lock
web/*.local.php
tmp
log
vendor
vendor
cfg/config.chainy.php
cfg/config.service.js
@@ -56,6 +56,7 @@ class TX extends \AmiLabs\CryptoKit\TX {
* Chainy protocol version
*/
const PROTOCOL_VERSION = '1';
/**
* Returns Chainy transaction type.
*
@@ -79,34 +80,40 @@ public static function decodeChainyTransaction($code){
if(!$oCache->exists()){
$oCfg = Application::getInstance()->getConfig();
$contract = $oCfg->get('addresses/destination/address');
$result = self::_callRPC("getChainyData", array($contract, $code));
$result = self::_callRPC("get", array($contract, $code));
$oCache->save($result);
}else{
$result = $oCache->load();
}
}
return json_decode($result, JSON_OBJECT_AS_ARRAY);
try{
$data = BlockchainIO::getInstance()->getRawTransaction($tx);
if(self::isChainyTransactionRaw($data)){
$opData = TX::getDecodedOpReturn($data, true);
$txType = self::getTransactionTypeByOpReturn($opData);
$aTX['type'] = $txType;
switch($txType){
case self::TX_TYPE_REDIRECT:
$aTX += self::decodeRedirectTransaction($opData, $data);
break;
case self::TX_TYPE_HASHLINK:
$aTX += self::decodeHashLinkTransaction($opData, $data);
break;
case self::TX_TYPE_INVALID:
default:
// todo
}
$result = json_decode($result, JSON_OBJECT_AS_ARRAY);
if(is_array($result)){
switch($result['type']){
case self::TX_TYPE_HASHLINK:
$result['filesize'] = self::getFileSize($result['filesize']);
switch($result['filetype']){
case TX::FILE_TYPE_PDF:
$result['filetype'] = 'pdf';
break;
case TX::FILE_TYPE_ARCHIVE:
$result['filetype'] = 'archive';
break;
case TX::FILE_TYPE_TEXT:
$result['filetype'] = 'text';
break;
case TX::FILE_TYPE_IMAGE:
$result['filetype'] = 'image';
break;
default:
$result['filetype'] = '';
}
$result['block'] = 'Unknown';
$result['tx'] = 'Unknown';
$result['date'] = 'Unknown';
break;
}
}catch(\Exception $e){ /* todo */ }
return $aTX;
}
return json_decode($result, JSON_OBJECT_AS_ARRAY);
}
/**
@@ -246,82 +253,39 @@ public static function createHashLinkTransaction($url, $description = FALSE){
return $result;
}
public static function getTransactionCode($txHash){
/**
* Returns Chainy link by transaction code.
*
* @param string $txHash
* @return string
*/
public static function getChainyLink($txHash){
$result = FALSE;
if(is_string($txHash) && (66 === strlen($txHash))){
$tx = self::_callRPC("getReceipt", array($txHash));
if(is_array($tx) && isset($tx['logs']) && count($tx['logs'])){
$log = $tx['logs'][0];
if("0xdad5c3eecfdb62dd69e6e72053b88029e1d6277d4bc773c00fef243982adcb7d" === $log['topics'][0]){
$data = $log['data'];
$data = substr($data, 192);
$data = preg_replace("/0+$/", "", $data);
$result = '';
for($i=0; $i < strlen($data)-1; $i+=2){
$result .= chr(hexdec($data[$i] . $data[$i+1]));
}
}
}
$result = self::_callRPC("getLink", array($txHash));
}
return $result;
}
/**
* Publishes chainy TX on blockchain.
*
* @param array $data
* @return array
*/
public static function publishData(array $data){
$result = array();
$result = FALSE;
$oCfg = Application::getInstance()->getConfig();
if($oCfg->get('autopublish', FALSE)){
$tx = self::_callRPC(
"createChainyTX",
array(
$oCfg->get('addresses/source/address'),
$oCfg->get('addresses/destination/address'),
json_encode($data, JSON_UNESCAPED_SLASHES)
)
);
// Transaction hash length is 66 bytes
$tx = self::_callRPC("add", array($oCfg->get('sender'), json_encode($data, JSON_UNESCAPED_SLASHES)));
// Transaction hash length should be 66 bytes
if(strlen($tx) == 66){
$result = array('hash' => $tx);
}
}
return $result;
}
protected static function _getTxData($type, array $data){
// @todo: check type
return array(
'id' => 'CHAINY',
'version' => 1,
'type' => $type,
) + $data;
}
/**
* Returns file type by filename or url.
*
* @param string $url URL of file
* @return int
*/
protected static function getFileType($url){
$urlNoParams = $url;
if(strpos($url, '?') !== false){
$urlNoParams = substr($url, 0, strpos($url, '?'));
}
$ext = strtolower(substr($urlNoParams, strrpos($urlNoParams, '.') + 1));
$fileType = self::FILE_TYPE_UNKNOWN;
$aFileTypes = array(
self::FILE_TYPE_PDF => array('pdf'),
self::FILE_TYPE_ARCHIVE => array('zip', 'rar', 'gz', 'arj', '7z', 'tgz', 'lzh'),
self::FILE_TYPE_TEXT => array('txt', 'doc', 'dox', 'rtf'),
self::FILE_TYPE_IMAGE => array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'psd', 'tiff', 'ico', 'pic', 'pcx')
);
foreach($aFileTypes as $type => $aExtensions){
if(in_array($ext, $aExtensions)){
$fileType = $type;
break;
}
}
return $fileType;
}
/**
* Encodes int to base58 string.
*
@@ -353,13 +317,30 @@ public static function decodeBase58($base58){
}
return $int_val;
}
/**
* Returns formatted Chainy data.
*
* @param string $type
* @param array $data
* @return array
*/
protected static function _getTxData($type, array $data){
// @todo: check type
return array(
'id' => 'CHAINY',
'version' => 1,
'type' => $type,
) + $data;
}
/**
* Returns filesize with size dimension units.
*
* @param int $size
* @return string
*/
public static function getFileSize($size){
protected static function getFileSize($size){
if($size < 1024){
return $size . 'B';
}
@@ -372,6 +353,34 @@ public static function getFileSize($size){
return round($size / (1024 * 1024 * 1024)) . 'GB';
}
/**
* Returns file type by filename or url.
*
* @param string $url URL of file
* @return int
*/
protected static function getFileType($url){
$urlNoParams = $url;
if(strpos($url, '?') !== false){
$urlNoParams = substr($url, 0, strpos($url, '?'));
}
$ext = strtolower(substr($urlNoParams, strrpos($urlNoParams, '.') + 1));
$fileType = self::FILE_TYPE_UNKNOWN;
$aFileTypes = array(
self::FILE_TYPE_PDF => array('pdf'),
self::FILE_TYPE_ARCHIVE => array('zip', 'rar', 'gz', 'arj', '7z', 'tgz', 'lzh'),
self::FILE_TYPE_TEXT => array('txt', 'doc', 'dox', 'rtf'),
self::FILE_TYPE_IMAGE => array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'psd', 'tiff', 'ico', 'pic', 'pcx')
);
foreach($aFileTypes as $type => $aExtensions){
if(in_array($ext, $aExtensions)){
$fileType = $type;
break;
}
}
return $fileType;
}
/**
* JSON RPC request implementation.
*
@@ -33,7 +33,7 @@ public function actionIndex(array $aParameters){
$byHash = $aParameters['byHash'];
if($byHash){
$txNo = $aParameters['hash'];
$link = TX::getTransactionCode($aParameters['hash']);
$link = TX::getChainyLink($aParameters['hash']);
$code = substr($link, strrpos($link, '/') + 1);
}else{
$code = $aParameters['code'];
@@ -45,34 +45,14 @@ public function actionIndex(array $aParameters){
$oLogger->log('Code:' . $code . ', IP:' . $ipAddress . ', Referer:' . $referer);
$result = TX::decodeChainyTransaction($code);
}
if(isset($result) && is_array($result)){
if($result['type'] == TX::TX_TYPE_HASHLINK){
$result['filesize'] = TX::getFileSize($result['filesize']);
switch($result['filetype']){
case TX::FILE_TYPE_PDF:
$result['filetype'] = 'pdf';
break;
case TX::FILE_TYPE_ARCHIVE:
$result['filetype'] = 'archive';
break;
case TX::FILE_TYPE_TEXT:
$result['filetype'] = 'text';
break;
case TX::FILE_TYPE_IMAGE:
$result['filetype'] = 'image';
break;
default:
$result['filetype'] = '';
}
$result['block'] = 'Unknown';
$result['tx'] = 'Unknown';
$result['date'] = 'Unknown';
$this->oView->set('aTX', $result);
}
if($result['type'] == TX::TX_TYPE_REDIRECT){
header('Location:' . $result['url']);
die();
if(is_array($result)){
switch($result['type']){
case TX::TX_TYPE_HASHLINK:
$this->oView->set('aTX', $result);
break;
case TX::TX_TYPE_HASHLINK:
header('Location:' . $result['url']);
die();
}
}else{
$oLogger->log('ERROR: Code ' . $code . ' not found (404), no corresponding transaction.');
@@ -159,15 +139,15 @@ public function actionAdd(array $aParameters){
* @return \AmiLabs\DevKit\Controller
*/
public function actionShort(array $aParameters){
$result = TX::getTransactionCode($aParameters['hash']);
$result = TX::getChainyLink($aParameters['hash']);
echo $result ? $result : '';
die();
}
/**
* Not found.
*/
protected function notFound(){
// header('Location: http://chainy.info/err/404', TRUE, 301);
die(404);
$link404 = $this->getConfig()->get('link404');
header('Location: ' . $link404, TRUE, 301);
}
}

0 comments on commit be3d2ef

Please sign in to comment.
You can’t perform that action at this time.