Skip to content
On this page

Device keys

A device key is an X25519 keypair that represents one client device (a browser, a phone, a desktop) belonging to the holder of a specific Bitcoin address. Multiple devices can bind to the same address; envelopes are wrapped to every bound device so each can decrypt independently.

GenerateLink to this section

TypeScript
import { generateDeviceKey } from '@orangecheck/lock-device';

const {
    device_sk, // 32-byte X25519 secret — never leaves this machine
    device_pk, // 32-byte X25519 public key
    device_id, // 16 random bytes, hex
    created_at, // ISO timestamp
} = generateDeviceKey();

Store device_sk in whatever local secure storage your client provides (IndexedDB + WebCrypto non-extractable keys in browsers; SecKeyCreateRandomKey on iOS; AndroidKeystore on Android).

Bind to a Bitcoin addressLink to this section

TypeScript
import {
    buildBindingStatement,
    deriveNostrKey,
} from '@orangecheck/lock-device';

declare const device_sk: Uint8Array, device_pk: string; // from Generate
declare const device_id: string, created_at: string;
declare function walletSignBIP322(message: string): Promise<string>; // your wallet's BIP-322 signer

const statement = buildBindingStatement({
    address: 'bc1qbob...',
    device_pk,
    nostr_pk: deriveNostrKey(device_sk).nostrPk,
    device_id,
    created_at,
});

const signature = await walletSignBIP322(statement); // base64

The statement is a canonical message with header oc-lock:device-bind:v3. It binds (address, device_id, device_pk, nostr_pk) together — a verifier who sees the signature and the statement knows that bc1qbob… has attested that this device_pk is theirs, and which Nostr key may publish it. Omitting nostr_pk produces the older v2 statement, which does not sign the publishing key. Readers still seal to a v2 device, but from 2027-01-21 a v2 record names no inbox key (authorizedDevices, spec §3.4); to keep one, re-bind the device as v3 with the v2 statement's created_at.

Publish to the Nostr directoryLink to this section

TypeScript
import { finalizeDeviceEvent } from '@orangecheck/lock-device';
import { DEFAULT_RELAYS, publishEvent } from '@orangecheck/nostr-core';

declare const device_sk: Uint8Array, device_pk: string, device_id: string; // from Generate
declare const statement: string, signature: string; // from Bind

const event = finalizeDeviceEvent({
    deviceSk: device_sk,
    address: 'bc1qbob...',
    device_id,
    device_pk,
    bindingStatement: statement,
    bindingSigBase64: signature,
});

const results = await publishEvent(event, DEFAULT_RELAYS); // one { relay, ok } per relay

@orangecheck/lock-device builds and signs the event; it does no networking. Publish it with any Nostr client — @orangecheck/nostr-core is the family's.

The event:

  • kind: 30078 (NIP-78 addressable replaceable)
  • d tag: oc-lock:device:<bc1qbob...> — one device record per Bitcoin address per pubkey
  • content: the binding statement itself, exactly the bytes the wallet signed
  • tags: addr, device_id, device_pk, alg (x25519) and binding_sig (the base64 BIP-322 signature). Tags are an index; a reader trusts only values that match the signed statement, which is what parseDeviceEvent checks
  • author pubkey: derived deterministically from device_sk via HKDF, so the same device always publishes under the same Nostr pubkey without needing to manage a separate Nostr keypair

Publishing again from the same device (same key, same d tag) takes the old event's place at each relay that accepts it; a relay that missed it still serves the old one. A rotated key derives a new Nostr pubkey, so its record replaces nothing: to rotate, bind the new device under a new device_id, then revoke the old one (Revocation).

Multiple devices per addressLink to this section

Each device publishes its own kind-30078 event. They share the d tag oc-lock:device:bc1qbob... but not an author: every device signs with its own derived Nostr key, so each keeps its own replaceable slot.

The sender queries all events matching #d:oc-lock:device:bc1qbob… and wraps the envelope for every device it finds. Each device can independently decrypt.

RevocationLink to this section

