From b2b82c9e818a593e1c5ff37392cdaf69931051b1 Mon Sep 17 00:00:00 2001 From: Moe Charm Date: Tue, 26 Aug 2025 21:35:32 +0900 Subject: [PATCH] P2PBox debug reply: avoid deep clone; send via shared transport to prevent bus endpoint override\n- Use Arc>> in spawned thread\n- Keeps original node endpoint intact --- ...2025-08-26-midnight-network-integration.md | 106 ++++++++++++++++++ src/boxes/p2p_box.rs | 8 +- 2 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 docs/ideas/new-features/2025-08-26-midnight-network-integration.md diff --git a/docs/ideas/new-features/2025-08-26-midnight-network-integration.md b/docs/ideas/new-features/2025-08-26-midnight-network-integration.md new file mode 100644 index 00000000..20d2859d --- /dev/null +++ b/docs/ideas/new-features/2025-08-26-midnight-network-integration.md @@ -0,0 +1,106 @@ +# Midnight Network Integration (MidnightBox) +Status: Research +Created: 2025-08-26 +Priority: Low (Challenge-specific) +Related: Privacy-preserving applications, ZK proofs + +## 概要 +Midnight Networkとの統合により、Nyashでプライバシー保護アプリケーションを開発可能にする。 + +## 背景 +- Midnight Network Privacy Challenge ($5,000)への参加機会 +- Compact言語(TypeScriptベースDSL)からTypeScript/JavaScript生成 +- ZKプルーフを使用したプライバシー保護スマートコントラクト + +## 実装アプローチ案 + +### 1. ビルトインBox実装 +```nyash +// MidnightBoxの使用例 +local midnight = new MidnightBox() +midnight.connect("contract-address") + +// ZKプルーフ生成 +local proof = midnight.createProof("validVote", { + voterId: "Alice", + choice: "OptionA" +}) + +// トランザクション送信 +local result = midnight.submitTransaction(proof) +``` + +### 2. HTML埋め込み戦略 +```html + + + + + + +``` + +### 3. 実装フロー +1. Compact言語で契約記述 +2. CompactコンパイラでTypeScript生成 +3. HTML/JavaScriptに統合 +4. NyashのExternCallで呼び出し + +## チャレンジ向け実装例 + +### プライバシー投票システム +```nyash +box PrivateVotingApp { + init { midnight, publicLedger, privateLedger } + + constructor() { + me.midnight = new MidnightBox() + me.publicLedger = new ArrayBox() // 証明のみ + me.privateLedger = new MapBox() // 暗号化データ + } + + vote(voterId, choice) { + // ZKプルーフで投票の有効性を証明 + local proof = me.midnight.proveValidVote({ + hasRightToVote: true, + hasNotVotedYet: true, + choiceIsValid: true + }) + + // 公開台帳には証明のみ + me.publicLedger.push(proof) + + // プライベート台帳に暗号化データ + me.privateLedger.set( + hash(voterId), + me.midnight.encrypt(choice) + ) + + return proof + } +} +``` + +## 技術的課題 +1. **WASM統合**: wasm-bindgenでJavaScript関数バインディング +2. **型安全性**: RustとTypeScriptの型マッピング +3. **非同期処理**: Midnight SDKの非同期APIとの連携 +4. **エラーハンドリング**: ZKプルーフ生成失敗時の処理 + +## 実装優先度 +- チャレンジ応募時のみ必要 +- モジュラービルトインBoxシステム実装後に検討 +- プロトタイプはExternCallで十分 + +## 参考資料 +- Midnight Network Documentation +- Compact Language Reference +- DEV.to Challenge Page + +## メモ +- 締切: 2025年9月7日 +- "Protect That Data"トラック: $3,500賞金 +- モックではなく実際のMidnight SDK使用が必要 \ No newline at end of file diff --git a/src/boxes/p2p_box.rs b/src/boxes/p2p_box.rs index cc3168b9..48d29849 100644 --- a/src/boxes/p2p_box.rs +++ b/src/boxes/p2p_box.rs @@ -380,18 +380,22 @@ mod tests { let last_from = Arc::clone(&self.last_from); let last_intent = Arc::clone(&self.last_intent_name); // create self clone for reply - let self_clone = self.clone(); + // Avoid deep clone (which re-registers transport). Use transport directly for reply. + let transport_arc = Arc::clone(&self.transport); let reply_name = reply_intent.map(|s| s.to_string()); t.register_intent_handler(&intent_name, Box::new(move |env| { if let Ok(mut lf) = last_from.write() { *lf = Some(env.from.clone()); } if let Ok(mut li) = last_intent.write() { *li = Some(env.intent.get_name().to_string_box().value); } if let Some(rn) = reply_name.clone() { let to = env.from.clone(); + let transport_arc = Arc::clone(&transport_arc); std::thread::spawn(move || { // slight delay to avoid lock contention std::thread::sleep(std::time::Duration::from_millis(5)); let intent = IntentBox::new(rn, serde_json::json!({})); - let _ = self_clone.send(Box::new(StringBox::new(to)), Box::new(intent)); + if let Ok(transport) = transport_arc.read() { + let _ = transport.send(&to, intent, Default::default()); + } }); } }));