move hashChannelList into common-hash.js

pull/1/head
ansuz 8 years ago
parent 63960bd38c
commit f42da4ecd8

@ -1,13 +1,23 @@
define([ define([
'/common/common-util.js', '/common/common-util.js',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Util, Crypto) { ], function (Util, Crypto) {
var Nacl = window.nacl;
var Hash = {}; var Hash = {};
var uint8ArrayToHex = Util.uint8ArrayToHex; var uint8ArrayToHex = Util.uint8ArrayToHex;
var hexToBase64 = Util.hexToBase64; var hexToBase64 = Util.hexToBase64;
var base64ToHex = Util.base64ToHex; 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) { var getEditHashFromKeys = Hash.getEditHashFromKeys = function (chanKey, keys) {
if (typeof keys === 'string') { if (typeof keys === 'string') {
return chanKey + keys; return chanKey + keys;
@ -231,6 +241,5 @@ define([
return hex; return hex;
}; };
return Hash; return Hash;
}); });

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

@ -1,9 +1,6 @@
define([ define([
'/common/rpc.js', '/common/rpc.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Rpc) { ], function (Rpc) {
var Nacl = window.nacl;
var create = function (network, proxy, cb) { var create = function (network, proxy, cb) {
if (!network) { if (!network) {
window.setTimeout(function () { window.setTimeout(function () {
@ -41,21 +38,28 @@ define([
// you can ask the server to pin a particular channel for you // you can ask the server to pin a particular channel for you
exp.pin = function (channels, cb) { 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); rpc.send('PIN', channels, cb);
}; };
// you can also ask to unpin a particular channel // you can also ask to unpin a particular channel
exp.unpin = function (channels, cb) { 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); 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 // ask the server what it thinks your hash is
exp.getServerHash = function (cb) { exp.getServerHash = function (cb) {
rpc.send('GET_HASH', edPublic, function (e, hash) { rpc.send('GET_HASH', edPublic, function (e, hash) {
@ -67,8 +71,15 @@ define([
}; };
// if local and remote hashes don't match, send a reset // if local and remote hashes don't match, send a reset
exp.reset = function (list, cb) { exp.reset = function (channels, cb) {
rpc.send('RESET', list, function (e, response) { // 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]); cb(e, response[0]);
}); });
}; };
@ -81,7 +92,12 @@ define([
// get the combined size of all channels (in bytes) for all the // get the combined size of all channels (in bytes) for all the
// channels which the server has pinned for your publicKey // channels which the server has pinned for your publicKey
exp.getFileListSize = function (cb) { 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); cb(e, exp);

Loading…
Cancel
Save