signet-protocol 0.1.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.
Files changed (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +112 -0
  3. package/dist/anomaly.d.ts +42 -0
  4. package/dist/anomaly.d.ts.map +1 -0
  5. package/dist/anomaly.js +209 -0
  6. package/dist/anomaly.js.map +1 -0
  7. package/dist/badge.d.ts +56 -0
  8. package/dist/badge.d.ts.map +1 -0
  9. package/dist/badge.js +171 -0
  10. package/dist/badge.js.map +1 -0
  11. package/dist/bonds.d.ts +39 -0
  12. package/dist/bonds.d.ts.map +1 -0
  13. package/dist/bonds.js +149 -0
  14. package/dist/bonds.js.map +1 -0
  15. package/dist/challenges.d.ts +18 -0
  16. package/dist/challenges.d.ts.map +1 -0
  17. package/dist/challenges.js +145 -0
  18. package/dist/challenges.js.map +1 -0
  19. package/dist/cold-call.d.ts +74 -0
  20. package/dist/cold-call.d.ts.map +1 -0
  21. package/dist/cold-call.js +176 -0
  22. package/dist/cold-call.js.map +1 -0
  23. package/dist/compliance.d.ts +82 -0
  24. package/dist/compliance.d.ts.map +1 -0
  25. package/dist/compliance.js +478 -0
  26. package/dist/compliance.js.map +1 -0
  27. package/dist/connections.d.ts +63 -0
  28. package/dist/connections.d.ts.map +1 -0
  29. package/dist/connections.js +170 -0
  30. package/dist/connections.js.map +1 -0
  31. package/dist/constants.d.ts +86 -0
  32. package/dist/constants.d.ts.map +1 -0
  33. package/dist/constants.js +124 -0
  34. package/dist/constants.js.map +1 -0
  35. package/dist/credentials.d.ts +190 -0
  36. package/dist/credentials.d.ts.map +1 -0
  37. package/dist/credentials.js +686 -0
  38. package/dist/credentials.js.map +1 -0
  39. package/dist/crypto.d.ts +27 -0
  40. package/dist/crypto.d.ts.map +1 -0
  41. package/dist/crypto.js +75 -0
  42. package/dist/crypto.js.map +1 -0
  43. package/dist/errors.d.ts +17 -0
  44. package/dist/errors.d.ts.map +1 -0
  45. package/dist/errors.js +29 -0
  46. package/dist/errors.js.map +1 -0
  47. package/dist/i18n.d.ts +98 -0
  48. package/dist/i18n.d.ts.map +1 -0
  49. package/dist/i18n.js +1118 -0
  50. package/dist/i18n.js.map +1 -0
  51. package/dist/identity-bridge.d.ts +52 -0
  52. package/dist/identity-bridge.d.ts.map +1 -0
  53. package/dist/identity-bridge.js +228 -0
  54. package/dist/identity-bridge.js.map +1 -0
  55. package/dist/identity-tree.d.ts +47 -0
  56. package/dist/identity-tree.d.ts.map +1 -0
  57. package/dist/identity-tree.js +69 -0
  58. package/dist/identity-tree.js.map +1 -0
  59. package/dist/index.d.ts +55 -0
  60. package/dist/index.d.ts.map +1 -0
  61. package/dist/index.js +86 -0
  62. package/dist/index.js.map +1 -0
  63. package/dist/key-derivation.d.ts +43 -0
  64. package/dist/key-derivation.d.ts.map +1 -0
  65. package/dist/key-derivation.js +212 -0
  66. package/dist/key-derivation.js.map +1 -0
  67. package/dist/lsag.d.ts +23 -0
  68. package/dist/lsag.d.ts.map +1 -0
  69. package/dist/lsag.js +35 -0
  70. package/dist/lsag.js.map +1 -0
  71. package/dist/merkle.d.ts +19 -0
  72. package/dist/merkle.d.ts.map +1 -0
  73. package/dist/merkle.js +155 -0
  74. package/dist/merkle.js.map +1 -0
  75. package/dist/policies.d.ts +22 -0
  76. package/dist/policies.d.ts.map +1 -0
  77. package/dist/policies.js +123 -0
  78. package/dist/policies.js.map +1 -0
  79. package/dist/range-proof.d.ts +6 -0
  80. package/dist/range-proof.d.ts.map +1 -0
  81. package/dist/range-proof.js +45 -0
  82. package/dist/range-proof.js.map +1 -0
  83. package/dist/relay.d.ts +106 -0
  84. package/dist/relay.d.ts.map +1 -0
  85. package/dist/relay.js +336 -0
  86. package/dist/relay.js.map +1 -0
  87. package/dist/ring-signature.d.ts +35 -0
  88. package/dist/ring-signature.d.ts.map +1 -0
  89. package/dist/ring-signature.js +56 -0
  90. package/dist/ring-signature.js.map +1 -0
  91. package/dist/shamir.d.ts +55 -0
  92. package/dist/shamir.d.ts.map +1 -0
  93. package/dist/shamir.js +253 -0
  94. package/dist/shamir.js.map +1 -0
  95. package/dist/signet-words.d.ts +42 -0
  96. package/dist/signet-words.d.ts.map +1 -0
  97. package/dist/signet-words.js +82 -0
  98. package/dist/signet-words.js.map +1 -0
  99. package/dist/store.d.ts +65 -0
  100. package/dist/store.d.ts.map +1 -0
  101. package/dist/store.js +290 -0
  102. package/dist/store.js.map +1 -0
  103. package/dist/trust-score.d.ts +9 -0
  104. package/dist/trust-score.d.ts.map +1 -0
  105. package/dist/trust-score.js +186 -0
  106. package/dist/trust-score.js.map +1 -0
  107. package/dist/types.d.ts +358 -0
  108. package/dist/types.d.ts.map +1 -0
  109. package/dist/types.js +15 -0
  110. package/dist/types.js.map +1 -0
  111. package/dist/utils.d.ts +11 -0
  112. package/dist/utils.d.ts.map +1 -0
  113. package/dist/utils.js +21 -0
  114. package/dist/utils.js.map +1 -0
  115. package/dist/validation.d.ts +33 -0
  116. package/dist/validation.d.ts.map +1 -0
  117. package/dist/validation.js +312 -0
  118. package/dist/validation.js.map +1 -0
  119. package/dist/verifiers.d.ts +18 -0
  120. package/dist/verifiers.d.ts.map +1 -0
  121. package/dist/verifiers.js +118 -0
  122. package/dist/verifiers.js.map +1 -0
  123. package/dist/vouches.d.ts +14 -0
  124. package/dist/vouches.d.ts.map +1 -0
  125. package/dist/vouches.js +103 -0
  126. package/dist/vouches.js.map +1 -0
  127. package/package.json +76 -0
  128. package/src/anomaly.ts +307 -0
  129. package/src/badge.ts +208 -0
  130. package/src/bonds.ts +203 -0
  131. package/src/challenges.ts +187 -0
  132. package/src/cold-call.ts +238 -0
  133. package/src/compliance.ts +612 -0
  134. package/src/connections.ts +216 -0
  135. package/src/constants.ts +146 -0
  136. package/src/credentials.ts +908 -0
  137. package/src/crypto.ts +85 -0
  138. package/src/errors.ts +31 -0
  139. package/src/i18n.ts +1347 -0
  140. package/src/identity-bridge.ts +262 -0
  141. package/src/identity-tree.ts +90 -0
  142. package/src/index.ts +452 -0
  143. package/src/lsag.ts +53 -0
  144. package/src/merkle.ts +176 -0
  145. package/src/policies.ts +154 -0
  146. package/src/range-proof.ts +66 -0
  147. package/src/relay.ts +433 -0
  148. package/src/ring-signature.ts +76 -0
  149. package/src/signet-words.ts +122 -0
  150. package/src/store.ts +336 -0
  151. package/src/trust-score.ts +208 -0
  152. package/src/types.ts +482 -0
  153. package/src/utils.ts +20 -0
  154. package/src/validation.ts +391 -0
  155. package/src/verifiers.ts +156 -0
  156. package/src/vouches.ts +141 -0
package/src/crypto.ts ADDED
@@ -0,0 +1,85 @@
1
+ // Signet Cryptographic Utilities
2
+ // Schnorr signatures (BIP-340) on secp256k1, event ID computation
3
+
4
+ import { schnorr } from '@noble/curves/secp256k1.js';
5
+ import { sha256 } from '@noble/hashes/sha2.js';
6
+ import { bytesToHex, hexToBytes, utf8ToBytes } from '@noble/hashes/utils.js';
7
+ import { zeroBytes } from './utils.js';
8
+ import type { UnsignedEvent, NostrEvent } from './types.js';
9
+
10
+ /** Generate a new secp256k1 keypair. Returns { privateKey, publicKey } as hex strings.
11
+ * Public key is x-only (32 bytes) per BIP-340 / Nostr convention.
12
+ *
13
+ * SECURITY NOTE: The Uint8Array is zeroed after conversion, but the returned hex
14
+ * strings are JS primitives and cannot be wiped from memory. This is a fundamental
15
+ * limitation of the JavaScript runtime. Callers handling private keys should minimise
16
+ * their lifetime and avoid logging or serialising them unnecessarily. */
17
+ export function generateKeyPair(): { privateKey: string; publicKey: string } {
18
+ const privateKeyRaw = schnorr.utils.randomSecretKey();
19
+ const publicKey = schnorr.getPublicKey(privateKeyRaw);
20
+ const privateKey = bytesToHex(privateKeyRaw);
21
+ const publicKeyHex = bytesToHex(publicKey);
22
+ zeroBytes(privateKeyRaw);
23
+ return {
24
+ privateKey,
25
+ publicKey: publicKeyHex,
26
+ };
27
+ }
28
+
29
+ /** Get x-only public key (32 bytes hex) from a private key */
30
+ export function getPublicKey(privateKey: string): string {
31
+ return bytesToHex(schnorr.getPublicKey(hexToBytes(privateKey)));
32
+ }
33
+
34
+ /** Serialize a Nostr event for hashing (NIP-01) */
35
+ export function serializeEvent(event: UnsignedEvent): string {
36
+ return JSON.stringify([
37
+ 0,
38
+ event.pubkey,
39
+ event.created_at,
40
+ event.kind,
41
+ event.tags,
42
+ event.content,
43
+ ]);
44
+ }
45
+
46
+ /** Compute the event ID (SHA-256 of the serialized event) */
47
+ export function getEventId(event: UnsignedEvent): string {
48
+ const serialized = serializeEvent(event);
49
+ return bytesToHex(sha256(utf8ToBytes(serialized)));
50
+ }
51
+
52
+ /** Sign an unsigned event, returning a full NostrEvent */
53
+ export async function signEvent(
54
+ event: UnsignedEvent,
55
+ privateKey: string
56
+ ): Promise<NostrEvent> {
57
+ const id = getEventId(event);
58
+ const sig = schnorr.sign(hexToBytes(id), hexToBytes(privateKey));
59
+ return {
60
+ ...event,
61
+ id,
62
+ sig: bytesToHex(sig),
63
+ };
64
+ }
65
+
66
+ /** Verify a signed Nostr event's signature */
67
+ export async function verifyEvent(event: NostrEvent): Promise<boolean> {
68
+ try {
69
+ const expectedId = getEventId(event);
70
+ if (expectedId !== event.id) return false;
71
+ return schnorr.verify(hexToBytes(event.sig), hexToBytes(event.id), hexToBytes(event.pubkey));
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ /** SHA-256 hash of arbitrary data, returned as hex */
78
+ export function hash(data: Uint8Array): string {
79
+ return bytesToHex(sha256(data));
80
+ }
81
+
82
+ /** SHA-256 hash of a UTF-8 string, returned as hex */
83
+ export function hashString(data: string): string {
84
+ return hash(utf8ToBytes(data));
85
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,31 @@
1
+ /** Base error for all Signet protocol errors */
2
+ export class SignetError extends Error {
3
+ constructor(message: string) {
4
+ super(message);
5
+ this.name = 'SignetError';
6
+ }
7
+ }
8
+
9
+ /** Validation errors (malformed events, missing fields, bounds exceeded) */
10
+ export class SignetValidationError extends SignetError {
11
+ constructor(message: string) {
12
+ super(message);
13
+ this.name = 'SignetValidationError';
14
+ }
15
+ }
16
+
17
+ /** Cryptographic errors (invalid keys, failed verification, bad proofs) */
18
+ export class SignetCryptoError extends SignetError {
19
+ constructor(message: string) {
20
+ super(message);
21
+ this.name = 'SignetCryptoError';
22
+ }
23
+ }
24
+
25
+ /** Election/voting errors (election not open, voter not eligible, etc.) */
26
+ export class SignetVotingError extends SignetError {
27
+ constructor(message: string) {
28
+ super(message);
29
+ this.name = 'SignetVotingError';
30
+ }
31
+ }