From e07fffc8349259cb8723695409ce3e2e37598504 Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Thu, 7 Feb 2013 18:34:18 +0100 Subject: [PATCH] JS: Move transaction preparation logic to Transaction class. --- src/js/remote.js | 60 +++++++-------------------------------- src/js/transaction.js | 66 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/src/js/remote.js b/src/js/remote.js index c208dd1a5..af05c2931 100644 --- a/src/js/remote.js +++ b/src/js/remote.js @@ -220,6 +220,12 @@ var Remote = function (opts, trace) { this._reserve_inc = undefined; this._server_status = undefined; + // Local signing implies local fees and sequences + if (this.local_signing) { + this.local_sequence = true; + this.local_fee = true; + } + // Cache information for accounts. this.accounts = { // Consider sequence numbers stable if you know you're not generating bad transactions. @@ -864,58 +870,12 @@ Remote.prototype.request_sign = function (secret, tx_json) { }; // Submit a transaction. -Remote.prototype.submit = function (transaction) { +Remote.prototype.request_submit = function () { var self = this; - if (transaction._secret && !this.trusted) - { - transaction.emit('error', { - 'result' : 'tejServerUntrusted', - 'result_message' : "Attempt to give a secret to an untrusted server." - }); - } - else { - if (self.local_sequence && !transaction.tx_json.Sequence) { - transaction.tx_json.Sequence = this.account_seq(transaction.tx_json.Account, 'ADVANCE'); - // console.log("Sequence: %s", transaction.tx_json.Sequence); - } + var request = new Request(this, 'submit'); - if (self.local_sequence && !transaction.tx_json.Sequence) { - // Look in the last closed ledger. - this.account_seq_cache(transaction.tx_json.Account, false) - .on('success_account_seq_cache', function () { - // Try again. - self.submit(transaction); - }) - .on('error_account_seq_cache', function (message) { - // XXX Maybe be smarter about this. Don't want to trust an untrusted server for this seq number. - - // Look in the current ledger. - self.account_seq_cache(transaction.tx_json.Account, 'CURRENT') - .on('success_account_seq_cache', function () { - // Try again. - self.submit(transaction); - }) - .on('error_account_seq_cache', function (message) { - // Forward errors. - transaction.emit('error', message); - }) - .request(); - }) - .request(); - } - else { - // Convert the transaction into a request and submit it. - - (new Request(this, 'submit')) - .build_path(transaction._build_path) - .tx_json(transaction.tx_json) - .secret(transaction._secret) - .on('success', function (message) { transaction.emit('success', message); }) // Forward successes and errors. - .on('error', function (message) { transaction.emit('error', message); }) - .request(); - } - } + return request; }; // @@ -929,7 +889,7 @@ Remote.prototype._server_subscribe = function () { var self = this; var feeds = [ 'ledger', 'server' ]; - + if (this._transaction_subs) feeds.push('transactions'); diff --git a/src/js/transaction.js b/src/js/transaction.js index 5e535c036..e9d7e8c66 100644 --- a/src/js/transaction.js +++ b/src/js/transaction.js @@ -219,6 +219,7 @@ Transaction.prototype.sign = function () { // // status is final status. Only works under a ledger_accepting conditions. // switch status: // case 'tesSUCCESS': all is well. +// case 'tejSecretUnknown': unable to sign transaction - secret unknown // case 'tejServerUntrusted': sending secret to untrusted server. // case 'tejInvalidAccount': locally detected error. // case 'tejLost': locally gave up looking @@ -236,7 +237,7 @@ Transaction.prototype.submit = function (callback) { 'error' : 'tejInvalidAccount', 'error_message' : 'Bad account.' }); - return; + return this; } // YYY Might check paths for invalid accounts. @@ -308,7 +309,68 @@ Transaction.prototype.submit = function (callback) { this.set_state('client_submitted'); - this.remote.submit(this); + if (self.remote.local_sequence && !self.tx_json.Sequence) { + self.tx_json.Sequence = this.remote.account_seq(self.tx_json.Account, 'ADVANCE'); + // console.log("Sequence: %s", self.tx_json.Sequence); + } + + if (self.remote.local_sequence && !self.tx_json.Sequence) { + // Look in the last closed ledger. + this.remote.account_seq_cache(self.tx_json.Account, false) + .on('success_account_seq_cache', function () { + // Try again. + self.submit(); + }) + .on('error_account_seq_cache', function (message) { + // XXX Maybe be smarter about this. Don't want to trust an untrusted server for this seq number. + + // Look in the current ledger. + self.remote.account_seq_cache(self.tx_json.Account, 'CURRENT') + .on('success_account_seq_cache', function () { + // Try again. + self.submit(); + }) + .on('error_account_seq_cache', function (message) { + // Forward errors. + self.emit('error', message); + }) + .request(); + }) + .request(); + return this; + } + + // Prepare request + + var request = this.remote.request_submit(); + + // Forward successes and errors. + request.on('success', function (message) { self.emit('success', message); }); + request.on('error', function (message) { self.emit('error', message); }); + + if (!this._secret && !this.tx_json.Signature) { + this.emit('error', { + 'result' : 'tejSecretUnknown', + 'result_message' : "Could not sign transactions because we." + }); + return this; + } else if (this.remote.local_signing) { + this.sign(); + } else { + if (!this.remote.trusted) { + this.emit('error', { + 'result' : 'tejServerUntrusted', + 'result_message' : "Attempt to give a secret to an untrusted server." + }); + return this; + } else { + request.secret(this._secret); + } + } + + request.build_path(this._build_path); + request.tx_json(this.tx_json); + request.request(); return this; }