From 950c42d909c0061115b99bba9f281501ee009914 Mon Sep 17 00:00:00 2001 From: bitful-pannul Date: Mon, 5 Aug 2024 18:35:33 +0300 Subject: [PATCH] kns: routers from strings to a byte array --- .../kns_indexer/kns_indexer/src/lib.rs | 27 ++++++++++++++----- kinode/src/register-ui/src/abis/helpers.ts | 12 ++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/kinode/packages/kns_indexer/kns_indexer/src/lib.rs b/kinode/packages/kns_indexer/kns_indexer/src/lib.rs index 21447a8f..4d5883eb 100644 --- a/kinode/packages/kns_indexer/kns_indexer/src/lib.rs +++ b/kinode/packages/kns_indexer/kns_indexer/src/lib.rs @@ -311,7 +311,7 @@ fn handle_log(our: &Address, state: &mut State, log: ð::Log) -> anyhow::Resul }; state.names.insert(child_hash.clone(), full_name.clone()); - println!("inserted child hash: {child_hash}, with full name: {full_name}"); + // println!("inserted child hash: {child_hash}, with full name: {full_name}"); state.nodes.insert( full_name.clone(), net::KnsUpdate { @@ -360,7 +360,7 @@ fn handle_log(our: &Address, state: &mut State, log: ð::Log) -> anyhow::Resul } } "~routers" => { - let routers = decode_routers(&decoded.data)?; + let routers = decode_routers(&decoded.data, &state)?; if let Some(node) = state.nodes.get_mut(&node_name) { node.routers = routers; // -> indirect @@ -482,10 +482,25 @@ fn add_temp_hardcoded_tlzs(state: &mut State) { ); } -/// Decodes bytes into an array of node identities, expecting UTF-8 encoded strings separated by newlines. -fn decode_routers(data: &[u8]) -> anyhow::Result> { - let data_str = std::str::from_utf8(data)?; - let routers = data_str.split(',').map(str::to_owned).collect(); +/// Decodes bytes into an array of keccak256 hashes (32 bytes each) and returns their full names. +fn decode_routers(data: &[u8], state: &State) -> anyhow::Result> { + if data.len() % 32 != 0 { + return Err(anyhow::anyhow!("got invalid data length for router hashes")); + } + + let mut routers = Vec::new(); + for chunk in data.chunks(32) { + let hash_str = format!("0x{}", hex::encode(chunk)); + + match state.names.get(&hash_str) { + Some(full_name) => routers.push(full_name.clone()), + None => print_to_terminal( + 1, + &format!("error: no name found for router hash {hash_str}"), + ), + } + } + Ok(routers) } diff --git a/kinode/src/register-ui/src/abis/helpers.ts b/kinode/src/register-ui/src/abis/helpers.ts index 9ee216b2..8cae088f 100644 --- a/kinode/src/register-ui/src/abis/helpers.ts +++ b/kinode/src/register-ui/src/abis/helpers.ts @@ -1,9 +1,17 @@ import { NetworkingInfo } from "../lib/types"; +import { kinohash } from "../utils/kinohash"; import { ipToBytes, portToBytes } from "../utils/kns_encoding"; import { multicallAbi, kinomapAbi, mechAbi, KINOMAP, MULTICALL } from "./"; import { encodeFunctionData, encodePacked, stringToHex, bytesToHex } from "viem"; +// Function to encode router names into keccak256 hashes +// Function to encode router names into keccak256 hashes +const encodeRouters = (routers: string[]): `0x${string}` => { + const hashedRouters = routers.map(router => kinohash(router).slice(2)); // Remove '0x' prefix + return `0x${hashedRouters.join('')}`; +}; + export const generateNetworkingKeys = async ({ direct, label, @@ -78,6 +86,8 @@ export const generateNetworkingKeys = async ({ ] }); + const encodedRouters = encodeRouters(allowed_routers); + const router_call = encodeFunctionData({ abi: kinomapAbi, @@ -86,7 +96,7 @@ export const generateNetworkingKeys = async ({ encodePacked(["bytes"], [stringToHex("~routers")]), encodePacked( ["bytes"], - [stringToHex(allowed_routers.join(","))] + [encodedRouters] )] });