diff --git a/node_modules/lib/mtproto.js b/node_modules/lib/mtproto.js index a8cf096..bbf47b8 100644 --- a/node_modules/lib/mtproto.js +++ b/node_modules/lib/mtproto.js @@ -11,9 +11,9 @@ // Import dependencies var util = require('util'); -var AbstractObject = require("lib/type-language").AbstractObject; -var Builder = require("lib/type-language").Builder; var crypto = require("lib/crypto-util"); +var TypeObject = require("telegram-tl-node").TypeObject; +var TypeBuilder = require("telegram-tl-node").TypeBuilder; // MtProto API as provided by Telegram var api = {"constructors": [ @@ -206,19 +206,7 @@ var api = {"constructors": [ ]}; // Declare the `mtproto` module -var mtproto = {}; - -// Register the `TypeLanguage class` list -mtproto._classes = ['ResPQ', 'P_Q_inner_data', 'Server_DH_Params', 'Server_DH_inner_data', 'Client_DH_Inner_Data', 'Set_client_DH_params_answer']; - -// Build registered classes -Builder.buildTypes(api.constructors, mtproto._classes, mtproto); - -// Register the `TypeLanguage method` list -mtproto._methods = ['req_pq', 'req_DH_params', 'set_client_DH_params']; - -// Build registered methods -Builder.buildTypes(api.methods, mtproto._methods, mtproto, true); +var mtproto = { _id: 'mtproto'}; // PlainMessage class // @@ -249,7 +237,7 @@ mtproto.PlainMessage = function (options) { this._logger = require('get-log')('mtproto.PlainMessage'); }; -util.inherits(mtproto.PlainMessage, AbstractObject); +util.inherits(mtproto.PlainMessage, TypeObject); // This method serialize the PlainMessage mtproto.PlainMessage.prototype.serialize = function () { @@ -290,5 +278,17 @@ mtproto.PlainMessage.prototype.getMessageId = function () { return this._messageId; }; +// Register the `TypeLanguage class` list +mtproto._classes = ['ResPQ', 'P_Q_inner_data', 'Server_DH_Params', 'Server_DH_inner_data', 'Client_DH_Inner_Data', 'Set_client_DH_params_answer']; + +// Build registered classes +TypeBuilder.buildTypes(api.constructors, mtproto._classes, mtproto); + +// Register the `TypeLanguage method` list +mtproto._methods = ['req_pq', 'req_DH_params', 'set_client_DH_params']; + +// Build registered methods +TypeBuilder.buildTypes(api.methods, mtproto._methods, mtproto, mtproto.PlainMessage); + // Export the package module.exports = exports = mtproto; \ No newline at end of file diff --git a/node_modules/lib/net/http-connection.js b/node_modules/lib/net/http-connection.js index fb73b62..b382367 100644 --- a/node_modules/lib/net/http-connection.js +++ b/node_modules/lib/net/http-connection.js @@ -11,7 +11,6 @@ // Import dependencies var http = require('http'); var util = require('util'); -var AbstractObject = require("lib/type-language").AbstractObject; var logger = require('get-log')('net.HttpConnection'); // The constructor accepts optionally an object to specify the connection address as following: diff --git a/node_modules/lib/net/tcp-connection.js b/node_modules/lib/net/tcp-connection.js index e051aec..83cea20 100644 --- a/node_modules/lib/net/tcp-connection.js +++ b/node_modules/lib/net/tcp-connection.js @@ -11,7 +11,7 @@ // Import dependencies var net = require('net'); var util = require('util'); -var AbstractObject = require("lib/type-language").AbstractObject; +var TypeObject = require("telegram-tl-node").TypeObject; var logger = require('get-log')('net.TcpConnection'); // The constructor accepts optionally an object to specify the connection address as following: @@ -191,7 +191,7 @@ TcpConnection.Message = function (options) { } }; -util.inherits(TcpConnection.Message, AbstractObject); +util.inherits(TcpConnection.Message, TypeObject); // This method serialize the Message TcpConnection.Message.prototype.serialize = function () { diff --git a/node_modules/lib/type-language/abstract-object.js b/node_modules/lib/type-language/abstract-object.js deleted file mode 100644 index feb8b7e..0000000 --- a/node_modules/lib/type-language/abstract-object.js +++ /dev/null @@ -1,307 +0,0 @@ -// telegram.link -// -// Copyright 2014 Enrico Stara 'enrico.stara@gmail.com' -// Released under the Simplified BSD License -// http://telegram.link - -// AbstractObject class -// -// The `AbstractObject` class is an abstraction of the `TypeLanguage Objects and Methods` providing read/write methods to serialize/de-serialize -// the TypeLanguage binary format - -// Import dependencies -var BigInteger = require('jsbn'); - -// The constructor may be called giving a `Buffer` with the binary image - eventually starting from an `offset` - -// in order to de-serialize a `TypeLanguage entity` via `read*` methods, -// otherwise you can call it without any argument and start serializing a new one via `write*` methods -function AbstractObject(buffer, offset) { - if (buffer) { - this._writeBuffers = null; - this._readOffset = 0; - buffer = Buffer.isBuffer(buffer) ? buffer : new Buffer(buffer); - this._buffer = offset ? buffer.slice(offset) : buffer; - } else { - this._writeBuffers = []; - this._writeOffset = 0; - } -} - -// The base method to de-serialize the object -AbstractObject.prototype.deserialize = function () { - var logger = this.constructor.logger; - if (!this.isReadonly()) { - if (logger) logger.warn('Unable to de-serialize, the buffer is undefined'); - return false; - } - var id = this._readBytes(4).toString('hex'); - if (logger && logger.isDebugEnabled()) { - logger.debug('read ID = %s', id); - } - if (this.id != id) { - if (logger) logger.warn('Unable to de-serialize, (read id) %s != (this.id) %s', id, this.id); - return false; - } - return this; -}; - -// The base method to serialize the object -AbstractObject.prototype.serialize = function () { - var logger = this.constructor.logger; - if (logger && logger.isDebugEnabled()) { - logger.debug('write ID = %s', this.id); - } - return this._writeBytes(new Buffer(this.id, 'hex')); -}; - -// The method finalizes the serialization process and retrieves the `Buffer` image of the object, -// putting the instance in `readonly` state -AbstractObject.prototype.retrieveBuffer = function () { - if (!this._buffer) { - this._buffer = Buffer.concat(this._writeBuffers); - } - this._writeBuffers = null; - this._writeOffset = 0; - this._readOffset = 0; - return this._buffer; -}; - -AbstractObject.prototype._addWriteBuffer = function (buffer) { - this._writeBuffers.push(buffer); - this._writeOffset += buffer.length; - var logger = this.constructor.logger; - if (logger && logger.isDebugEnabled()) { - logger.debug('Write offset %s', this._writeOffset); - } -}; - -// The method writes the `int` value given as argument -AbstractObject.prototype.writeInt = function (intValue) { - if (this.isReadonly()) { - return false; - } - var buffer = new Buffer(4); - buffer.writeUInt32LE(intValue, 0, true); - var logger = this.constructor.logger; - if (logger && logger.isDebugEnabled()) { - logger.debug('intValue %s, intBuffer %s', intValue, buffer.toString('hex')); - } - this._addWriteBuffer(buffer); - return true; -}; - -// The method writes the `BigInteger` value given as argument, you have to provide the value as `String` type -// and specify a `byte length`, where `length % 4 == 0` -AbstractObject.prototype._writeBigInt = function (bigInteger, byteLength) { - if (this.isReadonly() || (byteLength % 4) !== 0) { - return false; - } - this._addWriteBuffer(stringValue2Buffer('' + bigInteger, byteLength)); - return true; -}; - -// The method writes the `byte[]` value given as argument, -// adding the bytes length at the beginning -// and adding padding at the end if needed -AbstractObject.prototype.writeBytes = function (bytes, useWordLength) { - if (this.isReadonly()) { - return false; - } - var bLength = useWordLength ? bytes.length / 4 : bytes.length; - var isShort = bLength < (useWordLength ? 0x7F : 0xFE); - var buffer = new Buffer(isShort ? 1 : 4); - var offset = 0; - if (isShort) { - buffer.writeUInt8(bLength, offset++); - } else { - buffer.writeUInt8((useWordLength ? 0x7F : 0xFE), offset++); - buffer.writeUInt8(bLength & 0xFF, offset++); - buffer.writeUInt8((bLength >> 8) & 0xFF, offset++); - buffer.writeUInt8((bLength >> 16) & 0xFF, offset++); - } - this._addWriteBuffer(buffer); - this._writeBytes(bytes); - // add padding if needed - if (!useWordLength) { - var padding = (offset + bytes.length) % 4; - if (padding > 0) { - buffer = new Buffer(4 - padding); - buffer.fill(0); - this._addWriteBuffer(buffer); - } - } - return true; -}; - -// The method writes the `string` value given as argument -AbstractObject.prototype.writeString = function (str) { - return this.writeBytes(str); -}; - -// The method writes the `byte[]` value given as argument -AbstractObject.prototype._writeBytes = function (bytes) { - if (this.isReadonly()) { - return false; - } - var buffer = !Buffer.isBuffer(bytes) ? new Buffer(bytes) : bytes; - this._addWriteBuffer(buffer); - return true; -}; - -// The method writes the `long` value given as argument -AbstractObject.prototype.writeLong = function (bigInteger) { - return (typeof bigInteger == 'string' || typeof bigInteger == 'number') ? this._writeBigInt(bigInteger, 8) : - this._writeBytes(bigInteger); -}; - -// The method writes the `int128` value given as argument -AbstractObject.prototype.writeInt128 = function (bigInteger) { - return (typeof bigInteger == 'string' || typeof bigInteger == 'number') ? this._writeBigInt(bigInteger, 16) : - this._writeBytes(bigInteger); -}; - -// The method writes the `int256` value given as argument -AbstractObject.prototype.writeInt256 = function (bigInteger) { - return (typeof bigInteger == 'string' || typeof bigInteger == 'number') ? this._writeBigInt(bigInteger, 32) : - this._writeBytes(bigInteger); -}; - - -// The method reads an `int` value starting from the current position -AbstractObject.prototype.readInt = function () { - if (!this.isReadonly() || (this._readOffset + 4) > this._buffer.length) { - return undefined; - } - var intValue = this._buffer.readUInt32LE(this._readOffset); - // Reading position will be increased of 4 - this._readOffset += 4; - return intValue; -}; - - -// The method reads a `byte[]` value starting from the current position, using the first byte(s) to get the length -AbstractObject.prototype.readBytes = function (useWordLength) { - var start = this._readOffset; - var bLength = this._buffer.readUInt8(this._readOffset++); - var logger = this.constructor.logger; - var isShort = bLength < 0x7F; - if (!isShort) { - bLength = this._buffer.readUInt8(this._readOffset++) + - (this._buffer.readUInt8(this._readOffset++) << 8) + - (this._buffer.readUInt8(this._readOffset++) << 16); - } - if (logger && logger.isDebugEnabled()) { - logger.debug('bufferLength = %s', bLength); - } - var buffer = this._readBytes(useWordLength ? bLength * 4 : bLength); - // consider padding if needed - var padding = (this._readOffset - start) % 4; - if (padding > 0) { - this._readOffset += 4 - padding; - } - return buffer; -}; - - -// The method reads a `string` value starting from the current position -AbstractObject.prototype.readString = function () { - return this.readBytes().toString('utf8'); -}; - - -// The method reads a `byte[]` value starting from the current position -AbstractObject.prototype._readBytes = function (byteLength) { - var end = this._readOffset + byteLength; - if (!this.isReadonly() || end > this._buffer.length) { - return undefined; - } - var buffer = this._buffer.slice(this._readOffset, end); - this._readOffset = end; - return buffer; -}; - -// The method reads a `BigInteger` value with a given byte length starting from the current position -AbstractObject.prototype._readBigInt = function (byteLength) { - var buffer = this._readBytes(byteLength); - return buffer ? buffer2StringValue(buffer) : undefined; -}; - -// The method reads a `long` value with starting from the current position -AbstractObject.prototype.readLong = function () { - return this._readBigInt(8); -}; - -// The method reads a `int128` value with starting from the current position -AbstractObject.prototype.readInt128 = function () { - return this._readBigInt(16); -}; - -// The method reads a `int256` value with starting from the current position -AbstractObject.prototype.readInt256 = function () { - return this._readBigInt(32); -}; - -// The method checks if the object has been already serialized and then it's `readonly` -AbstractObject.prototype.isReadonly = function () { - if (this._buffer) { - return true; - } - return false; -}; - -// The method retrieves the current read position -AbstractObject.prototype.getReadOffset = function () { - return this._readOffset; -}; - -function stringValue2Buffer(stringValue, byteLength) { - if ((stringValue).slice(0, 2) == '0x') { - var input = stringValue.slice(2); - var length = input.length; - var buffers = []; - var j = 0; - for (var i = length; i > 0 && j < byteLength; i -= 2) { - buffers.push(new Buffer(input.slice(i - 2, i), 'hex')); - j++; - } - var buffer = Buffer.concat(buffers); - var paddingLength = byteLength - buffer.length; - if(paddingLength > 0) { - var padding = new Buffer(paddingLength); - padding.fill(0); - buffer = Buffer.concat([buffer, padding]); - } - return buffer; - } - else { - return bigInt2Buffer(new BigInteger(stringValue), byteLength); - } -} - -function bigInt2Buffer(bigInt, byteLength) { - var byteArray = bigInt.toByteArray(); - var bArrayLength = byteArray.length; - var buffer = new Buffer(byteLength); - buffer.fill(0); - var length = Math.min(bArrayLength, byteLength); - for (var i = 0; i < length; i++) { - var value = byteArray[bArrayLength - 1 - i]; - buffer[i] = value; - } - return buffer; -} - -function buffer2StringValue(buffer) { - var length = buffer.length; - var output = '0x'; - for (var i = length; i > 0; i--) { - output += buffer.slice(i - 1, i).toString('hex'); - } - return output; -} - - -// Export the class -module.exports = exports = AbstractObject; -exports.stringValue2Buffer = stringValue2Buffer; -exports.buffer2StringValue = buffer2StringValue; \ No newline at end of file diff --git a/node_modules/lib/type-language/builder.js b/node_modules/lib/type-language/builder.js deleted file mode 100644 index 40e9c19..0000000 --- a/node_modules/lib/type-language/builder.js +++ /dev/null @@ -1,350 +0,0 @@ -// telegram.link -// -// Copyright 2014 Enrico Stara 'enrico.stara@gmail.com' -// Released under the Simplified BSD License -// http://telegram.link - -// Builder class -// -// This class can build dynamically a `AbstractObject` concrete sub-class -// parsing `TL-Schema` for both `MTProto` and `Telegram API` - -// Export the class -module.exports = exports = Builder; - -// Export the method -exports.buildTypes = buildTypes; -exports.registerType = registerType; -exports.retrieveType = retrieveType; - -// Import dependencies -var util = require('util'); -var getLogger = require('get-log'); -var logger = getLogger('type-language.Builder'); -var AbstractObject = require('./index').AbstractObject; - -// Compile a reg exp to resolve Type declaration in TL-Schema -var typeResolver = /^(\w+)(<(\w+)>)?$/; - -// The constructor requires the following params: -// `{ -// module: 'the module name where add this new Type', -// tlSchema: 'the TypeLanguage schema that describes the Type', -// buildFunction: 'true if it's a TypeFunction, false otherwise' -// }` -function Builder(params) { - this.module = params.module; - if (!this.module) { - logger.warn(' Target \'module\' parameter is mandatory!'); - console.trace(); - return; - } - this.tlSchema = params.tlSchema; - if (!this.tlSchema) { - logger.warn('\'tlSchema\' parameter is mandatory!'); - return; - } - this._methods = []; - - // Check if is required creating a function - if (params.buildFunction) { - this._type = this.buildTypeFunction(); - } else { - this._type = this.buildTypeConstructor(); - } -} - -// Return the built type -Builder.prototype.getType = function () { - return this._type; -}; - -// Return the type function payload if expected -Builder.prototype.getFunctionPayload = function () { - return this._functionPayload; -}; - -// Return the required internal module/class -Builder.prototype.require = function (module) { - var required = require('./index')[module]; - return required; -}; - -// Return the required lib module/class -var lib; -Builder.prototype.requireFromLib = function (module) { - lib = lib || { - mtproto : require('lib/mtproto'), - 'type-language' : require('lib/type-language') - }; - return lib[module]; -}; - -// This function builds a new `TypeLanguage` function parsing the `TL-Schema method` -Builder.prototype.buildTypeFunction = function () { - var methodName = this.tlSchema.method; - // If needed, create the function payload class re-calling Builder constructor. - this._functionPayload = new Builder({module: this.module, tlSchema: this.tlSchema}).getType(); - - // Start creating the body of the new Type function - var body = - '\tvar start = new Date().getTime();\n' + - '\tvar self = arguments.callee;\n' + - '\tvar callback = options.callback;\n' + - '\tvar conn = options.conn;\n' + - '\tif (!conn) {\n' + - '\t\tvar msg = \'The \\\'conn\\\' option is missing, it\\\'s mandatory\';\n' + - '\t\tself.logger.warn(msg);\n' + - '\t\tif(callback) callback({code: \'EILLEGALARG\', msg: msg});\n' + - '\t\treturn;\n' + - '\t}\n'; - body += - '\tvar PlainMessage = self.require(\'mtproto\').PlainMessage;\n' + - '\tvar type_language = self.require(\'type-language\');\n' + - '\tvar module = self.require(\'' + this.module + '\');\n'; - body += - '\tvar reqPayload = new module._' + methodName + '(options);\n' + - '\tvar reqMsg = new PlainMessage({message: reqPayload.serialize()});\n'; - body += - '\tconn.connect(function (ex1) {\n' + - '\t\tif(ex1) {\n' + - '\t\t\tself.logger.error(\'Unable to connect: %s \', ex1);\n' + - '\t\t\tif(callback) callback(ex1);\n' + - '\t\t\treturn;\n' + - '\t\t}\n' + - '\t\tconn.write(reqMsg.serialize(), function (ex2) {\n' + - '\t\t\tif(ex2) {\n' + - '\t\t\t\tself.logger.error(\'Unable to write: %s \', ex2);\n' + - '\t\t\t\tif(callback) callback(ex2);\n' + - '\t\t\t\treturn;\n' + - '\t\t\t}\n' + - '\t\t\tconn.read(function (ex3, response) {\n' + - '\t\t\t\tif(ex3) {\n' + - '\t\t\t\t\tself.logger.error(\'Unable to read: %s \', ex3);\n' + - '\t\t\t\t\tif(callback) callback(ex3);\n' + - '\t\t\t\t\treturn;\n' + - '\t\t\t\t}\n' + - '\t\t\t\ttry {\n' + - '\t\t\t\t\tvar resMsg = new PlainMessage({buffer: response}).deserialize();\n' + - '\t\t\t\t\tresMsg = resMsg.getMessage();\n' + - '\t\t\t\t\tvar Type = type_language.Builder.retrieveType(resMsg);\n' + - '\t\t\t\t\tvar resObj = new Type({buffer: resMsg});\n' + - '\t\t\t\t\tresObj.deserialize();\n' + - '\t\t\t\t\tvar duration = new Date().getTime() - start;\n' + - '\t\t\t\t\tif(self.logger.isDebugEnabled()) self.logger.debug(\'Executed in %sms\', duration);\n' + - '\t\t\t\t\tif(callback) callback(null, resObj, duration);\n' + - '\t\t\t\t} catch(ex4) {\n' + - '\t\t\t\t\tself.logger.error(\'Unable to deserialize response due to %s \', ex4);\n' + - '\t\t\t\t\tif(callback) callback(ex4);\n' + - '\t\t\t\t}\n' + - '\t\t\t});\n' + - '\t\t});\n' + - '\t});'; - if (logger.isDebugEnabled()) { - logger.debug('Body for %s type function:', methodName); - logger.debug('\n' + body); - } - /*jshint evil:true */ - // Create the new Type function - var typeFunction = new Function('options', body); - typeFunction.require = this.requireFromLib; - typeFunction.logger = getLogger(this.module + '.' + methodName); - return typeFunction; -}; - -// This function builds a new `TypeLanguage` class (an `AbstractObject` concrete sub-class) -// parsing the `TL-Schema constructor` -Builder.prototype.buildTypeConstructor = function () { - // Start creating the body of the new Type constructor, first calling super() - var body = - '\tvar super_ = this.constructor.super_.bind(this);\n' + - '\tvar opts = options ? options : {};\n' + - '\tthis.constructor.util._extend(this, opts.props);\n' + - '\tsuper_(opts.buffer, opts.offset);\n'; - // Init fields - var typeName = this.tlSchema.method ? - this.tlSchema.method : - (this.tlSchema.predicate.charAt(0).toUpperCase() + this.tlSchema.predicate.slice(1)); - - var buffer = new Buffer(4); - buffer.writeUInt32LE(this.tlSchema.id, 0, true); - var typeId = buffer.toString('hex'); - var fullTypeName = this.module + '.' + typeName; - body += - '\tthis.id = "' + typeId + '";\n' + - '\tthis.typeName = "' + fullTypeName + '";\n'; - body += this._buildSerialize(); - body += this._buildDeserialize(); - // Add to body all the read/write methods - for (var i = 0; i < this._methods.length; i++) { - body += this._methods[i]; - } - if (logger.isDebugEnabled()) { - logger.debug('Body for %s type constructor:', typeName); - logger.debug('\n' + body); - } - /*jshint evil:true */ - // Create the new Type sub-class of AbstractObject - var typeConstructor = new Function('options', body); - typeConstructor.id = typeId; - typeConstructor.typeName = fullTypeName; - typeConstructor.require = this.require; - typeConstructor.util = require('util'); - typeConstructor.logger = getLogger(fullTypeName); - util.inherits(typeConstructor, AbstractObject); - return registerType(typeConstructor); -}; - -// Create the `serialize()` method -Builder.prototype._buildSerialize = function () { - var body = - '\tthis.serialize = function serialize () {\n' + - '\t\tvar super_serialize = this.constructor.super_.prototype.serialize.bind(this);\n' + - '\t\tif (!super_serialize()) {\n' + - '\t\t\treturn false;\n' + - '\t\t}\n'; - // Parse the `TL-Schema params` - if (this.tlSchema.params) { - for (var i = 0; i < this.tlSchema.params.length; i++) { - var param = this.tlSchema.params[i]; - var type = param.type.match(typeResolver); - var typeName = type[1]; - // Manage Object type - if (typeName.charAt(0) == typeName.charAt(0).toUpperCase()) { - body += - '\t\tthis._writeBytes(this.' + param.name + '.serialize());\n'; - } - // Manage primitive type - else { - typeName = typeName.charAt(0).toUpperCase() + typeName.slice(1); - body += - '\t\tthis.' + this._buildWriteProperty(param.name, typeName) + '();\n'; - } - } - } - body += - '\t\treturn this.retrieveBuffer();\n' + - '\t}\n'; - return body; -}; - -// Create the `write[property]()` method -Builder.prototype._buildWriteProperty = function (propertyName, typeName) { - var functionName = 'write' + propertyName.charAt(0).toUpperCase() + propertyName.slice(1); - var body = - '\tthis.' + functionName + ' = function ' + functionName + '() {\n'; - body += - '\t\tthis.constructor.logger.debug(\'write \\\'%s\\\' = %s\', \'' + propertyName + '\', this.' + propertyName + - ('Bytes' == typeName ? '.toString(\'hex\')' : '') + ');\n'; - body += - '\t\tthis.write' + typeName + '(this.' + propertyName + ');\n'; - body += - '\t};\n'; - this._methods.push(body); - return functionName; -}; - -// create the `deserialize()` method -Builder.prototype._buildDeserialize = function () { - var body = - '\tthis.deserialize = function deserialize () {\n' + - '\t\tvar super_deserialize = this.constructor.super_.prototype.deserialize.bind(this);\n' + - '\t\tif (!super_deserialize()) {\n' + - '\t\t\treturn false;\n' + - '\t\t}\n'; - // Parse the `TL-Schema params` - if (this.tlSchema.params) { - for (var i = 0; i < this.tlSchema.params.length; i++) { - var param = this.tlSchema.params[i]; - var type = param.type.match(typeResolver); - var typeName = type[1]; - if (!type[3]) { - // Manage Object type - if (typeName.charAt(0) == typeName.charAt(0).toUpperCase()) { - body += - '\t\tvar ' + typeName + ' = this.constructor.require(\'' + typeName + '\');\n' + - '\t\tvar obj = new ' + typeName + '({buffer: this._buffer, offset: this.getReadOffset()}).deserialize();\n' + - '\t\tif (obj) {\n' + - '\t\t\tthis.' + param.name + ' = obj;\n' + - '\t\t\tthis._readOffset += obj.getReadOffset()\n' + - '\t\t}\n'; - } - // Manage primitive type - else { - typeName = typeName.charAt(0).toUpperCase() + typeName.slice(1); - body += - '\t\tthis.' + this._buildReadProperty(param.name, typeName) + '();\n'; - } - } - // Manage generic type - else { - var typeParam = type[3]; - body += - '\t\tvar ' + typeName + ' = this.constructor.require(\'' + typeName + '\');\n' + - '\t\tvar obj = new ' + typeName + '({type: \'' + typeParam + '\', ' + - 'buffer: this._buffer, offset: this.getReadOffset()}).deserialize();\n' + - '\t\tif (obj) {\n' + - '\t\t\tthis.' + param.name + ' = obj;\n' + - '\t\t\tthis._readOffset += obj.getReadOffset();\n' + - '\t\t}\n'; - } - } - } - // check if all the buffer has been read - body += - '\t\tif(this._readOffset !== this._buffer.length) {\n' + - '\t\t\tthrow new Error(\'De-serialization failed! readOffset(\' + this._readOffset + \') ' + - '!= buffer.length(\' + this._buffer.length + \')\');\n' + - '\t\t}\n'; - - body += - '\t\treturn this;\n' + - '\t}\n'; - return body; -}; - -// Create the `read[property]()` method -Builder.prototype._buildReadProperty = function (propertyName, typeName) { - var functionName = 'read' + (propertyName.charAt(0).toUpperCase() + propertyName.slice(1)); - var body = - '\tthis.' + functionName + ' = function ' + functionName + '() {\n'; - body += - '\t\tthis.' + propertyName + ' = this.read' + typeName + '();\n'; - body += - '\t\tthis.constructor.logger.debug(\'read \\\'%s\\\' = %s, offset = %s\', \'' + propertyName + '\', this.' + propertyName + - ('Bytes' == typeName ? '.toString(\'hex\')' : '') + ', this._readOffset);\n'; - body += - '\t};\n'; - this._methods.push(body); - return functionName; -}; - -// type cache -var types = {}; - -// Register a Type constructor -function registerType(type) { - if (logger.isDebugEnabled()) logger.debug('Register Type \'%s\' with id [%s]', type.typeName, type.id); - return (types[type.id] = type); -} - -// Retrieve a Type constructor -function retrieveType(buffer) { - var typeId = buffer.slice(0, 4).toString('hex'); - var type = types[typeId]; - if (logger.isDebugEnabled()) logger.debug('Retrive Type \'%s\' with id [%s]', type.typeName, typeId); - return type; -} - -// Types builder -function buildTypes(schemas, types, targetModule, isMethodType) { - for (var i = 0; i < schemas.length; i++) { - var type = schemas[i]; - if (types.lastIndexOf(type[isMethodType ? 'method' : 'type']) >= 0) { - var typeName = isMethodType ? type.method : (type.predicate.charAt(0).toUpperCase() + type.predicate.slice(1)); - var builder = new Builder({module: 'mtproto', tlSchema: type, buildFunction: isMethodType}); - targetModule[typeName] = builder.getType(); - targetModule['_' + typeName] = builder.getFunctionPayload(); - } - } -} \ No newline at end of file diff --git a/node_modules/lib/type-language/index.js b/node_modules/lib/type-language/index.js deleted file mode 100644 index c46bcf3..0000000 --- a/node_modules/lib/type-language/index.js +++ /dev/null @@ -1,9 +0,0 @@ -// telegram.link -// -// Copyright 2014 Enrico Stara 'enrico.stara@gmail.com' -// Released under the Simplified BSD License -// http://telegram.link - -exports.AbstractObject = require('./abstract-object'); -exports.Builder = require('./builder'); -exports.Vector = require('./vector'); \ No newline at end of file diff --git a/node_modules/lib/type-language/vector.js b/node_modules/lib/type-language/vector.js deleted file mode 100644 index d8f26ee..0000000 --- a/node_modules/lib/type-language/vector.js +++ /dev/null @@ -1,74 +0,0 @@ -// telegram.link -// -// Copyright 2014 Enrico Stara 'enrico.stara@gmail.com' -// Released under the Simplified BSD License -// http://telegram.link - -// Vector class -// -// This class is the `TypeLanguage` list implementation, a concrete sub-class of the `AbstractObject` class - -// Import dependencies -var util = require('util'); -var AbstractObject = require('./index').AbstractObject; -var Builder = require('./index').Builder; - -// Vector extends AbstractObject -util.inherits(Vector, AbstractObject); - -// To get an instance for `serialization`: -// -// new Vector({type: 'long', list: [1,2,3]}); -// Provide the `list` property to fill the vector and the type of the content, `int` is the default: -// -// To get an instance for `de-serialization`: -// -// new Vector({type: 'int128', buffer: myBuffer, offset: currentPosition}); -// Provide a `buffer` and eventually an `offset` where start -// -// The `constructor`: -function Vector(options) { - var super_ = this.constructor.super_.bind(this); - var opts = util._extend({ type: 'int'}, options); - super_(opts.buffer, opts.offset); - this.id = '15c4b51c'; - this.type = opts.type.charAt(0).toUpperCase() + opts.type.slice(1); - this._list = !opts.list ? [] : opts.list; - this.constructor.logger = require('get-log')('type_language.Vector'); -} - -// The method de-serializes the list starting from the initialized buffer -Vector.prototype.deserialize = function () { - var super_deserialize = this.constructor.super_.prototype.deserialize.bind(this); - if (!super_deserialize()) { - return false; - } - var listLength = this.readInt(); - for (var i = 0; i < listLength; i++) { - this._list[i] = this['read' + this.type](); - } - return this; -}; - -// The method serializes the list starting from the initialized buffer -Vector.prototype.serialize = function () { - var super_serialize = this.constructor.super_.prototype.serialize.bind(this); - if (!super_serialize()) { - return false; - } - var listLength = this._list.length; - this.writeInt(listLength); - for (var i = 0; i < listLength; i++) { - this['write' + this.type](this._list[i]); - } - return this.retrieveBuffer(); -}; - -// The method retrieves a copy of the internal list -Vector.prototype.getList = function () { - return this._list.slice(); -}; - -// Export the class -module.exports = exports = Vector; - diff --git a/package.json b/package.json index 00f50ee..0cb2228 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram.link", - "version": "0.1.1", + "version": "0.1.2", "description": "Telegram API Node.js library", "keywords": [ "telegram", @@ -18,6 +18,7 @@ }, "license": "BSD-2-Clause", "dependencies": { + "telegram-tl-node": "latest", "get-log": "latest", "get-flow": "latest", "jsbn": "latest", diff --git a/telegram.link.js b/telegram.link.js index 52fc8ea..e4d7402 100644 --- a/telegram.link.js +++ b/telegram.link.js @@ -20,18 +20,12 @@ getLogger.PROJECT_NAME = require('./package.json').name; // Export the class module.exports = exports = TelegramLink; -// Export modules -exports.mtproto = require('lib/mtproto'); -exports.net = require('lib/net'); -exports.type_language = require('lib/type-language'); -exports.crypto_util = require('lib/crypto-util'); - // Import dependencies -var crypto = exports.crypto_util; -var TcpConnection = exports.net.TcpConnection; -var HttpConnection = exports.net.HttpConnection; -var AbstractObject = exports.type_language.AbstractObject; -var mtproto = exports.mtproto; +var crypto = require('lib/crypto-util'); +var TcpConnection = require('lib/net').TcpConnection; +var HttpConnection = require('lib/net').HttpConnection; +var TypeObject = require('telegram-tl-node').TypeObject; +var mtproto = require('lib/mtproto'); var flow = require('get-flow'); // The constructor requires a primary telegram DataCenter address as argument @@ -193,8 +187,8 @@ TelegramLink.prototype.authorization = function (callback) { // Decrypt DH parameters and synch the local time with the server time function decryptDHParams(obj, duration) { - var newNonce = AbstractObject.stringValue2Buffer(obj.newNonce, 32); - var serverNonce = AbstractObject.stringValue2Buffer(obj.resPQ.server_nonce, 16); + var newNonce = TypeObject.stringValue2Buffer(obj.newNonce, 32); + var serverNonce = TypeObject.stringValue2Buffer(obj.resPQ.server_nonce, 16); if (logger.isDebugEnabled()) { logger.debug('newNonce = %s, serverNonce = %s', newNonce.toString('hex'), serverNonce.toString('hex')); } diff --git a/test/lib/mtproto.test.js b/test/lib/mtproto.test.js index c872770..1ce52f8 100644 --- a/test/lib/mtproto.test.js +++ b/test/lib/mtproto.test.js @@ -1,9 +1,9 @@ require('should'); var net = require('net'); var mtproto = require('lib/mtproto'); -var AbstractObject = require('lib/type-language').AbstractObject; -var Vector = require('lib/type-language').Vector; var TcpConnection = require("lib/net").TcpConnection; +var TypeObject = require('telegram-tl-node').TypeObject; +var TypeVector = require('telegram-tl-node').TypeVector; describe('mtproto', function () { @@ -24,13 +24,13 @@ describe('mtproto', function () { console.log('tcpMessage %s', requestBuffer.toString('hex')); var requestBuffer = new mtproto.PlainMessage({buffer: requestBuffer}).deserialize().getMessage(); console.log('plainMessage %s', requestBuffer.toString('hex')); - var reqPQ = new mtproto._req_pq({buffer: requestBuffer}).deserialize(); + var reqPQ = new mtproto.req_pq.PayloadType({buffer: requestBuffer}).deserialize(); // console.log(reqPQ); var resPQ = new mtproto.ResPQ({props: { nonce: reqPQ.nonce, server_nonce: '0x30739073a54aba77a81ea1f4334dcfa5', pq: new Buffer('17ed48941a08f981', 'hex'), - server_public_key_fingerprints: new Vector({type: 'long', list: ['0xc3b42b026ce86b21']}) + server_public_key_fingerprints: new TypeVector({type: 'long', list: ['0xc3b42b026ce86b21']}) }}); var resPQBuffer = resPQ.serialize(); console.log('Server: Send resPQ %s', resPQBuffer.toString('hex')); @@ -51,9 +51,6 @@ describe('mtproto', function () { describe('#method reqPQ', function () { it('should works', function (done) { - var obj = new mtproto._req_pq(); - obj.should.be.ok; - obj.should.be.an.instanceof(AbstractObject); var nonce = '0xf67b7768bf4854bb15fa840ec843875f'; mtproto.req_pq({ props: { @@ -76,7 +73,7 @@ describe('mtproto', function () { var className = mtproto._classes[i]; var obj = new mtproto[className](); obj.should.be.ok; - obj.should.be.an.instanceof(AbstractObject); + obj.should.be.an.instanceof(TypeObject); } done(); }) @@ -91,7 +88,7 @@ describe('mtproto', function () { var msg = new mtproto.PlainMessage(); msg.should.be.ok; msg.should.be.an.instanceof(mtproto.PlainMessage); - msg.should.be.an.instanceof(AbstractObject); + msg.should.be.an.instanceof(TypeObject); msg.isReadonly().should.be.false; msg = new mtproto.PlainMessage({message: new Buffer('FFFF', 'hex')}); diff --git a/test/lib/type-language/abstract-object.test.js b/test/lib/type-language/abstract-object.test.js deleted file mode 100644 index e1345b6..0000000 --- a/test/lib/type-language/abstract-object.test.js +++ /dev/null @@ -1,217 +0,0 @@ -require('should'); -var AbstractObject = require('lib/type-language').AbstractObject; - -describe('AbstractObject', function () { - - describe('#init()', function () { - it('should create a AbstractObject instance', function (done) { - var obj = new AbstractObject(); - obj.should.be.ok; - done(); - }) - }); - - describe('#retrieveBuffer()', function () { - it('should retrieve the buffer and put object in readonly', function (done) { - var obj = new AbstractObject(); - obj.isReadonly().should.be.false; - obj.retrieveBuffer(); - obj.isReadonly().should.be.true; - done(); - }) - }); - - describe('#writeInt()', function () { - it('should write an int value', function (done) { - var obj = new AbstractObject(); - obj.writeInt(0x80a0c0e0).should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.should.be.eql(new Buffer([0xe0, 0xc0, 0xa0, 0x80])); - obj.writeInt(0).should.be.false; - - obj = new AbstractObject(); - obj.writeInt(-2).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.should.be.eql(new Buffer([254, 255, 255, 255])); - done(); - }) - }); - - describe('#_writeBigInt()', function () { - it('should write a big int value', function (done) { - var obj = new AbstractObject(); - obj._writeBigInt('1', 4).should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.should.be.eql(new Buffer([1, 0, 0, 0])); - - obj = new AbstractObject(); - obj._writeBigInt('1', 8).should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(8); - bytes.should.be.eql(new Buffer([1, 0, 0, 0, 0, 0, 0, 0])); - - obj = new AbstractObject(); - obj._writeBigInt(1, 4).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.toString('hex').should.be.eql('01000000'); - - obj = new AbstractObject(); - obj._writeBigInt('0x01', 4).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.toString('hex').should.be.eql('01000000'); - - obj = new AbstractObject(); - obj._writeBigInt('0xee000000ff', 4).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.toString('hex').should.be.eql('ff000000'); - - obj = new AbstractObject(); - obj._writeBigInt('1022202216703' /*'0xee000000ff'*/, 4).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(4); - bytes.toString('hex').should.be.eql('ff000000'); - - obj = new AbstractObject(); - obj._writeBigInt('18441921394529845472', 8).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(8); - bytes.toString('hex').should.be.eql('e0c0a080ccddeeff'); - - obj = new AbstractObject(); - obj._writeBigInt('0xffeeddcc80a0c0e0', 8).should.be.true; - bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(8); - bytes.toString('hex').should.be.eql('e0c0a080ccddeeff'); - done(); - }) - }); - - describe('#writeInt256', function () { - it('should write an int256 value ', function (done) { - var obj = new AbstractObject(); - obj.writeInt256('0xffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100').should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(32); - bytes.toString('hex').should.be.eql('00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff'); - done(); - }) - }); - - - describe('#writeBytes()', function () { - it('should write few bytes', function (done) { - var obj = new AbstractObject(); - obj.writeBytes(new Buffer('130c81d08c748257', 'hex')).should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(12); - bytes.toString('hex').should.be.eql('08130c81d08c748257000000'); - done(); - }) - }); - - describe('#writeBytes()', function () { - it('should write a lot of bytes', function (done) { - var obj = new AbstractObject(); - var buffer = new Buffer(605); - buffer.fill(254); - obj.writeBytes(buffer).should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(612); - done(); - }) - }); - describe('#writeString()', function () { - it('should write a string', function (done) { - var obj = new AbstractObject(); - obj.writeString('Lorem ipsum dolor sit amet, consectetur adipisci elit, ' + - 'sed eiusmod tempor incidunt ut labore et dolore magna aliqua.').should.be.true; - var bytes = obj.retrieveBuffer(); - bytes.length.should.be.equal(120); - bytes.toString('hex').should.be.eql('744c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6' + - 'e736563746574757220616469706973636920656c69742c2073656420656975736d6f642074656d706f7220696e6369647' + - '56e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c697175612e000000'); - done(); - }) - }); - - describe('#readInt()', function () { - it('should read an int value', function (done) { - var obj = new AbstractObject(new Buffer('feffffff', 'hex')); - var intValue = obj.readInt(); - intValue.should.be.equal(4294967294); - obj.getReadOffset().should.be.equal(4); - done(); - }) - }); - - describe('#_readBigInt()', function () { - it('should read a big int value', function (done) { - var obj = new AbstractObject(new Buffer('8899aabbccddeeff', 'hex')); - var bigintValue = obj._readBigInt(8); - bigintValue.should.be.equal('0xffeeddccbbaa9988'); - obj.getReadOffset().should.be.equal(8); - done(); - }) - }); - - describe('#readInt256()', function () { - it('should read an int256 value', function (done) { - var obj = new AbstractObject(new Buffer('00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff', 'hex')); - var intValue = obj.readInt256(); - intValue.should.be.eql('0xffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100'); - obj.getReadOffset().should.be.equal(32); - done(); - }) - }); - - - describe('#readBytes()', function () { - it('should read few bytes', function (done) { - var obj = new AbstractObject(new Buffer('08130c81d08c748257000000', 'hex')); - var bytes = obj.readBytes(); - bytes.toString('hex').should.be.eql('130c81d08c748257'); - obj.getReadOffset().should.be.equal(12); - done(); - }) - }); - - describe('#readBytes()', function () { - it('should read a lot of bytes', function (done) { - - var buffers = []; - buffers.push(new Buffer('7f5d0200', 'hex')); - var data = new Buffer(605); - data.fill(254); - buffers.push(data); - var padding = new Buffer(3); - padding.fill(0); - buffers.push(padding); - var buffer = Buffer.concat(buffers); - var obj = new AbstractObject(buffer); - var bytes = obj.readBytes(); - bytes.length.should.be.equal(605); - obj.getReadOffset().should.be.equal(612); - done(); - }) - }); - - describe('#readString()', function () { - it('should read a string', function (done) { - var obj = new AbstractObject(new Buffer('744c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6' + - 'e736563746574757220616469706973636920656c69742c2073656420656975736d6f642074656d706f7220696e6369647' + - '56e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c697175612e000000', 'hex')); - var str = obj.readString(); - str.should.be.eql('Lorem ipsum dolor sit amet, consectetur adipisci elit, ' + - 'sed eiusmod tempor incidunt ut labore et dolore magna aliqua.'); - done(); - }) - }); - -}); - diff --git a/test/lib/type-language/builder.test.js b/test/lib/type-language/builder.test.js deleted file mode 100644 index 46e27d2..0000000 --- a/test/lib/type-language/builder.test.js +++ /dev/null @@ -1,93 +0,0 @@ -require('should'); -var Builder = require('lib/type-language').Builder; -var AbstractObject = require('lib/type-language').AbstractObject; -var Vector = require('lib/type-language').Vector; - -describe('Builder', function () { - - describe('#buildTypeConstructor({P_Q_inner_data})', function () { - it('should return a P_Q_inner_data', function (done) { - var P_Q_inner_data = new Builder({module: 'mtproto', tlSchema:{"id": "-2083955988", "predicate": "p_q_inner_data", "params": [ - {"name": "pq", "type": "bytes"}, - {"name": "p", "type": "bytes"}, - {"name": "q", "type": "bytes"}, - {"name": "nonce", "type": "int128"}, - {"name": "server_nonce", "type": "int128"}, - {"name": "new_nonce", "type": "int256"} - ], "type": "P_Q_inner_data"}}).getType(); -// console.log(P_Q_inner_data.toString()); - P_Q_inner_data.should.be.an.instanceof(Function); - var obj = new P_Q_inner_data(); - obj.should.be.an.instanceof(P_Q_inner_data); - obj.should.be.an.instanceof(AbstractObject); - obj.id.should.be.eql('ec5ac983'); - obj.typeName.should.be.eql('mtproto.P_q_inner_data'); - - done(); - }) - }); - - describe('#buildTypeConstructor({ResPQ}).deserialize()', function () { - it('should build and de-serialize an instance of ResPQ', function (done) { - var ResPQ = new Builder({module: 'mtproto',tlSchema: {"id": "85337187", "predicate": "resPQ", "params": [ - {"name": "nonce", "type": "int128"}, - {"name": "server_nonce", "type": "int128"}, - {"name": "pq", "type": "bytes"}, - {"name": "server_public_key_fingerprints", "type": "Vector"} - ], "type": "ResPQ"}}).getType(); -// console.log(ResPQ.toString()); - ResPQ.should.be.an.instanceof(Function); - var obj = new ResPQ({buffer: new Buffer( - '632416053E0549828CCA27E966B301A48FECE2FCA5CF4D33F4A11EA877BA4AA5739073300817ED48941A08F98100000015C4B51C01000000216BE86C022BB4C3', - 'hex')}); - obj.should.be.an.instanceof(ResPQ); - obj.should.be.an.instanceof(AbstractObject); - obj.deserialize(); -// console.log(obj); - obj.should.have.properties({ - id: '63241605', - typeName: 'mtproto.ResPQ', - nonce: '0xfce2ec8fa401b366e927ca8c8249053e', - server_nonce: '0x30739073a54aba77a81ea1f4334dcfa5' - }); - obj.server_public_key_fingerprints.should.have.properties({ - id: '15c4b51c', - type: 'Long', - _list: ['0xc3b42b026ce86b21'] - }); - done(); - }) - }); - - describe('#buildTypeConstructor({ResPQ}).serialize()', function () { - it('should build and serialize an instance of ResPQ', function (done) { - var ResPQ = new Builder({module: 'builder',tlSchema: {"id": "85337187", "predicate": "resPQ", "params": [ - {"name": "nonce", "type": "int128"}, - {"name": "server_nonce", "type": "int128"}, - {"name": "pq", "type": "bytes"}, - {"name": "server_public_key_fingerprints", "type": "Vector"} - ], "type": "ResPQ"}}).getType(); - -// console.log(ResPQ.toString()); - - ResPQ.should.be.an.instanceof(Function); - - var obj = new ResPQ({props: { - nonce: '0xfce2ec8fa401b366e927ca8c8249053e', - server_nonce: '0x30739073a54aba77a81ea1f4334dcfa5', - pq: new Buffer('17ed48941a08f981', 'hex'), - server_public_key_fingerprints: new Vector({type: 'long', list: ['0xc3b42b026ce86b21']}) - } - }); - var objBuffer = obj.serialize(); - objBuffer.toString('hex').toUpperCase().should.be. - eql('632416053E0549828CCA27E966B301A48FECE2FCA5CF4D33F4A11EA877BA4AA5739073300817ED48941A08F98100000015C4B51C01000000216BE86C022BB4C3') - - var obj2 = new ResPQ({buffer: objBuffer}); - obj2.id.should.be.eql('63241605'); - - done(); - }) - }); -}); - diff --git a/test/lib/type-language/vector.test.js b/test/lib/type-language/vector.test.js deleted file mode 100644 index 87cb3ea..0000000 --- a/test/lib/type-language/vector.test.js +++ /dev/null @@ -1,57 +0,0 @@ -require('should'); -var Vector = require('lib/type-language').Vector; -var AbstractObject = require('lib/type-language').AbstractObject; - -describe('Vector', function () { - - describe('#init()', function () { - it('should return an instance', function (done) { - var list = new Vector(); - list.should.be.ok; - list.should.be.an.instanceof(Vector); - list.should.be.an.instanceof(AbstractObject); - list.should.have.properties({id: '15c4b51c', type: 'Int'}); - list.isReadonly().should.be.false; - - var list = new Vector({type: 'long', buffer: new Buffer('15C4B51C01000000216BE86C022BB4C3', 'hex')}); - list.should.have.properties({id: '15c4b51c', type: 'Long'}); - list.isReadonly().should.be.true; - - var list = new Vector({type: 'long', list: [1,2,3]}); - list.should.have.properties({id: '15c4b51c', type: 'Long'}); - list.isReadonly().should.be.false; - list.getList().should.be.eql([1,2,3]); - - done(); - }) - }); - - describe('#deserialize()', function () { - it('should de-serialize the list', function (done) { - var list = new Vector({type: 'long', buffer: new Buffer('15C4B51C01000000216BE86C022BB4C3', 'hex')}); - list.deserialize().should.be.ok; - list.getList().length.should.be.equal(1); - list.getList().pop().should.be.equal('0xc3b42b026ce86b21'); - done(); - }) - }); - - describe('#deserialize()', function () { - it('should not de-serialize the list cause type id mismatch', function (done) { - var list = new Vector({type: 'long', buffer: new Buffer('25C4B51C01000000216BE86C022BB4C3', 'hex')}); - list.deserialize().should.not.be.ok; - done(); - }) - }); - - describe('#serialize()', function () { - it('should serialize the list', function (done) { - var list = new Vector({type: 'long', list: ['0xc3b42b026ce86b21']}); - var buffer = list.serialize(); - buffer.should.be.ok; - buffer.toString('hex').should.be.equal('15c4b51c01000000216be86c022bb4c3'); - done(); - }) - }); - -});