Implement var_str
This commit is contained in:
parent
18e38dee2b
commit
1b950bb872
|
@ -13,11 +13,11 @@ var bitmessage = require("./");
|
|||
* var_int.
|
||||
* @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_integer}
|
||||
*/
|
||||
exports.var_int = {
|
||||
var var_int = exports.var_int = {
|
||||
/**
|
||||
* Decode var_int.
|
||||
* @param {Buffer} buf - Buffer that starts with encoded var_int
|
||||
* @return {{value: (number|Int64), length: number, rest: number}}
|
||||
* @return {{value: (number|Int64), length: number, rest: Buffer}}
|
||||
* Decoded var_int structure.
|
||||
*/
|
||||
decode: function(buf) {
|
||||
|
@ -105,3 +105,35 @@ exports.var_int = {
|
|||
return buf;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* var_str.
|
||||
* @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_string}
|
||||
*/
|
||||
exports.var_str = {
|
||||
/**
|
||||
* Decode var_str.
|
||||
* @param {Buffer} buf - Buffer that starts with encoded var_str
|
||||
* @return {{str: string, length: number, rest: Buffer}}
|
||||
* Decoded var_str structure.
|
||||
*/
|
||||
decode: function(buf) {
|
||||
var decoded = var_int.decode(buf);
|
||||
var strLength = decoded.value;
|
||||
// XXX(Kagami): Spec doesn't mention encoding, using UTF-8.
|
||||
var str = decoded.rest.slice(0, strLength).toString();
|
||||
var rest = decoded.rest.slice(strLength);
|
||||
return {str: str, length: decoded.length + strLength, rest: rest};
|
||||
},
|
||||
|
||||
/**
|
||||
* Encode string into var_str.
|
||||
* @param {string} str - A string
|
||||
* @return {Buffer} Encoded var_str.
|
||||
*/
|
||||
encode: function(str) {
|
||||
// XXX(Kagami): Spec doesn't mention encoding, using UTF-8.
|
||||
var strBuf = new Buffer(str);
|
||||
return Buffer.concat([var_int.encode(strBuf.length), strBuf]);
|
||||
},
|
||||
};
|
||||
|
|
21
test.js
21
test.js
|
@ -8,6 +8,7 @@ var Int64 = bitmessage.Int64;
|
|||
var Address = bitmessage.Address;
|
||||
var wif = bitmessage.wif;
|
||||
var var_int = bitmessage.struct.var_int;
|
||||
var var_str = bitmessage.struct.var_str;
|
||||
var bmcrypto = require("./lib/crypto");
|
||||
|
||||
describe("Core structures", function() {
|
||||
|
@ -56,6 +57,26 @@ describe("Core structures", function() {
|
|||
expect(var_int.encode.bind(null, "test")).to.throw(Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe("var_str", function() {
|
||||
it("should decode", function() {
|
||||
var res;
|
||||
res = var_str.decode(Buffer("0474657374", "hex"));
|
||||
expect(res.str).to.equal("test");
|
||||
expect(res.length).to.equal(5);
|
||||
expect(res.rest.toString("hex")).to.equal("");
|
||||
|
||||
res = var_str.decode(Buffer("00", "hex"));
|
||||
expect(res.str).to.equal("");
|
||||
expect(res.length).to.equal(1);
|
||||
expect(res.rest.toString("hex")).to.equal("");
|
||||
});
|
||||
|
||||
it("should encode", function() {
|
||||
expect(var_str.encode("test").toString("hex")).to.equal("0474657374");
|
||||
expect(var_str.encode("").toString("hex")).to.equal("00");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("WIF", function() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user