CLI tools for KevaCoin blockchain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
2.8 KiB

<?php
// Init version
define(
'_CLITOR_IS_',
'1.3.0'
);
// Init helper
function _exec(
string $processor,
string $command
): mixed
{
if (false !== exec(sprintf('%s %s', $processor, $command), $output))
{
$rows = [];
foreach($output as $row)
{
$rows[] = $row;
}
if ($result = @json_decode(implode(PHP_EOL, $rows)))
{
return $result;
}
}
return false;
}
// Check processor exits
if (empty($argv[1]))
{
print(
'processor required' . PHP_EOL
);
exit;
}
if (!file_exists($argv[1]))
{
print(
'processor does not exist' . PHP_EOL
);
exit;
}
// Check file exits
if (empty($argv[2]))
{
print(
'filename required' . PHP_EOL
);
exit;
}
if (!file_exists($argv[2]))
{
print(
'filename does not exist' . PHP_EOL
);
exit;
}
// Get file name
$basename = basename(
$argv[2]
);
// Check filename not longer of protocol
if (mb_strlen($basename) > 255)
{
// Get file hash sum
$basename = md5_file(
$argv[2]
);
// Dump event
print(
'Filename to long, used md5_file as key...' . PHP_EOL
);
}
// Split content to smaller parts (according to the protocol limits)
$pieces = str_split(
base64_encode(
file_get_contents(
$argv[2]
)
),
3072
);
// Count total pieces
$total = count(
$pieces
);
// Create namespace to collect there data pieces
$ns = _exec(
$argv[1],
sprintf(
"keva_namespace '%s'",
str_replace(
"'",
"\'",
$basename
)
)
);
// Validate namespace created
if (empty($ns->namespaceId))
{
print(
'could not create namespace record' . PHP_EOL
);
exit;
}
// Create meta record
print_r(
_exec(
$argv[1],
sprintf(
'keva_put %s %s %s', $ns->namespaceId, '_CLITOR_IS_', _CLITOR_IS_
)
)
);
// Begin pieces saving
foreach ($pieces as $key => $value)
{
print_r(
_exec(
$argv[1],
sprintf(
"%s '%s' '%s' '%s'",
'keva_put',
$ns->namespaceId,
$key,
$value
)
)
);
// Apply delays to prevent too-long-mempool-chain reject
$delay = isset($argv[4]) && $argv[4] > 0 ? (int) $argv[4] : 60;
printf(
'%s/%s sent, waiting %s seconds...' . PHP_EOL,
$key + 1,
$total,
$delay
);
sleep(
$delay
);
}
// Done
printf(
'data successfully sent to namespace "%s"' . PHP_EOL,
$ns->namespaceId
);
printf(
"run to extract: /usr/bin/php '%s/get.php' '%s' '%s' '/path/to/%s'" . PHP_EOL,
__DIR__,
$argv[1],
$ns->namespaceId,
$basename
);