mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 07:19:41 +01:00
STRIPE_SECRET_KEY and the seven other STRIPE_* vars appear zero times outside
src/ and zero times in .env.example, so isStripeReady() could only ever return
false. Three API routes, a checkout route, a tier grid and a "Manage
subscription" button were all gated on it and permanently unreachable.
Confirmed empty in production before touching the schema:
select count(*) filter (where stripe_customer_id is not null),
count(*) filter (where stripe_subscription_id is not null),
count(*) from users;
→ 0 | 0 | 7
So this removes no data and, more to the point, changes nothing a user sees:
every code path already fell through to the branch below it.
WHAT STAYS. Plans are real and untouched. They are granted through the
OrangeCat/Bitcoin rail (/api/orangecat/entitlement -> recordOcBillingGrant),
with the expiry cron reverting a lapsed pass to free. The pricing CTA cascade
loses only its unreachable middle step: free -> TBA -> [stripe] -> BTC ->
start-free.
WHAT WAS ACTIVELY WRONG. Because isStripeReady() was false, the one part of
Billing that DID render was a notice telling the reader to "add
STRIPE_SECRET_KEY and price IDs" — an environment variable shown to a person
who does not deploy the app and could not act on it. That is the same mistake
NotificationsPill's comment already calls out. Billing now states the plan and
links to /pricing.
Removed: lib/stripe.ts, api/stripe/{checkout,portal,webhook}, api/checkout/
[plan], getUserByStripeCustomerId, two user columns (migration 0063), two
entries from the user-client-view redaction lists, the api/stripe/webhook entry
in proxy.ts's public matcher, the stripe npm dependency, and the smoke/auth
probes for routes that no longer exist.
BillingSettings drops "use client" — with the handlers gone it holds no state,
no effect and no search-param read.
Two mentions deliberately KEPT because they are not the rail: the placeholder
prose describing what a user might build ("Simple dashboard, automated
reminders, Stripe payments…") and the demo project's stack string.
pnpm run verify green. 28 files, 519 deletions.
Claude-Session: https://claude.ai/code/session_01UvjGNAS9CMfEGNW26tUR4P
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
173 lines
7.6 KiB
TypeScript
173 lines
7.6 KiB
TypeScript
// Solon signing + webhook verification — the cross-repo contracts.
|
|
//
|
|
// The signing vector was generated by Solon's own signMessage() (fixed key
|
|
// 0x11…11, UUID session). RFC6979 signing is deterministic, so the signature
|
|
// bytes must match EXACTLY — any drift in magic/varint/digest/header breaks
|
|
// this test instead of breaking Loki's votes in prod. Pure: no DB, no network.
|
|
import assert from "node:assert";
|
|
import {
|
|
addressFromPrivateKey,
|
|
signBitcoinMessage,
|
|
solonVoteMessage,
|
|
} from "../../src/lib/integrations/solon-message";
|
|
import { verifySolonWebhookSignature } from "../../src/lib/integrations/solon-webhook";
|
|
import { createHash, createHmac, createPublicKey, createVerify } from "node:crypto";
|
|
import bs58check from "bs58check";
|
|
|
|
const PRIV = "1111111111111111111111111111111111111111111111111111111111111111";
|
|
const ADDRESS = "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9";
|
|
const SESSION = "11111111-2222-4333-8444-555555555555";
|
|
const MESSAGE = `Solon vote\nsession:${SESSION}\nchoice:yes\nvoter:${ADDRESS}`;
|
|
const SIGNATURE =
|
|
"IPTWOIU6N/j674TVg2T2ej7uRDi5AkcGVRtfY9CC04ezFV6g6um12yrjJ5W8silm95MnfPeVFjIz0HYtd6Aises=";
|
|
|
|
// Address derivation matches Solon's.
|
|
assert.strictEqual(addressFromPrivateKey(PRIV), ADDRESS, "address derivation drifted from Solon");
|
|
|
|
// Canonical message is byte-identical to Solon's voteMessage().
|
|
assert.strictEqual(
|
|
solonVoteMessage({ sessionId: SESSION, choice: "yes", memberAddress: ADDRESS }),
|
|
MESSAGE,
|
|
"canonical vote message drifted from Solon",
|
|
);
|
|
|
|
// Deterministic signature matches Solon's signer byte-for-byte.
|
|
assert.strictEqual(
|
|
signBitcoinMessage(MESSAGE, PRIV),
|
|
SIGNATURE,
|
|
"signature drifted from Solon's signer",
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Foreign witness — OpenSSL, not @noble, verifies the pinned signature.
|
|
//
|
|
// Everything above is one library checking itself: the same code produces the
|
|
// bytes and consumes them, so a change that moves the wire format on BOTH
|
|
// sides passes every assertion while invalidating every signature real wallets
|
|
// produce and every vote already in Solon's audit trail. node:crypto is
|
|
// OpenSSL and speaks secp256k1, so it is the one check here that a
|
|
// @noble/secp256k1 upgrade cannot quietly satisfy.
|
|
//
|
|
// Written and passing against v2 BEFORE the v3 bump. That ordering is the
|
|
// whole technique — a vector added after a migration is fitted to the new
|
|
// output rather than guarding against it.
|
|
|
|
// Pinned on v2, and anchored two lines down to Solon's ADDRESS so this literal
|
|
// can never decay into "whatever the current library happens to return".
|
|
const PUBKEY_UNCOMPRESSED =
|
|
"044f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa" +
|
|
"385b6b1b8ead809ca67454d9683fcf2ba03456d6fe2c4abe2b07f0fbdbb2f1c1";
|
|
|
|
const pubUncompressed = Buffer.from(PUBKEY_UNCOMPRESSED, "hex");
|
|
assert.strictEqual(pubUncompressed.length, 65, "uncompressed point must be 65 bytes");
|
|
assert.strictEqual(pubUncompressed[0], 0x04, "uncompressed point must start with 0x04");
|
|
|
|
// Compress by y-parity — no EC math, so this stays true independent of any
|
|
// library. hash160 + base58check must then reproduce Solon's address, which
|
|
// pins the key material to a value this repo did not generate.
|
|
const pubCompressed = Buffer.concat([
|
|
Buffer.of((pubUncompressed[64] & 1) === 1 ? 0x03 : 0x02),
|
|
pubUncompressed.subarray(1, 33),
|
|
]);
|
|
const hash160 = createHash("ripemd160")
|
|
.update(createHash("sha256").update(pubCompressed).digest())
|
|
.digest();
|
|
assert.strictEqual(
|
|
bs58check.encode(Buffer.concat([Buffer.of(0x00), hash160])),
|
|
ADDRESS,
|
|
"pinned public key no longer derives Solon's address",
|
|
);
|
|
|
|
// Rebuild the signed preimage here instead of importing it, so the magic
|
|
// string and the varint construction are checked too.
|
|
const MAGIC = Buffer.from("Bitcoin Signed Message:\n", "utf8");
|
|
const msgBytes = Buffer.from(MESSAGE, "utf8");
|
|
assert.ok(msgBytes.length < 0xfd, "vector must exercise the single-byte varint path");
|
|
const preimage = Buffer.concat([
|
|
Buffer.of(MAGIC.length),
|
|
MAGIC,
|
|
Buffer.of(msgBytes.length),
|
|
msgBytes,
|
|
]);
|
|
|
|
// OpenSSL applies its own sha256 on top of what it is given, so feed it the
|
|
// SINGLE hash to arrive at Bitcoin's double-sha256.
|
|
const inner = createHash("sha256").update(preimage).digest();
|
|
|
|
const sigBytes = Buffer.from(SIGNATURE, "base64");
|
|
assert.strictEqual(sigBytes.length, 65, "signature must be header || r || s");
|
|
assert.ok(
|
|
sigBytes[0] >= 31 && sigBytes[0] <= 34,
|
|
`compressed-key recovery header out of band: ${sigBytes[0]}`,
|
|
);
|
|
|
|
// Minimal DER: strip leading zeros, then re-pad if the high bit would read as
|
|
// a negative integer.
|
|
const derInt = (raw: Buffer): Buffer => {
|
|
let i = 0;
|
|
while (i < raw.length - 1 && raw[i] === 0) i++;
|
|
const v = raw.subarray(i);
|
|
const body = v[0] & 0x80 ? Buffer.concat([Buffer.of(0x00), v]) : v;
|
|
return Buffer.concat([Buffer.of(0x02, body.length), body]);
|
|
};
|
|
const rDer = derInt(sigBytes.subarray(1, 33));
|
|
const sDer = derInt(sigBytes.subarray(33, 65));
|
|
const derSig = Buffer.concat([Buffer.of(0x30, rDer.length + sDer.length), rDer, sDer]);
|
|
|
|
// SPKI wrapper for an uncompressed secp256k1 point.
|
|
const spki = Buffer.concat([
|
|
Buffer.from("3056301006072a8648ce3d020106052b8104000a034200", "hex"),
|
|
pubUncompressed,
|
|
]);
|
|
const publicKey = createPublicKey({ key: spki, format: "der", type: "spki" });
|
|
|
|
// NB: crypto.verify(null, digest, …) returns false for EC keys — the
|
|
// null-algorithm form is Ed25519-only, and that false is not a bad signature.
|
|
assert.ok(
|
|
createVerify("sha256").update(inner).verify(publicKey, derSig),
|
|
"OpenSSL rejected the signature @noble produced — the wire format drifted",
|
|
);
|
|
|
|
// A witness that cannot fail proves nothing: flip a byte and require rejection.
|
|
const tamperedDigest = Buffer.from(inner);
|
|
tamperedDigest[0] ^= 0xff;
|
|
assert.ok(
|
|
!createVerify("sha256").update(tamperedDigest).verify(publicKey, derSig),
|
|
"OpenSSL accepted a tampered digest — the foreign witness is not actually checking",
|
|
);
|
|
|
|
// Webhook HMAC verification: accepts the correctly signed body, both header forms.
|
|
const secret = "test-secret";
|
|
const body = '{"event":"decision.finalized","decision_id":"x"}';
|
|
const hex = createHmac("sha256", secret).update(body).digest("hex");
|
|
assert.ok(verifySolonWebhookSignature(body, `sha256=${hex}`, secret), "sha256= form rejected");
|
|
assert.ok(verifySolonWebhookSignature(body, hex, secret), "bare hex form rejected");
|
|
assert.ok(
|
|
!verifySolonWebhookSignature(body, `sha256=${"0".repeat(64)}`, secret),
|
|
"forged accepted",
|
|
);
|
|
assert.ok(
|
|
!verifySolonWebhookSignature(`${body} `, `sha256=${hex}`, secret),
|
|
"tampered body accepted",
|
|
);
|
|
assert.ok(!verifySolonWebhookSignature(body, null, secret), "missing header accepted");
|
|
|
|
// Webhook receivers authenticate via HMAC, not a session — the auth middleware
|
|
// matcher MUST exclude them, or NextAuth 401s the POST before the route runs.
|
|
// This exact omission shipped once (Solon doorbell unreachable in prod until a
|
|
// signed replay caught it): pin every self-authenticating receiver here.
|
|
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
const proxySource = readFileSync(join(__dirname, "../../src/proxy.ts"), "utf8");
|
|
const matcherExclusion = proxySource.match(/"\/\(\(\?!(.+)\)\.\+\)"/)?.[1];
|
|
assert.ok(matcherExclusion, "proxy.ts matcher exclusion pattern not found");
|
|
for (const receiver of ["api/orangecat/", "api/solon/"]) {
|
|
assert.ok(
|
|
matcherExclusion.split("|").includes(receiver.replace(/\./g, "\\.")) ||
|
|
matcherExclusion.split("|").includes(receiver),
|
|
`auth middleware must exclude ${receiver} (HMAC-authenticated receiver)`,
|
|
);
|
|
}
|
|
|
|
console.log("solon-message: signing vector + webhook verification + middleware exclusion OK");
|