-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
169 lines (138 loc) · 5.05 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* * * * * * * *
*
* Web socket client, with Nostr code from https://github.com/supertestnet/vanilla-js-nostr
*
* * */
var ws;
function computeRawPrivkey( node ) {
return bitcoinjs.ECPair.fromPrivateKey( node.privateKey, { network: bitcoinjs.networks.mainnet } );
}
function getPrivkeyHex( backupwords, path, index ) {
var seed = bip39.mnemonicToSeedSync( backupwords );
var node = bip32.fromSeed( seed );
var path = "m/" + path + "/" + index;
var root = node;
var child = root.derivePath( path );
return computeRawPrivkey( child );
}
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
function connect(){
try{
ws = new WebSocket(host);
ws.onopen = function () {
var data = "System message: Connection established successfully";
listMsg(data);
subscribe(pubKey);
};
ws.onmessage = function (e) {
console.log('ONMESSAGE',e.data);
var msg = JSON.parse(e.data);
listMsg(msg[2].content);
};
ws.onerror = function () {
var data = "System message: something went wrong, please exit and try again.";
listMsg(data);
};
}catch(ex){
listMsg(ex);
}
$("#chat-message").focus();
}
function disconnect(){
$("#user_list").empty();
listMsg("Goodbye!");
ws.close();
}
function listMsg(data) {
var msg_list = document.getElementById("messages");
var msg = document.createElement("p");
msg.innerHTML = data;
msg_list.appendChild(msg);
msg_list.scrollTop = msg_list.scrollHeight;
}
function connected(){
return ws?true:false;
}
function socketState(){
return ws.readyState;
}
$('body').on('click','#chat-btn-connect',function(e){
if (!connected()) {
connect();
}else if (socketState()==1){
disconnect();
}else if (socketState()==3){
listMsg('WebSocket Desconectado');
connect();
}else{
listMsg('WebSocket - status '+socketState());
}
});
function send( ) {
var message = $('#chat-message').val();
$('#chat-message').val('');
listMsg(' yo > '+ message);
console.log('SEND',message);
var now = Math.floor( ( new Date().getTime() ) / 1000 );
console.log( 'NOW', now );
var newevent = [
0,
pubKey,
now,
1,
[],
message
];
var msgjson = JSON.stringify( newevent ); console.log( "msgjson: '" + msgjson + "'" );
var msghash = bitcoinjs.crypto.sha256( msgjson ).toString( 'hex' ); console.log( "msghash: '" + msghash + "'" );
nobleSecp256k1.schnorr.sign( msghash, privKey ).then(
value => {
sig = value; console.log( "the sig is:", sig );
nobleSecp256k1.schnorr.verify(
sig,
msghash,
pubKey
).then(
value => { console.log( "is the signature valid for the above pubkey over the message 'test'?", value );
if ( value ) {
var fullevent = {
"id": msghash,
"pubkey": pubKey,
"created_at": now,
"kind": 1,
"tags": [],
"content": message,
"sig": sig
}
var sendable = [ "EVENT", fullevent ];
sessionStorage.sendable = JSON.stringify( sendable );
ws.send( '["EVENT",' + JSON.stringify( JSON.parse( sessionStorage.sendable )[ 1 ] ) + ']' );
}
}
);
}
);
}
function subscribe( pubkey ) {
var filter = { "authors": [ pubkey ] };
var subscription = [ "REQ", "Test", filter ];
subscription = JSON.stringify( subscription );
sessionStorage.subscription = subscription;
ws.send( sessionStorage.subscription );
}
$('body').on('click','#chat-btn-send',function(e){
console.log('CLICK','SEND',$('#chat-message').val());
send();
});
$('body').on('keypress','#chat-message',function(e){
code=e.keyCode?e.keyCode:e.which;
if(code.toString()==13){
console.log('KEYPRESS','SEND',$('#chat-message').val());
send();
}
});