Tutorials ‐ Signatures - HelloZeroNet/Plugin-PeerMessage GitHub Wiki
This tutorial is splitted into 3 parts, each adding more features. If you just want to get correct code, check out the last part.
This is what we'll get when we finish this tutorial:
+---------------------+ +------+
[email protected]: | | | Send |
+---------------------+ +------+
web/[email protected]: Hello!
Identity address
- Let's create a site and
p2p.json
, like specified in Tutorials ‐ Simple chat. - Change
test.js
:
zeroFrame = new ZeroFrame();
function sendMessage(text) {
// send message "text" to everybody and sign it with your identity address
zeroFrame.cmd("peerBroadcast", {text, privatekey: false});
}
zeroFrame.onRequest = function(cmd, message) {
if(cmd == "peerReceive") {
zeroFrame.cmd("peerValid", [message.params.hash]); // This message is valid - broadcast it to other peers
let text = message.params.message; // Get message itself
let identityAddress = message.params.signed_by; // and identity address
console.log(identityAddress + ":", text); // and print them
}
}
Now you'll also see other's identity address in console.
Login via ZeroID
However, the identity address you see is just random - it's generated for this very site, not by ZeroID or another auth provider. So, let's add login via ZeroID:
zeroFrame = new ZeroFrame();
// Login to ZeroID
zeroFrame.cmd("certSelect", ["zeroid.bit"](/HelloZeroNet/Plugin-PeerMessage/wiki/"zeroid.bit"));
function sendMessage(text) {
zeroFrame.cmd("peerBroadcast", {text, privatekey: false});
}
zeroFrame.onRequest = function(cmd, message) {
if(cmd == "peerReceive") {
zeroFrame.cmd("peerValid", [message.params.hash]);
let text = message.params.message;
let identityAddress = message.params.signed_by;
console.log(identityAddress + ":", text);
}
}
Now you will be asked to log in to ZeroID, and when you do, you can send messages with your ZeroID auth address (e.g. 1Cy3ntkN2GN9MH6EaW6eHpi4YoRS2nK5Di
- [email protected]
, not 1PYMrwZdN79PCEvjZXo6kyfPYCc2E3AFQR
- some random string).
Usernames
Now let's show usernames instead of identity addresses.
- To do so, we must add another property to p2p.json:
{
"filter": ".*",
"cert_signers": {
"zeroid.bit": ["1iD5ZQJMNXu43w1qLB8sfdHVKppVMduGz"]
}
}
If you're going to support kaffie.id and other ID providers, add them as well.
- Now change
test.js
again:
zeroFrame = new ZeroFrame();
zeroFrame.cmd("certSelect", ["zeroid.bit"](/HelloZeroNet/Plugin-PeerMessage/wiki/"zeroid.bit"));
function sendMessage(text) {
zeroFrame.cmd("peerBroadcast", {text, privatekey: false});
}
zeroFrame.onRequest = function(cmd, message) {
if(cmd == "peerReceive") {
zeroFrame.cmd("peerValid", [message.params.hash]);
let text = message.params.message;
let userName = message.params.cert; // get ZeroID
console.log(userName + ":", text); // and show it instead of identity address
}
}