feat: add publishEntry

This commit is contained in:
Derrick Hammer 2023-12-11 22:26:42 -05:00
parent fbffadab8c
commit 5c19245b4b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 47 additions and 2 deletions

View File

@ -31,7 +31,7 @@ import {
ExecuteRequestError, ExecuteRequestError,
Headers, Headers,
} from "./request.js"; } from "./request.js";
import { subscribeToEntry } from "./methods/registry.js"; import { publishEntry, subscribeToEntry } from "./methods/registry.js";
/** /**
* Custom client options. * Custom client options.
@ -137,6 +137,7 @@ export class S5Client {
// Registry // Registry
subscribeToEntry = subscribeToEntry; subscribeToEntry = subscribeToEntry;
publishEntry = publishEntry;
/** /**
* The S5 Client which can be used to access S5-net. * The S5 Client which can be used to access S5-net.

View File

@ -5,8 +5,13 @@ import { ensureBytes } from "@noble/curves/abstract/utils";
import WS from "isomorphic-ws"; import WS from "isomorphic-ws";
import { buildRequestUrl } from "#request.js"; import { buildRequestUrl } from "#request.js";
import { Packer, SignedRegistryEntry } from "@lumeweb/libs5"; import { Packer, SignedRegistryEntry } from "@lumeweb/libs5";
import { deserializeRegistryEntry } from "@lumeweb/libs5/lib/service/registry.js"; import {
deserializeRegistryEntry,
verifyRegistryEntry,
} from "@lumeweb/libs5/lib/service/registry.js";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import { throwValidationError } from "../utils/validation.js";
import { base64url } from "multiformats/bases/base64";
export const DEFAULT_GET_ENTRY_OPTIONS = { export const DEFAULT_GET_ENTRY_OPTIONS = {
...DEFAULT_BASE_OPTIONS, ...DEFAULT_BASE_OPTIONS,
endpointGetEntry: "/s5/registry", endpointGetEntry: "/s5/registry",
@ -23,10 +28,16 @@ export const DEFAULT_SUBSCRIBE_ENTRY_OPTIONS = {
endpointSubscribeEntry: "/s5/registry/subscription", endpointSubscribeEntry: "/s5/registry/subscription",
}; };
export const DEFAULT_PUBLISH_ENTRY_OPTIONS = {
...DEFAULT_BASE_OPTIONS,
endpointPublishEntry: "/s5/registry",
};
export type BaseCustomOptions = CustomClientOptions; export type BaseCustomOptions = CustomClientOptions;
export type CustomRegistryOptions = BaseCustomOptions & { export type CustomRegistryOptions = BaseCustomOptions & {
endpointSubscribeEntry?: string; endpointSubscribeEntry?: string;
endpointPublishEntry?: string;
}; };
export async function subscribeToEntry( export async function subscribeToEntry(
@ -71,3 +82,36 @@ export async function subscribeToEntry(
}, },
}; };
} }
export async function publishEntry(
this: S5Client,
signedEntry: SignedRegistryEntry,
customOptions?: CustomRegistryOptions,
) {
const opts = {
...DEFAULT_PUBLISH_ENTRY_OPTIONS,
...this.customOptions,
...customOptions,
};
if (!verifyRegistryEntry(signedEntry)) {
throwValidationError(
"signedEntry", // name of the variable
signedEntry, // actual value
"parameter", // valueKind (assuming it's a function parameter)
"a valid signed registry entry", // expected description
);
}
return await this.executeRequest({
...opts,
endpointPath: opts.endpointPublishEntry,
method: "post",
data: {
pk: base64url.encode(signedEntry.pk),
revision: signedEntry.revision,
data: base64url.encode(signedEntry.data),
signature: base64url.encode(signedEntry.signature),
},
});
}