The address signs a revocation statement, and a fresh Nostr key carries it. You do not need the old device or its key, so this works for a lost or stolen device. Never carry it with the key of another device that is still in use: relays replace a record by (pubkey, kind, d), so it would take that device's record's place.

TypeScript
import type { ParsedDeviceEvent } from '@orangecheck/lock-device';

import {
    buildRevocationStatement,
    finalizeDeviceEvent,
    revokedAtFor,
} from '@orangecheck/lock-device';
import { publishEvent } from '@orangecheck/nostr-core';

declare const oldDeviceId: string; // the device to retire
declare function walletSignBIP322(message: string): Promise<string>; // your wallet's BIP-322 signer

// records: the verified device records you read for the address. The date is
// now, or that device's own signed created_at if it is later.
declare const records: ParsedDeviceEvent[];
const revocation = buildRevocationStatement({
    address: 'bc1qbob...',
    device_id: oldDeviceId,
    revoked_at: revokedAtFor(records, 'bc1qbob...', oldDeviceId),
});

const sig = await walletSignBIP322(revocation);

// A one-off key carries it. The authority is the wallet's signature, not the key.
await publishEvent(
    finalizeDeviceEvent({
        deviceSk: crypto.getRandomValues(new Uint8Array(32)),
        address: 'bc1qbob...',
        device_id: oldDeviceId,
        device_pk: 'revoked',
        bindingStatement: revocation,
        bindingSigBase64: sig,
    })
);

A sender resolving the address collects the records from every relay it queried, verifying each event under NIP-01 before it merges them (the Nostr wrapper, never which key carried it), drops any whose binding_sig does not verify, and seals only to binds that no revocation retires. A revocation of (address, device_id) retires each bind of that pair whose signed created_at is at or before its signed revoked_at, whichever key carried either record, and holds back a bind dated later than its own clock. liveDevices from @orangecheck/lock-device applies the rule, and pendingDevices lists the binds held back (spec §3.5).

A revocation one relay holds retires the bind another relay still serves, so a sender that reads several relays needs only one of them to have it. A sender that reads none of the relays holding it keeps sealing to the device.

When a record countsLink to this section

lock.ochk.io counts device records by the family's write quorum and read floor. It publishes to nostr-core's five relays, then asks each relay on its own for the event.

  • Writes need 2 of the five. Registering a browser, revoking it, revoking another device with your wallet, and updating a record to v3 are done only once 2 of the five serve the record back. Below that the page says "Not in effect yet" and offers Try again, which sends the same signed event again; the wallet is not asked to sign again. A browser being registered keeps its key either way. A revocation this browser sends without a wallet signature only replaces its own record, so it needs all five.
  • A revocation is checked. After it is in effect, lock.ochk.io reads the address's devices again and says it is done only when that read, at the floor, no longer shows the device. With too few answers it says "Not checked yet".
  • Reads that rest on an absence need 4 of the 5, with none cut off. Sealing to an address needs them, because only then does a missing revocation mean there is none; with fewer, or with a relay that stopped partway, it sends nothing and says "Too few relays answered." The list of an address's other devices needs them too.

RotationLink to this section

To rotate a device key cleanly:

  1. Generate a new device keypair locally.
  2. Publish the new binding event.
  3. Publish a revocation event for the old device_id.
  4. (Optional) Re-decrypt historical envelopes with the old key, then securely wipe the old device_sk.

For forward secrecy guarantees (old envelopes unrecoverable if old key leaks later), don't keep the old device_sk after rotation.

Deterministic Nostr key derivationLink to this section

The directory event is authored by a Nostr pubkey derived from the device secret via HKDF:

Code
nostr_sk = HKDF-SHA256(device_sk, salt="oc-lock/v2/nostr-key", info="nostr-sk", length=32)
nostr_pk = schnorr.getPublicKey(nostr_sk)   // BIP-340 x-only

deriveNostrKey(device_sk) in @orangecheck/lock-device returns both.

This lets the same device publish under the same Nostr pubkey across sessions without the user managing a separate Nostr keypair. It also prevents one device from spoofing the directory record of another (different device secrets → different Nostr pubkeys).

See alsoLink to this section