nostr-bray 1.9.1 → 1.9.2

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/dist/context.d.ts CHANGED
@@ -24,8 +24,11 @@ export declare class IdentityContext {
24
24
  getSigningFunction(): SignFn;
25
25
  /** List all known identities — returns public info only, never private keys */
26
26
  listIdentities(): PublicIdentity[];
27
- /** Create a linkage proof for the active identity */
27
+ /** Create a linkage proof for the active identity.
28
+ * Only works for derived identities — the master IS the raw key, not a tree child. */
28
29
  prove(mode?: 'blind' | 'full'): LinkageProof;
30
+ /** Get the nsec-tree root's master pubkey (the derivation anchor, distinct from the raw key's pubkey) */
31
+ get treeRootPubkey(): string;
29
32
  /** Get the active identity's private key for NIP-17/44/04 crypto operations */
30
33
  get activePrivateKey(): Uint8Array;
31
34
  /** Test helper — returns reference to private key bytes for verifying zeroise */
package/dist/context.js CHANGED
@@ -1,7 +1,21 @@
1
1
  import { fromNsec, fromMnemonic, derive, zeroise } from 'nsec-tree';
2
2
  import { derivePersona } from 'nsec-tree/persona';
3
3
  import { createBlindProof, createFullProof } from 'nsec-tree/proof';
4
- import { finalizeEvent } from 'nostr-tools/pure';
4
+ import { finalizeEvent, getPublicKey } from 'nostr-tools/pure';
5
+ import { decode, npubEncode, nsecEncode } from 'nostr-tools/nip19';
6
+ /** Build an Identity-compatible object from raw key bytes */
7
+ function rawIdentity(privateKey) {
8
+ const publicKeyHex = getPublicKey(privateKey);
9
+ const publicKey = Buffer.from(publicKeyHex, 'hex');
10
+ return {
11
+ nsec: nsecEncode(privateKey),
12
+ npub: npubEncode(publicKeyHex),
13
+ privateKey: new Uint8Array(privateKey), // copy so original can be cleaned
14
+ publicKey: new Uint8Array(publicKey),
15
+ purpose: 'master',
16
+ index: 0,
17
+ };
18
+ }
5
19
  export class IdentityContext {
6
20
  root;
7
21
  cache = new Map();
@@ -10,25 +24,33 @@ export class IdentityContext {
10
24
  maxCacheSize;
11
25
  constructor(secretKey, format, opts) {
12
26
  this.maxCacheSize = opts?.maxCache ?? 5;
27
+ // Parse the raw secret key bytes — this IS the user's actual Nostr identity
28
+ let rawKeyBytes;
13
29
  if (format === 'mnemonic') {
30
+ // Mnemonic: create tree root, derive a "default" identity as master
14
31
  this.root = fromMnemonic(secretKey);
32
+ const derived = derive(this.root, 'master', 0);
33
+ this.masterEntry = { identity: derived, purpose: 'master', index: 0, lastUsed: Date.now() };
34
+ this.activeEntry = this.masterEntry;
35
+ return;
15
36
  }
16
37
  else if (format === 'hex') {
17
- const bytes = Buffer.from(secretKey, 'hex');
18
- this.root = fromNsec(bytes);
38
+ rawKeyBytes = Buffer.from(secretKey, 'hex');
19
39
  }
20
40
  else {
21
- this.root = fromNsec(secretKey);
41
+ // nsec bech32
42
+ rawKeyBytes = decode(secretKey).data;
22
43
  }
23
- // Derive master identity — kept separate from LRU cache
24
- const masterIdentity = derive(this.root, 'master', 0);
44
+ // Master identity = the user's actual keypair (their real npub)
25
45
  this.masterEntry = {
26
- identity: masterIdentity,
46
+ identity: rawIdentity(rawKeyBytes),
27
47
  purpose: 'master',
28
48
  index: 0,
29
49
  lastUsed: Date.now(),
30
50
  };
31
51
  this.activeEntry = this.masterEntry;
52
+ // nsec-tree root for child derivation
53
+ this.root = fromNsec(rawKeyBytes);
32
54
  }
33
55
  /** Current active identity's npub (bech32) */
34
56
  get activeNpub() {
@@ -131,14 +153,22 @@ export class IdentityContext {
131
153
  }
132
154
  return result;
133
155
  }
134
- /** Create a linkage proof for the active identity */
156
+ /** Create a linkage proof for the active identity.
157
+ * Only works for derived identities — the master IS the raw key, not a tree child. */
135
158
  prove(mode = 'blind') {
159
+ if (this.activeEntry === this.masterEntry) {
160
+ throw new Error('Cannot prove master identity — it is the raw key, not a derived child. Switch to a derived identity first.');
161
+ }
136
162
  const child = this.activeEntry.identity;
137
163
  if (mode === 'full') {
138
164
  return createFullProof(this.root, child);
139
165
  }
140
166
  return createBlindProof(this.root, child);
141
167
  }
168
+ /** Get the nsec-tree root's master pubkey (the derivation anchor, distinct from the raw key's pubkey) */
169
+ get treeRootPubkey() {
170
+ return this.root.masterPubkey;
171
+ }
142
172
  /** Get the active identity's private key for NIP-17/44/04 crypto operations */
143
173
  get activePrivateKey() {
144
174
  return this.activeEntry.identity.privateKey;
@@ -67,18 +67,23 @@ export async function handleIdentityMigrate(ctx, pool, args) {
67
67
  }
68
68
  // Execute migration: re-sign migratable events
69
69
  await handleIdentityRestore(ctx, pool, backup);
70
- // Publish linkage proof connecting old → new
71
- const proof = ctx.prove('full');
72
- const sign = ctx.getSigningFunction();
73
- const proofEvent = await sign({
74
- kind: 30078,
75
- created_at: Math.floor(Date.now() / 1000),
76
- tags: [
77
- ['d', `migration:${args.oldPubkeyHex}`],
78
- ['p', args.oldPubkeyHex],
79
- ],
80
- content: JSON.stringify(proof),
81
- });
82
- await pool.publish(args.oldNpub, proofEvent);
70
+ // Publish linkage proof connecting old → new (only if operating as a derived identity)
71
+ try {
72
+ const proof = ctx.prove('full');
73
+ const sign = ctx.getSigningFunction();
74
+ const proofEvent = await sign({
75
+ kind: 30078,
76
+ created_at: Math.floor(Date.now() / 1000),
77
+ tags: [
78
+ ['d', `migration:${args.oldPubkeyHex}`],
79
+ ['p', args.oldPubkeyHex],
80
+ ],
81
+ content: JSON.stringify(proof),
82
+ });
83
+ await pool.publish(args.oldNpub, proofEvent);
84
+ }
85
+ catch {
86
+ // Master identity can't produce tree proofs — migration proceeds without linkage proof
87
+ }
83
88
  return { status: 'migrated', summary };
84
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nostr-bray",
3
- "version": "1.9.1",
3
+ "version": "1.9.2",
4
4
  "description": "MCP server giving AI agents sovereign Nostr identities — key derivation, personas, attestations, duress detection, zaps",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",