2023-01-31 10:07:23 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
|
|
|
import type { Peer } from "@lumeweb/peer-discovery";
|
2023-01-31 12:18:45 +00:00
|
|
|
import { hexToBuf } from "@siaweb/libweb/dist";
|
2023-01-31 10:07:23 +00:00
|
|
|
export class PeerDiscoveryClient extends Client {
|
|
|
|
public async register(source: string): Promise<void> {
|
|
|
|
const bag = await this.loadBound(source);
|
|
|
|
const ret = await bag.callModule("register");
|
|
|
|
this.handleError(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async remove(name: string): Promise<boolean> {
|
|
|
|
return await this.callModuleReturn("remove", { name });
|
|
|
|
}
|
|
|
|
|
|
|
|
public async removeAll(): Promise<void> {
|
|
|
|
await this.callModuleReturn("removeAll");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async exists(name: string): Promise<boolean> {
|
|
|
|
return await this.callModuleReturn("remove", { name });
|
|
|
|
}
|
2023-01-31 12:18:45 +00:00
|
|
|
public async discover(pubkey: string | Uint8Array): Promise<Peer | boolean> {
|
|
|
|
if (typeof pubkey === "string") {
|
|
|
|
let buf = hexToBuf(pubkey);
|
|
|
|
if (buf[1]) {
|
|
|
|
throw new Error(buf[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey = buf[0];
|
|
|
|
}
|
2023-01-31 10:07:23 +00:00
|
|
|
return await this.callModuleReturn("discover", { pubkey });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createClient = factory<PeerDiscoveryClient>(
|
|
|
|
PeerDiscoveryClient,
|
|
|
|
"FACTQhR-sNQ0K9Nh5QlHlkp5q57rxBdc2DGgacoTdwtIoA"
|
|
|
|
);
|
|
|
|
|
|
|
|
export { Peer };
|