forked from jb55/nostr-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (139 loc) · 3.92 KB
/
index.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
const Relay = require('./lib/relay')
const RelayPool = require('./lib/relay-pool')
const noble = require('noble-secp256k1')
const crypto = require('crypto')
async function signId(privkey, id) {
return await noble.schnorr.sign(id, privkey)
}
function utf8_encode(txt) {
if (typeof TextEncoder !== 'undefined' && TextEncoder) {
const encoder = new TextEncoder()
return encoder.encode(txt)
} else {
const util = require('util');
const encoder = new util.TextEncoder('utf-8');
return encoder.encode(txt)
}
}
async function calculateId(ev) {
const commit = eventCommitment(ev)
const sha256 = noble.utils.sha256;
const buf = utf8_encode(commit);
return hexEncode(await sha256(buf))
}
function eventCommitment(ev) {
const {pubkey,created_at,kind,tags,content} = ev
return JSON.stringify([0, pubkey, created_at, kind, tags, content])
}
function delegationCommitment(pk, conditions) {
return `nostr:delegation:${pk}:${conditions}`
}
async function signDelegationToken(privkey, unsigned_token)
{
const hash = hexEncode(await noble.utils.sha256(unsigned_token))
return (await signId(privkey, hash))
}
async function createDelegation(privkey, publisherPubkey, conditions) {
const pubkey = getPublicKey(privkey)
const unsigned_token = delegationCommitment(publisherPubkey, conditions)
const token = await signDelegationToken(privkey, unsigned_token)
return {pubkey, publisherPubkey, conditions, token}
}
function createDelegationTag(delegation) {
const { pubkey, conditions, token } = delegation
return ["delegation", pubkey, conditions, token]
}
function upsert_delegation_tag(tags, delegation)
{
let found = false
for (const tag of tags) {
if (tag.length >= 4 && tag[0] === "delegation") {
tag[1] = delegation.pubkey
tag[2] = delegation.conditions
tag[3] = delegation.token
return
}
}
tags.push(createDelegationTag(delegation))
}
async function createDelegationEvent(publisher_privkey, ev, delegation) {
let tags = ev.tags || []
upsert_delegation_tag(tags, delegation)
ev.tags = tags
ev.pubkey = delegation.publisherPubkey
ev.id = await calculateId(ev)
ev.sig = await signId(publisher_privkey, ev.id)
return ev
}
function hexChar(val) {
if (val < 10)
return String.fromCharCode(48 + val)
if (val < 16)
return String.fromCharCode(97 + val - 10)
}
function hexEncode(buf) {
let str = ""
for (let i = 0; i < buf.length; i++) {
const c = buf[i]
str += hexChar(c >> 4)
str += hexChar(c & 0xF)
}
return str
}
function base64_decode(str)
{
if (typeof Buffer !== 'undefined' && Buffer) {
return Buffer.from(str, 'base64')
} else if (typeof atob !== 'undefined' && atob) {
return atob(str)
}
throw new Error("no base64 implementation")
}
function encryptDm(privkey, to, msg) {
const shared_point = noble.getSharedSecret(privkey, '02' + to)
const shared_x = shared_point.substr(2, 64)
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(
'aes-256-cbc',
Buffer.from(shared_x, 'hex'),
iv
)
let encrypted = cipher.update(msg, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted + "?iv=" + iv.toString('base64')
}
function decryptDm(privkey, ev) {
let [enc, iv] = ev.content.split("?")
if (!iv || !enc)
return
iv = iv.slice(3)
iv = base64_decode(iv)
const shared_point = noble.getSharedSecret(privkey, '02' + ev.pubkey)
const shared_x = shared_point.substr(2, 64)
const decipher = crypto.createDecipheriv(
'aes-256-cbc',
Buffer.from(shared_x, 'hex'),
iv
)
let decrypted = decipher.update(enc, "base64", "utf8")
decrypted += decipher.final("utf8")
return decrypted
}
function getPublicKey(privkey) {
return noble.schnorr.getPublicKey(privkey)
}
module.exports = {
Relay,
RelayPool,
signId,
calculateId,
getPublicKey,
decryptDm,
encryptDm,
delegationCommitment,
createDelegationTag,
createDelegationEvent,
createDelegation,
signDelegationToken,
eventCommitment
}