|
| 1 | +-- Saved query name: v28_qa_terminal_05_wallet_signer_gate |
| 2 | +-- Purpose: run before "Submit deposit to Bitcode" when the Terminal Deposit |
| 3 | +-- button reports wallet signing/settlement readiness. V28 staging accepts a |
| 4 | +-- provider-backed pending Bitcoin message signature as live signer readiness, |
| 5 | +-- while unsigned provider-session posture remains blocked. |
| 6 | + |
| 7 | +WITH recent_wallet_users AS ( |
| 8 | + SELECT |
| 9 | + u.id AS user_id, |
| 10 | + u.email, |
| 11 | + u.raw_app_meta_data ->> 'provider' AS auth_provider, |
| 12 | + u.raw_user_meta_data ->> 'sub' AS bitcoin_subject, |
| 13 | + u.created_at AS auth_created_at, |
| 14 | + u.last_sign_in_at |
| 15 | + FROM auth.users u |
| 16 | + WHERE |
| 17 | + u.raw_app_meta_data ->> 'provider' = 'custom:bitcode-bitcoin' |
| 18 | + OR u.raw_user_meta_data ->> 'sub' LIKE 'bitcoin:%' |
| 19 | + ORDER BY u.created_at DESC |
| 20 | + LIMIT 10 |
| 21 | +), |
| 22 | +profile_wallets AS ( |
| 23 | + SELECT |
| 24 | + p.id AS user_id, |
| 25 | + p.settings #>> '{bitcodeProfile,walletBinding,provider}' AS profile_wallet_provider, |
| 26 | + p.settings #>> '{bitcodeProfile,walletBinding,status}' AS profile_wallet_status, |
| 27 | + p.settings #>> '{bitcodeProfile,walletBinding,network}' AS profile_wallet_network, |
| 28 | + p.settings #>> '{bitcodeProfile,walletBinding,address}' AS profile_wallet_address, |
| 29 | + p.settings #>> '{bitcodeProfile,walletBinding,authAddress}' AS profile_auth_address, |
| 30 | + p.settings #>> '{bitcodeProfile,walletBinding,paymentAddress}' AS profile_payment_address, |
| 31 | + p.updated_at AS profile_updated_at |
| 32 | + FROM public.user_profiles p |
| 33 | +), |
| 34 | +wallet_connections AS ( |
| 35 | + SELECT DISTINCT ON (c.user_id) |
| 36 | + c.user_id, |
| 37 | + c.provider, |
| 38 | + c.is_active, |
| 39 | + coalesce(c.connection_data ->> 'verification_state', c.connection_data ->> 'status') AS verification_state, |
| 40 | + coalesce(c.connection_data ->> 'proof_kind', c.connection_data ->> 'proofKind') AS proof_kind, |
| 41 | + coalesce(c.connection_data ->> 'address', c.connection_data ->> 'wallet_address') AS connection_address, |
| 42 | + c.connection_data ->> 'network' AS connection_network, |
| 43 | + coalesce(c.connection_data ->> 'authAddress', c.connection_data ->> 'auth_address') AS connection_auth_address, |
| 44 | + coalesce(c.connection_data ->> 'paymentAddress', c.connection_data ->> 'payment_address') AS connection_payment_address, |
| 45 | + coalesce(c.connection_data ->> 'addressType', c.connection_data ->> 'address_type') AS address_type, |
| 46 | + nullif(trim(coalesce(c.connection_data ->> 'message', '')), '') IS NOT NULL AS has_wallet_message, |
| 47 | + nullif(trim(coalesce(c.connection_data ->> 'signature', '')), '') IS NOT NULL AS has_wallet_signature, |
| 48 | + c.updated_at AS wallet_connection_updated_at |
| 49 | + FROM public.user_connections c |
| 50 | + WHERE c.provider IN ('bitcoin-wallet', 'unisat', 'leather', 'okx-bitcoin', 'xverse', 'manual-bitcoin') |
| 51 | + ORDER BY c.user_id, c.is_active DESC, c.updated_at DESC NULLS LAST |
| 52 | +) |
| 53 | +SELECT |
| 54 | + u.user_id, |
| 55 | + u.email, |
| 56 | + u.auth_provider, |
| 57 | + u.bitcoin_subject, |
| 58 | + w.provider AS wallet_provider, |
| 59 | + w.is_active AS wallet_active, |
| 60 | + w.verification_state, |
| 61 | + w.proof_kind, |
| 62 | + w.connection_network, |
| 63 | + w.connection_address, |
| 64 | + w.connection_auth_address, |
| 65 | + w.connection_payment_address, |
| 66 | + w.address_type, |
| 67 | + w.has_wallet_message, |
| 68 | + w.has_wallet_signature, |
| 69 | + p.profile_wallet_status, |
| 70 | + p.profile_wallet_network, |
| 71 | + p.profile_wallet_address, |
| 72 | + p.profile_auth_address, |
| 73 | + p.profile_payment_address, |
| 74 | + CASE |
| 75 | + WHEN w.user_id IS NULL THEN 'blocker:missing_wallet_connection' |
| 76 | + WHEN w.is_active IS DISTINCT FROM true THEN 'blocker:wallet_connection_inactive' |
| 77 | + WHEN w.verification_state = 'verified' THEN 'wallet_signer_verified' |
| 78 | + WHEN w.verification_state = 'pending' |
| 79 | + AND w.proof_kind = 'bitcoin_message_signature' |
| 80 | + AND w.has_wallet_message |
| 81 | + AND w.has_wallet_signature |
| 82 | + THEN 'wallet_signer_pending_signed_proof_accepted_for_v28_staging' |
| 83 | + WHEN w.verification_state = 'pending' |
| 84 | + THEN 'blocker:wallet_pending_without_signed_bitcoin_message_proof' |
| 85 | + ELSE 'blocker:wallet_connection_not_settlement_ready' |
| 86 | + END AS terminal_deposit_signer_gate_state, |
| 87 | + u.auth_created_at, |
| 88 | + u.last_sign_in_at, |
| 89 | + p.profile_updated_at, |
| 90 | + w.wallet_connection_updated_at |
| 91 | +FROM recent_wallet_users u |
| 92 | +LEFT JOIN profile_wallets p ON p.user_id = u.user_id |
| 93 | +LEFT JOIN wallet_connections w ON w.user_id = u.user_id |
| 94 | +ORDER BY u.auth_created_at DESC; |
0 commit comments