tx proposal test. PrivateKey

This commit is contained in:
Matias Alejo Garcia 2014-04-10 02:16:57 -03:00
commit fe989c5c39
6 changed files with 155 additions and 64 deletions

View file

@ -6,15 +6,19 @@ var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32;
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;
var util = bitcore.util;
var PublicKeyRing = require('./PublicKeyRing');
function PrivateKey(opts) {
this.id = opts.id;
this.network = opts.networkName === 'testnet' ?
networks.testnet : networks.livenet;
this.BIP32 = opts.BIP32 || new BIP32(this.network.name);
this.BIP32 = opts.BIP32 || new BIP32(opts.extendedPrivateKeyString || this.network.name);
this._calcId();
};
PrivateKey.prototype._calcId = function() {
this.id = util.ripe160(this.BIP32.extendedPublicKey).toString('hex');
};
PrivateKey.prototype.getBIP32 = function(index,isChange) {
if (typeof index === 'undefined') {
@ -24,6 +28,21 @@ PrivateKey.prototype.getBIP32 = function(index,isChange) {
PublicKeyRing.ChangeBranch(index):PublicKeyRing.PublicBranch(index) );
};
PrivateKey.fromObj = function(o) {
return new PrivateKey({
extendedPrivateKeyString: o.extendedPrivateKeyString,
networkName: o.networkName,
});
};
PrivateKey.prototype.toObj = function() {
return {
extendedPrivateKeyString: this.BIP32.extendedPrivateKeyString(),
networkName: this.network.name,
};
};
PrivateKey.prototype.get = function(index,isChange) {
var derivedBIP32 = this.getBIP32(index,isChange);
var wk = new WalletKey({network: this.network});
@ -34,6 +53,7 @@ PrivateKey.prototype.get = function(index,isChange) {
PrivateKey.prototype.getAll = function(addressIndex, changeAddressIndex) {
var ret = [];
for(var i=0;i<addressIndex; i++) {
ret.push(this.get(i,false));
}

View file

@ -13,34 +13,53 @@ var Storage = imports.Storage || require('./Storage');
var storage = Storage.default();
function TxProposal(opts) {
this.tx = opts.tx;
this.seenBy = {};
this.signedBy = {};
this.tx = opts.tx;
this.seenBy = opts.seenBy || {};
this.signedBy = opts.signedBy || {};
};
module.exports = require('soop')(TxProposal);
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 = [];
var ret = [];
this.txs.forEach(function(tx) {
TxProposals.fromObj = function(o) {
var ret = new TxProposals({
networkName: o.networkName,
});
o.txs.forEach(function(t) {
var tx = new Transaction;
tx.parse(t.txHex);
ret.txs.push({
seenBy: t.seenBy,
signedBy: t.signedBy,
tx: tx,
});
});
return ret;
};
TxProposals.prototype.create = function(toAddress, amountSat, utxos, privs) {
TxProposals.prototype.toObj = function() {
var ret = [];
this.txs.forEach(function(t) {
ret.push({
seenBy: t.seenBy,
signedBy: t.signedBy,
txHex: t.tx.serialize(),
});
});
return {
txs: ret,
networkName: this.network.name,
};
};
TxProposals.prototype.create = function(toAddress, amountSat, utxos, priv) {
var pkr = this.publicKeyRing;
if (! pkr.isComplete() ) {
@ -57,18 +76,21 @@ TxProposals.prototype.create = function(toAddress, amountSat, utxos, privs) {
.setOutputs([{address: toAddress, amountSat: amountSat}])
;
if (privs) {
b.sign(privs);
if (priv) {
//console.log('*** SIGNING IDX:', pkr.addressIndex, pkr.changeAddressIndex);
b.sign( priv.getAll(pkr.addressIndex, pkr.changeAddressIndex) );
}
var tx = b.build();
var me = {};
if (priv)
me[priv.id] = Date.now();
this.txs.push(
new TxProposal({
signedBy: {
},
seenBy: {
},
tx: tx
signedBy: me,
seenBy: me,
tx: tx,
})
);
return tx;