Fix Address#ripe/Address#getShortRipe API

This commit is contained in:
Kagami Hiiragi 2015-01-28 17:24:31 +03:00
parent ceceaee721
commit 4610701a3e
3 changed files with 12 additions and 20 deletions

View File

@ -90,26 +90,19 @@ function getaddrchecksum(data) {
} }
/** /**
* Calculate the ripe hash of the address. * Get the ripe hash of the address without prefix zeroes.
* @param {?Object} opts - Options * @return {Buffer} A short ripe hash.
* @return {Buffer} Resulting ripe hash.
*/ */
Address.prototype.getRipe = function(opts) { Address.prototype.getShortRipe = function() {
opts = opts || {};
var ripe = this.ripe; var ripe = this.ripe;
if (opts.short) {
return ripe.slice(20 - getripelen(ripe)); return ripe.slice(20 - getripelen(ripe));
} else {
return ripe;
}
}; };
function getdoublehash(addr) { function getdoublehash(addr) {
var ripe = addr.getRipe();
var dataToHash = Buffer.concat([ var dataToHash = Buffer.concat([
var_int.encode(addr.version), var_int.encode(addr.version),
var_int.encode(addr.stream), var_int.encode(addr.stream),
ripe, addr.ripe,
]); ]);
return bmcrypto.sha512(bmcrypto.sha512(dataToHash)); return bmcrypto.sha512(bmcrypto.sha512(dataToHash));
} }
@ -187,11 +180,10 @@ function checkripelen(ripelen, version) {
* @return {string} Address string. * @return {string} Address string.
*/ */
Address.prototype.encode = function() { Address.prototype.encode = function() {
var ripe = this.getRipe({short: true});
var data = Buffer.concat([ var data = Buffer.concat([
var_int.encode(this.version), var_int.encode(this.version),
var_int.encode(this.stream), var_int.encode(this.stream),
ripe, this.getShortRipe(),
]); ]);
var addr = Buffer.concat([data, getaddrchecksum(data)]); var addr = Buffer.concat([data, getaddrchecksum(data)]);
return "BM-" + bs58.encode(addr); return "BM-" + bs58.encode(addr);

View File

@ -139,7 +139,7 @@ var getpubkey = exports.getpubkey = {
assert(to.version <= 4, "Address version is too high"); assert(to.version <= 4, "Address version is too high");
opts.version = to.version; opts.version = to.version;
opts.stream = to.stream; opts.stream = to.stream;
opts.objectPayload = to.version < 4 ? to.getRipe() : to.getTag(); opts.objectPayload = to.version < 4 ? to.ripe : to.getTag();
// POW calculation here. // POW calculation here.
var nonce = new Buffer(8); var nonce = new Buffer(8);
opts.nonce = nonce; opts.nonce = nonce;

10
test.js
View File

@ -743,7 +743,7 @@ describe("High-level classes", function() {
expect(addr.stream).to.equal(1); expect(addr.stream).to.equal(1);
expect(bufferEqual(addr.signPrivateKey, WIF.decode("5JY1CFeeyN4eyfL35guWAuUqu5VLmd7LojtkNP6wmt5msZxxZ57"))).to.be.true; expect(bufferEqual(addr.signPrivateKey, WIF.decode("5JY1CFeeyN4eyfL35guWAuUqu5VLmd7LojtkNP6wmt5msZxxZ57"))).to.be.true;
expect(bufferEqual(addr.encPrivateKey, WIF.decode("5J1oDgZDicNhUgbfzBDQqi2m5jUPnDrfZinnTqEEEaLv63jVFTM"))).to.be.true; expect(bufferEqual(addr.encPrivateKey, WIF.decode("5J1oDgZDicNhUgbfzBDQqi2m5jUPnDrfZinnTqEEEaLv63jVFTM"))).to.be.true;
expect(addr.getRipe().toString("hex")).to.equal("00ac14944b00decea5628eb40d0ff4b0f9ee9eca"); expect(addr.ripe.toString("hex")).to.equal("00ac14944b00decea5628eb40d0ff4b0f9ee9eca");
expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F"); expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F");
}); });
@ -754,7 +754,7 @@ describe("High-level classes", function() {
expect(addr.stream).to.equal(1); expect(addr.stream).to.equal(1);
expect(bufferEqual(addr.signPrivateKey, WIF.decode("5JY1CFeeyN4eyfL35guWAuUqu5VLmd7LojtkNP6wmt5msZxxZ57"))).to.be.true; expect(bufferEqual(addr.signPrivateKey, WIF.decode("5JY1CFeeyN4eyfL35guWAuUqu5VLmd7LojtkNP6wmt5msZxxZ57"))).to.be.true;
expect(bufferEqual(addr.encPrivateKey, WIF.decode("5J1oDgZDicNhUgbfzBDQqi2m5jUPnDrfZinnTqEEEaLv63jVFTM"))).to.be.true; expect(bufferEqual(addr.encPrivateKey, WIF.decode("5J1oDgZDicNhUgbfzBDQqi2m5jUPnDrfZinnTqEEEaLv63jVFTM"))).to.be.true;
expect(addr.getRipe().toString("hex")).to.equal("00ac14944b00decea5628eb40d0ff4b0f9ee9eca"); expect(addr.ripe.toString("hex")).to.equal("00ac14944b00decea5628eb40d0ff4b0f9ee9eca");
expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F"); expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F");
}); });
@ -788,9 +788,9 @@ describe("High-level classes", function() {
addr.encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); addr.encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex");
expect(addr.encPublicKey.toString("hex")).to.equal("04c6ed1b56f2da97fec1b762d43364566faf082c1e4918ae1dbb41757cad41b03b2cc5087f341414e63f6eee72a1fbf0b5f346a1bb3ba944cad204ca597db2bfc8"); expect(addr.encPublicKey.toString("hex")).to.equal("04c6ed1b56f2da97fec1b762d43364566faf082c1e4918ae1dbb41757cad41b03b2cc5087f341414e63f6eee72a1fbf0b5f346a1bb3ba944cad204ca597db2bfc8");
expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56");
expect(addr.getRipe({short: true}).toString("hex")).to.equal("3ab6655de4bd8c603eba9b00dd5970725fdd56"); expect(addr.getShortRipe().toString("hex")).to.equal("3ab6655de4bd8c603eba9b00dd5970725fdd56");
addr.encPrivateKey = Buffer("009969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); addr.encPrivateKey = Buffer("009969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex");
expect(addr.getRipe({short: true}).toString("hex")).to.equal("69617ddb1946dc327cadffcf33889fed587fc1e7"); expect(addr.getShortRipe().toString("hex")).to.equal("69617ddb1946dc327cadffcf33889fed587fc1e7");
}); });
it("should implement isAddress method", function() { it("should implement isAddress method", function() {
@ -806,7 +806,7 @@ describe("High-level classes", function() {
it("should allow to generate shorter address", function() { it("should allow to generate shorter address", function() {
this.timeout(300000); this.timeout(300000);
var addr = Address.fromRandom({ripeLength: 18}); var addr = Address.fromRandom({ripeLength: 18});
var ripe = addr.getRipe({short: true}); var ripe = addr.getShortRipe();
expect(ripe.length).to.be.at.most(18); expect(ripe.length).to.be.at.most(18);
}); });
} }