/** * Complete Purchase Flow Component * * This React component demonstrates the complete user experience * for purchasing products using TurnkeyHQ + Stacks integration * without requiring browser extensions. */ import React, { useState, useEffect } from 'react'; import { TurnkeyStacksIntegration } from './turnkey-stacks-example'; interface Product { id: string; name: string; price: number; // in STX description: string; image: string; category: string; } interface Wallet { address: string; subOrganizationId: string; privateKeyId: string; publicKey: string; } interface PurchaseResult { txId: string; explorerUrl: string; amount: number; recipient: string; productId: string; } const PurchaseFlow: React.FC = () => { // State management const [wallet, setWallet] = useState(null); const [userEmail, setUserEmail] = useState(''); const [loading, setLoading] = useState(false); const [purchasing, setPurchasing] = useState(null); const [purchaseHistory, setPurchaseHistory] = useState([]); const [error, setError] = useState(null); // Initialize integration const integration = new TurnkeyStacksIntegration(); // Sample products const products: Product[] = [ { id: '1', name: 'Premium NFT Collection', price: 0.5, description: 'Exclusive digital artwork with unique properties', image: '/images/nft-premium.jpg', category: 'NFT', }, { id: '2', name: 'VIP Platform Access', price: 1.0, description: 'Premium access to all platform features', image: '/images/vip-access.jpg', category: 'Membership', }, { id: '3', name: 'Custom Smart Contract', price: 2.5, description: 'Deploy your own custom Clarity smart contract', image: '/images/smart-contract.jpg', category: 'Development', }, { id: '4', name: 'Stacks Development Course', price: 0.8, description: 'Complete course on Stacks and Clarity development', image: '/images/course.jpg', category: 'Education', }, ]; // Load wallet from localStorage on component mount useEffect(() => { const savedWallet = localStorage.getItem('turnkey-wallet'); if (savedWallet) { setWallet(JSON.parse(savedWallet)); } }, []); // Create secure wallet with TurnkeyHQ const handleCreateWallet = async () => { if (!userEmail) { setError('Please enter your email address'); return; } setLoading(true); setError(null); try { const userId = `user-${Date.now()}`; // Create wallet with TurnkeyHQ const newWallet = await integration.createUserWallet(userId, userEmail); // Register user on Stacks contract await integration.registerUserOnContract( newWallet.address, newWallet.publicKey, newWallet.subOrganizationId, newWallet.privateKeyId ); setWallet(newWallet); localStorage.setItem('turnkey-wallet', JSON.stringify(newWallet)); console.log('Wallet created and user registered successfully'); } catch (err) { setError(`Failed to create wallet: ${err.message}`); } finally { setLoading(false); } }; // Execute purchase transaction const handlePurchase = async (product: Product) => { if (!wallet) { setError('Wallet not initialized'); return; } setPurchasing(product.id); setError(null); try { const amount = product.price * 1000000; // Convert to microSTX const userNonce = 0; // In real implementation, get from contract const result = await integration.executePurchase( wallet.address, process.env.NEXT_PUBLIC_CONTRACT_ADDRESS!, // Your contract address amount, product.id, wallet.subOrganizationId, wallet.privateKeyId, userNonce ); setPurchaseHistory(prev => [...prev, result]); // Show success message alert(`Purchase successful! Transaction: ${result.txId}`); } catch (err) { setError(`Purchase failed: ${err.message}`); } finally { setPurchasing(null); } }; // Transfer STX using SIP-010 function const handleTransferSTX = async (recipient: string, amount: number) => { if (!wallet) { setError('Wallet not initialized'); return; } setLoading(true); setError(null); try { const result = await integration.transferSTX( wallet.address, recipient, amount * 1000000, // Convert to microSTX wallet.subOrganizationId, wallet.privateKeyId, 'Transfer from embedded wallet' ); alert(`Transfer successful! Transaction: ${result.txId}`); } catch (err) { setError(`Transfer failed: ${err.message}`); } finally { setLoading(false); } }; // Wallet creation form if (!wallet) { return (

Secure Wallet

Create a secure wallet without browser extensions

{error && (

{error}

)}
setUserEmail(e.target.value)} placeholder="Enter your email" className="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" disabled={loading} />

🔒 Security Features

  • • Private keys stored in secure hardware enclaves
  • • No browser extensions required
  • • SIP-010 compliant transactions
  • • Built-in rate limiting and protection
); } // Main purchase interface return (
{/* Header */}

Stacks Marketplace

Secure purchases powered by TurnkeyHQ + Stacks

Wallet Address

{wallet.address.slice(0, 10)}...{wallet.address.slice(-8)}

{error && (

{error}

)} {/* Quick Actions */}

Quick Actions

{/* Products Grid */}
{products.map((product) => (
{product.name[0]}
{product.category} {product.price} STX

{product.name}

{product.description}

))}
{/* Purchase History */} {purchaseHistory.length > 0 && (

Recent Purchases

{purchaseHistory.map((purchase, index) => (

Product {purchase.productId}

{purchase.amount / 1000000} STX to {purchase.recipient.slice(0, 10)}...

{purchase.txId.slice(0, 10)}... View
))}
)}
); }; export default PurchaseFlow;