made Identity#create asynchronous
This commit is contained in:
parent
4afcad4601
commit
7e8f1a1668
3 changed files with 79 additions and 55 deletions
|
|
@ -82,10 +82,14 @@ Identity.prototype.getName = function() {
|
||||||
* @param cb
|
* @param cb
|
||||||
* @return {undefined}
|
* @return {undefined}
|
||||||
*/
|
*/
|
||||||
Identity.create = function(opts) {
|
Identity.create = function(opts, cb) {
|
||||||
opts = _.extend({}, opts);
|
opts = _.extend({}, opts);
|
||||||
|
|
||||||
return new Identity(opts);
|
var iden = new Identity(opts);
|
||||||
|
iden.store(opts, function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
return cb(null, iden);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -386,7 +390,9 @@ Identity.prototype.bindWallet = function(w) {
|
||||||
});
|
});
|
||||||
w.on('ready', function() {
|
w.on('ready', function() {
|
||||||
log.debug('<ready> Wallet' + w.getName());
|
log.debug('<ready> Wallet' + w.getName());
|
||||||
self.store({noWallets:true}, function() {
|
self.store({
|
||||||
|
noWallets: true
|
||||||
|
}, function() {
|
||||||
self.storeWallet(w);
|
self.storeWallet(w);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,26 @@ angular.module('copayApp.services')
|
||||||
.factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) {
|
.factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) {
|
||||||
var root = {};
|
var root = {};
|
||||||
|
|
||||||
|
root.check = function(scope) {
|
||||||
|
copay.Identity.checkIfExistsAny({
|
||||||
|
pluginManager: pluginManager,
|
||||||
|
}, function(anyProfile) {
|
||||||
|
copay.Wallet.checkIfExistsAny({
|
||||||
|
pluginManager: pluginManager,
|
||||||
|
}, function(anyWallet) {
|
||||||
|
scope.retreiving = false;
|
||||||
|
scope.anyProfile = anyProfile ? true : false;
|
||||||
|
scope.anyWallet = anyWallet ? true : false;
|
||||||
|
|
||||||
|
if (!scope.anyProfile) {
|
||||||
|
$location.path('/createProfile');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
root.create = function(scope, form) {
|
root.create = function(scope, form) {
|
||||||
var iden = copay.Identity.create({
|
copay.Identity.create({
|
||||||
email: form.email.$modelValue,
|
email: form.email.$modelValue,
|
||||||
password: form.password.$modelValue,
|
password: form.password.$modelValue,
|
||||||
pluginManager: pluginManager,
|
pluginManager: pluginManager,
|
||||||
|
|
@ -13,31 +31,30 @@ angular.module('copayApp.services')
|
||||||
networkName: config.networkName,
|
networkName: config.networkName,
|
||||||
walletDefaults: config.wallet,
|
walletDefaults: config.wallet,
|
||||||
passphraseConfig: config.passphraseConfig,
|
passphraseConfig: config.passphraseConfig,
|
||||||
|
failIfExists: true,
|
||||||
|
}, function(err, iden) {
|
||||||
|
if (err || !iden) {
|
||||||
|
controllerUtils.onErrorDigest(scope, 'User already exists!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var walletOptions = {
|
||||||
|
nickname: iden.fullName,
|
||||||
|
networkName: config.networkName,
|
||||||
|
requiredCopayers: 1,
|
||||||
|
totalCopayers: 1,
|
||||||
|
password: iden.password,
|
||||||
|
name: 'My wallet',
|
||||||
|
};
|
||||||
|
iden.createWallet(walletOptions, function(err, wallet) {
|
||||||
|
if (err || !wallet) {
|
||||||
|
controllerUtils.onErrorDigest(scope, 'Could not create default wallet');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
scope.loading = false;
|
||||||
|
controllerUtils.bindProfile(scope, iden, wallet.id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var walletOptions = {
|
|
||||||
nickname: iden.fullName,
|
|
||||||
networkName: config.networkName,
|
|
||||||
requiredCopayers: 1,
|
|
||||||
totalCopayers: 1,
|
|
||||||
password: iden.password,
|
|
||||||
name: 'My wallet',
|
|
||||||
};
|
|
||||||
iden.createWallet(walletOptions, function(err, wallet) {
|
|
||||||
if (err) {
|
|
||||||
controllerUtils.onErrorDigest(
|
|
||||||
scope, 'Could not create default wallet');
|
|
||||||
} else {
|
|
||||||
iden.store({failIfExists: true}, function(err) {
|
|
||||||
if (err) {
|
|
||||||
controllerUtils.onErrorDigest(scope, 'User already exists!');
|
|
||||||
} else {
|
|
||||||
controllerUtils.bindProfile(scope, iden, wallet.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
scope.loading = false;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -60,7 +77,7 @@ angular.module('copayApp.services')
|
||||||
}
|
}
|
||||||
scope.loading = false;
|
scope.loading = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,6 @@ describe('Identity model', function() {
|
||||||
w.netStart = sinon.stub();
|
w.netStart = sinon.stub();
|
||||||
w.args = args;
|
w.args = args;
|
||||||
return w;
|
return w;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -126,11 +125,11 @@ describe('Identity model', function() {
|
||||||
var args = createIdentity();
|
var args = createIdentity();
|
||||||
args.blockchain.on = sinon.stub();
|
args.blockchain.on = sinon.stub();
|
||||||
var old = Identity.prototype.createWallet;
|
var old = Identity.prototype.createWallet;
|
||||||
Identity.prototype.createWallet = sinon.stub().yields(null, getNewWallet());
|
Identity.create(args.params, function(err, iden) {
|
||||||
var iden = Identity.create(args.params);
|
should.not.exist(err);
|
||||||
should.exist(iden);
|
should.exist(iden);
|
||||||
should.exist(iden.wallets);
|
should.exist(iden.wallets);
|
||||||
Identity.prototype.createWallet = old;
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -169,9 +168,9 @@ describe('Identity model', function() {
|
||||||
args = createIdentity();
|
args = createIdentity();
|
||||||
args.params.noWallets = true;
|
args.params.noWallets = true;
|
||||||
var old = Identity.prototype.createWallet;
|
var old = Identity.prototype.createWallet;
|
||||||
Identity.prototype.createWallet = sinon.stub().yields(null, getNewWallet());
|
Identity.create(args.params, function(err, res) {
|
||||||
iden = Identity.create(args.params);
|
iden = res;
|
||||||
Identity.prototype.createWallet = old;
|
});
|
||||||
});
|
});
|
||||||
it('should be able to create wallets with given pk', function(done) {
|
it('should be able to create wallets with given pk', function(done) {
|
||||||
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
|
var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m';
|
||||||
|
|
@ -227,12 +226,13 @@ describe('Identity model', function() {
|
||||||
importWallet: sinon.stub().returns(getNewWallet()),
|
importWallet: sinon.stub().returns(getNewWallet()),
|
||||||
};
|
};
|
||||||
|
|
||||||
var iden = Identity.create(args.params);
|
Identity.create(args.params, function(err, iden) {
|
||||||
iden.retrieveWalletFromStorage('dummy', opts, function(err, wallet) {
|
iden.retrieveWalletFromStorage('dummy', opts, function(err, wallet) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
opts.importWallet.calledOnce.should.equal(true);
|
opts.importWallet.calledOnce.should.equal(true);
|
||||||
should.exist(wallet);
|
should.exist(wallet);
|
||||||
done();
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -255,13 +255,15 @@ describe('Identity model', function() {
|
||||||
cryptoUtil: fakeCrypto,
|
cryptoUtil: fakeCrypto,
|
||||||
};
|
};
|
||||||
|
|
||||||
var iden = Identity.create(args.params);
|
Identity.create(args.params, function(err, iden) {
|
||||||
iden.importEncryptedWallet(123, 'password', opts, function(err) {
|
sinon.stub(iden, 'importWalletFromObj').yields(null);
|
||||||
should.not.exist(err);
|
iden.importEncryptedWallet(123, 'password', opts, function(err) {
|
||||||
fakeCrypto.kdf.getCall(0).args[0].should.equal('password');
|
should.not.exist(err);
|
||||||
fakeCrypto.decrypt.getCall(0).args[0].should.equal('passphrase');
|
fakeCrypto.kdf.getCall(0).args[0].should.equal('password');
|
||||||
fakeCrypto.decrypt.getCall(0).args[1].should.equal(123);
|
fakeCrypto.decrypt.getCall(0).args[0].should.equal('passphrase');
|
||||||
done();
|
fakeCrypto.decrypt.getCall(0).args[1].should.equal(123);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -312,10 +314,9 @@ describe('Identity model', function() {
|
||||||
net.on = sinon.stub();
|
net.on = sinon.stub();
|
||||||
net.start = sinon.spy();
|
net.start = sinon.spy();
|
||||||
var old = Identity.prototype.createWallet;
|
var old = Identity.prototype.createWallet;
|
||||||
Identity.prototype.createWallet = sinon.stub().yields(null, getNewWallet());
|
Identity.create(args.params, function(err, res) {
|
||||||
|
iden = res;
|
||||||
iden = Identity.create(args.params);
|
});
|
||||||
Identity.prototype.createWallet = old;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should yield bad network error', function(done) {
|
it('should yield bad network error', function(done) {
|
||||||
|
|
@ -368,7 +369,7 @@ describe('Identity model', function() {
|
||||||
net.start.onFirstCall().callsArg(1);
|
net.start.onFirstCall().callsArg(1);
|
||||||
net.greet = sinon.stub();
|
net.greet = sinon.stub();
|
||||||
iden.createWallet = sinon.stub();
|
iden.createWallet = sinon.stub();
|
||||||
iden.createWallet.onFirstCall().yields(null,null);
|
iden.createWallet.onFirstCall().yields(null, null);
|
||||||
net.on.withArgs('data').yields('senderId', {
|
net.on.withArgs('data').yields('senderId', {
|
||||||
type: 'walletId',
|
type: 'walletId',
|
||||||
networkName: 'testnet',
|
networkName: 'testnet',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue