Skip to content
On this page

api

Full reference: ochk.io/docs/sdk/vote-core — auto-generated from the TypeScript source on every release. The narrative below is the curated overview; the SDK reference page is the source of truth for every export, type, and signature.

The web UI is convenience. The curl output is the authoritative result.

httpLink to this section

Code
GET /api/tally?poll=<64-char hex poll_id>

Any query parameter besides poll answers 308 to /api/tally?poll=<id>.

Fetches the poll from the default Nostr relay set, pulls every matching ballot, verifies every BIP-322 signature, resolves the snapshot block, looks up each voter's coins at mempool.space, runs the pure tally function, and returns JSON. When mempool.space gives no coin list (its cap answer, a 429, a 5xx or a body that is not a coin list), or the request fails before its 8-second timeout, blockstream.info is asked once, if at least 5 of the tally's 60 seconds are left. Any other refusal, and mempool.space's timeout, are final.

200 tallied:

JSON
{
    "poll_id": "9825268c…",
    "basis": "voter_clocks",
    "state": "tallied",
    "snapshot_block": 947961,
    "turnout": { "voters": 1, "weight": 12311 },
    "tallies": { "yes": 12311, "no": 0 },
    "counted": ["5d0c…"],
    "deadline": "2026-05-05T02:33:00Z",
    "ballot_count": 1,
    "counted_at": "2026-11-03T09:19:28.688Z",
    "reveal_published": false,
    "relays": [{ "relay": "wss://relay.ochk.io", "answered": true, "events": 1 }, …],
    "utxo_source": {
        "explorer": "mempool.space, then blockstream.info",
        "counts": "coins unspent now and confirmed at or before snapshot_block",
        "not_counted": "coins spent after snapshot_block"
    }
}

Every result carries basis: anchored when ballots are timed by their Bitcoin anchors, voter_clocks when by the voter's own created_at. An anchored poll's result also carries close_height, and its tally is state: "provisional", with an unanchored count, until the tip reaches H_close + G + 6 (see tally algorithm). A tally carries counted, the ballot_id that stood for each voter, in ascending order, and counted_at, when the oldest voter's coins it counted were read. Both 200 answers carry the poll's own deadline (ISO 8601 UTC), so a caller can tell a closed poll from an open one without reading the poll event.

200 awaiting_reveal (secret-mode poll pre-close):

JSON
{
    "poll_id": "cc27…",
    "state": "awaiting_reveal",
    "basis": "voter_clocks",
    "deadline": "2026-10-20T00:00:00Z",
    "ballot_count": 3,
    "relays": [{ "relay": "wss://relay.ochk.io", "answered": true, "events": 3 }, …]
}

Errors (JSON with an error string):

  • 400 — poll query param missing or not 64 hex.
  • 404 — no relay that answered holds the poll. If the id is a poll's Nostr event id, the body names the poll_id to use instead.
  • 409 — state: "deferred": the snapshot block is not yet buried six blocks deep, or, for an anchored poll, no block has passed the deadline yet. Retry later.
  • 422 — the poll event cannot be read, its content does not match the poll id, or its creator signature does not verify.
  • 429 — more than 30 tallies a minute from one caller, counted on each server instance. Retry after Retry-After.
  • 503 — no relay finished answering the poll, ballot, reveal or (for an anchored poll) stamp query, or the block at the deadline could not be read. A tally over a ballot set no relay finished sending would be a guess.
  • 503 — state: "unread": a counted voter's coins could not be read, so nothing is counted. The body's unread lists each such voter with its reason (utxo_cap_unread, chain_unread or provider_refused_unread), and unread_count and unread_reasons count them, in all and by reason. A list that was not read is never an empty list.
  • 500 — tally_failed: the tally itself threw.

A counted answer is kept for up to 60 seconds, at the edge and in the browser, and any other answer for up to 15 seconds, except an unread one, which is not kept. Voters' coins are read at most every two minutes, so a coin spent just now can still count for about three minutes; counted_at says when they were read. Live tallies on the poll page recompute client-side against the same function.

libraryLink to this section

npm install @orangecheck/vote-core @orangecheck/bip322

The main exports (the SDK reference lists every one):

