{"action":"closed","issue":{"url":"https://api.github.com/repos/dcodeIO/ProtoBuf.js/issues/208","labels_url":"https://api.github.com/repos/dcodeIO/ProtoBuf.js/issues/208/labels{/name}","comments_url":"https://api.github.com/repos/dcodeIO/ProtoBuf.js/issues/208/comments","events_url":"https://api.github.com/repos/dcodeIO/ProtoBuf.js/issues/208/events","html_url":"https://github.com/dcodeIO/ProtoBuf.js/issues/208","id":51876105,"number":208,"title":"RangeError: Illegal offset while decoding varint32","user":{"login":"hideoussquid","id":8070052,"avatar_url":"https://avatars.githubusercontent.com/u/8070052?v=3","gravatar_id":"","url":"https://api.github.com/users/hideoussquid","html_url":"https://github.com/hideoussquid","followers_url":"https://api.github.com/users/hideoussquid/followers","following_url":"https://api.github.com/users/hideoussquid/following{/other_user}","gists_url":"https://api.github.com/users/hideoussquid/gists{/gist_id}","starred_url":"https://api.github.com/users/hideoussquid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hideoussquid/subscriptions","organizations_url":"https://api.github.com/users/hideoussquid/orgs","repos_url":"https://api.github.com/users/hideoussquid/repos","events_url":"https://api.github.com/users/hideoussquid/events{/privacy}","received_events_url":"https://api.github.com/users/hideoussquid/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dcodeIO/ProtoBuf.js/labels/unsure","name":"unsure","color":"eb6420"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2014-12-13T04:23:04Z","updated_at":"2015-06-12T13:12:38Z","closed_at":"2015-06-12T13:12:38Z","body":"Hi there,\r\n\r\nI've got an odd problem, not sure where the fault lies.\r\n\r\nI am sending messages to a device and ProtoBuf.js is throwing errors on some of the decodings.\r\n\r\nMy relevant .proto schema:\r\n```\r\n// Specifies algorithm used for deterministic wallet.\r\nenum Algorithm\r\n{\r\n\t// See https://en.bitcoin.it/wiki/BIP_0032\r\n\tBIP32 = 0;\r\n\t// Note that we are unlikely to support Electrum's determinstic\r\n\t// wallet algorithm.\r\n\tELECTRUM = 1;\r\n}\r\n\r\n// Reset state of wallet. If a wallet is loaded, this message will unload\r\n// (lock) it.\r\n//\r\n// Direction: Host to Device\r\n// Responses: Features\r\nmessage Initialize\r\n{\r\n\t// Arbitrary session identifier, which will be echoed back in the response\r\n\t// (a Features message).\r\n    required bytes session_id = 1 ; // [(nanopb).max_size = 64];\r\n}\r\n\r\n// List of features supported by the device.\r\n//\r\n// Direction: Device To Host\r\n// Responses: none\r\nmessage Features\r\n{\r\n\t// Echoed from Initialize message.\r\n\trequired bytes echoed_session_id = 1 ; // [(nanopb).max_size = 64];\r\n\t// Name of the manufacturer eg. \"Aperture Science\".\r\n\toptional string vendor = 2;\r\n\t// Major version number of the device eg. 0.\r\n\toptional uint32 major_version = 3;\r\n\t// Minor version number of the device eg. 9.\r\n\toptional uint32 minor_version = 4;\r\n\t// Vendor-specific configuration information (eg. firmware build options).\r\n\toptional string config = 5;\r\n\t// Whether device is able to use OtpRequest interjections.\r\n\toptional bool otp = 6;\r\n\t// Whether device is able to use PinRequest interjections.\r\n\toptional bool pin = 7;\r\n\t// Whether device expects supporting transactions to be included when\r\n\t// signing a transaction.\r\n\toptional bool spv = 8;\r\n\t// List of supported deterministic wallet algorithms.\r\n\trepeated Algorithm algo = 9 ; // [(nanopb).max_count = 2];\r\n\t// Whether DebugLink is enabled. Production builds will never have\r\n\t// DebugLink enabled.\r\n\toptional bool debug_link = 10;\r\n}\r\n```\r\n\r\nCreating a simple message to send from the webpage to the device (message *Initialize*):\r\n```javascript\r\n   function initialize_protobuf_encode() {\r\n    var ProtoBuf = dcodeIO.ProtoBuf;\r\n    var ByteBuffer = dcodeIO.ByteBuffer;\r\n    var builder = ProtoBuf.loadProtoFile(\"js/messages.proto\"),\r\n    Device = builder.build();\r\n    var tempBuffer = ByteBuffer.allocate(1024);\r\n    var bb = new ByteBuffer();\r\n    bb.writeUint8(0x31);\r\n    bb.writeUint8(0x32);\r\n    bb.flip();\r\n    var initializeContents = new Device.Initialize({\r\n        \"session_id\": bb,\r\n     });\r\n    tempBuffer = initializeContents.encode();\r\n    var tempTXstring = tempBuffer.toString('hex');\r\n    txSize = d2h((tempTXstring.length) / 2).toString('hex');\r\n    var j;\r\n    var txLengthOriginal = txSize.length;\r\n    for (j = 0; j < (8 - txLengthOriginal); j++) {\r\n        var prefix = \"0\";\r\n        txSize = prefix.concat(txSize);\r\n    }\r\n    tempTXstring = txSize.concat(tempTXstring);\r\n    var command = \"0017\"; \r\n    tempTXstring = command.concat(tempTXstring);\r\n    var magic = \"2323\"\r\n    tempTXstring = magic.concat(tempTXstring);\r\n    autoCannedTransaction(tempTXstring);\r\n```\r\n\r\nThis correctly encodes the following: 2323001700000004**0a023132**\r\n[the leading hex is for the machine and is not interpreted by the protobuf engine as it is stripped]\r\n\r\nThis correctly sets a session_id to \"12\" i.e. 0x31 0x32\r\n\r\nThe response comes back:\r\n2323 003A 00000030 **0A02313212144C4F434B424F5820666F722041524D2076302E37180020092A0546595554423001380140014A01005000**\r\nProtoBuf.js:965 Uncaught RangeError: Illegal offset: 0 <= 48 (+1) <= 48\r\n\r\nHow I am attempting to decode:\r\n```javascript\r\nfunction processResults(command, length, payload) {\r\n    var ProtoBuf = dcodeIO.ProtoBuf;\r\n    var ByteBuffer = dcodeIO.ByteBuffer;\r\n    var builder = ProtoBuf.loadProtoFile(\"js/messages.proto\"),\r\n    Device = builder.build();\r\n    command = command.substring(2, 4)\r\n    console.log('to process:' + command + ' ' + length + ' ' + payload);\r\n    switch (command) {\r\n        case \"3A\": // initialize\r\n            var featuresMessage = Device.Features.decodeHex(payload);\r\n            console.log(\"session id: \" + featuresMessage.echoed_session_id);\r\n            break;\r\n.\r\n.\r\n.\r\n}\r\n```\r\n\r\nIt's continually throwing the RangeError, seems to be when it is evaluating the leading varint32.\r\n\r\nUsing \"Protocol Buffers Workbench\", it would appear that the messages are correctly encoded, as I get the following decoded output:\r\n```\r\nechoed_session_id: \"12\"\r\nvendor: \"LOCKBOX for ARM v0.7\"\r\nmajor_version: 0\r\nminor_version: 9\r\nconfig: \"FYUTB\"\r\notp: true\r\npin: true\r\nspv: true\r\nalgo: BIP32\r\ndebug_link: false\r\n```\r\n\r\nSome of my other messages encode/decode correctly, but there's something wrong with this one. What am I doing wrong?"}}