move hashChannelList into common-hash.js

This commit is contained in:
ansuz 2017-04-14 11:40:28 +02:00
parent 63960bd38c
commit f42da4ecd8
3 changed files with 40 additions and 15 deletions

View File

@ -1,13 +1,23 @@
define([
'/common/common-util.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Util, Crypto) {
var Nacl = window.nacl;
var Hash = {};
var uint8ArrayToHex = Util.uint8ArrayToHex;
var hexToBase64 = Util.hexToBase64;
var base64ToHex = Util.base64ToHex;
// This implementation must match that on the server
// it's used for a checksum
Hash.hashChannelList = function (list) {
return Nacl.util.encodeBase64(Nacl.hash(Nacl.util
.decodeUTF8(JSON.stringify(list))));
};
var getEditHashFromKeys = Hash.getEditHashFromKeys = function (chanKey, keys) {
if (typeof keys === 'string') {
return chanKey + keys;
@ -231,6 +241,5 @@ define([
return hex;
};
return Hash;
});

View File

@ -726,7 +726,7 @@ load pinpad dynamically only after you know that it will be needed */
if (!pinsReady()) { return void cb ('[RPC_NOT_READY]'); }
var list = getCanonicalChannelList();
var local = rpc.hashChannelList(list);
var local = Hash.hashChannelList(list);
rpc.getServerHash(function (e, hash) {
if (e) { return void cb(e); }
cb(void 0, hash === local);

View File

@ -1,9 +1,6 @@
define([
'/common/rpc.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Rpc) {
var Nacl = window.nacl;
var create = function (network, proxy, cb) {
if (!network) {
window.setTimeout(function () {
@ -41,21 +38,28 @@ define([
// you can ask the server to pin a particular channel for you
exp.pin = function (channels, cb) {
// TODO use isArray if it's safe
if (!channels && channels.length) {
window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
rpc.send('PIN', channels, cb);
};
// you can also ask to unpin a particular channel
exp.unpin = function (channels, cb) {
// TODO use isArray if it's safe
if (!channels && channels.length) {
window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
rpc.send('UNPIN', channels, cb);
};
// This implementation must match that on the server
// it's used for a checksum
exp.hashChannelList = function (list) {
return Nacl.util.encodeBase64(Nacl.hash(Nacl.util
.decodeUTF8(JSON.stringify(list))));
};
// ask the server what it thinks your hash is
exp.getServerHash = function (cb) {
rpc.send('GET_HASH', edPublic, function (e, hash) {
@ -67,8 +71,15 @@ define([
};
// if local and remote hashes don't match, send a reset
exp.reset = function (list, cb) {
rpc.send('RESET', list, function (e, response) {
exp.reset = function (channels, cb) {
// TODO use isArray if it's safe
if (!channels && channels.length) {
window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
rpc.send('RESET', channels, function (e, response) {
cb(e, response[0]);
});
};
@ -81,7 +92,12 @@ define([
// 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) {
rpc.send('GET_TOTAL_SIZE', undefined, cb);
rpc.send('GET_TOTAL_SIZE', undefined, function (e, response) {
if (e) { return void cb(e); }
if (response && response.length) {
cb(void 0, response[0]);
}
});
};
cb(e, exp);