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;
};
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
var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
this.trusted = trusted;
@@ -404,7 +417,6 @@ Remote.prototype.request_ledger_entry = function (type) {
// Submit a transaction.
Remote.prototype.submit = function (transaction) {
debugger;
var self = this;
if (this.trace) console.log("remote: submit: %s", JSON.stringify(transaction.transaction));
@@ -439,6 +451,9 @@ Remote.prototype.submit = function (transaction) {
else {
var submit_request = new Request(this, 'submit');
submit_request.transaction(transaction.transaction);
submit_request.secret(transaction.secret);
// Forward successes and errors.
submit_request.on('success', function (message) { transaction.emit('success', 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;
if (advance) account_root_entry.seq += 1;
if (advance) account_info.seq += 1;
}
return seq;
@@ -520,12 +535,15 @@ Remote.prototype.account_cache = function (account) {
// Only care about a closed ledger.
// YYY Might be more advanced and work with a changing current ledger.
request.ledger_closed = this.ledger_closed;
request.account_root = account;
request.ledger(this.ledger_closed); // XXX Requires active server_subscribe
request.account_root(account);
request.on('success', function (message) {
var seq = message.node.Sequence;
if (!self.accounts[account])
self.accounts[account] = {};
self.accounts[account].seq = seq;
// If the caller also waits for 'success', they might run before this.
@@ -572,11 +590,6 @@ Transaction.prototype.on = function (e, c) {
Transaction.prototype.submit = function () {
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 ('Payment' === transaction.TransactionType
&& transaction.Flags & exports.flags.Payment.CreateAccount) {
@@ -618,7 +631,11 @@ Transaction.prototype.flags = function (flags) {
if (undefined == this.transaction.Flags)
this.transaction.Flags = 0;
for (flag in 'object' === typeof flags ? flags : [ flags ]) {
var flag_set = 'object' === typeof flags ? flags : [ flags ];
for (index in flag_set) {
var flag = flag_set[index];
if (flag in transaction_flags)
{
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;
};
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) {
this.secret = this.account_secret(src);
this.transaction.TransactionType = 'OfferCreate';
this.transaction.Account = this.account_default(src);
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:
// - If the connection is reliable and the server is not merely forwarding and is not malicious,
Transaction.prototype.payment = function (src, dst, deliver_amount) {
this.secret = this.account_secret(src);
this.transaction.TransactionType = 'Payment';
this.transaction.Account = this.account_default(src);
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) {
this.secret = this.account_secret(src);
this.transaction.TransactionType = 'CreditSet';
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);
done();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).request();
@@ -94,12 +94,12 @@ buster.testCase("Remote functions", {
buster.assert('node' in r);
done();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).request();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).request();
@@ -119,14 +119,14 @@ buster.testCase("Remote functions", {
buster.assert(false);
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert.equals(m.error, 'remoteError');
buster.assert.equals(m.remote.error, 'malformedAddress');
done();
}).request();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).request();
@@ -146,14 +146,14 @@ buster.testCase("Remote functions", {
buster.assert(false);
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert.equals(m.error, 'remoteError');
buster.assert.equals(m.remote.error, 'entryNotFound');
done();
}).request();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).request();
@@ -175,7 +175,7 @@ buster.testCase("Remote functions", {
buster.assert('node_binary' in r);
done();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).request();
@@ -186,7 +186,7 @@ buster.testCase("Remote functions", {
}).request();
},
'// create account' :
'create account' :
function (done) {
alpha.transaction()
.payment('root', 'alice', Amount.from_json("10000"))
@@ -197,7 +197,7 @@ buster.testCase("Remote functions", {
// Need to verify account and balance.
done();
}).on('error', function(m) {
console.log(m);
console.log("error: %s", m);
buster.assert(false);
}).submit();