From 7951d031363ee9531a3839850af4206d1e1caed8 Mon Sep 17 00:00:00 2001 From: Dmitry Boldyrev Date: Wed, 15 Mar 2017 09:03:01 +0300 Subject: [PATCH] Update readme, fix bugs Signed-off-by: Dmitry Boldyrev --- README.md | 120 +++++++++++++++++++++++++++++++++------------- examples/init.js | 4 +- package.json | 44 ++++++++--------- src/index.js | 8 ++-- src/tl/index.js | 2 +- test/node.test.js | 4 +- 6 files changed, 118 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 5086a4a..c2c0dba 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## About MTProto.. -**MTProto** is the [Telegram Messenger](http://www.telegram.org ) protocol +**MTProto** is the [Telegram Messenger](http://www.telegram.org) protocol _"designed for access to a server API from applications running on mobile devices"_. The Mobile Protocol is subdivided into three components ([from the official site](https://core.telegram.org/mtproto#general-description)): @@ -31,7 +31,9 @@ The **telegram-mtproto** library implements the **Mobile Protocol** and provides - Promise-based API - - Both **TCP** and **HTTP connections** implemented in the transport layer + - **HTTP connections** implemented in the transport layer + + - **Web worker** support for blazing fast crypto math works in background - A cipher implementation for **AES and RSA encryption** in the security layer @@ -41,60 +43,112 @@ The **telegram-mtproto** library implements the **Mobile Protocol** and provides - **MTProto TL-Schema** compilation as **javascript classes and functions** + - Custom **async storage** support for saving user data between sessions + + +## Installation + +```bash +$ npm install --save telegram-mtproto@beta +``` + ## Usage ```javascript -const { Telegram, network } = require('telegram-mtproto') +import MTProto from 'telegram-mtproto' -const fileSchema = require('./api-schema-57.json') +const phone = { + num : '+9996620001', + code: '22222' +} -const telegram = new Telegram(fileSchema) -const addKey = key => telegram.addPublicKey(key) -publicKeys.forEach(addKey) +const api = { + invokeWithLayer: 0xda9b0d0d, + layer : 57, + initConnection : 0x69796de9, + api_id : 49631 +} -const connection = new network.http(server) -const client = telegram.createClient() +const server = { + dev: true //We will connect to the test server. +} //Any empty configurations fields can just not be specified -client.setConnection(connection) -connection.connect() - .then(() => console.log(`api ready for requests`)) - .then(() => client.callApi('auth.sendCode', { +const client = MTProto({ server, api }) + +async function connect(){ + const { phone_code_hash } = await client('auth.sendCode', { phone_number : phone.num, current_number: false, - api_id : config.id, - api_hash : config.hash - }).then( - ({ phone_code_hash }) => - client.callApi('auth.signIn', { - phone_number : phone.num, - phone_code_hash: phone_code_hash, - phone_code : phone.code - })) + api_id : 49631, + api_hash : 'fb050b8f6771e15bfda5df2409931569' + }) + const { user } = await client('auth.signIn', { + phone_number : phone.num, + phone_code_hash: phone_code_hash, + phone_code : phone.code + }) + + console.log('signed as ', user) +} + +connect() ``` -## Installation - -```bash -$ npm install --save telegram-mtproto +Above we used two functions from the API. +```typescript +type auth.sendCode = (phone_number: string, sms_type: int, + api_id: int, api_hash: string, lang_code: string) => { + phone_registered: boolean, + phone_code_hash: string, + send_call_timeout: int, + is_password: boolean + } + +type auth.signIn = (phone_number: string, phone_code_hash: string, phone_code: string) => { + expires: int, + user: User +} ``` +[More][send-code] about [them][sign-in], as well as about many other methods, you can read in the [official documentation][docs]. -## API +Additional examples can be obtained from [examples][examples] folder. -- **network**. Classes for network connection +## Storage - - **http** +You can use your own storages like [localForage][localForage] for saving data. +Module accepts the following interface - - **tcp** +```typescript +interface AsyncStorage { + get(key: string): Promise; + set(obj: any): Promise; + remove(...keys: string[]): Promise; + clear(): Promise; +} +``` -- **tl**. Telegram schema api +```javascript +import { MTProto } from 'telegram-mtproto' +import { api } from './config' +import CustomStorage from './storage' -- **Telegram**. High level api. +const client = MTProto({ + api, + app: { + storage: CustomStorage + } +}) - - **createClient** *() => TelegramClient* +``` ## License The project is released under the [Mit License](./LICENSE) +[examples]: https://github.com/zerobias/telegram-mtproto/tree/develop/examples +[localForage]: https://github.com/localForage/localForage +[docs]: https://core.telegram.org/ +[send-code]: https://core.telegram.org/method/auth.sendCode +[sign-in]: https://core.telegram.org/method/auth.signIn [npm-url]: https://www.npmjs.org/package/telegram-mtproto [npm-image]: https://badge.fury.io/js/telegram-mtproto.svg \ No newline at end of file diff --git a/examples/init.js b/examples/init.js index 58e634a..a91477d 100644 --- a/examples/init.js +++ b/examples/init.js @@ -1,4 +1,4 @@ -const { ApiManager } = require('../lib/index') +const { MTProto } = require('../lib') const api = { invokeWithLayer: 0xda9b0d0d, @@ -11,6 +11,6 @@ const api = { const server = { webogram: true, dev: true } -const telegram = new ApiManager({ api, server }) +const telegram = MTProto({ api, server }) module.exports = telegram \ No newline at end of file diff --git a/package.json b/package.json index 33fb05b..008c377 100644 --- a/package.json +++ b/package.json @@ -26,55 +26,55 @@ "author": "Zero Bias", "dependencies": { "@goodmind/node-cryptojs-aes": "^0.5.0", - "ajv": "^4.11.3", + "ajv": "^4.11.5", "ajv-keywords": "^1.5.1", "axios": "^0.15.3", "bluebird": "^3.4.7", - "debug": "^2.6.1", + "debug": "^2.6.3", "detect-node": "^2.0.3", - "eventemitter2": "^3.0.0", + "eventemitter2": "^3.0.2", "pako": "^1.0.4", "ramda": "^0.23.0", "randombytes": "^2.0.3", "rusha": "^0.8.5", - "setimmediate": "^1.0.5", - "worker-loader": "^0.7.1" + "worker-loader": "^0.8.0" }, "devDependencies": { "@types/debug": "0.0.29", - "babel-cli": "^6.23.0", - "babel-core": "^6.23.1", + "babel-cli": "^6.24.0", + "babel-core": "^6.24.0", "babel-eslint": "^7.1.1", - "babel-loader": "^6.3.2", + "babel-loader": "^6.4.0", "babel-plugin-closure-elimination": "^1.1.14", "babel-plugin-transform-async-to-generator": "^6.22.0", "babel-plugin-transform-async-to-module-method": "^6.22.0", "babel-plugin-transform-class-properties": "^6.23.0", - "babel-plugin-transform-es2015-modules-commonjs": "^6.23.0", + "babel-plugin-transform-es2015-modules-commonjs": "^6.24.0", "babel-plugin-transform-flow-strip-types": "^6.22.0", "babel-plugin-transform-object-rest-spread": "^6.23.0", - "cross-env": "^3.1.4", - "eslint": "^3.15.0", - "eslint-plugin-babel": "^4.0.1", - "eslint-plugin-flowtype": "^2.30.0", - "eslint-plugin-promise": "^3.4.2", - "fast-async": "^6.2.1", - "flow-bin": "^0.40.0", + "cross-env": "^3.2.4", + "eslint": "^3.17.1", + "eslint-plugin-babel": "^4.1.1", + "eslint-plugin-flowtype": "^2.30.3", + "eslint-plugin-promise": "^3.5.0", + "flow-bin": "^0.41.0", "hirestime": "^3.1.0", - "nightmare": "^2.9.1", + "nightmare": "^2.10.0", "prompt": "^1.0.0", - "rimraf": "^2.5.4", - "tap": "^10.1.1", + "rimraf": "^2.6.1", + "tap": "^10.3.0", "webpack": "^2.2.1" }, "engines": { - "node": ">0.10.30" + "node": ">=6.0" + }, + "bugs": { + "url": "https://github.com/zerobias/telegram-mtproto/issues" }, "files": [ "lib", "test", "examples", - "dist", "schema", "source", "es", @@ -99,5 +99,5 @@ "type": "git", "url": "git+https://github.com/zerobias/telegram-mtproto.git" }, - "version": "2.1.1" + "version": "2.2.0" } diff --git a/src/index.js b/src/index.js index f8a6551..c6e01dd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,10 @@ +import MTProto from './service/main/wrap' + export { CryptoWorker } from './crypto' export { bin } from './bin' export { ApiManager } from './service/api-manager/index' import * as MtpTimeManager from './service/time-manager' export { MtpTimeManager } - -import MTProto from './service/main/wrap' -export default MTProto -module.exports = MTProto \ No newline at end of file +export { MTProto } +export default MTProto \ No newline at end of file diff --git a/src/tl/index.js b/src/tl/index.js index a5a210a..d6e6f45 100644 --- a/src/tl/index.js +++ b/src/tl/index.js @@ -500,7 +500,7 @@ export class Deserialization { const compressed = this.fetchBytes(`${field}[packed_string]`) const uncompressed = gzipUncompress(compressed) const buffer = bytesToArrayBuffer(uncompressed) - const newDeserializer = new Deserialization(buffer) + const newDeserializer = new Deserialization(buffer, { mtproto: this.mtproto, override: this.override }, this.api, this.mtApi) return newDeserializer.fetchObject(type, field) } diff --git a/test/node.test.js b/test/node.test.js index 9d9aaad..cfc3745 100644 --- a/test/node.test.js +++ b/test/node.test.js @@ -1,5 +1,5 @@ const test = require('tap').test -const MTProto = require('../lib/index') +const { MTProto } = require('../lib') const phone = { num : '+9996620000', @@ -16,7 +16,7 @@ const api = { } const server = { dev : true, - webogram: true + webogram: false } const config = {