Skip to content

Instantly share code, notes, and snippets.

@daviddahl
Created October 20, 2014 15:57
Show Gist options
  • Save daviddahl/ba14de9bae8ed43a533d to your computer and use it in GitHub Desktop.
Save daviddahl/ba14de9bae8ed43a533d to your computer and use it in GitHub Desktop.
var app = {};
app.createUser = function(user, password) {
crypton.generateAccount(user, password, function (err, account){
if (err) { console.error(err); return;}
console.log(account);
})
};
app.auth = function (username, password) {
crypton.authorize(username, password, function (err, session) {
if (err) { console.error(err); return;}
app.session = session;
console.log(session);
});
};
app.getPeer = function (name) {
app.session.getPeer(name, function(err, peer){
if (err) { console.error(err); return;}
app.peer = peer;
console.log(peer);
});
};
app.trustPeer = function (peer) {
peer.trust(function (err) {
if (err) {console.error(err); return;}
console.log('peer is trusted');
});
};
app.sendMessage = function (peerName, textMessage) {
app.session.getPeer(peerName, function (err, peer) {
if (err) {console.error(err); return;}
if (!peer.trusted) {
console.error('peer not trusted!');
return;
}
var headers = {testAppMessage: true};
var payload = {content: textMessage};
peer.sendMessage(headers, payload, function (err, messageId){
if (err) { console.error(err); return;}
console.log('message ID: ', messageId);
});
});
};
app.getMessages = function () {
app.session.inbox.poll(function (rawMessages) {
console.log(rawMessages);
});
};
app.createContainer = function (name) {
app.session.create(name, function (err, container) {
if (err) { console.error(err); return;}
console.log('container created');
});
};
app.loadContainer = function (name) {
app.session.load(name, function (err, container) {
if (err) { console.error(err); return;}
app.currentContainer = container;
});
};
app.loadHMACContainer = function (hmac, peer) {
app.session.loadWithHmac(hmac, peer, function (err, container) {
if (err) { console.error(err); return;}
app.currentContainer = container;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment