mirror of
https://github.com/uqbar-dao/nectar.git
synced 2024-11-26 11:53:31 +03:00
Merge pull request #468 from kinode-dao/bp/routershash
kns: routers encoding update
This commit is contained in:
commit
89593cf726
@ -357,7 +357,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
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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]
|
||||
)]
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user