Skip to content

Commit 77d219c

Browse files
authored
Merge branch 'main' into mp-tracker
2 parents 493088b + 5dfac70 commit 77d219c

39 files changed

Lines changed: 5957 additions & 412 deletions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { withAuth } from '@/lib/auth/middleware'
3+
import { initializeReputationRegistry } from '@/lib/blockchain-reputation'
4+
5+
/**
6+
* Initialize on-chain reputation registry for a user
7+
* POST /api/blockchain-reputation/initialize
8+
*
9+
* This creates the initial reputation data entry on the Stellar blockchain
10+
*/
11+
export const POST = withAuth(async (request: NextRequest, auth) => {
12+
try {
13+
const body = await request.json()
14+
const { secretKey } = body
15+
16+
if (!secretKey) {
17+
return NextResponse.json(
18+
{ error: 'Secret key is required', code: 'MISSING_SECRET_KEY' },
19+
{ status: 400 }
20+
)
21+
}
22+
23+
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
24+
25+
const result = await initializeReputationRegistry(
26+
auth.walletAddress,
27+
secretKey,
28+
horizonUrl
29+
)
30+
31+
if (!result.success) {
32+
return NextResponse.json(
33+
{ error: result.error, code: 'INITIALIZATION_FAILED' },
34+
{ status: 500 }
35+
)
36+
}
37+
38+
return NextResponse.json({
39+
success: true,
40+
message: 'Reputation registry initialized successfully',
41+
transactionHash: result.transactionHash,
42+
data: result.data
43+
}, { status: 201 })
44+
} catch (error) {
45+
return NextResponse.json(
46+
{
47+
error: error instanceof Error ? error.message : 'Unknown error',
48+
code: 'INTERNAL_ERROR'
49+
},
50+
{ status: 500 }
51+
)
52+
}
53+
})
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { getReputationFromChain, verifyReputationRegistry } from '@/lib/blockchain-reputation'
3+
4+
/**
5+
* Query on-chain reputation data
6+
* GET /api/blockchain-reputation/query?wallet=ADDRESS
7+
*
8+
* This retrieves the immutable reputation record from the Stellar blockchain
9+
*/
10+
export const GET = async (request: NextRequest) => {
11+
try {
12+
const walletAddress = request.nextUrl.searchParams.get('wallet')
13+
14+
if (!walletAddress) {
15+
return NextResponse.json(
16+
{ error: 'Wallet address is required', code: 'MISSING_WALLET' },
17+
{ status: 400 }
18+
)
19+
}
20+
21+
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
22+
23+
const result = await getReputationFromChain(walletAddress, horizonUrl)
24+
25+
if (!result.success) {
26+
return NextResponse.json(
27+
{ error: result.error, code: 'QUERY_FAILED' },
28+
{ status: 404 }
29+
)
30+
}
31+
32+
return NextResponse.json({
33+
success: true,
34+
data: result.data,
35+
wallet: walletAddress
36+
})
37+
} catch (error) {
38+
return NextResponse.json(
39+
{
40+
error: error instanceof Error ? error.message : 'Unknown error',
41+
code: 'INTERNAL_ERROR'
42+
},
43+
{ status: 500 }
44+
)
45+
}
46+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { withAuth } from '@/lib/auth/middleware'
3+
import { recordContractCompletion } from '@/lib/blockchain-reputation'
4+
5+
/**
6+
* Record a contract completion on-chain
7+
* POST /api/blockchain-reputation/record-completion
8+
*
9+
* This updates the on-chain reputation when a contract is completed
10+
*/
11+
export const POST = withAuth(async (request: NextRequest, auth) => {
12+
try {
13+
const body = await request.json()
14+
const { successful } = body
15+
16+
if (typeof successful !== 'boolean') {
17+
return NextResponse.json(
18+
{ error: 'successful boolean field is required', code: 'MISSING_SUCCESSFUL' },
19+
{ status: 400 }
20+
)
21+
}
22+
23+
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
24+
25+
const result = await recordContractCompletion(
26+
auth.walletAddress,
27+
successful,
28+
horizonUrl
29+
)
30+
31+
if (!result.success) {
32+
return NextResponse.json(
33+
{ error: result.error, code: 'RECORD_FAILED' },
34+
{ status: 500 }
35+
)
36+
}
37+
38+
return NextResponse.json({
39+
success: true,
40+
message: successful ? 'Contract completion recorded successfully' : 'Contract failure recorded',
41+
transactionHash: result.transactionHash,
42+
data: result.data
43+
})
44+
} catch (error) {
45+
return NextResponse.json(
46+
{
47+
error: error instanceof Error ? error.message : 'Unknown error',
48+
code: 'INTERNAL_ERROR'
49+
},
50+
{ status: 500 }
51+
)
52+
}
53+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { withAuth } from '@/lib/auth/middleware'
3+
import { recordDispute } from '@/lib/blockchain-reputation'
4+
5+
/**
6+
* Record a dispute on-chain
7+
* POST /api/blockchain-reputation/record-dispute
8+
*
9+
* This updates the on-chain reputation when a dispute is filed
10+
*/
11+
export const POST = withAuth(async (request: NextRequest, auth) => {
12+
try {
13+
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
14+
15+
const result = await recordDispute(auth.walletAddress, horizonUrl)
16+
17+
if (!result.success) {
18+
return NextResponse.json(
19+
{ error: result.error, code: 'RECORD_FAILED' },
20+
{ status: 500 }
21+
)
22+
}
23+
24+
return NextResponse.json({
25+
success: true,
26+
message: 'Dispute recorded successfully',
27+
transactionHash: result.transactionHash,
28+
data: result.data
29+
})
30+
} catch (error) {
31+
return NextResponse.json(
32+
{
33+
error: error instanceof Error ? error.message : 'Unknown error',
34+
code: 'INTERNAL_ERROR'
35+
},
36+
{ status: 500 }
37+
)
38+
}
39+
})
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { withAuth } from '@/lib/auth/middleware'
3+
import { updateReputationOnChain } from '@/lib/blockchain-reputation'
4+
5+
/**
6+
* Update on-chain reputation data
7+
* POST /api/blockchain-reputation/update
8+
*
9+
* This updates the immutable reputation record on the Stellar blockchain
10+
*/
11+
export const POST = withAuth(async (request: NextRequest, auth) => {
12+
try {
13+
const body = await request.json()
14+
const { completionScore, disputeCount, totalContracts } = body
15+
16+
// Validate that at least one field is being updated
17+
if (completionScore === undefined && disputeCount === undefined && totalContracts === undefined) {
18+
return NextResponse.json(
19+
{ error: 'At least one field must be specified for update', code: 'NO_UPDATE_FIELDS' },
20+
{ status: 400 }
21+
)
22+
}
23+
24+
// Validate completion score range if provided
25+
if (completionScore !== undefined && (completionScore < 0 || completionScore > 100)) {
26+
return NextResponse.json(
27+
{ error: 'Completion score must be between 0 and 100', code: 'INVALID_SCORE' },
28+
{ status: 400 }
29+
)
30+
}
31+
32+
// Validate counts are non-negative if provided
33+
if (disputeCount !== undefined && disputeCount < 0) {
34+
return NextResponse.json(
35+
{ error: 'Dispute count cannot be negative', code: 'INVALID_DISPUTE_COUNT' },
36+
{ status: 400 }
37+
)
38+
}
39+
40+
if (totalContracts !== undefined && totalContracts < 0) {
41+
return NextResponse.json(
42+
{ error: 'Total contracts cannot be negative', code: 'INVALID_CONTRACT_COUNT' },
43+
{ status: 400 }
44+
)
45+
}
46+
47+
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
48+
49+
const result = await updateReputationOnChain(
50+
auth.walletAddress,
51+
{
52+
completionScore,
53+
disputeCount,
54+
totalContracts
55+
},
56+
horizonUrl
57+
)
58+
59+
if (!result.success) {
60+
return NextResponse.json(
61+
{ error: result.error, code: 'UPDATE_FAILED' },
62+
{ status: 500 }
63+
)
64+
}
65+
66+
return NextResponse.json({
67+
success: true,
68+
message: 'Reputation updated successfully',
69+
transactionHash: result.transactionHash,
70+
data: result.data
71+
})
72+
} catch (error) {
73+
return NextResponse.json(
74+
{
75+
error: error instanceof Error ? error.message : 'Unknown error',
76+
code: 'INTERNAL_ERROR'
77+
},
78+
{ status: 500 }
79+
)
80+
}
81+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { verifyReputationRegistry } from '@/lib/blockchain-reputation'
3+
4+
/**
5+
* Verify if reputation registry exists on-chain
6+
* GET /api/blockchain-reputation/verify?wallet=ADDRESS
7+
*
8+
* This checks if a wallet has initialized their reputation registry
9+
*/
10+
export const GET = async (request: NextRequest) => {
11+
try {
12+
const walletAddress = request.nextUrl.searchParams.get('wallet')
13+
14+
if (!walletAddress) {
15+
return NextResponse.json(
16+
{ error: 'Wallet address is required', code: 'MISSING_WALLET' },
17+
{ status: 400 }
18+
)
19+
}
20+
21+
const horizonUrl = process.env.STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
22+
23+
const result = await verifyReputationRegistry(walletAddress, horizonUrl)
24+
25+
return NextResponse.json({
26+
success: true,
27+
exists: result.exists,
28+
wallet: walletAddress,
29+
error: result.error
30+
})
31+
} catch (error) {
32+
return NextResponse.json(
33+
{
34+
error: error instanceof Error ? error.message : 'Unknown error',
35+
code: 'INTERNAL_ERROR'
36+
},
37+
{ status: 500 }
38+
)
39+
}
40+
}

0 commit comments

Comments
 (0)