P2PBox debug reply: avoid deep clone; send via shared transport to prevent bus endpoint override\n- Use Arc<RwLock<Box<dyn Transport>>> in spawned thread\n- Keeps original node endpoint intact
This commit is contained in:
@ -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
|
||||
<!-- Compact → TypeScript → JavaScript -->
|
||||
<script src="midnight-js-sdk.js"></script>
|
||||
<script src="voting-contract.js"></script>
|
||||
|
||||
<!-- Nyash WASM統合 -->
|
||||
<script type="module">
|
||||
window.midnightContract = new VotingContract();
|
||||
// Nyashから extern_call() で呼び出し
|
||||
</script>
|
||||
```
|
||||
|
||||
### 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使用が必要
|
||||
@ -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());
|
||||
}
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user