add #addWallet to profile
This commit is contained in:
parent
77a01370cb
commit
7abdc77611
4 changed files with 91 additions and 42 deletions
|
|
@ -144,7 +144,9 @@ Identity.isAvailable = function(email, opts, cb) {
|
||||||
|
|
||||||
Identity.prototype.store = function(opts, cb) {
|
Identity.prototype.store = function(opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.profile.store(function() {
|
self.profile.store(opts, function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
var l = self.wallets.length,
|
var l = self.wallets.length,
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!l) return cb();
|
if (!l) return cb();
|
||||||
|
|
@ -169,7 +171,7 @@ Identity.prototype.store = function(opts, cb) {
|
||||||
Identity.prototype.obtainNetworkName = function(obj) {
|
Identity.prototype.obtainNetworkName = function(obj) {
|
||||||
return obj.networkName ||
|
return obj.networkName ||
|
||||||
(obj.opts ? obj.opts.networkName : null) ||
|
(obj.opts ? obj.opts.networkName : null) ||
|
||||||
(obj.publicKeyRing ? obj.publicKeyRing.networkName :null) ||
|
(obj.publicKeyRing ? obj.publicKeyRing.networkName : null) ||
|
||||||
obj.privateKey.networkName;
|
obj.privateKey.networkName;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -391,12 +393,17 @@ Identity.prototype.createWallet = function(opts, cb) {
|
||||||
opts.version = opts.version || this.version;
|
opts.version = opts.version || this.version;
|
||||||
|
|
||||||
this.storage.setPassphrase(opts.passphrase);
|
this.storage.setPassphrase(opts.passphrase);
|
||||||
|
|
||||||
|
|
||||||
var w = this._getWallet(opts);
|
var w = this._getWallet(opts);
|
||||||
var self = this;
|
this.profile.addWallet(w.id, function(err) {
|
||||||
w.store(function(err) {
|
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
self.storage.setLastOpened(w.id, function(err) {
|
var self = this;
|
||||||
return cb(err, w);
|
w.store(function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
self.storage.setLastOpened(w.id, function(err) {
|
||||||
|
return cb(err, w);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ function Profile(info, password, storage) {
|
||||||
|
|
||||||
this.email = info.email;
|
this.email = info.email;
|
||||||
this.extra = info.extra;
|
this.extra = info.extra;
|
||||||
|
this.walletIds = {};
|
||||||
this.hash = Profile.hash(this.email, password);
|
this.hash = Profile.hash(this.email, password);
|
||||||
this.storage = storage;
|
this.storage = storage;
|
||||||
};
|
};
|
||||||
|
|
@ -44,6 +45,17 @@ Profile.open = function(storage, cb) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Profile.prototype.addWallet = function(walletId, cb) {
|
||||||
|
if (this.walletIds[walletId])
|
||||||
|
return cb(new Error('WEXIST: Wallet already on profile'));
|
||||||
|
|
||||||
|
this.walletIds[walletId] = Date.now();
|
||||||
|
this.store({
|
||||||
|
overwrite: true
|
||||||
|
}, cb);
|
||||||
|
};
|
||||||
|
|
||||||
Profile.prototype.store = function(opts, cb) {
|
Profile.prototype.store = function(opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var val = self.toObj();
|
var val = self.toObj();
|
||||||
|
|
|
||||||
|
|
@ -46,11 +46,16 @@ describe('Identity model', function() {
|
||||||
|
|
||||||
profile = sinon.stub();
|
profile = sinon.stub();
|
||||||
profile.test = sinon.stub();
|
profile.test = sinon.stub();
|
||||||
profile.store = sinon.stub();
|
profile.store = sinon.stub().yields(null);;
|
||||||
profile.store.yields(null);
|
|
||||||
Identity._newProfile = sinon.stub().returns(profile);
|
Identity._newProfile = sinon.stub().returns(profile);
|
||||||
|
|
||||||
iden = new Identity(email, password, config);
|
iden = new Identity(email, password, config);
|
||||||
|
|
||||||
|
var w = sinon.stub();
|
||||||
|
w.store = sinon.stub().yields(null);
|
||||||
|
iden._getWallet = sinon.stub().returns(w);
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -187,6 +192,40 @@ describe('Identity model', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('#createWallet', function() {
|
||||||
|
it('should create wallet', function(done) {
|
||||||
|
iden.createWallet(null, function(err, w) {
|
||||||
|
should.exist(w);
|
||||||
|
should.not.exist(err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create wallets with given pk', function(done) {
|
||||||
|
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
|
||||||
|
iden.createWallet({
|
||||||
|
privateKeyHex: priv,
|
||||||
|
}, function(err, w) {
|
||||||
|
iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.equal(priv);
|
||||||
|
should.not.exist(err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create wallets with random pk', function(done) {
|
||||||
|
iden.createWallet(null, function(err, w1) {
|
||||||
|
iden.createWallet(null, function(err, w2) {
|
||||||
|
iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.not.equal(
|
||||||
|
iden._getWallet.getCall(1).args[0].privateKey.toObj().extendedPrivateKeyString
|
||||||
|
);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// TODO this is a WALLET TEST! not Wallet Factory. Move it.
|
// TODO this is a WALLET TEST! not Wallet Factory. Move it.
|
||||||
describe.skip('#fromObj / #toObj', function() {
|
describe.skip('#fromObj / #toObj', function() {
|
||||||
it('round trip', function() {
|
it('round trip', function() {
|
||||||
|
|
@ -492,38 +531,6 @@ describe('Identity model', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#createWallet', function() {
|
|
||||||
it('should create wallet', function(done) {
|
|
||||||
iden.createWallet(null, function(err, w) {
|
|
||||||
should.exist(w);
|
|
||||||
should.not.exist(err);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be able to create wallets with given pk', function(done) {
|
|
||||||
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
|
|
||||||
iden.createWallet({
|
|
||||||
privateKeyHex: priv,
|
|
||||||
}, function(err, w) {
|
|
||||||
iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.equal(priv);
|
|
||||||
should.not.exist(err);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be able to create wallets with random pk', function(done) {
|
|
||||||
iden.createWallet(null, function(err, w1) {
|
|
||||||
iden.createWallet(null, function(err, w2) {
|
|
||||||
iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.not.equal(
|
|
||||||
iden._getWallet.getCall(1).args[0].privateKey.toObj().extendedPrivateKeyString
|
|
||||||
);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#joinWallet', function() {
|
describe('#joinWallet', function() {
|
||||||
var opts = {
|
var opts = {
|
||||||
secret: '8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM',
|
secret: '8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM',
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,27 @@ describe('Profile model', function() {
|
||||||
p2.should.deep.equal(p);
|
p2.should.deep.equal(p);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe.only('#addWallet', function() {
|
||||||
|
it('should add a wallet id', function(done) {
|
||||||
|
var p = new Profile(opts, password, storage);
|
||||||
|
p.addWallet('123', function(err) {
|
||||||
|
p.walletIds['123'].should.be.above(123456789);
|
||||||
|
storage.set.getCall(0).args[1].should.deep.equal(p.toObj());
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
it('should keep old value', function(done) {
|
||||||
|
var p = new Profile(opts, password, storage);
|
||||||
|
p.walletIds['123']=1;
|
||||||
|
p.addWallet('123', function(err) {
|
||||||
|
p.walletIds['123'].should.equal(1);
|
||||||
|
should.not.exist(storage.set.getCall(0));
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('#store', function() {
|
describe('#store', function() {
|
||||||
it('should call storage set', function(done) {
|
it('should call storage set', function(done) {
|
||||||
var p = new Profile(opts, password, storage);
|
var p = new Profile(opts, password, storage);
|
||||||
|
|
@ -66,7 +87,9 @@ describe('Profile model', function() {
|
||||||
it('should use overwrite param', function(done) {
|
it('should use overwrite param', function(done) {
|
||||||
storage.get = sinon.stub().yields(123);
|
storage.get = sinon.stub().yields(123);
|
||||||
var p = new Profile(opts, password, storage);
|
var p = new Profile(opts, password, storage);
|
||||||
p.store({overwrite:true}, function(err) {
|
p.store({
|
||||||
|
overwrite: true
|
||||||
|
}, function(err) {
|
||||||
storage.set.getCall(0).args[1].should.deep.equal(p.toObj());
|
storage.set.getCall(0).args[1].should.deep.equal(p.toObj());
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
done();
|
done();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue