Utility functions
Utility functions in Bitsocial React Hooks. Usage, options, and examples.
setPkcJs(PKC) // swap the underlying protocol client implementation, e.g. for mocks or Electron
setOnlineStatus(online: boolean | undefined) // report network status where navigator.onLine is missing (React Native, e.g. from NetInfo); undefined goes back to navigator.onLine. Feeds skip a community's expired posts cache while online and may use it while offline; a change recomputes feeds
deleteDatabases() // delete all databases, including all caches and accounts data, then reload the app because the stores keep their state in memory
deleteCaches() // delete the cached comments, cached communities and cached pages only, no accounts data
createCrosspost(comment: Comment): Crosspost // build the immutable payload accepted by pkc-js
isAccountComment(comment: Comment, options?: {accountName?: string}): boolean // useIsAccountComment outside React, e.g. to check every reply of a thread
validateComment(comment: Comment, options?: {validateReplies?: boolean, community?: Community}): Promise<{valid: true} | {valid: false, reason: string}> // validate a comment from an untrusted source outside React
validateEthWallet(wallet: Wallet, authorPublicKey: string): Promise<void> // resolves only for a valid BSIP-22 proof, throws otherwise
// from '@bitsocial/bitsocial-react-hooks/author-wallet-proof', a separate entry point so the curve code
// (about 55 KB) loads only in apps or views that import it
verifyAuthorWallet(wallet: Wallet, authorPublicKey: string, options?: {nowSeconds?: number}): {outcome: "valid"} | {outcome: "unknown"} | {outcome: "invalid", reason: string} // BSIP-22 check of an author.wallets entry; authorPublicKey is the key address (12D3KooW...) of the record's signing key, never author.address, and a valid outcome counts only if the record's own signature also verifies under that key
getWalletMessageToSign(authorPublicKey: string, walletAddress: string, timestamp: number): string // the exact BSIP-22 message a wallet signs to prove it belongs to the author
getAvailablePostSortTypes(community: Community): string[]
getAvailableReplySortTypes(comment: Comment): string[]
getPreloadedPostSortTypes(community: Community): string[] // every sort preloaded in community.posts.pages, in the record's key order (not guaranteed to be the community's configured order)
getPreloadedReplySortTypes(comment: Comment): string[]
getPreloadedPostSortType(community: Community): string | undefined // first preloaded sort in the record's key order, undefined when none is preloaded
getPreloadedReplySortType(comment: Comment): string | undefined
resolvePostSortType(community: Community, requestedSortType?: string): string | undefined // without a requested sort: the first preloaded sort, else the first sort with a page cid
resolveReplySortType(comment: Comment, requestedSortType?: string): string | undefined
getPostPageSortType(community: Community, requestedSortType?: string): string | undefined // page sort serving the request, e.g. the preloaded page a single-page community re-sorts client-side
getReplyPageSortType(comment: Comment, requestedSortType?: string): string | undefined
getSortTimeframeSeconds(sortType?: string): number | undefined // time window of top*/controversial* timeframe sorts
isFlatSortType(sortType?: string): boolean
Publish multiple media attachments through usePublishComment({ communityAddress, gallery }).
gallery is an array of CommentGalleryItem: { url, htmlTagName: "img" | "video" | "audio", width?, height?, alt? }.
A gallery can accompany link or stand alone, including in a reply with parentCid and postCid.
The existing comment/feed/account hooks expose it as comment.gallery; no separate gallery hook
is needed. Pending callbacks and local account history retain the attachments.
Galleries require the community to set features.maxPostGalleryItems or
features.maxReplyGalleryItems to a positive limit; absent or zero disables them. The protocol
client validates item structure, and the community checks HTTPS media URLs, matching media tags,
limits and media restrictions. Hooks do not upload media, infer gallery dimensions, or render it.
createCrosspost requires a fully loaded comment with comment.cid and
comment.raw.comment. Publish the returned payload through the existing action hook:
const crosspost = createCrosspost(sourceComment);
const { publishComment } = usePublishComment({
communityAddress: targetCommunityAddress,
title: sourceComment.title,
crosspost,
});
await publishComment();
Render the author-signed embedded record immediately with useCrosspost. The referenced
community is only confirmed after its signed CommentUpdate has loaded, exposed as
isCommunityVerified:
const original = useCrosspost({ crosspost: post.crosspost });
return <CrosspostCard comment={original} verified={original.isCommunityVerified} />;
validateComment checks a comment received from an untrusted source, such as a third-party
server, in the shape {cid, raw: {comment, commentUpdate}} used by preloaded page comments. It
verifies the author’s signature, that raw.comment hashes to cid, and that the community signed
raw.commentUpdate for that cid, including its preloaded replies unless validateReplies is
false. It runs locally, without an account or network, so it doesn’t wait for accounts to
initialize. Pass the community loaded with useCommunity as community to check the update
against its current signing key; the comment must then name that community, or carry its public key when
either side has no name. Without it, the update must be signed by the communityPublicKey in the
author-signed comment, which fails for communities whose signing key changed and for old
records that only carry a domain subplebbitAddress. Without
community it does not check which community you expected, so compare raw.comment.communityPublicKey
with the public key of the community you are displaying; raw.comment.communityName alone is only
the author’s claim.
const results = await Promise.all(
untrustedComments.map((comment) => validateComment(comment, { community })),
);
const validComments = untrustedComments.filter((_, index) => results[index].valid);
useIsAccountComment and isAccountComment tell whether the account published a loaded comment.
A comment counts as the account’s when it is in the account’s local history (pending comments
included), when it is signed by the account’s key, or when the community that aliased it on a
pseudonymous board tagged it as the account’s in raw.commentUpdate.pseudonymityAuthorTag (pkc-js
docs/protocol/pseudonymity-author-tag.md). The last two need only the account key, so a second
device with the same account recognizes posts published from the first one. Nothing is stored:
the check runs on each loaded comment, deriving one key per account and community, so a post
recognized this way doesn’t appear in useAccountComments or useNotifications. The signature and
the tag are read, not verified: pass comments loaded through the hooks, or check a comment from
another source with validateComment first. A board can tag any post on itself as the account’s,
so a malicious board can produce a false “(You)” on that board only. Anyone holding the account’s
private key can recognize all of its tagged posts, so treat an exported account like a password.
The tag check’s code (about 40 KB) stays out of the startup bundle and loads the first time a
comment needs it: until then, a comment recognized only by its tag reads as not the account’s.
useIsAccountComment rerenders once it has loaded; call isAccountComment again later, or on an
earlier comment, e.g. when a thread opens, to start the load before you need the answer.
const isYou = useIsAccountComment({ comment: quotedReply });
const repliesToYou = replies.filter((reply) => isAccountComment(parentCommentsByCid[reply.parentCid]));