copay/js/models/PublicKeyRing.js

240 lines
5.9 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
/*
2014-04-04 15:36:24 -07:00
* This follow Electrum convetion, as described in
2014-03-26 11:26:31 -07:00
* https://bitcointalk.org/index.php?topic=274182.0
2014-03-26 19:55:46 -07:00
*
2014-04-04 15:36:24 -07:00
* We should probably adopt the next standard once it's ready, as discussed in:
2014-03-26 19:55:46 -07:00
* 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-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;
this.copayersBIP32 = [];
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;
2014-04-04 16:16:15 -07:00
w.addressIndex = data.addressIndex;
w.changeAddressIndex = data.changeAddressIndex;
// this.bip32 = ;
w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
return new BIP32(pk);
2014-03-26 13:55:02 -07:00
});
w.dirty = 0;
return w;
};
2014-04-04 16:16:15 -07:00
PublicKeyRing.prototype._toObj = function() {
return {
2014-03-26 13:55:02 -07:00
id: this.id,
network: this.network.name,
2014-04-04 11:57:28 -07:00
requiredCopayers: this.neededCopayers,
totalCopayers: this.totalCopayers,
2014-04-04 16:16:15 -07:00
changeAddressIndex: this.changeAddressIndex,
addressIndex: this.addressIndex,
copayersExtPubKeys: this.copayersBIP32.map( function (b) {
return b.extendedPublicKeyString();
2014-03-26 13:55:02 -07:00
}),
2014-04-04 16:16:15 -07:00
};
};
PublicKeyRing.prototype.serialize = function () {
return JSON.stringify(this._toObj());
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 () {
return this.copayersBIP32.length;
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');
};
PublicKeyRing.prototype._newExtendedPublicKey = function () {
return new BIP32(this.network.name)
.extendedPublicKeyString();
};
PublicKeyRing.prototype.addCopayer = 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
if (!newEpk) {
newEpk = this._newExtendedPublicKey();
}
2014-03-26 11:26:31 -07:00
this.copayersBIP32.forEach(function(b){
if (b.extendedPublicKeyString() === newEpk)
2014-03-26 11:26:31 -07:00
throw new Error('already have that key');
});
this.copayersBIP32.push(new BIP32(newEpk));
2014-03-26 13:55:02 -07:00
this.dirty = 1;
return newEpk;
2014-03-26 11:26:31 -07:00
};
2014-04-04 15:59:00 -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 = [];
var l = this.copayersBIP32.length;
2014-03-26 18:00:42 -07:00
for(var i=0; i<l; i++) {
var path = (isChange ? CHANGE_BRANCH : PUBLIC_BRANCH) + index;
var bip32 = this.copayersBIP32[i].derive(path);
pubKeys[i] = bip32.eckey.public;
2014-03-26 18:00:42 -07:00
}
2014-03-26 19:55:46 -07:00
return pubKeys;
};
PublicKeyRing.prototype._checkIndexRange = function (index, isChange) {
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');
}
};
PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
this._checkIndexRange(index, isChange);
2014-04-04 15:59:00 -07:00
var pubKeys = this.getCopayersPubKeys(index, isChange);
2014-04-04 11:57:28 -07:00
var script = Script.createMultisig(this.requiredCopayers, pubKeys);
return script;
};
PublicKeyRing.prototype.getAddress = function (index, isChange) {
this._checkIndexRange(index, isChange);
var script = this.getRedeemScript(index,isChange);
var hash = coinUtil.sha256ripe160(script.getBuffer());
var version = this.network.addressScript;
2014-03-26 18:00:42 -07:00
var addr = new Address(version, hash);
return addr.as('base58');
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.changeAddressIndex++;
2014-04-04 15:59:00 -07:00
else
this.addressIndex++;
2014-03-26 18:00:42 -07:00
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);