rpc all the things

This commit is contained in:
ansuz
2017-04-03 19:24:57 +02:00
parent 4f7e25eeb9
commit a5e5ddae08
4 changed files with 158 additions and 38 deletions

View File

@@ -66,7 +66,7 @@ types of messages:
}
};
var create = function (network, edPrivateKey, edPublicKey) {
var create = function (network, edPrivateKey, edPublicKey, cb) {
var signKey;
try {
@@ -74,15 +74,19 @@ types of messages:
if (signKey.length !== 64) {
throw new Error('private key did not match expected length of 64');
}
} catch (err) { throw err; }
} catch (err) {
return void cb(err);
}
var pubBuffer;
try {
pubBuffer = Nacl.util.decodeBase64(edPublicKey);
if (pubBuffer.length !== 32) {
throw new Error('expected public key to be 32 uint');
return void cb('expected public key to be 32 uint');
}
} catch (err) { throw err; }
} catch (err) {
return void cb(err);
}
var ctx = {
seq: new Date().getTime(),
@@ -97,7 +101,12 @@ types of messages:
var data = [type, msg];
var sig = signMsg(data, signKey);
data.unshift(ctx.cookie); //
if (ctx.cookie && ctx.cookie.join) {
data.unshift(ctx.cookie.join('|')); //
} else {
data.unshift(ctx.cookie);
}
data.unshift(edPublicKey);
data.unshift(sig);
@@ -105,20 +114,21 @@ types of messages:
return sendMsg(ctx, data, cb);
};
var getCookie = function (cb) {
send('COOKIE', "", function (e, msg) {
console.log('cookie message', e, msg);
cb(e, msg);
});
};
network.on('message', function (msg, sender) {
onMsg(ctx, msg);
});
return {
send: send,
ready: getCookie,
};
send('COOKIE', "", function (e, msg) {
if (e) { return void cb(e); }
console.log(msg); // DO something with the returned cookie
ctx.cookie = msg;
cb(void 0, {
send: send,
});
});
};
return { create: create };

View File

@@ -17,15 +17,12 @@ define([
var edPrivate = proxy.edPrivate;
var edPublic = proxy.edPublic;
var rpc = RPC.create(network, edPrivate, edPublic);
var payload = {
a: Math.floor(Math.random() * 1000),
b: 7,
};
rpc.ready(function () {
RPC.create(network, edPrivate, edPublic, function (e, rpc) {
// console.log(payload);
rpc.send('ECHO', payload, function (e, msg) {
if (e) { return void console.error(e); }
@@ -68,6 +65,25 @@ define([
console.log(msg);
});
(function () {
// compute what you think the hash should be
// then ask the server if what it has matches your records
rpc.send('GET_HASH', edPublic, function (e, hash) {
if (e) { return void console.error(e); }
console.log("user pins hash is [%s]", hash);
// if it does, awesome!
// you should be able to pin and unpin things easily
// if it doesn't, send a reset, and start re-pinning
});
}());
if (false) {
(function () {
var bytes = 0;