Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KeyStore.dumpAddress and KeyStore.loadAddress #145

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions lib/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,114 @@ KeyStore.prototype.getAddresses = function (hdPathString) {

};

KeyStore.prototype.getAddressWithEncKey = function (address, hdPathString) {
address = strip0x(address);

return {
address: address,
encPrivKey: this.ksData[hdPathString].encPrivKeys[address]
}
}

KeyStore.prototype.getAddressWithEncKeys = function (hdPathString) {
if (hdPathString === undefined) {
hdPathString = this.defaultHdPathString;
}

this._checkHdPathPurpose(hdPathString, 'sign');

let addresses = this.ksData[hdPathString].addresses;

return addresses.map(function(addr) {
return this.getAddressWithEncKey(addr, hdPathString);
}.bind(this));
}

/**
* Serialize a address
*
* Schema:
* {
* "address": "<address>",
* "encPrivKey": {
* "key": "<the encrypted private key of this address>",
* "nounce": "<nounce used to encrypt>"
* }
* }
*/
KeyStore.prototype.dumpAddress = function (address, hdPathString) {

if (hdPathString === undefined) {
hdPathString = this.defaultHdPathString;
}

this._checkHdPathPurpose(hdPathString, 'sign');

address = strip0x(address);
this._checkAddressExists(address, hdPathString);

var jsonAddr = this.getAddressWithEncKey(address, hdPathString)

return JSON.stringify(jsonAddr);
}

KeyStore.prototype.loadAddress = function (serAddr, hdPathString) {

var jsonAddr = JSON.parse(serAddr);
var addr = jsonAddr.address;

if (hdPathString === undefined) {
hdPathString = this.defaultHdPathString;
}

this._validateAddressSerSchema(jsonAddr);
this._checkHdPathPurpose(hdPathString, 'sign');

try {
// already exists
this._checkAddressExists(addr, hdPathString);
console.warn('Address ' + addr + ' already exists in keystore at ' + hdPathString);

return ;

} catch(err) {
// load new address
this.ksData[hdPathString].encPrivKeys[addr] = jsonAddr.encPrivKey;
this.ksData[hdPathString].addresses.push(addr);
}
}

KeyStore.prototype._validateAddressSerSchema = function (jsonAddr) {
if (typeof jsonAddr === 'string') {
jsonAddr = JSON.parse(jsonAddr);
}

if (!jsonAddr.address) {
throw new Error('"address" is missing from the address serialization');
}

if (!jsonAddr.encPrivKey) {
throw new Error('"key" is missing from the address serialization');
}

return true;
}

KeyStore.prototype._checkHdPathPurpose = function (hdPathString, purpose) {
if (this.ksData[hdPathString].info.purpose !== purpose) {
throw new Error('Addresses not defined when purpose is not "' + purpose + '"');
}
}

KeyStore.prototype._checkAddressExists = function (address, hdPathString) {
address = strip0x(address);
if (this.ksData[hdPathString].encPrivKeys[address] == undefined) {
throw new Error('Address not found in KeyStore');
}

return true;
}

KeyStore.prototype.getSeed = function (pwDerivedKey) {

if(!this.isDerivedKeyCorrect(pwDerivedKey)) {
Expand Down Expand Up @@ -458,6 +566,15 @@ KeyStore.prototype.exportPrivateKey = function (address, pwDerivedKey, hdPathStr
return privKey;
};

KeyStore.prototype.resetAddress = function(hdPathString) {
if (hdPathString === undefined) {
hdPathString = this.defaultHdPathString;
}

this.ksData[hdPathString].encPrivKeys = [];
this.ksData[hdPathString].addresses = [];
};

KeyStore.prototype.generateNewAddress = function(pwDerivedKey, n, hdPathString) {

if(!this.isDerivedKeyCorrect(pwDerivedKey)) {
Expand Down
56 changes: 56 additions & 0 deletions test/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,62 @@ describe("Keystore", function() {

});

describe("dumpAddress", function() {

it("returns a valid dump", function() {
var validKS = fixtures.valid[0];
let pwDerivedKey = Uint8Array.from(validKS.pwDerivedKey);

var ks = new keyStore(validKS.mnSeed, pwDerivedKey)
ks.generateNewAddress(pwDerivedKey, 5);

var addr = ks.getAddresses();
addr = addr[0];

var ser = ks.dumpAddress(addr);
var jsonAddr = JSON.parse(ser);
expect(jsonAddr.address).to.eql(addr);
expect(jsonAddr.encPrivKey).to.eql(ks.ksData[ks.defaultHdPathString].encPrivKeys[addr]);
});

it('check if the address exists', function() {
var validKS = fixtures.valid[0];
var pwDerivedKey = Uint8Array.from(validKS.pwDerivedKey);

var ks = new keyStore(validKS.mnSeed, pwDerivedKey)

expect(function() {
ks.dumpAddress('0x0');
}).to.throw();
});
});

describe('loadAddress', function() {
it('loads the addresss dump', function(done) {
var validKS = fixtures.valid[0];
var pwDerivedKey = Uint8Array.from(validKS.pwDerivedKey);

var ks = new keyStore(validKS.mnSeed, pwDerivedKey)

var ser = JSON.stringify({
address: '12345',
encPrivKey: {
key: '12345=privKey',
nonce: '12345=nonce'
}
});

ks.loadAddress(ser);

ks.hasAddress('12345', function(err, has) {
expect(err).to.be.null;
expect(has).to.be.true;
done();
});

});
});

describe("Seed functions", function() {
it('returns the unencrypted seed', function(done) {
var ks = new keyStore(fixtures.valid[0].mnSeed, Uint8Array.from(fixtures.valid[0].pwDerivedKey))
Expand Down