-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
128 lines (125 loc) · 4.75 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://bundle.run/noble-secp256k1@1.2.14"></script>
<script src="https://bundle.run/browserify-cipher@1.0.1"></script>
<title>AFSTR</title>
</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));
var event = {
content: "Everything Satoshi is genius!",
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]));
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]));
}); //close this bracket, but we’ll put more stuff in here later
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;
}
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;
}
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));
}
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>