mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-22 05:05:48 +00:00
JS: better secret and seq handling.
This commit is contained in:
committed by
Stefan Thomas
parent
5fcc92c66a
commit
0c5c12aa12
58
js/remote.js
58
js/remote.js
@@ -23,7 +23,13 @@ var Remote = function (trusted, websocket_ip, websocket_port, trace) {
|
|||||||
this.stand_alone = undefined;
|
this.stand_alone = undefined;
|
||||||
|
|
||||||
// Cache information for accounts.
|
// Cache information for accounts.
|
||||||
this.account = {};
|
this.account = {
|
||||||
|
// Consider sequence numbers stable if you know you're not generating bad transactions.
|
||||||
|
// Otherwise, clear it to have it automatically refreshed from the network.
|
||||||
|
|
||||||
|
// acount : { seq : __ }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
// Cache for various ledgers.
|
// Cache for various ledgers.
|
||||||
// XXX Clear when ledger advances.
|
// XXX Clear when ledger advances.
|
||||||
@@ -45,14 +51,6 @@ var fees = {
|
|||||||
'offer' : 100,
|
'offer' : 100,
|
||||||
};
|
};
|
||||||
|
|
||||||
// For accounts we cache things like sequence numbers.
|
|
||||||
var accounts = {
|
|
||||||
// Consider sequence numbers stable if you know you're not generating bad transactions.
|
|
||||||
// Otherwise, clear it to have it automatically refreshed from the network.
|
|
||||||
|
|
||||||
// acount : { seq : __ }
|
|
||||||
};
|
|
||||||
|
|
||||||
Remote.method('connect_helper', function () {
|
Remote.method('connect_helper', function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
@@ -151,8 +149,8 @@ Remote.method('disconnect', function (done) {
|
|||||||
ws.close();
|
ws.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send a command. The comman should lack the id.
|
// Send a request. The request should lack the id.
|
||||||
// <-> command: what to send, consumed.
|
// <-> request: what to send, consumed.
|
||||||
Remote.method('request', function (request, onDone, onFailure) {
|
Remote.method('request', function (request, onDone, onFailure) {
|
||||||
this.id += 1; // Advance id.
|
this.id += 1; // Advance id.
|
||||||
|
|
||||||
@@ -252,15 +250,14 @@ Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
|||||||
|
|
||||||
// Submit a json transaction.
|
// Submit a json transaction.
|
||||||
// done(value)
|
// done(value)
|
||||||
// <-> value: { 'status', status, 'result' : result, ... }
|
// XXX <-> value: { 'status', status, 'result' : result, ... }
|
||||||
// done may be called up to 3 times.
|
Remote.method('submit', function (request, onDone, onFailure) {
|
||||||
Remote.method('submit', function (json, private_key, onDone, onFailure) {
|
|
||||||
var req = {};
|
var req = {};
|
||||||
|
|
||||||
req.command = 'submit';
|
req.command = 'submit';
|
||||||
req.json = json;
|
req.request = request;
|
||||||
|
|
||||||
if (private_key && !this.trusted)
|
if (req.secret && !this.trusted)
|
||||||
{
|
{
|
||||||
onFailure({ 'error' : 'untrustedSever', 'request' : req });
|
onFailure({ 'error' : 'untrustedSever', 'request' : req });
|
||||||
}
|
}
|
||||||
@@ -291,12 +288,16 @@ Remote.method('server_subscribe', function (onDone, onFailure) {
|
|||||||
|
|
||||||
// Refresh accounts[account].seq
|
// Refresh accounts[account].seq
|
||||||
// done(result);
|
// done(result);
|
||||||
Remote.method('account_seq', function (account, onDone, onFailure) {
|
Remote.method('account_seq', function (account, advance, onDone, onFailure) {
|
||||||
var account_root_entry = this.accounts[account];
|
var account_root_entry = this.accounts[account];
|
||||||
|
|
||||||
if (account_root_entry && account_root_entry.seq)
|
if (account_root_entry && account_root_entry.seq)
|
||||||
{
|
{
|
||||||
onDone(account_root_entry.seq);
|
var seq = account_root_entry.seq;
|
||||||
|
|
||||||
|
if (advance) account_root_entry.seq += 1;
|
||||||
|
|
||||||
|
onDone(advance);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -308,8 +309,11 @@ Remote.method('account_seq', function (account, onDone, onFailure) {
|
|||||||
},
|
},
|
||||||
function (r) {
|
function (r) {
|
||||||
// Extract the seqence number from the account root entry.
|
// Extract the seqence number from the account root entry.
|
||||||
this.accounts[account].seq = r.seq;
|
var seq = r.seq;
|
||||||
onDone(r.seq);
|
|
||||||
|
this.accounts[account].seq = seq + 1;
|
||||||
|
|
||||||
|
onDone(seq);
|
||||||
},
|
},
|
||||||
onFailure
|
onFailure
|
||||||
);
|
);
|
||||||
@@ -317,13 +321,23 @@ Remote.method('account_seq', function (account, onDone, onFailure) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// A submit that fills in the sequence number.
|
// A submit that fills in the sequence number.
|
||||||
Remote.method('submit_seq', function (onDone, onFailure) {
|
Remote.method('submit_seq', function (transaction, onDirty, onDone, onFailure) {
|
||||||
|
// Get the next sequence number for the account.
|
||||||
|
this.account_seq(transaction.Signer, true,
|
||||||
|
function (seq) {
|
||||||
|
request.seq = seq;
|
||||||
|
this.submit(onDone, onFailure);
|
||||||
|
},
|
||||||
|
onFailure);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mark an account's root node as dirty.
|
||||||
|
Remote.method('dirty_account_root', function (account) {
|
||||||
|
delete this.ledgers.current.account_root.account;
|
||||||
});
|
});
|
||||||
|
|
||||||
exports.Remote = Remote;
|
exports.Remote = Remote;
|
||||||
exports.remoteConfig = remoteConfig;
|
exports.remoteConfig = remoteConfig;
|
||||||
exports.fees = fees;
|
exports.fees = fees;
|
||||||
exports.accounts = accounts;
|
|
||||||
|
|
||||||
// vim:sw=2:sts=2:ts=8
|
// vim:sw=2:sts=2:ts=8
|
||||||
|
|||||||
Reference in New Issue
Block a user