Skip to content

Release DAEMON v4 production#173

Open
nullxnothing wants to merge 22 commits into
mainfrom
v4
Open

Release DAEMON v4 production#173
nullxnothing wants to merge 22 commits into
mainfrom
v4

Conversation

@nullxnothing
Copy link
Copy Markdown
Owner

Summary

  • ship DAEMON v4 production hardening and cloud console readiness
  • add agent workbench safety queues, IDLE/Shipline/Metaplex flows, and production smoke coverage
  • preserve DAEMON AI auto access mode and deterministic native rebuilds for tests/package/smoke

Verification

  • pnpm run release:check:local


function bearerToken(req: Request): string | null {
const header = req.header('authorization') ?? ''
const match = header.match(/^Bearer\s+(.+)$/i)
Comment on lines +88 to +102
app.use(async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
if (req.path === '/health') return next()
const token = bearerToken(req)
if (!token) return res.status(401).json({ ok: false, error: 'Missing bearer token' })
try {
const entitlement = await options.auth.verifyBearerToken(token)
if (!entitlement.features.includes('daemon-ai')) {
return res.status(403).json({ ok: false, error: 'DAEMON AI entitlement required' })
}
req.daemonAuth = { token, entitlement }
return next()
} catch (error) {
return res.status(401).json({ ok: false, error: errorMessage(error) })
}
})
Comment on lines +553 to +632
app.post('/v1/subscribe', async (req: Request, res: Response) => {
const price = priceConfig(env, req.body?.plan ?? req.query.plan)
const paymentHeader = req.header('x-payment') ?? req.header('payment-signature')
if (!paymentHeader) {
const required = paymentRequiredHeader(price)
res.setHeader('PAYMENT-REQUIRED', required)
res.setHeader('X-Payment-Required', required)
return responseError(res, 402, 'Payment required', 'daemon_pro_payment_required')
}

try {
const payment = await paymentVerifier.verifyPayment(paymentHeader, price)
const existingPayment = subscriptionByPayment(db, payment.paymentId)
if (existingPayment && existingPayment.wallet_address !== payment.walletAddress) {
writeAudit(db, {
walletAddress: payment.walletAddress,
action: 'payment_replay',
plan: payment.plan,
accessSource: 'payment',
paymentId: payment.paymentId,
metadata: { originalWallet: existingPayment.wallet_address },
})
return responseError(res, 409, 'Payment has already been used', 'daemon_pro_payment_replayed')
}
if (existingPayment) {
if (existingPayment.revoked_at !== null || existingPayment.expires_at <= Date.now()) {
return responseError(res, 409, 'Payment has already been used', 'daemon_pro_payment_replayed')
}
const entitlement = entitlementForSubscription(existingPayment)
return res.json({
ok: true,
idempotent: true,
jwt: issueJwt(entitlement, options.jwtSecret),
expiresAt: existingPayment.expires_at,
features: entitlement.features,
tier: entitlement.plan,
plan: entitlement.plan,
paymentId: existingPayment.payment_id,
paidUsdc: payment.paidUsdc,
})
}

const expiresAt = Date.now() + daysToMs(price.durationDays)
const entitlement = entitlementFor({
walletAddress: payment.walletAddress,
plan: payment.plan,
accessSource: 'payment',
expiresAt,
})
writeSubscription(db, {
walletAddress: payment.walletAddress,
plan: payment.plan,
accessSource: 'payment',
paymentId: payment.paymentId,
expiresAt,
features: entitlement.features,
})
writeAudit(db, {
walletAddress: payment.walletAddress,
action: 'payment_subscribe',
plan: payment.plan,
accessSource: 'payment',
paymentId: payment.paymentId,
metadata: { paidUsdc: payment.paidUsdc },
})

return res.json({
ok: true,
jwt: issueJwt(entitlement, options.jwtSecret),
expiresAt,
features: entitlement.features,
tier: payment.plan,
plan: payment.plan,
paymentId: payment.paymentId,
paidUsdc: payment.paidUsdc,
})
} catch (error) {
return responseError(res, 402, error instanceof Error ? error.message : String(error), 'daemon_pro_payment_invalid')
}
})
Comment on lines +634 to +670
app.post('/v1/subscribe/holder/challenge', async (req, res) => {
const wallet = optionalString(req.body?.wallet)
if (!wallet) return responseError(res, 400, 'wallet is required', 'daemon_pro_bad_request')
let walletAddress: string
try {
walletAddress = assertPublicKey(wallet, 'wallet')
} catch (error) {
return responseError(res, 400, error instanceof Error ? error.message : String(error), 'daemon_pro_bad_request')
}

const price = priceConfig(env, 'pro')
const currentAmount = price.holderMint ? await holderVerifier.getHolderBalance(walletAddress, price.holderMint).catch(() => 0) : 0
const status = holderStatus(price, currentAmount)
if (!status.enabled) return responseError(res, 503, 'Holder access is not configured', 'daemon_holder_not_configured')

const nonce = crypto.randomUUID()
const message = [
'DAEMON holder access claim',
`Wallet: ${walletAddress}`,
`Nonce: ${nonce}`,
`Issued At: ${new Date().toISOString()}`,
'No transaction or token transfer is required.',
].join('\n')
const now = Date.now()
db.prepare(`
INSERT INTO daemon_holder_challenges (nonce, wallet_address, message, expires_at, used_at, created_at)
VALUES (?, ?, ?, ?, NULL, ?)
`).run(nonce, walletAddress, message, now + HOLDER_CHALLENGE_TTL_MS, now)
writeAudit(db, {
walletAddress,
action: 'holder_challenge',
accessSource: 'holder',
metadata: { eligible: status.eligible, currentAmount: status.currentAmount },
})

res.json({ ok: true, data: { nonce, message, holderStatus: status } })
})
Comment on lines +672 to +749
app.post('/v1/subscribe/holder/claim', async (req, res) => {
const wallet = optionalString(req.body?.wallet)
const nonce = optionalString(req.body?.nonce)
const signature = optionalString(req.body?.signature)
if (!wallet || !nonce || !signature) return responseError(res, 400, 'wallet, nonce, and signature are required', 'daemon_pro_bad_request')

let walletAddress: string
try {
walletAddress = assertPublicKey(wallet, 'wallet')
} catch (error) {
return responseError(res, 400, error instanceof Error ? error.message : String(error), 'daemon_pro_bad_request')
}

const challenge = db.prepare(`
SELECT wallet_address, nonce, message, expires_at, used_at
FROM daemon_holder_challenges
WHERE nonce = ?
`).get(nonce) as HolderChallengeRow | undefined
if (!challenge || challenge.wallet_address !== walletAddress) return responseError(res, 401, 'Invalid holder challenge', 'daemon_holder_invalid_challenge')
if (challenge.used_at !== null) return responseError(res, 409, 'Holder challenge has already been used', 'daemon_holder_challenge_replayed')
if (challenge.expires_at <= Date.now()) return responseError(res, 401, 'Holder challenge has expired', 'daemon_holder_challenge_expired')

let signatureBytes: Uint8Array
try {
signatureBytes = bs58.decode(signature)
} catch {
return responseError(res, 400, 'Invalid holder signature encoding', 'daemon_pro_bad_request')
}
const verified = nacl.sign.detached.verify(
Buffer.from(challenge.message, 'utf8'),
signatureBytes,
new PublicKey(walletAddress).toBytes(),
)
if (!verified) return responseError(res, 401, 'Invalid holder signature', 'daemon_holder_invalid_signature')

const price = priceConfig(env, 'pro')
if (!price.holderMint || !price.holderMinAmount) return responseError(res, 503, 'Holder access is not configured', 'daemon_holder_not_configured')
const currentAmount = await holderVerifier.getHolderBalance(walletAddress, price.holderMint)
if (currentAmount < price.holderMinAmount) return responseError(res, 403, 'Wallet does not meet holder access requirements', 'daemon_holder_insufficient_balance')

const expiresAt = Date.now() + daysToMs(price.durationDays)
const entitlement = entitlementFor({
walletAddress,
plan: 'pro',
accessSource: 'holder',
expiresAt,
})
db.transaction(() => {
db.prepare('UPDATE daemon_holder_challenges SET used_at = ? WHERE nonce = ?').run(Date.now(), nonce)
writeSubscription(db, {
walletAddress,
plan: 'pro',
accessSource: 'holder',
paymentId: `holder:${nonce}`,
expiresAt,
features: entitlement.features,
})
writeAudit(db, {
walletAddress,
action: 'holder_claim',
plan: 'pro',
accessSource: 'holder',
paymentId: `holder:${nonce}`,
metadata: { currentAmount },
})
})()

res.json({
ok: true,
data: {
jwt: issueJwt(entitlement, options.jwtSecret),
expiresAt,
features: entitlement.features,
tier: 'pro',
plan: 'pro',
},
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants