Merge pull request #468 from kinode-dao/bp/routershash

kns: routers encoding update
This commit is contained in:
bitful-pannul 2024-08-05 21:31:51 +03:00 committed by GitHub
commit 89593cf726
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 6 deletions

View File

@ -357,7 +357,7 @@ fn handle_log(our: &Address, state: &mut State, log: &eth::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
@ -479,10 +479,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<Vec<String>> {
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<Vec<String>> {
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)
}

View File

@ -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]
)]
});