Advanced
RK-1
Use this path when your site issues its own one-time challenge and verifies a device-signed proof. The default quickstart on the home page is enough for most sites.
Your server issues a challenge, the person approves in Rhea Key, and you verify the signature against that exact challenge before creating a session. Issue a new session id; never reuse the old one. The rhea-success event fires only after your verifier accepts the result.
<script src="https://rheakey.com/sdk/rhea.js" async></script>
<rhea-button app-name="My App"></rhea-button>
<script>
// Fires only after your /rhea-verify route answered 2xx.
document.addEventListener("rhea-success", () => location.reload());
document.addEventListener("rhea-error", (e) => console.warn("Rhea Key:", e.detail.code));
</script>1. POST /rhea-challenge
- Mint a random hex
nonce(16 bytes),issuedAtandexpirationTime(ISO 8601, at most 120 seconds later). - Store them under a random 32-byte id and set
rhea_rk1=<id>; HttpOnly; Secure; SameSite=Lax; Path=/. - Answer
{ "nonce": "…", "issuedAt": "…", "expirationTime": "…" }.
2. What the person signs
<host> wants you to sign in with your Rhea Key account:
<0x address>
<statement>
URI: <origin>
Version: 1
Chain ID: 137
Nonce: <nonce>
Issued At: <issuedAt>
Expiration Time: <expirationTime>3. POST /rhea-verify
The body is { "message", "signature", "address" }. Check, in this order, and refuse on the first failure:
- Take and delete the challenge for the cookie id in one step, so it can never be used twice.
- The first line names your exact host, and
URIhas your exact origin. Versionis exactly1, andChain IDis the one you expect (137unless you setchain-idon the button).Nonce,Issued AtandExpiration Timeequal the stored challenge, and the time has not passed.- The address on line 2 equals
address, and the signer recovered fromsignatureequals it too. - Start a new session for that address and answer 2xx.
// server.mjs (Node 20+): npm i express cookie-parser ethers
import express from "express";
import cookieParser from "cookie-parser";
import { randomBytes } from "node:crypto";
import { verifyMessage } from "ethers";
const DOMAIN = "localhost:3000"; // your host, exactly as the browser shows it
const ORIGIN = "http://localhost:3000"; // your origin
const CHAIN_ID = "137"; // the button default; match your chain-id attribute if you set one
const TTL_MS = 120_000; // two minutes at most
const challenges = new Map(); // production: Redis GETDEL or SQL DELETE ... RETURNING
const app = express();
app.use(express.json({ limit: "8kb" }));
app.use(cookieParser());
app.post("/rhea-challenge", (req, res) => {
const id = randomBytes(32).toString("hex");
const now = Date.now();
const challenge = {
nonce: randomBytes(16).toString("hex"),
issuedAt: new Date(now).toISOString(),
expirationTime: new Date(now + TTL_MS).toISOString(),
};
challenges.set(id, challenge);
res.cookie("rhea_rk1", id, {
httpOnly: true, sameSite: "lax", path: "/", maxAge: TTL_MS,
secure: ORIGIN.startsWith("https:"),
});
res.json(challenge);
});
app.post("/rhea-verify", (req, res) => {
const id = req.cookies.rhea_rk1;
const challenge = challenges.get(id);
challenges.delete(id); // single use, even when the check fails
res.clearCookie("rhea_rk1", { path: "/" });
const { message, signature, address } = req.body ?? {};
try {
if (!challenge || typeof message !== "string" || typeof address !== "string") throw 0;
const lines = message.split("\n");
const field = (name) => {
const hits = lines.filter((l) => l.startsWith(name + ": "));
if (hits.length !== 1) throw 0;
return hits[0].slice(name.length + 2);
};
if (lines[0] !== `${DOMAIN} wants you to sign in with your Rhea Key account:`) throw 0;
if (lines[1].toLowerCase() !== address.toLowerCase()) throw 0;
if (new URL(field("URI")).origin !== ORIGIN) throw 0;
if (field("Version") !== "1") throw 0;
if (field("Chain ID") !== CHAIN_ID) throw 0;
if (field("Nonce") !== challenge.nonce) throw 0;
if (field("Issued At") !== challenge.issuedAt) throw 0;
if (field("Expiration Time") !== challenge.expirationTime) throw 0;
if (Date.now() > Date.parse(challenge.expirationTime)) throw 0;
if (verifyMessage(message, signature).toLowerCase() !== address.toLowerCase()) throw 0;
} catch {
return res.status(401).json({ error: "sign_in_failed" });
}
// Signed in as `address`. Start a NEW session here; never reuse the old session id.
res.status(204).end();
});
app.listen(3000);When @rheakey/sdk is on npm, createRk1Verifier from @rheakey/sdk/rk1 does all of this for you.
