implement message save to blockchain

This commit is contained in:
ghost 2024-05-01 01:31:35 +03:00
parent 3ce5759389
commit 8cb4fe21ca
2 changed files with 145 additions and 51 deletions

View File

@ -13,6 +13,17 @@
{ {
"namespace":"", "namespace":"",
"account":null "account":null
},
"event":
{
"put":
{
"debug":
{
"enabled":true,
"template":"[{time}] [put] {host}#{crid} namespace: {name} transaction: {txid} left: {keva}"
}
}
} }
}, },
"nps": "nps":
@ -56,7 +67,7 @@
{ {
"response": "response":
[ [
"\u001b[34m\u001b[1mWelcome to KevaChat!\u001b[0m", "\u001b[36m\u001b[1mWelcome to KevaChat NPS!\u001b[0m",
"\u001b[34mEnter captcha to confirm you are human\u001b[0m" "\u001b[34mEnter captcha to confirm you are human\u001b[0m"
], ],
"debug": "debug":
@ -73,7 +84,7 @@
{ {
"success": "success":
[ [
"\u001b[34mGood, enter your message (commit by dot)\u001b[0m" "\u001b[34mWell, enter your message (dot to commit)\u001b[0m"
], ],
"failure": "failure":
[ [
@ -84,13 +95,20 @@
{ {
"success": "success":
[ [
"\u001b[32mThanks, your message successfully sent!\u001b[0m" "\u001b[34mThanks, your message successfully sent!\u001b[0m",
"\u001b[35mNS:{name}\u001b[0m",
"\u001b[35mID:{txid}\u001b[0m"
], ],
"failure": "failure":
{ {
"length":
[
"\u001b[31mReached message length limit!\u001b[0m"
],
"internal": "internal":
[ [
"\u001b[31mSomething went wrong, please make your feedback!\u001b[0m" "\u001b[31mSomething went wrong, please make your feedback!\u001b[0m",
"\u001b[36mhttps://github.com/kevachat/npsapp/issues\u001b[0m"
] ]
} }
} }

View File

@ -107,6 +107,9 @@ class Ratchet implements MessageComponentInterface
// Init connection counter // Init connection counter
$connection->count = 0; $connection->count = 0;
// Init connection message
$connection->message = '';
// Debug open event on enabled // Debug open event on enabled
if ($config->event->open->debug->enabled) if ($config->event->open->debug->enabled)
{ {
@ -146,53 +149,6 @@ class Ratchet implements MessageComponentInterface
// Init config namespace // Init config namespace
$config = $this->_config->nps->event->message; $config = $this->_config->nps->event->message;
// Captcha request first for unconfirmed connections
if (!$connection->confirmed)
{
// Request match captcha
if ($request == $connection->captcha)
{
$connection->confirmed = true;
$connection->send(
implode(
PHP_EOL,
$config->response->captcha->success
) . PHP_EOL
);
}
// Captcha request invalid
else
{
$connection->confirmed = false;
$connection->send(
implode(
PHP_EOL,
$config->response->captcha->failure
) . PHP_EOL
);
// Drop connection or do something else..
$connection->close();
}
}
// @TODO compose request to KevaCoin, return transaction ID
else
{
// Save massage to kevacoin
/*
$connection->send(
implode(
PHP_EOL,
$config->response->captcha->failure
) . PHP_EOL
);
*/
}
// Debug message event on enabled // Debug message event on enabled
if ($config->debug->enabled) if ($config->debug->enabled)
{ {
@ -220,6 +176,126 @@ class Ratchet implements MessageComponentInterface
) . PHP_EOL ) . PHP_EOL
); );
} }
// Connection confirmed
if ($connection->confirmed)
{
// Check message commit by dot
if ($request == '.')
{
// Save massage to KevaCoin blockchain
if ($txid = $this->_kevacoin->kevaPut($this->_config->kevacoin->wallet->namespace, time(), $connection->message))
{
// Return success response
$connection->send(
str_replace(
[
'{name}',
'{txid}'
],
[
$this->_config->kevacoin->wallet->namespace,
$txid
],
implode(
PHP_EOL,
$config->response->submit->success
) . PHP_EOL
)
);
// Print transaction debug info on enabled
if ($this->_config->kevacoin->event->put->debug->enabled)
{
print(
str_ireplace(
[
'{time}',
'{host}',
'{crid}',
'{name}',
'{txid}',
'{keva}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId,
(string) $this->_config->kevacoin->wallet->namespace,
(string) $txid,
(string) $this->_kevacoin->getBalance(
$this->_config->kevacoin->wallet->account
)
],
$this->_config->kevacoin->event->put->debug->template
) . PHP_EOL
);
}
}
// Could not receive transaction, something went wrong
else
{
$connection->send(
implode(
PHP_EOL,
$config->response->submit->failure->internal
) . PHP_EOL
);
}
// Close connection at this point
$connection->close();
}
// Complete message by new line sent
$connection->message .= $request . PHP_EOL;
// Check total message length limit allowed by KevaCoin protocol
if (mb_strlen($connection->message) > 3074)
{
$connection->send(
implode(
PHP_EOL,
$config->response->submit->failure->length
) . PHP_EOL
);
$connection->close();
}
}
// Captcha request
else
{
// Request match captcha
if ($request == $connection->captcha)
{
$connection->confirmed = true;
$connection->send(
implode(
PHP_EOL,
$config->response->captcha->success
) . PHP_EOL
);
}
// Captcha request invalid
else
{
$connection->confirmed = false;
$connection->send(
implode(
PHP_EOL,
$config->response->captcha->failure
) . PHP_EOL
);
$connection->close();
}
}
} }
public function onClose( public function onClose(