wallet test passing

This commit is contained in:
Matias Alejo Garcia 2014-11-22 20:00:18 -03:00
commit e9005c2ca0
3 changed files with 85 additions and 68 deletions

View file

@ -38,6 +38,17 @@ function TxProposal(opts) {
this.readonly = opts.readonly || null;
this.merchant = opts.merchant || null;
this.paymentProtocolURL = opts.paymentProtocolURL || null;
if (opts.creator) {
var now = Date.now();
var me = {};
me[opts.creator] = now;
this.signedBy = this.seenBy = me;
this.creator = opts.creator;
this.createdTs = now;
}
this._sync();
}
@ -50,7 +61,7 @@ TxProposal.prototype._checkPayPro = function() {
if (!this.merchant.outs || this.merchant.outs.length !== 1)
throw new Error('PayPro: Unsopported number of outputs');
if (this.merchant.expires < (this.getSent() || Date.now()/1000.) )
if (this.merchant.expires < (this.getSent() || Date.now() / 1000.))
throw new Error('PayPro: Request expired');
if (!this.merchant.total || !this.merchant.outs[0].amountSatStr || !this.merchant.outs[0].address)

View file

@ -1541,7 +1541,9 @@ Wallet.prototype.fetchPaymentRequest = function(options, cb) {
var merchantData, err;
try {
merchantData = self.parsePaymentRequest(options, pr);
} catch (e) { err = e};
} catch (e) {
err = e
};
log.debug('PayPro request data', merchantData);
return cb(err, merchantData);
@ -1831,7 +1833,7 @@ Wallet.prototype.sendPaymentTx = function(ntxid, options, cb) {
ack = paypro.makePaymentACK(data);
return self.receivePaymentRequestACK(ntxid, tx, txp, ack, cb);
})
.error(function(data, status ) {
.error(function(data, status) {
log.debug('Sending to server was not met with a returned tx.');
log.debug('XHR status: ' + status);
self._processTxProposalSent(ntxid, function(err, txid) {
@ -2136,7 +2138,7 @@ Wallet.prototype.createTx = function(opts, cb) {
var self = this;
var toAddress = opts.toAddress;
var amountSat = opts.amountSat;
preconditions.checkArgument(!opts.comment || opts.comment.length <= 100);
var comment = opts.comment;
var url = opts.url;
if (url && !opts.merchantData) {
@ -2148,7 +2150,7 @@ Wallet.prototype.createTx = function(opts, cb) {
if (err) return cb(err);
opts.merchantData = merchantData;
opts.amountSat = merchantData.outs[0].address;
opts.toAddress = merchantData.outs[0].amount;
opts.toAddress = merchantData.outs[0].amount;
self.createTx(opts, cb);
});
};
@ -2158,19 +2160,21 @@ Wallet.prototype.createTx = function(opts, cb) {
this.getUnspent(function(err, safeUnspent) {
if (err) return cb(new Error('Could not get list of UTXOs'));
var ntxid;
var ntxid, txp;
try {
var txp = self.createTxProposal(toAddress, amountSat, safeUnspent, opts.builderOpts);
if (opts.merchantData)
txp.addMerchantData(opts.merchantData);
var ntxid = self.addNewTxProposal(txp);
log.debug('TXP Added: ', ntxid);
txp = self.createTxProposal(toAddress, amountSat, comment, safeUnspent, opts.builderOpts);
} catch (e) {
log.error(e);
return cb(e);
}
if (opts.merchantData)
txp.addMerchantData(opts.merchantData);
var ntxid = self.txProposals.add(txp);
log.debug('TXP Added: ', ntxid);
if (!ntxid) {
return cb(new Error('Error creating the transaction'));
}
@ -2194,10 +2198,11 @@ var sanitize = function(address) {
* @desc Create a transaction proposal
* @TODO: Document more
*/
Wallet.prototype.createTxProposal = function(toAddress, amountSat, utxos, builderOpts) {
Wallet.prototype.createTxProposal = function(toAddress, amountSat, comment, utxos, builderOpts) {
preconditions.checkArgument(toAddress);
preconditions.checkArgument(amountSat);
preconditions.checkArgument(_.isArray(utxos));
preconditions.checkArgument(!comment || comment.length <= 100, 'Comment too long');
builderOpts = builderOpts || {};
var pkr = this.publicKeyRing;
@ -2257,32 +2262,11 @@ Wallet.prototype.createTxProposal = function(toAddress, amountSat, utxos, builde
inputChainPaths: inputChainPaths,
comment: comment,
builder: b,
creator: this.getMyCopayerId(),
});
};
/* addNewTxProposal
* adds a transaction proposal to the list. Sets current copayer and creation metadata.
*
* @param {txp} Transaction Proposal Object
* @desc returns normalized transaction ID
* @param {ntxid}
*/
Wallet.prototype.addNewTxProposal = function(txp) {
var myId = this.getMyCopayerId();
var now = Date.now();
var me = {};
me[myId] = now;
// Add metadata to TxP
txp.signedBy = txp.seenBy = me;
txp.creator = myId;
txp.createdTs = now;
var ntxid = this.txProposals.add(txp);
return ntxid;
};
/**
* @desc Updates all the indexes for the current publicKeyRing. This scans
* the blockchain looking for transactions on derived addresses.