This module presents a simple high-level API for using telehash for both node and browserify.
The browser crypto that powers this is only possible thanks to the incredible work done by the team behind Forge, Tom Wu, and the Sanford Javascript Crypto Library.
Telehash apps always need one or more seeds to bootstrap from, the default development testing ones are in seeds.json. You can run your own seed via npm start
or manually via node seed.js
. The JSON object from the seed can be passed in to the init function (shown below) as "seeds":{...} in the args or stored in a seeds.json file with that passed in.
To create a new hashname:
var th = require("telehash");
th.init({}, function(err, self){
if(err) return console.log("hashname generation/startup failed",err);
// self.id contains a the public/private keys and parts
});
Needs an id object from a previously created self.id to load the existing hashname from:
var th = require("telehash");
th.init({id:id}, function(err, self){
if(err) return console.log("hashname failed to come online",err);
// use self.* now
});
The first object passed in to the load
function takes the following arguments:
- id - An object previously created, or a string pointing to a file to load the object from.
- seeds - An object in the seeds format, or a string pointing to a file to load from.
Once you have a hashname running you can use the common API with it.
Below is a trivial example, taking off from a properly initialized switch. For an "In the wild" example, see ndn-telehashTransport, which allows one to use telehash for named data networking.
channelName = "AWESOME"
funcion packetHandler(err, packet, chan, callback){
//check for error
if (err) return console.log("oh noes, we got an error! ", err);
//packet.js and packet.body are your json message and binary data(if present)
console.log(packet.js, packet.body) // {myName: "Steve"}, null <-- see firstPacket below
//call the callback with true to send an ack and recieve subsequent packets
callback(true)
//send a response
var response = {
js: {
isAwesome: true,
ownsCat: false,
interests: [
"telehash",
"dogs",
"bad horror movies"
]
}
body: myPreconstrucedBuffer //some Uint8Array.buffer or node.js Buffer.buffer
}
chan.send(response)
}
//begin listening for incoming packets on a channel named 'AWESOME'
self.listen(channelName, packetHandler)
//start a channel to another hashname, assuming they have the listener above
//(hashname given generated by use of keyboard smashing, you should use a real hashname)
firstPacket = {
js: {
myName: "Steve"
}
}
targetHashname = "fj04f4mc5405085mq043q04c48u5mc045mc09mwq098m4c03m084c50493"
self.start(targetHashname, channelName, firstPacket, packetHandler)