From 1edcecb26bfe1b802dacf3ce33b76844c34a5353 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 14 Dec 2019 18:31:28 +0100 Subject: [PATCH] Fixes to TON --- .../MadelineProto/AbstractAPIFactory.php | 5 + src/danog/MadelineProto/TL/Conversion/TD.php | 2 +- src/danog/MadelineProto/TL/TL.php | 37 ++++-- src/danog/MadelineProto/TON/API.php | 14 +- src/danog/MadelineProto/TON/APIFactory.php | 22 ++++ src/danog/MadelineProto/TON/InternalDoc.php | 22 +++- src/danog/MadelineProto/TON/Lite.php | 120 ++++++++++++++++++ .../MadelineProto/TON/{ADNL.php => Tools.php} | 25 +++- .../TON/{ => schemes}/lite_api.tl | 0 .../TON/{ => schemes}/ton_api.tl | 0 .../TON/{ => schemes}/tonlib_api.tl | 0 ton/lite-client.php | 21 +++ ton/ton-lite-client-test1.config.json | 22 ++++ tools/build_docs.php | 6 +- 14 files changed, 274 insertions(+), 22 deletions(-) create mode 100644 src/danog/MadelineProto/TON/Lite.php rename src/danog/MadelineProto/TON/{ADNL.php => Tools.php} (64%) rename src/danog/MadelineProto/TON/{ => schemes}/lite_api.tl (100%) rename src/danog/MadelineProto/TON/{ => schemes}/ton_api.tl (100%) rename src/danog/MadelineProto/TON/{ => schemes}/tonlib_api.tl (100%) create mode 100644 ton/lite-client.php create mode 100644 ton/ton-lite-client-test1.config.json diff --git a/src/danog/MadelineProto/AbstractAPIFactory.php b/src/danog/MadelineProto/AbstractAPIFactory.php index 0bb9f2c4..8cafce32 100644 --- a/src/danog/MadelineProto/AbstractAPIFactory.php +++ b/src/danog/MadelineProto/AbstractAPIFactory.php @@ -38,6 +38,11 @@ abstract class AbstractAPIFactory extends AsyncConstruct $this->async = &$async; } + public function async($async) + { + $this->async = $async; + } + public function __call($name, $arguments) { $yielded = Tools::call($this->__call_async($name, $arguments)); diff --git a/src/danog/MadelineProto/TL/Conversion/TD.php b/src/danog/MadelineProto/TL/Conversion/TD.php index 4bf41a62..a6c02c18 100644 --- a/src/danog/MadelineProto/TL/Conversion/TD.php +++ b/src/danog/MadelineProto/TL/Conversion/TD.php @@ -86,7 +86,7 @@ trait TD return $params; } if (!isset($params['_'])) { - \array_walk($params, [$this, 'mtproto_to_td']); + \array_walk($params, [$this, 'mtprotoToTd']); return $params; } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 8d633774..7d16fe04 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -211,11 +211,7 @@ class TL if (\strpos($line, ' ?= ') !== false) { continue; } - $name = \preg_replace(['/#.*/', '/\\s.*/'], '', $line); - if (\in_array($name, ['bytes', 'int128', 'int256', 'int512', 'int', 'long', 'double', 'string', 'bytes', 'object', 'function'])) { - continue; - } - $line = \preg_replace('/[(]([\w\.]+) ([\w\.]+)[)]/', '$1<$2>', $line); + $line = \preg_replace(['/[(]([\w\.]+) ([\w\.]+)[)]/', '/\s+/'], ['$1<$2>', ' '], $line); if (\strpos($line, ';') === false) { $lineBuf .= $line; continue; @@ -225,7 +221,11 @@ class TL $lineBuf = ''; } - $clean = \preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/', '/\s+/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', '', ' '], $line); + $name = \preg_replace(['/#.*/', '/\\s.*/'], '', $line); + if (\in_array($name, ['bytes', 'int128', 'int256', 'int512', 'int', 'long', 'double', 'string', 'bytes', 'object', 'function'])) { + continue; + } + $clean = \preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $line); $id = \hash('crc32b', $clean); if (\preg_match('/^[^\s]+#([a-f0-9]*)/i', $line, $matches)) { @@ -262,6 +262,12 @@ class TL $TL_dict[$type][$key]['params'][] = ['name' => $explode[0], 'type' => $explode[1]]; } } + /* + if (!$TL_dict[$type][$key][$type === 'constructors' ? 'predicate' : 'method']) { + var_dump($line); + \var_dump($TL_dict[$type][$key]); + }*/ + $key++; } } else { @@ -443,19 +449,28 @@ class TL return \danog\MadelineProto\Tools::packSignedLong($object); case 'int128': if (\strlen($object) !== 16) { - throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_16']); + $object = \base64_decode($object); + if (\strlen($object) !== 16) { + throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_16']); + } } return (string) $object; case 'int256': if (\strlen($object) !== 32) { - throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_32']); + $object = \base64_decode($object); + if (\strlen($object) !== 32) { + throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_32']); + } } return (string) $object; case 'int512': if (\strlen($object) !== 64) { - throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_64']); + $object = \base64_decode($object); + if (\strlen($object) !== 64) { + throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_64']); + } } return (string) $object; @@ -861,6 +876,8 @@ class TL */ public function deserialize($stream, $type = ['type' => '']) { + //var_dump($type); + if (\is_string($stream)) { $res = \fopen('php://memory', 'rw+b'); \fwrite($res, $stream); @@ -965,6 +982,8 @@ class TL } } } + //var_dump($constructorData); + if ($constructorData['predicate'] === 'gzip_packed') { if (!isset($type['subtype'])) { $type['subtype'] = ''; diff --git a/src/danog/MadelineProto/TON/API.php b/src/danog/MadelineProto/TON/API.php index abb688fb..cf4ef83f 100644 --- a/src/danog/MadelineProto/TON/API.php +++ b/src/danog/MadelineProto/TON/API.php @@ -19,13 +19,25 @@ namespace danog\MadelineProto\TON; +use danog\MadelineProto\Magic; + /** * TON API. */ class API extends InternalDoc { - public function __construct() + /** + * Construct API + * + * @param array $settings Settings + */ + public function __construct(array $settings) { Magic::classExists(); + + $this->API = new Lite($settings); + foreach (\get_class_methods($this->API) as $method) { + $this->methods[$method] = [$this->API, strtolower($method)]; + } } } diff --git a/src/danog/MadelineProto/TON/APIFactory.php b/src/danog/MadelineProto/TON/APIFactory.php index bf60c6ae..896a4362 100644 --- a/src/danog/MadelineProto/TON/APIFactory.php +++ b/src/danog/MadelineProto/TON/APIFactory.php @@ -77,4 +77,26 @@ class APIFactory extends AbstractAPIFactory * @var liteServer */ public $liteServer; + + /** + * Just proxy async requests to API + * + * @param string $name Method name + * @param array $arguments Arguments + * + * @return mixed + */ + public function __call_async($name, $arguments) + { + $lower_name = \strtolower($name); + if ($this->namespace !== '' || !isset($this->methods[$lower_name])) { + $name = $this->namespace.$name; + $aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : []; + $aargs['apifactory'] = true; + $args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : []; + + return $this->API->methodCallAsyncRead($name, $args, $aargs); + } + return $this->methods[$lower_name](...$arguments); + } } diff --git a/src/danog/MadelineProto/TON/InternalDoc.php b/src/danog/MadelineProto/TON/InternalDoc.php index e4021316..bd6343d7 100644 --- a/src/danog/MadelineProto/TON/InternalDoc.php +++ b/src/danog/MadelineProto/TON/InternalDoc.php @@ -863,8 +863,26 @@ interface engine class InternalDoc extends APIFactory { - public function test(array $extra = []) + /** + * Connect to the lite endpoints specified in the config file. + * + * @param string $config Path to config file + * + * @return \Generator + */ + public function connect(string $config, array $extra = []) { - return $this->__call(__FUNCTION__, [$extra]); + return $this->__call(__FUNCTION__, [$config, $extra]); + } + /** + * Asynchronously run async callable. + * + * @param callable $func Function + * + * @return \Generator + */ + public function loop(callable $func, array $extra = []) + { + return $this->__call(__FUNCTION__, [$func, $extra]); } } diff --git a/src/danog/MadelineProto/TON/Lite.php b/src/danog/MadelineProto/TON/Lite.php new file mode 100644 index 00000000..6adcc438 --- /dev/null +++ b/src/danog/MadelineProto/TON/Lite.php @@ -0,0 +1,120 @@ +. + * + * @author Daniil Gentili + * @copyright 2016-2019 Daniil Gentili + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * + * @link https://docs.madelineproto.xyz MadelineProto documentation + */ + +namespace danog\MadelineProto\TON; + +use danog\MadelineProto\Logger; +use danog\MadelineProto\TL\TL; + +use function Amp\File\get; + +/** + * TON API. + */ +class Lite +{ + /** + * Lite client config. + * + * @var array + */ + private $config; + /** + * Misc settings. + * + * @var array + */ + private $settings = []; + /** + * TL serializer instance. + * + * @var \danog\MadelineProto\TL\TL + */ + private $TL; + /** + * Logger instance. + * + * @var Logger + */ + public $logger; + /** + * Construct settings. + * + * @param array $settings + */ + public function __construct(array $settings) + { + $this->settings = $settings; + $this->logger = Logger::getLoggerFromSettings($this->settings); + $this->TL = new TL($this); + $this->TL->init( + [ + 'lite_api' => __DIR__.'/schemes/lite_api.tl', + 'ton_api' => __DIR__.'/schemes/ton_api.tl', + ] + ); + } + /** + * Connect to the lite endpoints specified in the config file. + * + * @param string $config Path to config file + * + * @return \Generator + */ + public function connect(string $config): \Generator + { + $config = \json_decode(yield get($config), true); + $config['_'] = 'liteclient.config.global'; + $config = Tools::convertJsonTL($config); + $config['validator']['init_block'] = $config['validator']['init_block'] ?? $config['validator']['zero_state']; + + $this->config = yield $this->TL->deserialize( + yield $this->TL->serializeObject( + ['type' => ''], + $config, + 'cleanup' + ) + ); + var_dump($this->config); + } + + /** + * Asynchronously run async callable. + * + * @param callable $func Function + * + * @return \Generator + */ + public function loop(callable $func): \Generator + { + return yield $func(); + } + + /** + * Convert parameters. + * + * @param array $parameters Parameters + * + * @return void + */ + public function botAPItoMTProto(array $parameters) + { + return $parameters; + } +} diff --git a/src/danog/MadelineProto/TON/ADNL.php b/src/danog/MadelineProto/TON/Tools.php similarity index 64% rename from src/danog/MadelineProto/TON/ADNL.php rename to src/danog/MadelineProto/TON/Tools.php index 9486da75..9311ad7a 100644 --- a/src/danog/MadelineProto/TON/ADNL.php +++ b/src/danog/MadelineProto/TON/Tools.php @@ -1,7 +1,7 @@ [ + 'logger' => Logger::ECHO_LOGGER + ] + ] +); + +$API->async(true); +$API->loop( + function () use ($API) { + yield $API->connect(__DIR__.'/ton-lite-client-test1.config.json'); + } +); diff --git a/ton/ton-lite-client-test1.config.json b/ton/ton-lite-client-test1.config.json new file mode 100644 index 00000000..47ecdb08 --- /dev/null +++ b/ton/ton-lite-client-test1.config.json @@ -0,0 +1,22 @@ +{ + "liteservers": [ + { + "ip": 1137658550, + "port": 4924, + "id": { + "@type": "pub.ed25519", + "key": "peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M=" + } + } + ], + "validator": { + "@type": "validator.config.global", + "zero_state": { + "workchain": -1, + "shard": -9223372036854775808, + "seqno": 0, + "root_hash": "F6OpKZKqvqeFp6CQmFomXNMfMj2EnaUSOXN+Mh+wVWk=", + "file_hash": "XplPz01CXAps5qeSWUtxcyBfdAo5zVb1N979KLSKD24=" + } + } +} diff --git a/tools/build_docs.php b/tools/build_docs.php index fed2418f..d899a8e9 100755 --- a/tools/build_docs.php +++ b/tools/build_docs.php @@ -100,9 +100,9 @@ $doc->mkAnnotations(); $ton = [ 'tl_schema' => [ - 'lite_api' => "$d/src/danog/MadelineProto/TON/lite_api.tl", - 'ton_api' => "$d/src/danog/MadelineProto/TON/ton_api.tl", - //'tonlib_api' => "$d/src/danog/MadelineProto/TON/tonlib_api.tl", + 'lite_api' => "$d/src/danog/MadelineProto/TON/schemes/lite_api.tl", + 'ton_api' => "$d/src/danog/MadelineProto/TON/schemes/ton_api.tl", + //'tonlib_api' => "$d/src/danog/MadelineProto/TON/schemes/tonlib_api.tl", ] ];