2019-09-17 09:28:38 +00:00
|
|
|
(function () {
|
|
|
|
var factory = function (Util, Rpc) {
|
2021-01-18 17:05:01 +00:00
|
|
|
var create = function (network, proxy, _cb, Cache) {
|
2019-09-17 09:28:38 +00:00
|
|
|
if (typeof(_cb) !== 'function') { throw new Error("Expected callback"); }
|
|
|
|
var cb = Util.once(Util.mkAsync(_cb));
|
|
|
|
|
|
|
|
if (!network) { return void cb('INVALID_NETWORK'); }
|
|
|
|
if (!proxy) { return void cb('INVALID_PROXY'); }
|
2017-04-04 10:13:31 +00:00
|
|
|
|
|
|
|
var edPrivate = proxy.edPrivate;
|
|
|
|
var edPublic = proxy.edPublic;
|
|
|
|
|
2019-09-17 09:28:38 +00:00
|
|
|
if (!(edPrivate && edPublic)) { return void cb('INVALID_KEYS'); }
|
2017-04-04 10:13:31 +00:00
|
|
|
|
|
|
|
Rpc.create(network, edPrivate, edPublic, function (e, rpc) {
|
|
|
|
if (e) { return void cb(e); }
|
|
|
|
|
|
|
|
var exp = {};
|
2017-04-07 13:20:30 +00:00
|
|
|
|
2019-09-18 13:02:32 +00:00
|
|
|
exp.destroy = rpc.destroy;
|
|
|
|
|
2017-04-07 13:20:30 +00:00
|
|
|
// expose the supplied publicKey as an identifier
|
2017-04-04 10:13:31 +00:00
|
|
|
exp.publicKey = edPublic;
|
2017-04-07 13:20:30 +00:00
|
|
|
|
|
|
|
// expose the RPC module's raw 'send' command
|
2017-04-04 10:13:31 +00:00
|
|
|
exp.send = rpc.send;
|
|
|
|
|
2017-04-07 13:20:30 +00:00
|
|
|
// you can ask the server to pin a particular channel for you
|
2021-06-30 12:25:01 +00:00
|
|
|
exp.pin = function (channels, _cb) {
|
|
|
|
var cb = Util.once(Util.mkAsync(_cb));
|
2017-04-14 13:34:22 +00:00
|
|
|
if (!Array.isArray(channels)) {
|
2021-06-30 12:25:01 +00:00
|
|
|
return void cb('[TypeError] pin expects an array');
|
2017-04-14 09:40:28 +00:00
|
|
|
}
|
2017-04-10 15:38:25 +00:00
|
|
|
rpc.send('PIN', channels, cb);
|
2017-04-07 13:20:30 +00:00
|
|
|
};
|
2017-04-05 15:28:04 +00:00
|
|
|
|
2017-04-07 13:20:30 +00:00
|
|
|
// you can also ask to unpin a particular channel
|
2021-06-30 12:25:01 +00:00
|
|
|
exp.unpin = function (channels, _cb) {
|
|
|
|
var cb = Util.once(Util.mkAsync(_cb));
|
2017-04-14 13:34:22 +00:00
|
|
|
if (!Array.isArray(channels)) {
|
2021-06-30 12:25:01 +00:00
|
|
|
return void cb('[TypeError] pin expects an array');
|
2017-04-14 09:40:28 +00:00
|
|
|
}
|
2017-04-10 15:38:25 +00:00
|
|
|
rpc.send('UNPIN', channels, cb);
|
2017-04-04 10:13:31 +00:00
|
|
|
};
|
2017-04-07 13:20:30 +00:00
|
|
|
|
2019-03-27 16:00:28 +00:00
|
|
|
// Get data for the admin panel
|
|
|
|
exp.adminRpc = function (obj, cb) {
|
|
|
|
if (!obj.cmd) {
|
|
|
|
setTimeout(function () {
|
|
|
|
cb('[TypeError] admin rpc expects a command');
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var params = [obj.cmd, obj.data];
|
|
|
|
rpc.send('ADMIN', params, cb);
|
|
|
|
};
|
|
|
|
|
2017-04-07 13:20:30 +00:00
|
|
|
// ask the server what it thinks your hash is
|
2017-04-04 10:13:31 +00:00
|
|
|
exp.getServerHash = function (cb) {
|
2017-04-07 13:20:30 +00:00
|
|
|
rpc.send('GET_HASH', edPublic, function (e, hash) {
|
2017-04-10 15:38:25 +00:00
|
|
|
if (!(hash && hash[0])) {
|
|
|
|
return void cb('NO_HASH_RETURNED');
|
|
|
|
}
|
2019-09-17 09:28:38 +00:00
|
|
|
cb(e, Array.isArray(hash) && hash[0] || undefined);
|
2017-04-07 13:20:30 +00:00
|
|
|
});
|
2017-04-04 10:13:31 +00:00
|
|
|
};
|
2017-03-13 09:56:08 +00:00
|
|
|
|
2017-04-07 13:20:30 +00:00
|
|
|
// if local and remote hashes don't match, send a reset
|
2021-06-30 12:25:01 +00:00
|
|
|
exp.reset = function (channels, _cb) {
|
|
|
|
var cb = Util.once(Util.mkAsync(_cb));
|
2017-04-14 13:34:22 +00:00
|
|
|
if (!Array.isArray(channels)) {
|
2021-06-30 12:25:01 +00:00
|
|
|
return void cb('[TypeError] pin expects an array');
|
2017-04-14 09:40:28 +00:00
|
|
|
}
|
2021-06-30 12:25:01 +00:00
|
|
|
rpc.send('RESET', channels, cb);
|
2017-04-05 15:28:04 +00:00
|
|
|
};
|
2017-04-07 13:20:30 +00:00
|
|
|
|
|
|
|
// get the combined size of all channels (in bytes) for all the
|
|
|
|
// channels which the server has pinned for your publicKey
|
|
|
|
exp.getFileListSize = function (cb) {
|
2017-04-14 09:40:28 +00:00
|
|
|
rpc.send('GET_TOTAL_SIZE', undefined, function (e, response) {
|
|
|
|
if (e) { return void cb(e); }
|
2017-06-09 13:28:53 +00:00
|
|
|
if (response && response.length && typeof(response[0]) === 'number') {
|
2017-04-14 09:40:28 +00:00
|
|
|
cb(void 0, response[0]);
|
2017-04-18 13:49:04 +00:00
|
|
|
} else {
|
|
|
|
cb('INVALID_RESPONSE');
|
2017-04-14 09:40:28 +00:00
|
|
|
}
|
|
|
|
});
|
2017-04-07 13:20:30 +00:00
|
|
|
};
|
2017-04-05 15:28:04 +00:00
|
|
|
|
2017-05-11 14:12:44 +00:00
|
|
|
// Update the limit value for all the users and return the limit for your publicKey
|
|
|
|
exp.updatePinLimits = function (cb) {
|
|
|
|
rpc.send('UPDATE_LIMITS', undefined, function (e, response) {
|
|
|
|
if (e) { return void cb(e); }
|
2017-05-15 16:03:12 +00:00
|
|
|
if (response && response.length && typeof(response[0]) === "number") {
|
2017-05-23 14:11:07 +00:00
|
|
|
cb (void 0, response[0], response[1], response[2]);
|
2017-05-11 14:12:44 +00:00
|
|
|
} else {
|
|
|
|
cb('INVALID_RESPONSE');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
// Get the storage limit associated with your publicKey
|
|
|
|
exp.getLimit = function (cb) {
|
|
|
|
rpc.send('GET_LIMIT', undefined, function (e, response) {
|
|
|
|
if (e) { return void cb(e); }
|
2017-05-15 16:03:12 +00:00
|
|
|
if (response && response.length && typeof(response[0]) === "number") {
|
2017-05-23 14:11:07 +00:00
|
|
|
cb (void 0, response[0], response[1], response[2]);
|
2017-05-11 14:12:44 +00:00
|
|
|
} else {
|
|
|
|
cb('INVALID_RESPONSE');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-05 16:55:25 +00:00
|
|
|
exp.trimHistory = function (data, _cb) {
|
|
|
|
var cb = Util.once(Util.mkAsync(_cb));
|
|
|
|
if (typeof(data) !== 'object' || !data.channel || !data.hash) {
|
|
|
|
return void cb('INVALID_ARGUMENTS');
|
|
|
|
}
|
|
|
|
rpc.send('TRIM_HISTORY', data, function (e) {
|
|
|
|
if (e) { return cb(e); }
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-12 16:54:08 +00:00
|
|
|
exp.clearOwnedChannel = function (channel, cb) {
|
|
|
|
if (typeof(channel) !== 'string' || channel.length !== 32) {
|
|
|
|
return void cb('INVALID_ARGUMENTS');
|
|
|
|
}
|
2019-06-07 13:43:24 +00:00
|
|
|
rpc.send('CLEAR_OWNED_CHANNEL', channel, function (e) {
|
2017-07-12 16:54:08 +00:00
|
|
|
if (e) { return cb(e); }
|
2019-06-07 13:43:24 +00:00
|
|
|
cb();
|
2018-01-29 11:40:09 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exp.removeOwnedChannel = function (channel, cb) {
|
2018-05-30 12:36:29 +00:00
|
|
|
if (typeof(channel) !== 'string' || [32,48].indexOf(channel.length) === -1) {
|
2021-08-12 13:51:33 +00:00
|
|
|
console.error('invalid channel to remove', channel);
|
2018-01-29 11:40:09 +00:00
|
|
|
return void cb('INVALID_ARGUMENTS');
|
|
|
|
}
|
|
|
|
rpc.send('REMOVE_OWNED_CHANNEL', channel, function (e, response) {
|
|
|
|
if (e) { return void cb(e); }
|
2018-02-14 18:41:07 +00:00
|
|
|
if (response && response.length && response[0] === "OK") {
|
|
|
|
cb();
|
2021-01-18 17:05:01 +00:00
|
|
|
if (Cache && Cache.clearChannel) {
|
|
|
|
Cache.clearChannel(channel);
|
|
|
|
}
|
2018-01-29 11:40:09 +00:00
|
|
|
} else {
|
|
|
|
cb('INVALID_RESPONSE');
|
2017-07-20 13:55:04 +00:00
|
|
|
}
|
2017-07-12 16:54:08 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-21 17:27:20 +00:00
|
|
|
exp.removePins = function (cb) {
|
|
|
|
rpc.send('REMOVE_PINS', undefined, function (e, response) {
|
|
|
|
if (e) { return void cb(e); }
|
|
|
|
if (response && response.length && response[0] === "OK") {
|
|
|
|
cb();
|
|
|
|
} else {
|
|
|
|
cb('INVALID_RESPONSE');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-28 13:30:39 +00:00
|
|
|
exp.uploadComplete = function (id, cb) {
|
|
|
|
rpc.send('UPLOAD_COMPLETE', id, function (e, res) {
|
2017-05-18 10:36:12 +00:00
|
|
|
if (e) { return void cb(e); }
|
|
|
|
var id = res[0];
|
|
|
|
if (typeof(id) !== 'string') {
|
|
|
|
return void cb('INVALID_ID');
|
2018-05-29 17:42:20 +00:00
|
|
|
}
|
|
|
|
cb(void 0, id);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exp.ownedUploadComplete = function (id, cb) {
|
|
|
|
rpc.send('OWNED_UPLOAD_COMPLETE', id, function (e, res) {
|
|
|
|
if (e) { return void cb(e); }
|
|
|
|
var id = res[0];
|
|
|
|
if (typeof(id) !== 'string') {
|
|
|
|
return void cb('INVALID_ID');
|
2017-05-18 10:36:12 +00:00
|
|
|
}
|
|
|
|
cb(void 0, id);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exp.uploadStatus = function (size, cb) {
|
|
|
|
if (typeof(size) !== 'number') {
|
2017-11-30 14:01:17 +00:00
|
|
|
return void setTimeout(function () {
|
2017-05-18 10:36:12 +00:00
|
|
|
cb('INVALID_SIZE');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
rpc.send('UPLOAD_STATUS', size, function (e, res) {
|
|
|
|
if (e) { return void cb(e); }
|
|
|
|
var pending = res[0];
|
|
|
|
if (typeof(pending) !== 'boolean') {
|
|
|
|
return void cb('INVALID_RESPONSE');
|
|
|
|
}
|
|
|
|
cb(void 0, pending);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-28 13:30:39 +00:00
|
|
|
exp.uploadCancel = function (size, cb) {
|
|
|
|
rpc.send('UPLOAD_CANCEL', size, function (e) {
|
2017-05-18 10:36:12 +00:00
|
|
|
if (e) { return void cb(e); }
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-27 10:43:15 +00:00
|
|
|
exp.writeLoginBlock = function (data, cb) {
|
2018-06-19 14:38:49 +00:00
|
|
|
if (!data) { return void cb('NO_DATA'); }
|
|
|
|
if (!data.publicKey || !data.signature || !data.ciphertext) {
|
|
|
|
console.log(data);
|
|
|
|
return void cb("MISSING_PARAMETERS");
|
|
|
|
}
|
2021-04-28 05:46:55 +00:00
|
|
|
if (['string', 'undefined'].indexOf(typeof(data.registrationProof)) === -1) {
|
2021-04-27 10:43:15 +00:00
|
|
|
return void cb("INVALID_REGISTRATION_PROOF");
|
|
|
|
}
|
2018-06-19 14:38:49 +00:00
|
|
|
|
|
|
|
rpc.send('WRITE_LOGIN_BLOCK', [
|
|
|
|
data.publicKey,
|
|
|
|
data.signature,
|
2021-04-27 10:43:15 +00:00
|
|
|
data.ciphertext,
|
|
|
|
data.registrationProof || undefined,
|
2018-06-19 14:38:49 +00:00
|
|
|
], function (e) {
|
|
|
|
cb(e);
|
|
|
|
});
|
2018-06-14 16:18:53 +00:00
|
|
|
};
|
|
|
|
|
2018-06-19 15:17:56 +00:00
|
|
|
exp.removeLoginBlock = function (data, cb) {
|
|
|
|
if (!data) { return void cb('NO_DATA'); }
|
|
|
|
if (!data.publicKey || !data.signature) {
|
|
|
|
console.log(data);
|
|
|
|
return void cb("MISSING_PARAMETERS");
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc.send('REMOVE_LOGIN_BLOCK', [
|
|
|
|
data.publicKey, // publicKey
|
|
|
|
data.signature, // signature
|
|
|
|
], function (e) {
|
|
|
|
cb(e);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-08-28 10:40:35 +00:00
|
|
|
// Get data for the admin panel
|
|
|
|
exp.setMetadata = function (obj, cb) {
|
|
|
|
rpc.send('SET_METADATA', {
|
|
|
|
channel: obj.channel,
|
|
|
|
command: obj.command,
|
|
|
|
value: obj.value
|
|
|
|
}, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-04 10:13:31 +00:00
|
|
|
cb(e, exp);
|
|
|
|
});
|
2017-03-13 09:56:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return { create: create };
|
2019-09-17 09:28:38 +00:00
|
|
|
};
|
|
|
|
if (typeof(module) !== 'undefined' && module.exports) {
|
|
|
|
module.exports = factory(require('./common-util'), require("./rpc"));
|
|
|
|
} else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) {
|
|
|
|
define([ '/common/common-util.js', '/common/rpc.js', ], function (Util, Rpc) { return factory(Util, Rpc); });
|
|
|
|
}
|
|
|
|
}());
|