Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions app/api/blockchain-reputation/initialize/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { NextRequest, NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/middleware'
import { initializeReputationRegistry } from '@/lib/blockchain-reputation'

/**
* Initialize on-chain reputation registry for a user
* POST /api/blockchain-reputation/initialize
*
* This creates the initial reputation data entry on the Stellar blockchain
*/
export const POST = withAuth(async (request: NextRequest, auth) => {
try {
const body = await request.json()
const { secretKey } = body

if (!secretKey) {
return NextResponse.json(
{ error: 'Secret key is required', code: 'MISSING_SECRET_KEY' },
{ status: 400 }
)
}

const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'

const result = await initializeReputationRegistry(
auth.walletAddress,
secretKey,
horizonUrl
)

if (!result.success) {
return NextResponse.json(
{ error: result.error, code: 'INITIALIZATION_FAILED' },
{ status: 500 }
)
}

return NextResponse.json({
success: true,
message: 'Reputation registry initialized successfully',
transactionHash: result.transactionHash,
data: result.data
}, { status: 201 })
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
code: 'INTERNAL_ERROR'
},
{ status: 500 }
)
}
})
46 changes: 46 additions & 0 deletions app/api/blockchain-reputation/query/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NextRequest, NextResponse } from 'next/server'
import { getReputationFromChain, verifyReputationRegistry } from '@/lib/blockchain-reputation'

/**
* Query on-chain reputation data
* GET /api/blockchain-reputation/query?wallet=ADDRESS
*
* This retrieves the immutable reputation record from the Stellar blockchain
*/
export const GET = async (request: NextRequest) => {
try {
const walletAddress = request.nextUrl.searchParams.get('wallet')

if (!walletAddress) {
return NextResponse.json(
{ error: 'Wallet address is required', code: 'MISSING_WALLET' },
{ status: 400 }
)
}

const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'

const result = await getReputationFromChain(walletAddress, horizonUrl)

if (!result.success) {
return NextResponse.json(
{ error: result.error, code: 'QUERY_FAILED' },
{ status: 404 }
)
}

return NextResponse.json({
success: true,
data: result.data,
wallet: walletAddress
})
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
code: 'INTERNAL_ERROR'
},
{ status: 500 }
)
}
}
53 changes: 53 additions & 0 deletions app/api/blockchain-reputation/record-completion/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { NextRequest, NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/middleware'
import { recordContractCompletion } from '@/lib/blockchain-reputation'

/**
* Record a contract completion on-chain
* POST /api/blockchain-reputation/record-completion
*
* This updates the on-chain reputation when a contract is completed
*/
export const POST = withAuth(async (request: NextRequest, auth) => {
try {
const body = await request.json()
const { successful } = body

if (typeof successful !== 'boolean') {
return NextResponse.json(
{ error: 'successful boolean field is required', code: 'MISSING_SUCCESSFUL' },
{ status: 400 }
)
}

const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'

const result = await recordContractCompletion(
auth.walletAddress,
successful,
horizonUrl
)

if (!result.success) {
return NextResponse.json(
{ error: result.error, code: 'RECORD_FAILED' },
{ status: 500 }
)
}

return NextResponse.json({
success: true,
message: successful ? 'Contract completion recorded successfully' : 'Contract failure recorded',
transactionHash: result.transactionHash,
data: result.data
})
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
code: 'INTERNAL_ERROR'
},
{ status: 500 }
)
}
})
39 changes: 39 additions & 0 deletions app/api/blockchain-reputation/record-dispute/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NextRequest, NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/middleware'
import { recordDispute } from '@/lib/blockchain-reputation'

/**
* Record a dispute on-chain
* POST /api/blockchain-reputation/record-dispute
*
* This updates the on-chain reputation when a dispute is filed
*/
export const POST = withAuth(async (request: NextRequest, auth) => {
try {
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'

const result = await recordDispute(auth.walletAddress, horizonUrl)

if (!result.success) {
return NextResponse.json(
{ error: result.error, code: 'RECORD_FAILED' },
{ status: 500 }
)
}

return NextResponse.json({
success: true,
message: 'Dispute recorded successfully',
transactionHash: result.transactionHash,
data: result.data
})
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
code: 'INTERNAL_ERROR'
},
{ status: 500 }
)
}
})
81 changes: 81 additions & 0 deletions app/api/blockchain-reputation/update/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { NextRequest, NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/middleware'
import { updateReputationOnChain } from '@/lib/blockchain-reputation'

/**
* Update on-chain reputation data
* POST /api/blockchain-reputation/update
*
* This updates the immutable reputation record on the Stellar blockchain
*/
export const POST = withAuth(async (request: NextRequest, auth) => {
try {
const body = await request.json()
const { completionScore, disputeCount, totalContracts } = body

// Validate that at least one field is being updated
if (completionScore === undefined && disputeCount === undefined && totalContracts === undefined) {
return NextResponse.json(
{ error: 'At least one field must be specified for update', code: 'NO_UPDATE_FIELDS' },
{ status: 400 }
)
}

// Validate completion score range if provided
if (completionScore !== undefined && (completionScore < 0 || completionScore > 100)) {
return NextResponse.json(
{ error: 'Completion score must be between 0 and 100', code: 'INVALID_SCORE' },
{ status: 400 }
)
}

// Validate counts are non-negative if provided
if (disputeCount !== undefined && disputeCount < 0) {
return NextResponse.json(
{ error: 'Dispute count cannot be negative', code: 'INVALID_DISPUTE_COUNT' },
{ status: 400 }
)
}

if (totalContracts !== undefined && totalContracts < 0) {
return NextResponse.json(
{ error: 'Total contracts cannot be negative', code: 'INVALID_CONTRACT_COUNT' },
{ status: 400 }
)
}

const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'

const result = await updateReputationOnChain(
auth.walletAddress,
{
completionScore,
disputeCount,
totalContracts
},
horizonUrl
)

if (!result.success) {
return NextResponse.json(
{ error: result.error, code: 'UPDATE_FAILED' },
{ status: 500 }
)
}

return NextResponse.json({
success: true,
message: 'Reputation updated successfully',
transactionHash: result.transactionHash,
data: result.data
})
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
code: 'INTERNAL_ERROR'
},
{ status: 500 }
)
}
})
40 changes: 40 additions & 0 deletions app/api/blockchain-reputation/verify/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NextRequest, NextResponse } from 'next/server'
import { verifyReputationRegistry } from '@/lib/blockchain-reputation'

/**
* Verify if reputation registry exists on-chain
* GET /api/blockchain-reputation/verify?wallet=ADDRESS
*
* This checks if a wallet has initialized their reputation registry
*/
export const GET = async (request: NextRequest) => {
try {
const walletAddress = request.nextUrl.searchParams.get('wallet')

if (!walletAddress) {
return NextResponse.json(
{ error: 'Wallet address is required', code: 'MISSING_WALLET' },
{ status: 400 }
)
}

const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'

const result = await verifyReputationRegistry(walletAddress, horizonUrl)

return NextResponse.json({
success: true,
exists: result.exists,
wallet: walletAddress,
error: result.error
})
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
code: 'INTERNAL_ERROR'
},
{ status: 500 }
)
}
}
Loading
Loading