create address + test in wallet
This commit is contained in:
parent
6227bb1e5f
commit
0bccc864fb
|
@ -23,7 +23,7 @@ Addressable.prototype.addAddress = function (isChange) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Addressable.prototype.getCurrentAddressPath = function (isChange) {
|
Addressable.prototype.getCurrentAddressPath = function (isChange) {
|
||||||
return HDPath.Branch(isChange ? this.changeAddressIndex : this.receiveAddressIndex, isChange, this.copayerIndex);
|
return HDPath.FullBranch(isChange ? this.changeAddressIndex : this.receiveAddressIndex, isChange, this.copayerIndex);
|
||||||
};
|
};
|
||||||
|
|
||||||
Addressable.prototype.getNewAddressPath = function (isChange) {
|
Addressable.prototype.getNewAddressPath = function (isChange) {
|
||||||
|
|
|
@ -13,15 +13,16 @@ var VERSION = '1.0.0';
|
||||||
var MESSAGE_SIGNING_PATH = "m/1/0";
|
var MESSAGE_SIGNING_PATH = "m/1/0";
|
||||||
|
|
||||||
function Copayer(opts) {
|
function Copayer(opts) {
|
||||||
Copayer.super_.apply(this, arguments);
|
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
opts.copayerIndex = opts.copayerIndex || 0;
|
||||||
|
Copayer.super_.apply(this, [opts]);
|
||||||
|
|
||||||
this.version = VERSION;
|
this.version = VERSION;
|
||||||
this.createdOn = Math.floor(Date.now() / 1000);
|
this.createdOn = Math.floor(Date.now() / 1000);
|
||||||
this.id = opts.id;
|
this.id = opts.id;
|
||||||
this.name = opts.name;
|
this.name = opts.name;
|
||||||
this.xPubKey = opts.xPubKey;
|
this.xPubKey = opts.xPubKey;
|
||||||
this.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently
|
this.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently
|
||||||
this.signingPubKey = opts.signingPubKey || this.getSigningPubKey();
|
this.signingPubKey = opts.signingPubKey || this.getSigningPubKey();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
||||||
|
var Bitcore = require('bitcore');
|
||||||
|
var BitcoreAddress = Bitcore.Address;
|
||||||
|
|
||||||
|
var Address = require('./address');
|
||||||
var Copayer = require('./copayer');
|
var Copayer = require('./copayer');
|
||||||
var Addressable = require('./Addressable');
|
var Addressable = require('./Addressable');
|
||||||
|
|
||||||
|
@ -23,6 +27,7 @@ function Wallet(opts) {
|
||||||
this.addressIndex = 0;
|
this.addressIndex = 0;
|
||||||
this.copayers = [];
|
this.copayers = [];
|
||||||
this.pubKey = opts.pubKey;
|
this.pubKey = opts.pubKey;
|
||||||
|
this.isTestnet = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* For compressed keys, m*73 + n*34 <= 496 */
|
/* For compressed keys, m*73 + n*34 <= 496 */
|
||||||
|
@ -70,6 +75,7 @@ Wallet.fromObj = function (obj) {
|
||||||
return new Copayer(copayer);
|
return new Copayer(copayer);
|
||||||
});
|
});
|
||||||
x.pubKey = obj.pubKey;
|
x.pubKey = obj.pubKey;
|
||||||
|
x.isTestnet = obj.isTestnet;
|
||||||
|
|
||||||
Wallet.super_.prototype.fromObj.apply(this, [obj]);
|
Wallet.super_.prototype.fromObj.apply(this, [obj]);
|
||||||
return x;
|
return x;
|
||||||
|
@ -88,4 +94,26 @@ Wallet.prototype.getCopayer = function (copayerId) {
|
||||||
return _.find(this.copayers, { id: copayerId });
|
return _.find(this.copayers, { id: copayerId });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Wallet.prototype._getBitcoreNetwork = function () {
|
||||||
|
return this.isTestnet ? Bitcore.Networks.testnet : Bitcore.Networks.livenet;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Wallet.prototype.createAddress = function (path) {
|
||||||
|
|
||||||
|
var publicKeys = _.map(this.copayers, function(copayer) {
|
||||||
|
var xpub = new Bitcore.HDPublicKey(copayer.xPubKey);
|
||||||
|
return xpub.derive(path).publicKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
var bitcoreAddress = BitcoreAddress.createMultisig(publicKeys, this.m, this._getBitcoreNetwork());
|
||||||
|
|
||||||
|
return new Address({
|
||||||
|
address: bitcoreAddress.toString(),
|
||||||
|
path: path,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = Wallet;
|
module.exports = Wallet;
|
||||||
|
|
|
@ -166,17 +166,22 @@ CopayServer.prototype._verifySignature = function(text, signature, pubKey) {
|
||||||
*/
|
*/
|
||||||
CopayServer.prototype.createAddress = function (opts, cb) {
|
CopayServer.prototype.createAddress = function (opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var isChange = opts.isChange;
|
||||||
|
|
||||||
Utils.checkRequired(opts, ['walletId', 'isChange']);
|
Utils.checkRequired(opts, ['walletId', 'isChange']);
|
||||||
|
|
||||||
Utils.runLocked(opts.walletId, cb, function (cb) {
|
Utils.runLocked(opts.walletId, cb, function (cb) {
|
||||||
self.getWallet({ id: opts.walletId }, function (err, wallet) {
|
self.getWallet({ id: opts.walletId }, function (err, wallet) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
var index = wallet.addressIndex++;
|
|
||||||
|
var copayer = wallet.copayers[0]; // TODO: Assign copayer from authentication.
|
||||||
|
|
||||||
|
var path = copayer.getNewAddressPath(isChange);
|
||||||
self.storage.storeWallet(wallet, function(err) {
|
self.storage.storeWallet(wallet, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var address = self._doCreateAddress(wallet.publicKeyRing, index, opts.isChange);
|
var address = wallet.createAddress(path);
|
||||||
|
|
||||||
self.storage.storeAddress(opts.walletId, address, function(err) {
|
self.storage.storeAddress(opts.walletId, address, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
|
|
@ -479,7 +479,7 @@ describe('Copay server', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should set pkr and status = complete on last copayer joining', function(done) {
|
it('should set pkr and status = complete on last copayer joining (2-3)', function(done) {
|
||||||
helpers.createAndJoinWallet('123', 2, 3, function(err, wallet) {
|
helpers.createAndJoinWallet('123', 2, 3, function(err, wallet) {
|
||||||
server.getWallet({
|
server.getWallet({
|
||||||
id: '123'
|
id: '123'
|
||||||
|
|
Loading…
Reference in New Issue