forked from supertestnet/nostr-workshop-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
114 lines (111 loc) · 5.33 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://bundle.run/noble-secp256k1@1.2.14"></script>
<script src="https://bundle.run/browserify-cipher@1.0.1"></script>
</head>
<body>
<script>
var hexToBytes = hex => Uint8Array.from( hex.match( /.{1,2}/g ).map( byte => parseInt( byte, 16 ) ) );
var bytesToHex = bytes => bytes.reduce( ( str, byte ) => str + byte.toString( 16 ).padStart( 2, "0" ), "" );
var { getSharedSecret, schnorr, utils } = nobleSecp256k1;
var sha256 = nobleSecp256k1.utils.sha256;
var privKey = bytesToHex( nobleSecp256k1.utils.randomPrivateKey() );
var pubKey = nobleSecp256k1.getPublicKey(privKey, true);
pubKey = pubKey.substring( 2 );
console.log( pubKey );
var relay = "wss://relay.damus.io";
var socket = new WebSocket( relay );
socket.addEventListener('message', async function( message ) {
var [ type, subId, event ] = JSON.parse( message.data );
var { kind, content } = event || {}
if (!event || event === true) return;
console.log('message:', event);
if (kind === 4) {
content = await decrypt(privKey, event.pubkey, content);
}
console.log('content:', content);
});
socket.addEventListener('open', async function( e ) {
console.log( "connected to " + relay );
var subId = bytesToHex( nobleSecp256k1.utils.randomPrivateKey() ).substring( 0, 16 );
var filter = { "authors": [ pubKey ] }
var subscription = [ "REQ", subId, filter ]
console.log('Subscription:', subscription);
socket.send(JSON.stringify( subscription ));
//put this stuff in the “open” event listener from earlier
var event = {
"content" : "this workshop is awesome!",
"created_at" : Math.floor( Date.now() / 1000 ),
"kind" : 1,
"tags" : [],
"pubkey" : pubKey,
}
var signedEvent = await getSignedEvent(event, privKey);
console.log('signedEvent:', signedEvent);
socket.send(JSON.stringify([ "EVENT", signedEvent ]));
//put this stuff in the “open” event listener from earlier
var message = "this message is super secret!"
var encrypted = encrypt( privKey, pubKey, message )
var event2 = {
"content" : encrypted,
"created_at" : Math.floor( Date.now() / 1000 ),
"kind" : 4,
"tags" : [ [ 'p', pubKey ] ],
"pubkey" : pubKey,
}
var signedEvent2 = await getSignedEvent(event2, privKey);
socket.send(JSON.stringify([ "EVENT", signedEvent2 ]));
});
//put this right above your closing script tag
async function getSignedEvent(event, privateKey) {
var eventData = JSON.stringify([
0, // Reserved for future use
event['pubkey'], // The sender's public key
event['created_at'], // Unix timestamp
event['kind'], // Message “kind” or type
event['tags'], // Tags identify replies/recipients
event['content'] // Your note contents
]);
event.id = bytesToHex( await sha256( ( new TextEncoder().encode( eventData ) ) ) );
event.sig = await schnorr.sign( event.id, privateKey );
return event;
}
//put this right above your closing script tag
function base64ToHex( str ) {
var raw = atob( str );
var result = '';
var i; for ( i=0; i<raw.length; i++ ) {
var hex = raw.charCodeAt( i ).toString( 16 );
result += ( hex.length === 2 ? hex : '0' + hex );
}
return result;
}
//put this right above your closing script tag
function encrypt( privkey, pubkey, text ) {
var key = nobleSecp256k1.getSharedSecret( privkey, '02' + pubkey, true ).substring( 2 );
var iv = window.crypto.getRandomValues(new Uint8Array(16));
var cipher = browserifyCipher.createCipheriv( 'aes-256-cbc', hexToBytes( key ), iv );
var encryptedMessage = cipher.update(text,"utf8","base64");
emsg = encryptedMessage + cipher.final( "base64" );
var uint8View = new Uint8Array( iv.buffer );
var decoder = new TextDecoder();
return emsg + "?iv=" + btoa( String.fromCharCode.apply( null, uint8View ) );
}
//put this right above your closing script tag
function decrypt( privkey, pubkey, ciphertext ) {
var [ emsg, iv ] = ciphertext.split( "?iv=" );
var key = nobleSecp256k1.getSharedSecret( privkey, '02' + pubkey, true ).substring( 2 );
var decipher = browserifyCipher.createDecipheriv(
'aes-256-cbc',
hexToBytes( key ),
hexToBytes( base64ToHex( iv ) )
);
var decryptedMessage = decipher.update( emsg, "base64" );
dmsg = decryptedMessage + decipher.final( "utf8" );
return dmsg;
}
</script>
</body>
</html>