Address#clone

This commit is contained in:
Kagami Hiiragi 2015-01-29 21:47:01 +03:00
parent ea9e8e60ef
commit a42ec4b8f7
2 changed files with 31 additions and 2 deletions

View File

@ -38,6 +38,14 @@ function Address(opts) {
objectAssign(this, opts);
}
/**
* Create a copy of the address object.
* @return {Address} Cloned address.
*/
Address.prototype.clone = function() {
return new Address(this);
};
/**
* Test if given object is an Address instance. NOTE: Implementation is
* just simple `instanceof` but it improves readability and consistent

25
test.js
View File

@ -843,14 +843,14 @@ describe("High-level classes", function() {
// FIXME(Kagami): Add more fail tests.
describe("Address", function() {
it("should decode Bitmessage address", function() {
var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z")
var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z");
expect(addr.version).to.equal(4);
expect(addr.stream).to.equal(1);
expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56");
});
it("should decode Bitmessage address badly formatted", function() {
var addr = Address.decode(" 2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z ")
var addr = Address.decode(" 2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z ");
expect(addr.version).to.equal(4);
expect(addr.stream).to.equal(1);
expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56");
@ -960,6 +960,27 @@ describe("High-level classes", function() {
expect(Address.isAddress({})).to.be.false;
});
it("should implement clone method", function() {
var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z");
expect(addr.version).to.equal(4);
expect(addr.stream).to.equal(1);
var addr2 = addr.clone();
expect(addr2.version).to.equal(4);
expect(addr2.stream).to.equal(1);
addr2.version = 3;
addr2.stream = 2;
expect(addr.getTag().toString("hex")).to.equal("facf1e3e6c74916203b7f714ca100d4d60604f0917696d0f09330f82f52bed1a");
expect(addr2.getTag().toString("hex")).to.equal("d6487aaea3d2d022d80abbce2605089523ba2b516b81c03545f19a5c85f15fa2");
var addr3 = Address({
signPrivateKey: Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"),
encPrivateKey: Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"),
});
var addr4 = addr3.clone();
expect(addr4.signPrivateKey.toString("hex")).to.equal("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f");
expect(addr4.encPrivateKey.toString("hex")).to.equal("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6");
});
// FIXME(Kagami): Don't run it in browser currently because it's
// very slow. This need to be fixed.
if (allTests && typeof window === "undefined") {