2014-03-26 11:26:31 -07:00
|
|
|
'use strict';
|
2014-03-26 21:18:29 -07:00
|
|
|
var imports = require('soop').imports();
|
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var BIP32 = bitcore.BIP32;
|
|
|
|
var Address = bitcore.Address;
|
|
|
|
var Script = bitcore.Script;
|
|
|
|
var coinUtil = bitcore.util;
|
|
|
|
var Transaction = bitcore.Transaction;
|
2014-03-27 08:14:49 -07:00
|
|
|
var buffertools = bitcore.buffertools;
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-03-26 21:18:29 -07:00
|
|
|
var Storage = imports.Storage || require('./Storage');
|
|
|
|
var log = imports.log || console.log;
|
2014-03-26 13:55:02 -07:00
|
|
|
|
2014-03-26 21:18:29 -07:00
|
|
|
var storage = Storage.default();
|
2014-03-26 11:26:31 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This follow Electrum convetion, as described on
|
|
|
|
* https://bitcointalk.org/index.php?topic=274182.0
|
2014-03-26 19:55:46 -07:00
|
|
|
*
|
|
|
|
* We should probably adapt the next standard once its ready as discussed at:
|
|
|
|
* http://sourceforge.net/p/bitcoin/mailman/message/32148600/
|
|
|
|
*
|
2014-03-26 11:26:31 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
var PUBLIC_BRANCH = 'm/0/';
|
|
|
|
var CHANGE_BRANCH = 'm/1/';
|
|
|
|
|
2014-03-26 13:55:02 -07:00
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
function Wallet(opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
this.network = opts.network === 'livenet' ?
|
|
|
|
bitcore.networks.livenet : bitcore.networks.testnet;
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
this.requiredCosigners = opts.requiredCosigners || 3;
|
2014-03-26 11:26:31 -07:00
|
|
|
this.totalCosigners = opts.totalCosigners || 5;
|
|
|
|
|
2014-03-26 13:55:02 -07:00
|
|
|
this.id = opts.id || Wallet.getRandomId();
|
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
this.dirty = 1;
|
2014-03-26 13:55:02 -07:00
|
|
|
this.cosignersWallets = [];
|
2014-03-26 11:26:31 -07:00
|
|
|
this.bip32 = new BIP32(opts.bytes || this.network.name);
|
2014-03-26 18:00:42 -07:00
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
this.changeAddressIndex=0;
|
|
|
|
this.addressIndex=0;
|
2014-03-26 18:00:42 -07:00
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-26 13:55:02 -07:00
|
|
|
Wallet.getRandomId = function () {
|
2014-03-27 08:14:49 -07:00
|
|
|
return buffertools.toHex(coinUtil.generateNonce());
|
2014-03-26 13:55:02 -07:00
|
|
|
};
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-03-26 13:55:02 -07:00
|
|
|
Wallet.decrypt = function (passphrase, encPayload) {
|
|
|
|
log('[wallet.js.35] TODO READ: passphrase IGNORED');
|
|
|
|
return encPayload;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.encrypt = function (passphrase, payload) {
|
|
|
|
log('[wallet.js.92] TODO: passphrase IGNORED');
|
|
|
|
return payload;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.read = function (id, passphrase) {
|
|
|
|
var encPayload = storage.read(id);
|
|
|
|
if (!encPayload)
|
|
|
|
throw new Error('Could not find wallet data');
|
|
|
|
var data;
|
|
|
|
try {
|
|
|
|
data = JSON.parse( Wallet.decrypt( passphrase, encPayload ));
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('error in storage: '+ e.toString());
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data.id !== id)
|
|
|
|
throw new Error('Wrong id in data');
|
|
|
|
|
|
|
|
var config = { network: data.network === 'livenet' ?
|
|
|
|
bitcore.networks.livenet : bitcore.networks.testnet
|
|
|
|
};
|
|
|
|
|
|
|
|
var w = new Wallet(config);
|
|
|
|
|
2014-03-26 18:00:42 -07:00
|
|
|
w.requiredCosigners = data.neededCosigners;
|
2014-03-26 13:55:02 -07:00
|
|
|
w.totalCosigners = data.totalCosigners;
|
|
|
|
w.cosignersWallets = data.cosignersExtPubKeys.map( function (pk) {
|
|
|
|
return new Wallet({bytes:pk, network: w.network.name});
|
|
|
|
});
|
|
|
|
|
|
|
|
w.dirty = 0;
|
|
|
|
|
|
|
|
return w;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype.serialize = function () {
|
|
|
|
return JSON.stringify({
|
|
|
|
id: this.id,
|
|
|
|
network: this.network.name,
|
2014-03-26 18:00:42 -07:00
|
|
|
requiredCosigners: this.neededCosigners,
|
2014-03-26 13:55:02 -07:00
|
|
|
totalCosigners: this.totalCosigners,
|
|
|
|
cosignersExtPubKeys: this.cosignersWallets.map( function (b) {
|
2014-03-26 19:55:46 -07:00
|
|
|
return b.getMasterExtendedPubKey();
|
2014-03-26 13:55:02 -07:00
|
|
|
}),
|
2014-03-26 19:55:46 -07:00
|
|
|
priv: this.getMasterExtendedPrivKey(),
|
2014-03-26 13:55:02 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype.store = function (passphrase) {
|
|
|
|
|
|
|
|
if (!this.id)
|
|
|
|
throw new Error('wallet has no id');
|
|
|
|
|
|
|
|
storage.save(this.id, Wallet.encrypt(passphrase,this.serialize()));
|
|
|
|
this.dirty = 0;
|
|
|
|
|
|
|
|
return true;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype.registeredCosigners = function () {
|
2014-03-26 13:55:02 -07:00
|
|
|
if (! this.cosignersWallets) return 1;
|
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
|
|
|
|
// 1 is self.
|
2014-03-26 13:55:02 -07:00
|
|
|
return 1 + this.cosignersWallets.length;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
Wallet.prototype.getMasterExtendedPrivKey = function () {
|
2014-03-26 13:55:02 -07:00
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
if (!this.bip32)
|
|
|
|
throw new Error('no priv key defined on the wallet');
|
|
|
|
|
|
|
|
return this.bip32.extended_private_key_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
Wallet.prototype.getMasterExtendedPubKey = function () {
|
2014-03-26 11:26:31 -07:00
|
|
|
return this.bip32.extended_public_key_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-26 18:00:42 -07:00
|
|
|
Wallet.prototype.haveAllRequiredPubKeys = function () {
|
|
|
|
return this.registeredCosigners() === this.totalCosigners;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype._checkKeys = function() {
|
|
|
|
|
|
|
|
if (!this.haveAllRequiredPubKeys())
|
|
|
|
throw new Error('dont have required keys yet');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-26 11:26:31 -07:00
|
|
|
// should receive an array also?
|
|
|
|
Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) {
|
|
|
|
|
2014-03-26 18:00:42 -07:00
|
|
|
if (this.haveAllRequiredPubKeys())
|
|
|
|
throw new Error('already have all required key:' + this.totalCosigners);
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
if (this.getMasterExtendedPubKey() === newEpk)
|
|
|
|
throw new Error('already have that key (self key)');
|
2014-03-26 11:26:31 -07:00
|
|
|
|
|
|
|
|
2014-03-26 13:55:02 -07:00
|
|
|
this.cosignersWallets.forEach(function(b){
|
2014-03-26 19:55:46 -07:00
|
|
|
if (b.getMasterExtendedPubKey() === newEpk)
|
2014-03-26 11:26:31 -07:00
|
|
|
throw new Error('already have that key');
|
|
|
|
});
|
|
|
|
|
2014-03-26 13:55:02 -07:00
|
|
|
this.cosignersWallets.push(new Wallet({bytes:newEpk, network: this.network.name } ));
|
|
|
|
this.dirty = 1;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-26 18:00:42 -07:00
|
|
|
Wallet.prototype.getPubKey = function (index,isChange) {
|
|
|
|
|
|
|
|
var path = (isChange ? CHANGE_BRANCH : PUBLIC_BRANCH) + index;
|
|
|
|
var bip32 = this.bip32.derive(path);
|
|
|
|
var pub = bip32.eckey.public;
|
|
|
|
return pub;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
Wallet.prototype.getCosignersPubKeys = function (index, isChange) {
|
2014-03-26 18:00:42 -07:00
|
|
|
this._checkKeys();
|
2014-03-26 11:26:31 -07:00
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
var pubKeys = [];
|
2014-03-26 18:00:42 -07:00
|
|
|
var l = this.cosignersWallets.length;
|
|
|
|
for(var i=0; i<l; i++) {
|
2014-03-26 19:55:46 -07:00
|
|
|
pubKeys[i] = this.cosignersWallets[i].getPubKey(index, isChange);
|
2014-03-26 18:00:42 -07:00
|
|
|
}
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
return pubKeys;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Wallet.prototype.getCosignersSortedPubKeys = function(index, isChange) {
|
|
|
|
var self = this;
|
|
|
|
var pubKeys = self.getCosignersPubKeys(index, isChange);
|
|
|
|
|
|
|
|
//sort lexicographically, i.e. as strings, i.e. alphabetically
|
|
|
|
// From https://github.com/ryanxcharles/treasure/blob/master/treasure.js
|
|
|
|
return pubKeys.sort(function(buf1, buf2) {
|
|
|
|
var len = buf1.length > buf1.length ? buf1.length : buf2.length;
|
|
|
|
for (var i = 0; i <= len; i++) {
|
|
|
|
if (buf1[i] === undefined)
|
|
|
|
return -1; //shorter strings come first
|
|
|
|
if (buf2[i] === undefined)
|
|
|
|
return 1;
|
|
|
|
if (buf1[i] < buf2[i])
|
|
|
|
return -1;
|
|
|
|
if (buf1[i] > buf2[i])
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Wallet.prototype.getAddress = function (index, isChange) {
|
|
|
|
|
2014-03-26 21:18:29 -07:00
|
|
|
if ( (isChange && index > this.changeAddressIndex)
|
|
|
|
|| (!isChange && index > this.addressIndex)) {
|
|
|
|
log('Out of bounds at getAddress: Index %d isChange: %d', index, isChange);
|
|
|
|
throw new Error('index out of bound');
|
|
|
|
}
|
|
|
|
|
2014-03-26 19:55:46 -07:00
|
|
|
var pubKeys = this.getCosignersSortedPubKeys(index, isChange);
|
|
|
|
|
2014-03-26 18:00:42 -07:00
|
|
|
var version = this.network.addressScript;
|
2014-03-26 19:55:46 -07:00
|
|
|
var script = Script.createMultisig(this.requiredCosigners, pubKeys);
|
2014-03-26 18:00:42 -07:00
|
|
|
var buf = script.buffer;
|
|
|
|
var hash = coinUtil.sha256ripe160(buf);
|
|
|
|
var addr = new Address(version, hash);
|
|
|
|
var addrStr = addr.as('base58');
|
|
|
|
return addrStr;
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
2014-03-26 18:00:42 -07:00
|
|
|
Wallet.prototype.createAddress = function(isChange) {
|
|
|
|
|
|
|
|
var ret =
|
|
|
|
this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange);
|
|
|
|
|
|
|
|
if (isChange)
|
|
|
|
this.addressIndex++;
|
|
|
|
else
|
|
|
|
this.changeAddressIndex++;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
};
|
2014-03-26 21:18:29 -07:00
|
|
|
|
|
|
|
Wallet.prototype.getAddresses = function() {
|
|
|
|
var ret = [];
|
|
|
|
|
|
|
|
for (var i=0; i<this.changeAddressIndex; i++) {
|
|
|
|
ret.push(this.getAddress(i,true));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i=0; i<this.addressIndex; i++) {
|
|
|
|
ret.push(this.getAddress(i,false));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2014-03-27 08:14:49 -07:00
|
|
|
Wallet.prototype.createTx = function(utxos,outs, changeAddress) {
|
2014-03-26 21:18:29 -07:00
|
|
|
var opts = {
|
2014-03-27 08:14:49 -07:00
|
|
|
remainderAddress: changeAddress || this.createAddress(1),
|
2014-03-26 21:18:29 -07:00
|
|
|
};
|
|
|
|
return Transaction.create(utxos, outs, opts);
|
|
|
|
};
|
2014-03-26 11:26:31 -07:00
|
|
|
|
|
|
|
// Input: Bitcore's Transaction, sign with ownPK
|
|
|
|
// return partially signed or fully signed tx
|
2014-03-26 13:55:02 -07:00
|
|
|
Wallet.prototype.signTx = function (tx) {
|
2014-03-26 11:26:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = require('soop')(Wallet);
|