| … | … | @@ -66,48 +66,56 @@ async function loadState() { |
| 66 | 66 | if (store) return true // Already loaded |
| 67 | 67 | |
| 68 | 68 | // Try to open Corestore - if it fails, storage doesn't exist yet |
| 69 | | let identityCore |
| 70 | 69 | try { |
| 71 | 70 | store = new Corestore(STORAGE_PATH) |
| 72 | 71 | await store.ready() |
| 73 | 72 | |
| 74 | | // Load identity |
| 75 | | identityCore = store.get({ name: 'identity' }) |
| 73 | // Load metadata first (needed for both systems) |
| 74 | metaBee = new Hyperbee(store.get({ name: 'metadata' }), { |
| 75 | keyEncoding: 'utf-8', |
| 76 | valueEncoding: 'json' |
| 77 | }) |
| 78 | await metaBee.ready() |
| 79 | |
| 80 | // Check for ANProto keypair (new system) FIRST |
| 81 | const keypairNode = await metaBee.get('anproto-keypair') |
| 82 | if (keypairNode && keypairNode.value) { |
| 83 | // New ANProto system found |
| 84 | const keypairStr = keypairNode.value |
| 85 | const { publicKey, secretKey } = ANProto.loadKeypair(keypairStr) |
| 86 | anprotoKeypair = { publicKey, secretKey } |
| 87 | |
| 88 | // Load feed using ANProto keypair |
| 89 | myFeed = store.get({ |
| 90 | publicKey: anprotoKeypair.publicKey, |
| 91 | secretKey: anprotoKeypair.secretKey |
| 92 | }) |
| 93 | await myFeed.ready() |
| 94 | |
| 95 | // User Hyperdrive will be created lazily when first image is uploaded |
| 96 | return true |
| 97 | } |
| 98 | |
| 99 | // Fallback: try old IdentityKey system |
| 100 | const identityCore = store.get({ name: 'identity' }) |
| 76 | 101 | await identityCore.ready() |
| 77 | 102 | |
| 78 | 103 | if (identityCore.length === 0) { |
| 104 | // Not initialized at all |
| 79 | 105 | await store.close() |
| 80 | 106 | store = null |
| 81 | | return null // Not initialized |
| 107 | metaBee = null |
| 108 | return null |
| 82 | 109 | } |
| 83 | 110 | |
| 111 | // Old system exists |
| 84 | 112 | const identitySeed = await identityCore.get(0) |
| 85 | 113 | identity = await IdentityKey.from({ seed: identitySeed }) |
| 86 | 114 | |
| 87 | | // Load personal feed |
| 115 | // Load personal feed (old system uses named feed) |
| 88 | 116 | myFeed = store.get({ name: 'my-feed' }) |
| 89 | 117 | await myFeed.ready() |
| 90 | 118 | |
| 91 | | // Load metadata |
| 92 | | metaBee = new Hyperbee(store.get({ name: 'metadata' }), { |
| 93 | | keyEncoding: 'utf-8', |
| 94 | | valueEncoding: 'json' |
| 95 | | }) |
| 96 | | await metaBee.ready() |
| 97 | | |
| 98 | | // Try to load ANProto keypair (new system) |
| 99 | | try { |
| 100 | | const keypairNode = await metaBee.get('anproto-keypair') |
| 101 | | if (keypairNode && keypairNode.value) { |
| 102 | | const keypairStr = keypairNode.value |
| 103 | | const { publicKey, secretKey } = ANProto.loadKeypair(keypairStr) |
| 104 | | anprotoKeypair = { publicKey, secretKey } |
| 105 | | } |
| 106 | | } catch (err) { |
| 107 | | // No ANProto keypair yet (old system or not initialized) |
| 108 | | anprotoKeypair = null |
| 109 | | } |
| 110 | | |
| 111 | 119 | // User Hyperdrive will be created lazily when first image is uploaded |
| 112 | 120 | return true |
| 113 | 121 | } catch (err) { |
| … | … | @@ -250,10 +258,8 @@ async function cmdInit() { |
| 250 | 258 | |
| 251 | 259 | // Generate ANProto keypair |
| 252 | 260 | const { keypair, publicKey, secretKey } = ANProto.gen() |
| 253 | | anprotoKeypair = { |
| 254 | | publicKey: b4a.from(publicKey, 'base64'), |
| 255 | | secretKey: b4a.from(secretKey, 'base64') |
| 256 | | } |
| 261 | // Note: publicKey and secretKey from gen() are Buffers |
| 262 | anprotoKeypair = { publicKey, secretKey } |
| 257 | 263 | |
| 258 | 264 | // Create personal feed using ANProto keypair |
| 259 | 265 | // This unifies identity: ANProto public key = Hypercore feed key |
| … | … | @@ -276,7 +282,7 @@ async function cmdInit() { |
| 276 | 282 | await metaBee.put('profile', { username: null }) |
| 277 | 283 | |
| 278 | 284 | console.log('✅ Wirecore initialized with ANProto!') |
| 279 | | console.log('ANProto public key:', publicKey) |
| 285 | console.log('ANProto public key:', b4a.toString(publicKey, 'base64')) |
| 280 | 286 | console.log('Feed key (same as ANProto key):', b4a.toString(myFeed.key, 'hex')) |
| 281 | 287 | console.log('') |
| 282 | 288 | console.log('💡 Set your username with: pear run . cli setname "YourName"') |
| … | … | @@ -875,8 +881,8 @@ const wsClients = new Set() |
| 875 | 881 | async function startWebServer(port = 3003) { |
| 876 | 882 | await loadState() |
| 877 | 883 | |
| 878 | | if (!identity) { |
| 879 | | console.error('❌ Not initialized. Run: pear run . init') |
| 884 | if (!anprotoKeypair && !identity) { |
| 885 | console.error('❌ Not initialized. Run: pear run . cli init') |
| 880 | 886 | return |
| 881 | 887 | } |
| 882 | 888 | |