TypeScript
import type {
    Ballot,
    // types
    Poll,
    Reveal,
    TallyResult,
    Utxo,
} from '@orangecheck/vote-core';

import {
    ageDays,
    // anchored polls: H_close, a ballot's anchor, and when a tally is final
    anchoredTallyIsFinal,
    anchorHeightFromStamps,
    ballotId,
    buildCommitMessage,
    canonicalBytes,
    // canonicalization
    canonicalize,
    // secret-mode commit
    commit,
    // collapse byte-identical ballot copies read from several relays
    distinctBallotCopies,
    isAnchoredPoll,
    isSupportedMode,
    // an object nests at most 32 levels (SPEC §7)
    MAX_NESTING_DEPTH,
    // the snapshot block for a "deadline" poll
    mempoolBlockTimeSource,
    nestedTooDeep,
    // ids (content-addressed, SHA-256 of canonical bytes with sig.value = "")
    pollId,
    qualifyingUtxos,
    resolveCloseBlock,
    resolvePollSnapshot,
    revealId,
    // the tally
    tally,
    totalQualifyingSats,
    // signature checks for one poll, ballot or reveal
    verifyBallot,
    verifyPoll,
    verifyReveal,
    // weight computation
    voterWeight,
} from '@orangecheck/vote-core';

example: build and sign a pollLink to this section

TypeScript
import type { Poll } from '@orangecheck/vote-core';

import { pollId } from '@orangecheck/vote-core';

declare function walletSignBIP322(message: string): Promise<string>; // your wallet's BIP-322 signer

const iso = (ms: number) =>
    new Date(ms).toISOString().replace(/\.\d{3}Z$/, 'Z');

const draft: Poll = {
    v: 0,
    kind: 'oc-vote/poll',
    creator: 'bc1q…',
    question: 'Should we ship?',
    options: [
        { id: 'yes', label: 'Yes' },
        { id: 'no', label: 'No' },
    ],
    deadline: iso(Date.now() + 7 * 86_400_000), // MUST be in the future when published
    snapshot_block: 'deadline',
    weight_mode: 'sats',
    weight_params: null,
    min_sats: 100_000,
    min_days: 30,
    mode: 'public',
    reveal_pk: null,
    tiebreak: 'latest',
    notes: null,
    created_at: iso(Date.now()),
    sig: { alg: 'bip322', pubkey: 'bc1q…', value: '' },
};

const id = pollId(draft);
// ask the wallet to BIP-322 sign `id` (the 64-hex string), then:
draft.sig.value = await walletSignBIP322(id);

example: tallyLink to this section

TypeScript
import type { Ballot, Poll, UtxoLookup } from '@orangecheck/vote-core';

import { verifySignature } from '@orangecheck/bip322';
import {
    mempoolBlockTimeSource,
    resolvePollSnapshot,
    tally,
} from '@orangecheck/vote-core';

declare const poll: Poll, ballots: Ballot[]; // the event contents from Nostr
declare const myUtxoSource: { fetch: UtxoLookup }; // your UTXO indexer

const snap = await resolvePollSnapshot(poll, mempoolBlockTimeSource());
if (!snap.ok) throw new Error(`${snap.code}: ${snap.reason}`);

const result = await tally({
    poll,
    ballots,
    snapshotBlock: snap.height, // required when poll.snapshot_block is "deadline"
    utxosAt: (addr, snapshotHeight) => myUtxoSource.fetch(addr, snapshotHeight),
    // Named arguments: vote-core's positional verifyBip322 takes
    // (address, message, signature), unlike every sibling package.
    verify: ({ address, message, signature }) =>
        verifySignature(address, message, signature),
});

For an anchored poll (deadline on or after 2026-11-15T00:00:00Z), also pass tipHeight, closeHeight and anchorHeight: see the tally algorithm.

The library is pure. Same inputs → byte-identical output across implementations. Its test suite checks it against the canonical fixtures in oc-vote-protocol/test-vectors/ (opens in a new tab).

dispute resolutionLink to this section

If the vote.ochk.io page tally disagrees with yours:

Shell
npx -y @orangecheck/vote-cli tally <poll_id>
# or: git clone oc-vote-protocol && implement tally() yourself

Whoever matches the canonical fixtures + the spec is correct. The web page is a convenience over the same function any client can run.