txproposal basic features and test

This commit is contained in:
Matias Alejo Garcia 2014-04-09 17:28:35 -03:00
commit 1caea99917
4 changed files with 255 additions and 12 deletions

View file

@ -14,17 +14,6 @@ var buffertools = bitcore.buffertools;
var Storage = imports.Storage || require('./Storage');
var storage = Storage.default();
/*
* This follow Electrum convetion, as described in
* https://bitcointalk.org/index.php?topic=274182.0
*
* We should probably adopt the next standard once it's ready, as discussed in:
* http://sourceforge.net/p/bitcoin/mailman/message/32148600/
*
*/
var PUBLIC_BRANCH = 'm/0/';
var CHANGE_BRANCH = 'm/1/';
function PublicKeyRing(opts) {
opts = opts || {};
@ -44,6 +33,22 @@ function PublicKeyRing(opts) {
this.addressIndex=0;
}
/*
* This follow Electrum convetion, as described in
* https://bitcointalk.org/index.php?topic=274182.0
*
* We should probably adopt the next standard once it's ready, as discussed in:
* http://sourceforge.net/p/bitcoin/mailman/message/32148600/
*
*/
PublicKeyRing.PublicBranch = function (index) {
return 'm/0/'+index;
};
PublicKeyRing.ChangeBranch = function (index) {
return 'm/1/'+index;
};
PublicKeyRing.getRandomId = function () {
return buffertools.toHex(coinUtil.generateNonce());
@ -177,7 +182,7 @@ PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
var pubKeys = [];
var l = this.copayersBIP32.length;
for(var i=0; i<l; i++) {
var path = (isChange ? CHANGE_BRANCH : PUBLIC_BRANCH) + index;
var path = isChange ? PublicKeyRing.ChangeBranch(index) : PublicKeyRing.PublicBranch(index);
var bip32 = this.copayersBIP32[i].derive(path);
pubKeys[i] = bip32.eckey.public;
}
@ -201,6 +206,7 @@ PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
return script;
};
PublicKeyRing.prototype.getAddress = function (index, isChange) {
this._checkIndexRange(index, isChange);
@ -238,6 +244,21 @@ PublicKeyRing.prototype.getAddresses = function() {
return ret;
};
PublicKeyRing.prototype.getRedeemScriptMap = function () {
var ret = {};
for (var i=0; i<this.changeAddressIndex; i++) {
ret[this.getAddress(i,true)] = this.getRedeemScript(i,true);
}
for (var i=0; i<this.addressIndex; i++) {
ret[this.getAddress(i)] = this.getRedeemScript(i);
}
return ret;
};
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {

77
js/models/TxProposals.js Normal file
View file

@ -0,0 +1,77 @@
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
var coinUtil = bitcore.util;
var Transaction = bitcore.Transaction;
var Builder = bitcore.TransactionBuilder;
var buffertools = bitcore.buffertools;
var Storage = imports.Storage || require('./Storage');
var storage = Storage.default();
/*
* This follow Electrum convetion, as described in
* https://bitcointalk.org/index.php?topic=274182.0
*
* We should probably adopt the next standard once it's ready, as discussed in:
* http://sourceforge.net/p/bitcoin/mailman/message/32148600/
*
*/
var PUBLIC_BRANCH = 'm/0/';
var CHANGE_BRANCH = 'm/1/';
function TxProposals(opts) {
opts = opts || {};
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.publicKeyRing = opts.publicKeyRing;
this.requiredCopayers = opts.requiredCopayers || 3;
this.txs = [];
this.dirty = 1;
}
TxProposals.prototype.list = function() {
var ret = [];
this.txs.forEach(function(tx) {
});
};
TxProposals.prototype.create = function(toAddress, amountSat, utxos, onePrivKey) {
var pkr = this.publicKeyRing;
if (! pkr.isComplete() ) {
throw new Error('publicKeyRing is not complete');
}
var opts = {
remainderOut: { address: pkr.generateAddress(true) }
};
var b = new Builder(opts)
.setUnspent(utxos)
.setHashToScriptMap(pkr.getRedeemScriptMap())
.setOutputs([{address: toAddress, amountSat: amountSat}])
;
if (onePrivKey) {
b.sign([onePrivKey]);
}
var tx = b.build();
var txHex = tx.serialize();
this.txs.push(txHex);
return txHex;
};
TxProposals.prototype.sign = function(index) {
};
module.exports = require('soop')(TxProposals);