copay/js/models/PublicKeyRing.js

228 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-03-26 11:26:31 -07:00
'use strict';
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
var Storage = imports.Storage || require('./Storage');
var log = imports.log || console.log;
2014-03-26 13:55:02 -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-04-04 11:57:28 -07:00
function PublicKeyRing(opts) {
2014-03-26 11:26:31 -07:00
opts = opts || {};
this.network = opts.network === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
2014-04-04 11:57:28 -07:00
this.requiredCopayers = opts.requiredCopayers || 3;
this.totalCopayers = opts.totalCopayers || 5;
2014-03-26 11:26:31 -07:00
2014-04-04 11:57:28 -07:00
this.id = opts.id || PublicKeyRing.getRandomId();
2014-03-26 13:55:02 -07:00
2014-03-26 11:26:31 -07:00
this.dirty = 1;
2014-04-04 11:57:28 -07:00
this.copayersWallets = [];
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 11:26:31 -07:00
}
2014-04-04 11:57:28 -07:00
PublicKeyRing.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-04-04 11:57:28 -07:00
PublicKeyRing.decrypt = function (passphrase, encPayload) {
2014-03-26 13:55:02 -07:00
log('[wallet.js.35] TODO READ: passphrase IGNORED');
return encPayload;
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.encrypt = function (passphrase, payload) {
2014-03-26 13:55:02 -07:00
log('[wallet.js.92] TODO: passphrase IGNORED');
return payload;
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.read = function (id, passphrase) {
2014-03-26 13:55:02 -07:00
var encPayload = storage.read(id);
if (!encPayload)
throw new Error('Could not find wallet data');
var data;
try {
2014-04-04 11:57:28 -07:00
data = JSON.parse( PublicKeyRing.decrypt( passphrase, encPayload ));
2014-03-26 13:55:02 -07:00
} 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
};
2014-04-04 11:57:28 -07:00
var w = new PublicKeyRing(config);
2014-03-26 13:55:02 -07:00
2014-04-04 11:57:28 -07:00
w.requiredCopayers = data.neededCopayers;
w.totalCopayers = data.totalCopayers;
w.copayersWallets = data.copayersExtPubKeys.map( function (pk) {
return new PublicKeyRing({bytes:pk, network: w.network.name});
2014-03-26 13:55:02 -07:00
});
w.dirty = 0;
return w;
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.serialize = function () {
2014-03-26 13:55:02 -07:00
return JSON.stringify({
id: this.id,
network: this.network.name,
2014-04-04 11:57:28 -07:00
requiredCopayers: this.neededCopayers,
totalCopayers: this.totalCopayers,
copayersExtPubKeys: this.copayersWallets.map( function (b) {
2014-03-26 19:55:46 -07:00
return b.getMasterExtendedPubKey();
2014-03-26 13:55:02 -07:00
}),
});
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.store = function (passphrase) {
2014-03-26 13:55:02 -07:00
if (!this.id)
throw new Error('wallet has no id');
2014-04-04 11:57:28 -07:00
storage.save(this.id, PublicKeyRing.encrypt(passphrase,this.serialize()));
2014-03-26 13:55:02 -07:00
this.dirty = 0;
return true;
2014-03-26 11:26:31 -07:00
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.registeredCopayers = function () {
if (! this.copayersWallets) return 1;
2014-03-26 13:55:02 -07:00
2014-03-26 11:26:31 -07:00
// 1 is self.
2014-04-04 11:57:28 -07:00
return 1 + this.copayersWallets.length;
2014-03-26 11:26:31 -07:00
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.getMasterExtendedPubKey = function () {
return this.bip32.extendedPublicKeyString();
2014-03-26 11:26:31 -07:00
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.haveAllRequiredPubKeys = function () {
return this.registeredCopayers() === this.totalCopayers;
2014-03-26 18:00:42 -07:00
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype._checkKeys = function() {
2014-03-26 18:00:42 -07:00
if (!this.haveAllRequiredPubKeys())
throw new Error('dont have required keys yet');
};
2014-03-26 11:26:31 -07:00
// should receive an array also?
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.addCopayerExtendedPubKey = function (newEpk) {
2014-03-26 11:26:31 -07:00
2014-03-26 18:00:42 -07:00
if (this.haveAllRequiredPubKeys())
2014-04-04 11:57:28 -07:00
throw new Error('already have all required key:' + this.totalCopayers);
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-04-04 11:57:28 -07:00
this.copayersWallets.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-04-04 11:57:28 -07:00
this.copayersWallets.push(new PublicKeyRing({bytes:newEpk, network: this.network.name } ));
2014-03-26 13:55:02 -07:00
this.dirty = 1;
2014-03-26 11:26:31 -07:00
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.getPubKey = function (index,isChange) {
2014-03-26 18:00:42 -07:00
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-04-04 11:57:28 -07:00
PublicKeyRing.prototype.getCopayersPubKeys = 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-04-04 11:57:28 -07:00
var l = this.copayersWallets.length;
2014-03-26 18:00:42 -07:00
for(var i=0; i<l; i++) {
2014-04-04 11:57:28 -07:00
pubKeys[i] = this.copayersWallets[i].getPubKey(index, isChange);
2014-03-26 18:00:42 -07:00
}
2014-03-26 19:55:46 -07:00
return pubKeys;
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.prototype.getAddress = function (index, isChange) {
2014-03-26 19:55:46 -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-04-04 11:57:28 -07:00
var pubKeys = this.getCopayersPubKeys();
2014-03-26 18:00:42 -07:00
var version = this.network.addressScript;
2014-04-04 11:57:28 -07:00
var script = Script.createMultisig(this.requiredCopayers, 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-04-04 11:57:28 -07:00
//generate a new address, update index.
PublicKeyRing.prototype.generateAddress = function(isChange) {
2014-03-26 18:00:42 -07:00
var ret =
this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange);
if (isChange)
this.addressIndex++;
else
this.changeAddressIndex++;
return ret;
};
2014-04-04 11:57:28 -07:00
PublicKeyRing.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-04-04 11:57:28 -07:00
module.exports = require('soop')(PublicKeyRing);