Make code more consistent
This commit is contained in:
parent
a7eb439706
commit
8ef3168af1
|
@ -54,7 +54,7 @@ Address.decode = function(str) {
|
||||||
var bytes = bs58.decode(str);
|
var bytes = bs58.decode(str);
|
||||||
var data = new Buffer(bytes.slice(0, -4));
|
var data = new Buffer(bytes.slice(0, -4));
|
||||||
var checksum = new Buffer(bytes.slice(-4));
|
var checksum = new Buffer(bytes.slice(-4));
|
||||||
assert(bufferEqual(checksum, getchecksum(data)), "Bad checkum");
|
assert(bufferEqual(checksum, getaddrchecksum(data)), "Bad checkum");
|
||||||
|
|
||||||
var decoded = var_int.decode(data);
|
var decoded = var_int.decode(data);
|
||||||
var version = decoded.value;
|
var version = decoded.value;
|
||||||
|
@ -72,7 +72,7 @@ Address.decode = function(str) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compute the Bitmessage checksum for the given data.
|
// Compute the Bitmessage checksum for the given data.
|
||||||
function getchecksum(data) {
|
function getaddrchecksum(data) {
|
||||||
return bmcrypto.sha512(bmcrypto.sha512(data)).slice(0, 4);
|
return bmcrypto.sha512(bmcrypto.sha512(data)).slice(0, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ Address.prototype.encode = function() {
|
||||||
var_int.encode(this.stream),
|
var_int.encode(this.stream),
|
||||||
ripe,
|
ripe,
|
||||||
]);
|
]);
|
||||||
var addr = Buffer.concat([data, getchecksum(data)]);
|
var addr = Buffer.concat([data, getaddrchecksum(data)]);
|
||||||
return "BM-" + bs58.encode(addr);
|
return "BM-" + bs58.encode(addr);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,8 @@ function isAscii(str) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getchecksum(data) {
|
// Compute the message checksum for the given data.
|
||||||
|
function getmsgchecksum(data) {
|
||||||
return bmcrypto.sha512(data).slice(0, 4);
|
return bmcrypto.sha512(data).slice(0, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,20 +46,21 @@ var message = exports.message = {
|
||||||
assert(buf.length >= 24, "Buffer is too small");
|
assert(buf.length >= 24, "Buffer is too small");
|
||||||
assert(buf.readUInt32BE(0, true) === message.MAGIC, "Wrong magic");
|
assert(buf.readUInt32BE(0, true) === message.MAGIC, "Wrong magic");
|
||||||
var command = buf.slice(4, 16);
|
var command = buf.slice(4, 16);
|
||||||
var firstNonNull;
|
var firstNonNull = 0;
|
||||||
for (var i = 11; i >=0; i--) {
|
for (var i = 11; i >=0; i--) {
|
||||||
assert(command[i] <= 127, "Non-ASCII characters in command");
|
assert(command[i] <= 127, "Non-ASCII characters in command");
|
||||||
if (firstNonNull === undefined && command[i] !== 0) {
|
if (!firstNonNull && command[i] !== 0) {
|
||||||
firstNonNull = i;
|
firstNonNull = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
command = command.slice(0, firstNonNull + 1).toString("ascii");
|
// Command could be empty.
|
||||||
|
command = command.slice(0, firstNonNull).toString("ascii");
|
||||||
var payloadLength = buf.readUInt32BE(16, true);
|
var payloadLength = buf.readUInt32BE(16, true);
|
||||||
assert(payloadLength <= 262144, "Payload is too big");
|
assert(payloadLength <= 262144, "Payload is too big");
|
||||||
var checksum = buf.slice(20, 24);
|
var checksum = buf.slice(20, 24);
|
||||||
var length = 24 + payloadLength;
|
var length = 24 + payloadLength;
|
||||||
var payload = buf.slice(24, length);
|
var payload = buf.slice(24, length);
|
||||||
assert(bufferEqual(checksum, getchecksum(payload)), "Bad checkum");
|
assert(bufferEqual(checksum, getmsgchecksum(payload)), "Bad checkum");
|
||||||
var rest = buf.slice(length);
|
var rest = buf.slice(length);
|
||||||
return {command: command, payload: payload, length: length, rest: rest};
|
return {command: command, payload: payload, length: length, rest: rest};
|
||||||
},
|
},
|
||||||
|
@ -77,7 +79,7 @@ var message = exports.message = {
|
||||||
buf.writeUInt32BE(message.MAGIC, 0, true);
|
buf.writeUInt32BE(message.MAGIC, 0, true);
|
||||||
buf.write(opts.command, 4);
|
buf.write(opts.command, 4);
|
||||||
buf.writeUInt32BE(opts.payload.length, 16, true);
|
buf.writeUInt32BE(opts.payload.length, 16, true);
|
||||||
getchecksum(opts.payload).copy(buf, 20);
|
getmsgchecksum(opts.payload).copy(buf, 20);
|
||||||
opts.payload.copy(buf, 24);
|
opts.payload.copy(buf, 24);
|
||||||
return buf;
|
return buf;
|
||||||
},
|
},
|
||||||
|
|
|
@ -12,7 +12,7 @@ var assert = require("./util").assert;
|
||||||
var bmcrypto = require("./crypto");
|
var bmcrypto = require("./crypto");
|
||||||
|
|
||||||
// Compute the WIF checksum for the given data.
|
// Compute the WIF checksum for the given data.
|
||||||
function getchecksum(data) {
|
function getwifchecksum(data) {
|
||||||
return bmcrypto.sha256(bmcrypto.sha256(data)).slice(0, 4);
|
return bmcrypto.sha256(bmcrypto.sha256(data)).slice(0, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ exports.decode = function(wif) {
|
||||||
assert(bytes[0] === 0x80, "Bad WIF");
|
assert(bytes[0] === 0x80, "Bad WIF");
|
||||||
var data = new Buffer(bytes.slice(0, -4));
|
var data = new Buffer(bytes.slice(0, -4));
|
||||||
var checksum = new Buffer(bytes.slice(-4));
|
var checksum = new Buffer(bytes.slice(-4));
|
||||||
assert(bufferEqual(checksum, getchecksum(data)), "Bad checkum");
|
assert(bufferEqual(checksum, getwifchecksum(data)), "Bad checkum");
|
||||||
return data.slice(1);
|
return data.slice(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ exports.decode = function(wif) {
|
||||||
*/
|
*/
|
||||||
exports.encode = function(privateKey) {
|
exports.encode = function(privateKey) {
|
||||||
var data = Buffer.concat([new Buffer([0x80]), privateKey]);
|
var data = Buffer.concat([new Buffer([0x80]), privateKey]);
|
||||||
var checksum = getchecksum(data);
|
var checksum = getwifchecksum(data);
|
||||||
var bytes = Buffer.concat([data, checksum]);
|
var bytes = Buffer.concat([data, checksum]);
|
||||||
return bs58.encode(bytes);
|
return bs58.encode(bytes);
|
||||||
};
|
};
|
||||||
|
|
11
test.js
11
test.js
|
@ -57,10 +57,6 @@ describe("Crypto", function() {
|
||||||
|
|
||||||
describe("Common structures", function() {
|
describe("Common structures", function() {
|
||||||
describe("message", function() {
|
describe("message", function() {
|
||||||
it("should encode", function() {
|
|
||||||
expect(message.encode({command: "test", payload: Buffer("payload")}).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should decode", function() {
|
it("should decode", function() {
|
||||||
var res;
|
var res;
|
||||||
res = message.decode(Buffer("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164", "hex"));
|
res = message.decode(Buffer("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164", "hex"));
|
||||||
|
@ -68,6 +64,13 @@ describe("Common structures", function() {
|
||||||
expect(res.payload.toString()).to.equal("payload");
|
expect(res.payload.toString()).to.equal("payload");
|
||||||
expect(res.length).to.equal(31);
|
expect(res.length).to.equal(31);
|
||||||
expect(res.rest.toString("hex")).to.equal("");
|
expect(res.rest.toString("hex")).to.equal("");
|
||||||
|
|
||||||
|
res = message.decode(Buffer("e9beb4d90000000000000000000000000000000770b33ce97061796c6f6164", "hex"));
|
||||||
|
expect(res.command).to.equal("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should encode", function() {
|
||||||
|
expect(message.encode({command: "test", payload: Buffer("payload")}).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user