Files
zigp2p/src/mwp/p2p_dht_adapter.zig
2026-02-07 18:01:12 -03:00

209 lines
7.3 KiB
Zig

//! P2P DHT Adapter for MWP
//!
//! Adapts zig-p2p's DHT to work with the Mutual Witness Protocol.
//!
//! Key insight: MWP's original design has both peers publish to the same
//! meeting key, then uses getAll() to retrieve all publications. However,
//! zig-p2p's DHT stores one value per key.
//!
//! Solution: We use peer-specific publication keys:
//! - Alice publishes at: hash(meeting_key || alice_pubkey)
//! - Bob publishes at: hash(meeting_key || bob_pubkey)
//! - When looking up, we query the peer's specific key
//!
//! This adapter wraps the P2P DHT and handles this key transformation.
const std = @import("std");
const types = @import("types.zig");
const meeting_point = @import("meeting_point.zig");
const Allocator = std.mem.Allocator;
const DHTInterface = meeting_point.DHTInterface;
/// Adapter to use zig-p2p's P2P DHT with MWP
pub const P2PDhtAdapter = struct {
allocator: Allocator,
/// Opaque pointer to zig-p2p P2P instance
/// This is *P2P from src/p2p/p2p.zig
p2p: *anyopaque,
/// Function pointers to P2P DHT methods (to avoid import cycle)
dht_store_fn: *const fn (p2p: *anyopaque, key: [32]u8, value: []const u8, ttl: u32) anyerror!void,
dht_find_value_fn: *const fn (p2p: *anyopaque, key: [32]u8) anyerror!?[]u8,
/// Our public key (for computing publication keys)
our_pubkey: [32]u8,
/// The target peer's public key we're trying to meet
/// Set before put/getAll operations
target_pubkey: ?[32]u8,
/// Default TTL for publications (seconds)
default_ttl: u32,
pub const Config = struct {
allocator: Allocator,
p2p: *anyopaque,
dht_store_fn: *const fn (p2p: *anyopaque, key: [32]u8, value: []const u8, ttl: u32) anyerror!void,
dht_find_value_fn: *const fn (p2p: *anyopaque, key: [32]u8) anyerror!?[]u8,
our_pubkey: [32]u8,
default_ttl: u32 = 300, // 5 minutes
};
pub fn init(config: Config) P2PDhtAdapter {
return .{
.allocator = config.allocator,
.p2p = config.p2p,
.dht_store_fn = config.dht_store_fn,
.dht_find_value_fn = config.dht_find_value_fn,
.our_pubkey = config.our_pubkey,
.target_pubkey = null,
.default_ttl = config.default_ttl,
};
}
/// Set the target peer for the next operations
/// Must be called before put/getAll when using the interface
pub fn setTarget(self: *P2PDhtAdapter, target_pubkey: [32]u8) void {
self.target_pubkey = target_pubkey;
}
/// Compute the publication key for a specific peer at a meeting point
/// publication_key = hash("mwp-pub" || meeting_key || publisher_pubkey)
fn computePublicationKey(meeting_key: [32]u8, publisher_pubkey: [32]u8) [32]u8 {
var hasher = std.crypto.hash.blake2.Blake2b256.init(.{});
hasher.update("mwp-pub-v1");
hasher.update(&meeting_key);
hasher.update(&publisher_pubkey);
var result: [32]u8 = undefined;
hasher.final(&result);
return result;
}
/// DHTInterface.put implementation
/// Publishes our data at our publication key (meeting_key + our_pubkey)
fn put(ptr: *anyopaque, meeting_key: [32]u8, value: []const u8) anyerror!void {
const self: *P2PDhtAdapter = @ptrCast(@alignCast(ptr));
// Compute our publication key
const pub_key = computePublicationKey(meeting_key, self.our_pubkey);
// Store in DHT
try self.dht_store_fn(self.p2p, pub_key, value, self.default_ttl);
}
/// DHTInterface.getAll implementation
/// Looks up the target peer's publication key (meeting_key + target_pubkey)
fn getAll(ptr: *anyopaque, meeting_key: [32]u8, allocator: Allocator) anyerror![][]u8 {
const self: *P2PDhtAdapter = @ptrCast(@alignCast(ptr));
const target = self.target_pubkey orelse return error.TargetNotSet;
// Compute target's publication key
const pub_key = computePublicationKey(meeting_key, target);
// Query DHT
const value = try self.dht_find_value_fn(self.p2p, pub_key);
if (value) |v| {
// Return as single-element array (DHT returns one value per key)
const result = try allocator.alloc([]u8, 1);
result[0] = v;
return result;
}
// No value found
return &[_][]u8{};
}
/// Get the DHTInterface for use with MWP
pub fn interface(self: *P2PDhtAdapter) DHTInterface {
return .{
.ptr = self,
.vtable = &.{
.put = put,
.getAll = getAll,
.subscribe = null, // Not implemented yet
},
};
}
};
/// Wrapper for MeetingPoint that handles the target peer setup
pub const P2PMeetingPoint = struct {
allocator: Allocator,
adapter: *P2PDhtAdapter,
meeting: meeting_point.MeetingPoint,
pub fn init(
allocator: Allocator,
adapter: *P2PDhtAdapter,
keypair: meeting_point.MeetingPoint.KeyPair,
addresses: []const types.Address,
) P2PMeetingPoint {
return .{
.allocator = allocator,
.adapter = adapter,
.meeting = meeting_point.MeetingPoint.init(
allocator,
adapter.interface(),
keypair,
addresses,
),
};
}
/// Publish our availability for a specific peer
pub fn publish(self: *P2PMeetingPoint, their_pubkey: [32]u8, ttl: u32) !types.MeetingPublication {
// Set target before publishing (for key computation)
self.adapter.setTarget(their_pubkey);
return self.meeting.publish(their_pubkey, ttl);
}
/// Wait for a peer's publication
pub fn waitForPeer(
self: *P2PMeetingPoint,
their_pubkey: [32]u8,
timeout_ms: u64,
) !struct { publication: types.MeetingPublication, seen_time: i64 } {
// Set target before querying
self.adapter.setTarget(their_pubkey);
return self.meeting.waitForPeer(their_pubkey, timeout_ms);
}
/// Find a peer's publication (non-blocking)
pub fn findPeer(self: *P2PMeetingPoint, their_pubkey: [32]u8) !?types.MeetingPublication {
self.adapter.setTarget(their_pubkey);
return self.meeting.findPeer(their_pubkey);
}
/// Compute meeting key (for debugging)
pub fn computeMeetingKey(our_pubkey: [32]u8, their_pubkey: [32]u8) [32]u8 {
return meeting_point.MeetingPoint.computeMeetingKey(our_pubkey, their_pubkey);
}
};
// Tests
test "P2PDhtAdapter publication key is deterministic" {
const meeting_key = [_]u8{0xAA} ** 32;
const pubkey = [_]u8{0xBB} ** 32;
const key1 = P2PDhtAdapter.computePublicationKey(meeting_key, pubkey);
const key2 = P2PDhtAdapter.computePublicationKey(meeting_key, pubkey);
try std.testing.expect(std.mem.eql(u8, &key1, &key2));
}
test "P2PDhtAdapter publication key differs by publisher" {
const meeting_key = [_]u8{0xAA} ** 32;
const alice_pubkey = [_]u8{0xBB} ** 32;
const bob_pubkey = [_]u8{0xCC} ** 32;
const alice_key = P2PDhtAdapter.computePublicationKey(meeting_key, alice_pubkey);
const bob_key = P2PDhtAdapter.computePublicationKey(meeting_key, bob_pubkey);
// Different publishers = different publication keys
try std.testing.expect(!std.mem.eql(u8, &alice_key, &bob_key));
}