nostr-tools 1.8.4 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/nostr.mjs +52 -39
- package/lib/esm/nostr.mjs.map +4 -4
- package/lib/nip19.d.ts +1 -0
- package/lib/nostr.bundle.js +2060 -1472
- package/lib/nostr.bundle.js.map +4 -4
- package/lib/nostr.cjs.js +54 -47
- package/lib/nostr.cjs.js.map +3 -3
- package/package.json +5 -5
package/lib/esm/nostr.mjs
CHANGED
|
@@ -5,17 +5,19 @@ var __export = (target, all) => {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
// keys.ts
|
|
8
|
-
import
|
|
8
|
+
import { schnorr } from "@noble/curves/secp256k1";
|
|
9
|
+
import { bytesToHex } from "@noble/hashes/utils";
|
|
9
10
|
function generatePrivateKey() {
|
|
10
|
-
return
|
|
11
|
+
return bytesToHex(schnorr.utils.randomPrivateKey());
|
|
11
12
|
}
|
|
12
13
|
function getPublicKey(privateKey) {
|
|
13
|
-
return
|
|
14
|
+
return bytesToHex(schnorr.getPublicKey(privateKey));
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
// event.ts
|
|
17
|
-
import
|
|
18
|
+
import { schnorr as schnorr2 } from "@noble/curves/secp256k1";
|
|
18
19
|
import { sha256 } from "@noble/hashes/sha256";
|
|
20
|
+
import { bytesToHex as bytesToHex2 } from "@noble/hashes/utils";
|
|
19
21
|
|
|
20
22
|
// utils.ts
|
|
21
23
|
var utils_exports = {};
|
|
@@ -166,7 +168,7 @@ function serializeEvent(evt) {
|
|
|
166
168
|
}
|
|
167
169
|
function getEventHash(event) {
|
|
168
170
|
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)));
|
|
169
|
-
return
|
|
171
|
+
return bytesToHex2(eventHash);
|
|
170
172
|
}
|
|
171
173
|
var isRecord = (obj) => obj instanceof Object;
|
|
172
174
|
function validateEvent(event) {
|
|
@@ -196,15 +198,15 @@ function validateEvent(event) {
|
|
|
196
198
|
return true;
|
|
197
199
|
}
|
|
198
200
|
function verifySignature(event) {
|
|
199
|
-
return
|
|
201
|
+
return schnorr2.verify(
|
|
200
202
|
event.sig,
|
|
201
203
|
getEventHash(event),
|
|
202
204
|
event.pubkey
|
|
203
205
|
);
|
|
204
206
|
}
|
|
205
207
|
function signEvent(event, key) {
|
|
206
|
-
return
|
|
207
|
-
|
|
208
|
+
return bytesToHex2(
|
|
209
|
+
schnorr2.sign(getEventHash(event), key)
|
|
208
210
|
);
|
|
209
211
|
}
|
|
210
212
|
|
|
@@ -721,9 +723,10 @@ __export(nip19_exports, {
|
|
|
721
723
|
noteEncode: () => noteEncode,
|
|
722
724
|
nprofileEncode: () => nprofileEncode,
|
|
723
725
|
npubEncode: () => npubEncode,
|
|
726
|
+
nrelayEncode: () => nrelayEncode,
|
|
724
727
|
nsecEncode: () => nsecEncode
|
|
725
728
|
});
|
|
726
|
-
import
|
|
729
|
+
import { bytesToHex as bytesToHex3, concatBytes, hexToBytes } from "@noble/hashes/utils";
|
|
727
730
|
import { bech32 } from "@scure/base";
|
|
728
731
|
var Bech32MaxSize = 5e3;
|
|
729
732
|
function decode(nip19) {
|
|
@@ -739,7 +742,7 @@ function decode(nip19) {
|
|
|
739
742
|
return {
|
|
740
743
|
type: "nprofile",
|
|
741
744
|
data: {
|
|
742
|
-
pubkey:
|
|
745
|
+
pubkey: bytesToHex3(tlv[0][0]),
|
|
743
746
|
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : []
|
|
744
747
|
}
|
|
745
748
|
};
|
|
@@ -755,9 +758,9 @@ function decode(nip19) {
|
|
|
755
758
|
return {
|
|
756
759
|
type: "nevent",
|
|
757
760
|
data: {
|
|
758
|
-
id:
|
|
761
|
+
id: bytesToHex3(tlv[0][0]),
|
|
759
762
|
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [],
|
|
760
|
-
author: tlv[2]?.[0] ?
|
|
763
|
+
author: tlv[2]?.[0] ? bytesToHex3(tlv[2][0]) : void 0
|
|
761
764
|
}
|
|
762
765
|
};
|
|
763
766
|
}
|
|
@@ -777,16 +780,25 @@ function decode(nip19) {
|
|
|
777
780
|
type: "naddr",
|
|
778
781
|
data: {
|
|
779
782
|
identifier: utf8Decoder.decode(tlv[0][0]),
|
|
780
|
-
pubkey:
|
|
781
|
-
kind: parseInt(
|
|
783
|
+
pubkey: bytesToHex3(tlv[2][0]),
|
|
784
|
+
kind: parseInt(bytesToHex3(tlv[3][0]), 16),
|
|
782
785
|
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : []
|
|
783
786
|
}
|
|
784
787
|
};
|
|
785
788
|
}
|
|
789
|
+
case "nrelay": {
|
|
790
|
+
let tlv = parseTLV(data);
|
|
791
|
+
if (!tlv[0]?.[0])
|
|
792
|
+
throw new Error("missing TLV 0 for nrelay");
|
|
793
|
+
return {
|
|
794
|
+
type: "nrelay",
|
|
795
|
+
data: utf8Decoder.decode(tlv[0][0])
|
|
796
|
+
};
|
|
797
|
+
}
|
|
786
798
|
case "nsec":
|
|
787
799
|
case "npub":
|
|
788
800
|
case "note":
|
|
789
|
-
return { type: prefix, data:
|
|
801
|
+
return { type: prefix, data: bytesToHex3(data) };
|
|
790
802
|
default:
|
|
791
803
|
throw new Error(`unknown prefix ${prefix}`);
|
|
792
804
|
}
|
|
@@ -816,13 +828,13 @@ function noteEncode(hex) {
|
|
|
816
828
|
return encodeBytes("note", hex);
|
|
817
829
|
}
|
|
818
830
|
function encodeBytes(prefix, hex) {
|
|
819
|
-
let data =
|
|
831
|
+
let data = hexToBytes(hex);
|
|
820
832
|
let words = bech32.toWords(data);
|
|
821
833
|
return bech32.encode(prefix, words, Bech32MaxSize);
|
|
822
834
|
}
|
|
823
835
|
function nprofileEncode(profile) {
|
|
824
836
|
let data = encodeTLV({
|
|
825
|
-
0: [
|
|
837
|
+
0: [hexToBytes(profile.pubkey)],
|
|
826
838
|
1: (profile.relays || []).map((url) => utf8Encoder.encode(url))
|
|
827
839
|
});
|
|
828
840
|
let words = bech32.toWords(data);
|
|
@@ -830,9 +842,9 @@ function nprofileEncode(profile) {
|
|
|
830
842
|
}
|
|
831
843
|
function neventEncode(event) {
|
|
832
844
|
let data = encodeTLV({
|
|
833
|
-
0: [
|
|
845
|
+
0: [hexToBytes(event.id)],
|
|
834
846
|
1: (event.relays || []).map((url) => utf8Encoder.encode(url)),
|
|
835
|
-
2: event.author ? [
|
|
847
|
+
2: event.author ? [hexToBytes(event.author)] : []
|
|
836
848
|
});
|
|
837
849
|
let words = bech32.toWords(data);
|
|
838
850
|
return bech32.encode("nevent", words, Bech32MaxSize);
|
|
@@ -843,12 +855,19 @@ function naddrEncode(addr) {
|
|
|
843
855
|
let data = encodeTLV({
|
|
844
856
|
0: [utf8Encoder.encode(addr.identifier)],
|
|
845
857
|
1: (addr.relays || []).map((url) => utf8Encoder.encode(url)),
|
|
846
|
-
2: [
|
|
858
|
+
2: [hexToBytes(addr.pubkey)],
|
|
847
859
|
3: [new Uint8Array(kind)]
|
|
848
860
|
});
|
|
849
861
|
let words = bech32.toWords(data);
|
|
850
862
|
return bech32.encode("naddr", words, Bech32MaxSize);
|
|
851
863
|
}
|
|
864
|
+
function nrelayEncode(url) {
|
|
865
|
+
let data = encodeTLV({
|
|
866
|
+
0: [utf8Encoder.encode(url)]
|
|
867
|
+
});
|
|
868
|
+
let words = bech32.toWords(data);
|
|
869
|
+
return bech32.encode("nrelay", words, Bech32MaxSize);
|
|
870
|
+
}
|
|
852
871
|
function encodeTLV(tlv) {
|
|
853
872
|
let entries = [];
|
|
854
873
|
Object.entries(tlv).forEach(([t, vs]) => {
|
|
@@ -860,7 +879,7 @@ function encodeTLV(tlv) {
|
|
|
860
879
|
entries.push(entry);
|
|
861
880
|
});
|
|
862
881
|
});
|
|
863
|
-
return
|
|
882
|
+
return concatBytes(...entries);
|
|
864
883
|
}
|
|
865
884
|
|
|
866
885
|
// references.ts
|
|
@@ -959,10 +978,10 @@ __export(nip04_exports, {
|
|
|
959
978
|
encrypt: () => encrypt
|
|
960
979
|
});
|
|
961
980
|
import { randomBytes } from "@noble/hashes/utils";
|
|
962
|
-
import
|
|
981
|
+
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
963
982
|
import { base64 } from "@scure/base";
|
|
964
983
|
async function encrypt(privkey, pubkey, text) {
|
|
965
|
-
const key =
|
|
984
|
+
const key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
|
|
966
985
|
const normalizedKey = getNormalizedX(key);
|
|
967
986
|
let iv = Uint8Array.from(randomBytes(16));
|
|
968
987
|
let plaintext = utf8Encoder.encode(text);
|
|
@@ -984,7 +1003,7 @@ async function encrypt(privkey, pubkey, text) {
|
|
|
984
1003
|
}
|
|
985
1004
|
async function decrypt(privkey, pubkey, data) {
|
|
986
1005
|
let [ctb64, ivb64] = data.split("?iv=");
|
|
987
|
-
let key =
|
|
1006
|
+
let key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
|
|
988
1007
|
let normalizedKey = getNormalizedX(key);
|
|
989
1008
|
let cryptoKey = await crypto.subtle.importKey(
|
|
990
1009
|
"raw",
|
|
@@ -1063,7 +1082,7 @@ __export(nip06_exports, {
|
|
|
1063
1082
|
privateKeyFromSeedWords: () => privateKeyFromSeedWords,
|
|
1064
1083
|
validateWords: () => validateWords
|
|
1065
1084
|
});
|
|
1066
|
-
import
|
|
1085
|
+
import { bytesToHex as bytesToHex4 } from "@noble/hashes/utils";
|
|
1067
1086
|
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
1068
1087
|
import {
|
|
1069
1088
|
generateMnemonic,
|
|
@@ -1076,7 +1095,7 @@ function privateKeyFromSeedWords(mnemonic, passphrase) {
|
|
|
1076
1095
|
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey;
|
|
1077
1096
|
if (!privateKey)
|
|
1078
1097
|
throw new Error("could not derive private key");
|
|
1079
|
-
return
|
|
1098
|
+
return bytesToHex4(privateKey);
|
|
1080
1099
|
}
|
|
1081
1100
|
function generateSeedWords() {
|
|
1082
1101
|
return generateMnemonic(wordlist);
|
|
@@ -1148,9 +1167,9 @@ var nip13_exports = {};
|
|
|
1148
1167
|
__export(nip13_exports, {
|
|
1149
1168
|
getPow: () => getPow
|
|
1150
1169
|
});
|
|
1151
|
-
import
|
|
1170
|
+
import { hexToBytes as hexToBytes2 } from "@noble/hashes/utils";
|
|
1152
1171
|
function getPow(id) {
|
|
1153
|
-
return getLeadingZeroBits(
|
|
1172
|
+
return getLeadingZeroBits(hexToBytes2(id));
|
|
1154
1173
|
}
|
|
1155
1174
|
function getLeadingZeroBits(hash) {
|
|
1156
1175
|
let total, i, bits;
|
|
@@ -1180,7 +1199,8 @@ __export(nip26_exports, {
|
|
|
1180
1199
|
createDelegation: () => createDelegation,
|
|
1181
1200
|
getDelegator: () => getDelegator
|
|
1182
1201
|
});
|
|
1183
|
-
import
|
|
1202
|
+
import { schnorr as schnorr3 } from "@noble/curves/secp256k1";
|
|
1203
|
+
import { bytesToHex as bytesToHex5 } from "@noble/hashes/utils";
|
|
1184
1204
|
import { sha256 as sha2562 } from "@noble/hashes/sha256";
|
|
1185
1205
|
function createDelegation(privateKey, parameters) {
|
|
1186
1206
|
let conditions = [];
|
|
@@ -1196,8 +1216,8 @@ function createDelegation(privateKey, parameters) {
|
|
|
1196
1216
|
let sighash = sha2562(
|
|
1197
1217
|
utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`)
|
|
1198
1218
|
);
|
|
1199
|
-
let sig =
|
|
1200
|
-
|
|
1219
|
+
let sig = bytesToHex5(
|
|
1220
|
+
schnorr3.sign(sighash, privateKey)
|
|
1201
1221
|
);
|
|
1202
1222
|
return {
|
|
1203
1223
|
from: getPublicKey(privateKey),
|
|
@@ -1228,7 +1248,7 @@ function getDelegator(event) {
|
|
|
1228
1248
|
let sighash = sha2562(
|
|
1229
1249
|
utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`)
|
|
1230
1250
|
);
|
|
1231
|
-
if (!
|
|
1251
|
+
if (!schnorr3.verify(sig, sighash, pubkey))
|
|
1232
1252
|
return null;
|
|
1233
1253
|
return pubkey;
|
|
1234
1254
|
}
|
|
@@ -1372,13 +1392,6 @@ function makeZapReceipt({
|
|
|
1372
1392
|
}
|
|
1373
1393
|
return zap;
|
|
1374
1394
|
}
|
|
1375
|
-
|
|
1376
|
-
// index.ts
|
|
1377
|
-
import * as secp256k18 from "@noble/secp256k1";
|
|
1378
|
-
import { hmac } from "@noble/hashes/hmac";
|
|
1379
|
-
import { sha256 as sha2563 } from "@noble/hashes/sha256";
|
|
1380
|
-
secp256k18.utils.hmacSha256Sync = (key, ...msgs) => hmac(sha2563, key, secp256k18.utils.concatBytes(...msgs));
|
|
1381
|
-
secp256k18.utils.sha256Sync = (...msgs) => sha2563(secp256k18.utils.concatBytes(...msgs));
|
|
1382
1395
|
export {
|
|
1383
1396
|
Kind,
|
|
1384
1397
|
SimplePool,
|