JS: Fix transaction creation.

This commit is contained in:
Arthur Britto
2012-10-14 02:23:52 -07:00
parent 5fc3d98ef5
commit d5c9cc78c7
2 changed files with 47 additions and 23 deletions

View File

@@ -79,6 +79,19 @@ Request.prototype.index = function (hash) {
return this; return this;
}; };
Request.prototype.secret = function (s) {
if (s)
this.message.secret = s;
return this;
};
Request.prototype.transaction = function (t) {
this.message.transaction = t;
return this;
};
// --> trusted: truthy, if remote is trusted // --> trusted: truthy, if remote is trusted
var Remote = function (trusted, websocket_ip, websocket_port, config, trace) { var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
this.trusted = trusted; this.trusted = trusted;
@@ -404,7 +417,6 @@ Remote.prototype.request_ledger_entry = function (type) {
// Submit a transaction. // Submit a transaction.
Remote.prototype.submit = function (transaction) { Remote.prototype.submit = function (transaction) {
debugger;
var self = this; var self = this;
if (this.trace) console.log("remote: submit: %s", JSON.stringify(transaction.transaction)); if (this.trace) console.log("remote: submit: %s", JSON.stringify(transaction.transaction));
@@ -439,6 +451,9 @@ Remote.prototype.submit = function (transaction) {
else { else {
var submit_request = new Request(this, 'submit'); var submit_request = new Request(this, 'submit');
submit_request.transaction(transaction.transaction);
submit_request.secret(transaction.secret);
// Forward successes and errors. // Forward successes and errors.
submit_request.on('success', function (message) { transaction.emit('success', message); }); submit_request.on('success', function (message) { transaction.emit('success', message); });
submit_request.on('error', function (message) { transaction.emit('error', message); }); submit_request.on('error', function (message) { transaction.emit('error', message); });
@@ -507,7 +522,7 @@ Remote.prototype.account_seq = function (account, advance) {
{ {
var seq = account_info.seq; var seq = account_info.seq;
if (advance) account_root_entry.seq += 1; if (advance) account_info.seq += 1;
} }
return seq; return seq;
@@ -520,12 +535,15 @@ Remote.prototype.account_cache = function (account) {
// Only care about a closed ledger. // Only care about a closed ledger.
// YYY Might be more advanced and work with a changing current ledger. // YYY Might be more advanced and work with a changing current ledger.
request.ledger_closed = this.ledger_closed; request.ledger(this.ledger_closed); // XXX Requires active server_subscribe
request.account_root = account; request.account_root(account);
request.on('success', function (message) { request.on('success', function (message) {
var seq = message.node.Sequence; var seq = message.node.Sequence;
if (!self.accounts[account])
self.accounts[account] = {};
self.accounts[account].seq = seq; self.accounts[account].seq = seq;
// If the caller also waits for 'success', they might run before this. // If the caller also waits for 'success', they might run before this.
@@ -572,19 +590,14 @@ Transaction.prototype.on = function (e, c) {
Transaction.prototype.submit = function () { Transaction.prototype.submit = function () {
var transaction = this.transaction; var transaction = this.transaction;
// Fill in secret from config, if needed.
if (undefined === transaction.secret && this.remote.config.accounts[this.Account]) {
this.secret = this.remote.config.accounts[this.Account].secret;
}
if (undefined === transaction.Fee) { if (undefined === transaction.Fee) {
if ('Payment' === transaction.TransactionType if ('Payment' === transaction.TransactionType
&& transaction.Flags & exports.flags.Payment.CreateAccount) { && transaction.Flags & exports.flags.Payment.CreateAccount) {
transaction.Fee = fees.account_create.to_json(); transaction.Fee = fees.account_create.to_json();
} }
else { else {
transaction.Fee = fees['default'].to_json(); transaction.Fee = fees['default'].to_json();
} }
} }
@@ -617,8 +630,12 @@ Transaction.prototype.flags = function (flags) {
if (undefined == this.transaction.Flags) if (undefined == this.transaction.Flags)
this.transaction.Flags = 0; this.transaction.Flags = 0;
var flag_set = 'object' === typeof flags ? flags : [ flags ];
for (index in flag_set) {
var flag = flag_set[index];
for (flag in 'object' === typeof flags ? flags : [ flags ]) {
if (flag in transaction_flags) if (flag in transaction_flags)
{ {
this.transaction.Flags += transaction_flags[flag]; this.transaction.Flags += transaction_flags[flag];
@@ -650,7 +667,13 @@ Transaction.prototype.account_default = function (account) {
return this.remote.config.accounts[account] ? this.remote.config.accounts[account].account : account; return this.remote.config.accounts[account] ? this.remote.config.accounts[account].account : account;
}; };
Transaction.prototype.account_secret = function (account) {
// Fill in secret from config, if needed.
return this.remote.config.accounts[account] ? this.remote.config.accounts[account].secret : undefined;
};
Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expiration) { Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expiration) {
this.secret = this.account_secret(src);
this.transaction.TransactionType = 'OfferCreate'; this.transaction.TransactionType = 'OfferCreate';
this.transaction.Account = this.account_default(src); this.transaction.Account = this.account_default(src);
this.transaction.Amount = deliver_amount.to_json(); this.transaction.Amount = deliver_amount.to_json();
@@ -670,7 +693,7 @@ Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expi
// When a transaction is submitted: // When a transaction is submitted:
// - If the connection is reliable and the server is not merely forwarding and is not malicious, // - If the connection is reliable and the server is not merely forwarding and is not malicious,
Transaction.prototype.payment = function (src, dst, deliver_amount) { Transaction.prototype.payment = function (src, dst, deliver_amount) {
this.secret = this.account_secret(src);
this.transaction.TransactionType = 'Payment'; this.transaction.TransactionType = 'Payment';
this.transaction.Account = this.account_default(src); this.transaction.Account = this.account_default(src);
this.transaction.Amount = deliver_amount.to_json(); this.transaction.Amount = deliver_amount.to_json();
@@ -680,6 +703,7 @@ Transaction.prototype.payment = function (src, dst, deliver_amount) {
} }
Remote.prototype.ripple_line_set = function (src, limit, quaility_in, quality_out) { Remote.prototype.ripple_line_set = function (src, limit, quaility_in, quality_out) {
this.secret = this.account_secret(src);
this.transaction.TransactionType = 'CreditSet'; this.transaction.TransactionType = 'CreditSet';
this.transaction.Account = this.account_default(src); this.transaction.Account = this.account_default(src);

View File

@@ -73,7 +73,7 @@ buster.testCase("Remote functions", {
buster.assert.equals(m.ledger_closed_index, 2); buster.assert.equals(m.ledger_closed_index, 2);
done(); done();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).request(); }).request();
@@ -94,12 +94,12 @@ buster.testCase("Remote functions", {
buster.assert('node' in r); buster.assert('node' in r);
done(); done();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).request(); }).request();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).request(); }).request();
@@ -119,14 +119,14 @@ buster.testCase("Remote functions", {
buster.assert(false); buster.assert(false);
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert.equals(m.error, 'remoteError'); buster.assert.equals(m.error, 'remoteError');
buster.assert.equals(m.remote.error, 'malformedAddress'); buster.assert.equals(m.remote.error, 'malformedAddress');
done(); done();
}).request(); }).request();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).request(); }).request();
@@ -146,14 +146,14 @@ buster.testCase("Remote functions", {
buster.assert(false); buster.assert(false);
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert.equals(m.error, 'remoteError'); buster.assert.equals(m.error, 'remoteError');
buster.assert.equals(m.remote.error, 'entryNotFound'); buster.assert.equals(m.remote.error, 'entryNotFound');
done(); done();
}).request(); }).request();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).request(); }).request();
@@ -175,7 +175,7 @@ buster.testCase("Remote functions", {
buster.assert('node_binary' in r); buster.assert('node_binary' in r);
done(); done();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).request(); }).request();
@@ -186,7 +186,7 @@ buster.testCase("Remote functions", {
}).request(); }).request();
}, },
'// create account' : 'create account' :
function (done) { function (done) {
alpha.transaction() alpha.transaction()
.payment('root', 'alice', Amount.from_json("10000")) .payment('root', 'alice', Amount.from_json("10000"))
@@ -197,7 +197,7 @@ buster.testCase("Remote functions", {
// Need to verify account and balance. // Need to verify account and balance.
done(); done();
}).on('error', function(m) { }).on('error', function(m) {
console.log(m); console.log("error: %s", m);
buster.assert(false); buster.assert(false);
}).submit(); }).submit();