Skip to content
Closed
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
51 changes: 51 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,54 @@ jobs:
- name: Run Soroban Contract Tests
working-directory: contracts
run: cargo test --workspace

- name: Install Pinned Rust Toolchain
uses: dtolnay/rust-toolchain@1.84.0
with:
targets: wasm32-unknown-unknown

- name: Build Escrow Contract (Deterministic)
working-directory: contracts
env:
RUSTFLAGS: "-C strip=symbols"
SOURCE_DATE_EPOCH: "0"
run: cargo build --release --target wasm32-unknown-unknown --locked

- name: Compute WASM Artifact Hash
id: wasm_hash
run: |
WASM_PATH=$(find contracts/target/wasm32-unknown-unknown/release -name "*.wasm" | head -n 1)
if [ -z "$WASM_PATH" ]; then
echo "::error::WASM artifact not generated; refusing to continue."
exit 1
fi
HASH=$(sha256sum "$WASM_PATH" | awk '{print $1}')
echo "wasm_path=$WASM_PATH" >> "$GITHUB_OUTPUT"
echo "wasm_sha256=$HASH" >> "$GITHUB_OUTPUT"
echo "WASM: $WASM_PATH"
echo "SHA256: $HASH"

- name: Write Build Metadata
env:
WASM_PATH: ${{ steps.wasm_hash.outputs.wasm_path }}
WASM_SHA256: ${{ steps.wasm_hash.outputs.wasm_sha256 }}
run: |
mkdir -p deployment-records
{
echo "git_commit=${GITHUB_SHA}"
echo "git_ref=${GITHUB_REF}"
echo "rust_toolchain=1.84.0"
echo "wasm_path=${WASM_PATH}"
echo "wasm_sha256=${WASM_SHA256}"
echo "built_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
} > deployment-records/build-metadata.env
cat deployment-records/build-metadata.env

- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
name: escrow-wasm-${{ github.sha }}
path: |
${{ steps.wasm_hash.outputs.wasm_path }}
deployment-records/build-metadata.env
retention-days: 365
68 changes: 34 additions & 34 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# next.js
/.next/
/out/

# production
/build

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# env files
.env*

# vercel
.vercel

# codex
.codex/
.codex

# file uploads (milestone deliverables)
/uploads

# typescript
*.tsbuildinfo
next-env.d.ts
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
# next.js
/.next/
/out/
# production
/build
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# env files
# vercel
.vercel
# codex
.codex/
.codex
# file uploads (milestone deliverables)
/uploads
# typescript
*.tsbuildinfo
next-env.d.ts
config.bat
73 changes: 71 additions & 2 deletions app/api/contracts/deploy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ interface DeployContractBody {
milestones?: unknown
}

/**
* Deployment environment guard.
*
* Testnet and Mainnet are configured independently via environment variables.
* The network passphrase and deployment network are validated against the
* configured environment before any on-chain action is taken, which prevents
* accidental deployment to the wrong network.
*/
const STELLAR_NETWORK = process.env.STELLAR_NETWORK ?? ''
const EXPECTED_NETWORK_PASSPHRASE = process.env.STELLAR_NETWORK_PASSPHRASE ?? ''
const MAINNET_DEPLOY_APPROVED = process.env.SOROBAN_MAINNET_DEPLOY_APPROVED === 'true'

function validateDeploymentEnvironment(): string | null {
if (STELLAR_NETWORK !== 'testnet' && STELLAR_NETWORK !== 'mainnet') {
return 'STELLAR_NETWORK must be set to "testnet" or "mainnet"'
}
if (!EXPECTED_NETWORK_PASSPHRASE) {
return 'STELLAR_NETWORK_PASSPHRASE is required for deployment'
}
const expected =
STELLAR_NETWORK === 'testnet'
? 'Test SDF Network ; September 2015'
: 'Public Global Stellar Network ; September 2015'
if (EXPECTED_NETWORK_PASSPHRASE !== expected) {
return `STELLAR_NETWORK_PASSPHRASE does not match network "${STELLAR_NETWORK}"`
}
if (STELLAR_NETWORK === 'mainnet' && !MAINNET_DEPLOY_APPROVED) {
return 'Mainnet deployment requires manual approval: set SOROBAN_MAINNET_DEPLOY_APPROVED=true after review'
}
return null
}

function isPositiveInt(v: unknown): v is number {
return typeof v === 'number' && Number.isInteger(v) && v > 0
}
Expand Down Expand Up @@ -55,6 +87,14 @@ function parseMilestones(raw: unknown): MilestoneInput[] | string {
}

export const POST = withAuth(async (request: NextRequest, auth) => {
const envError = validateDeploymentEnvironment()
if (envError) {
return NextResponse.json(
{ error: envError, code: 'DEPLOYMENT_CONFIG_INVALID' },
{ status: 503 }
)
}

let body: DeployContractBody
try {
body = await request.json()
Expand Down Expand Up @@ -145,6 +185,16 @@ export const POST = withAuth(async (request: NextRequest, auth) => {
return NextResponse.json({ error: message, code: 'DEPLOYMENT_FAILED' }, { status: 500 })
}

if (!deployment.contractAddress || !deployment.txHash) {
return NextResponse.json(
{
error: 'Deployment did not return a contract address and transaction hash; no record was written',
code: 'DEPLOYMENT_INCOMPLETE',
},
{ status: 500 }
)
}

let contract
try {
contract = await createContract({
Expand All @@ -164,7 +214,15 @@ export const POST = withAuth(async (request: NextRequest, auth) => {
}
} catch (err) {
console.error('[contracts/deploy] DB persistence error:', err)
return NextResponse.json({ error: 'Failed to persist contract data', code: 'DB_ERROR' }, { status: 500 })
return NextResponse.json(
{
error: 'Failed to persist contract data; on-chain deployment txHash ' + deployment.txHash + ' must be reconciled manually',
code: 'DB_ERROR',
txHash: deployment.txHash,
contractAddress: deployment.contractAddress,
},
{ status: 500 }
)
}

const users = await sql`SELECT id FROM users WHERE wallet_address = ${auth.walletAddress} LIMIT 1`
Expand All @@ -181,6 +239,8 @@ export const POST = withAuth(async (request: NextRequest, auth) => {
currency,
milestonesCount: milestones.length,
freelancerId: body.freelancerId,
network: STELLAR_NETWORK,
txHash: deployment.txHash,
},
}).catch((err: unknown) => console.error('[activity] Failed to log contract_created:', err))

Expand All @@ -192,7 +252,15 @@ export const POST = withAuth(async (request: NextRequest, auth) => {
actorUserId: Number(actorId),
actorWallet: auth.walletAddress,
newState: 'active',
metadata: { totalAmount: body.totalAmount, currency, milestonesCount: milestones.length },
metadata: {
totalAmount: body.totalAmount,
currency,
milestonesCount: milestones.length,
network: STELLAR_NETWORK,
networkPassphrase: deployment.networkPassphrase,
contractAddress: deployment.contractAddress,
txHash: deployment.txHash,
},
}, auth.walletAddress).catch((err: unknown) => console.error('[audit-log] Failed to log contract_creation:', err))
}

Expand All @@ -203,6 +271,7 @@ export const POST = withAuth(async (request: NextRequest, auth) => {
contractAddress: contract.contract_address,
txHash: contract.contract_tx_hash,
networkPassphrase: contract.network_passphrase,
network: STELLAR_NETWORK,
status: contract.status,
totalAmount: contract.total_amount,
currency: contract.currency,
Expand Down
Loading