bitcore-node-zcash/examples/messages.html

123 lines
2.9 KiB
HTML
Raw Normal View History

2014-07-31 12:07:14 -07:00
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
form {
background: #000;
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
form input {
border: 0;
padding: 10px;
2014-08-01 09:26:17 -07:00
width: 40%;
2014-07-31 12:07:14 -07:00
margin-right: .5%;
}
form button {
width: 9%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin: 0;
padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
</style>
</head>
<body>
2014-08-01 15:00:52 -07:00
<h3 id="pubkey">Generating public key...</h3>
2014-07-31 12:07:14 -07:00
<ul id="messages"></ul>
<form action="">
2014-08-01 15:00:52 -07:00
<input id="other" placeholder="other user's key" autocomplete="off" />
2014-08-01 09:26:17 -07:00
<input id="m" placeholder="Text message..." autocomplete="off" />
2014-07-31 12:07:14 -07:00
<button>Send</button>
</form>
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
2014-08-01 15:00:52 -07:00
<script src="./bitcore.js"></script>
2014-07-31 12:07:14 -07:00
<script>
2014-08-05 09:04:38 -07:00
var fixedPK = prompt('Enter your private key or leave empty to generate one', 'a52e54d90c1ff4846e6f164e56405094cc96ecc0ea7732831e24418204c8ab55');
2014-08-01 09:26:17 -07:00
$(document).ready(function()
2014-07-31 12:07:14 -07:00
{
2014-08-01 09:26:17 -07:00
// load dependencies
var socket = io('http://localhost:3001');
var bitcore = require('bitcore');
var util = bitcore.util;
var Key = bitcore.Key;
2014-08-01 15:00:52 -07:00
var AuthMessage = bitcore.AuthMessage;
var Buffer = bitcore.Buffer;
2014-08-01 09:26:17 -07:00
// generate new identity
var pk = Key.generateSync();
2014-08-05 09:04:38 -07:00
if (fixedPK) {
pk.private = new Buffer(fixedPK, 'hex');
pk.regenerateSync();
}
console.log(pk.private.toString('hex'));
2014-08-01 15:00:52 -07:00
var pubkey = pk.public.toString('hex');
$('#pubkey').text('Your key: '+pubkey);
2014-08-01 09:26:17 -07:00
// show message
var show = function(from, text) {
$('#messages').append($('<li>').text(from+': ' + text));
};
2014-08-01 09:26:17 -07:00
// send chat handler
$('form').submit(function(e)
2014-07-31 12:07:14 -07:00
{
e.preventDefault();
2014-08-01 15:00:52 -07:00
var text = $('#m').val()
if (text.length === 0) {
return;
2014-08-01 09:26:17 -07:00
}
2014-08-01 15:00:52 -07:00
var otherPubkey = $('#other').val();
var data = AuthMessage.encode(otherPubkey, pk, text);
2014-08-01 09:26:17 -07:00
socket.emit('message', data);
show('You', text);
2014-08-01 09:26:17 -07:00
$('#m').val('');
return;
2014-08-01 09:26:17 -07:00
});
// receive chat handler
2014-08-01 15:00:52 -07:00
socket.emit('subscribe', pubkey);
2014-08-05 11:58:08 -07:00
var ts = undefined; // timestamp to sync, undefined syncs all
socket.emit('sync', ts);
2014-08-01 09:26:17 -07:00
socket.on('message', function(msg)
{
try {
var data = AuthMessage.decode(pk, msg);
} catch(e) {
alert(e);
}
show(msg.pubkey, data.payload);
2014-07-31 12:07:14 -07:00
});
});
</script>
</body>
</html>