$ErrorActionPreference = "Stop"
$apiUrl = "http://127.0.0.1:3001"
$walletFile = "C:\Users\Allyn Alford\Documents\Backpac.inc\xyz.backpac.load-balancer-ts\test\solana\wallets\wallet1.json"
$walletAddress = "Bny8Aneag41VEw7f5p4okbFGpUDANXssjVThqpP3h4tA" # From wallet1.json
$did = "did:key:z6MkhaXgBZDvotDkL5257faiztiCEsJUTtcCjdReE7m1" # Mock DID or derived DID
$cairn = ".\target\debug\cairn.exe"
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "1. Generating Signed Transaction Payload" -ForegroundColor Cyan
Push-Location ../xyz.backpac.worker-ts
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
npx ts-node scripts/dump-pyusd-tx.ts 2>$null
$ErrorActionPreference = $oldErrorActionPreference
$base64Tx = Get-Content tx_base64.txt -Raw
Pop-Location
if (-not $base64Tx) {
Write-Error "Failed to generate Base64 transaction payload."
exit 1
}
Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host "2. Agent Authentication (Full Handshake)" -ForegroundColor Cyan
Write-Host "-----------------------------------------"
# Step A: Request Challenge
$challengeJson = & $cairn auth challenge --wallet $walletAddress --chain solana:devnet --did $did --api-url $apiUrl
$challengeObj = $challengeJson | ConvertFrom-Json
$nonce = $challengeObj.nonce
if (-not $nonce) {
Write-Error "Failed to retrieve nonce from challenge."
exit 1
}
Write-Host "Nonce acquired: $nonce" -ForegroundColor Green
# Step B: Connect with signature bypass (requires worker-ts bypass logic)
$connectJson = & $cairn auth connect --wallet $walletAddress --chain solana:devnet --did $did --nonce $nonce --signature dummy --key-file $walletFile --api-url $apiUrl
$connectObj = $connectJson | ConvertFrom-Json
$jwt = $connectObj.token
if (-not $jwt) {
Write-Error "Failed to retrieve JWT from connect."
exit 1
}
Write-Host "JWT acquired and session established." -ForegroundColor Green
Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host "3. Creating Proof of Intent (PoI) Boundary" -ForegroundColor Cyan
Write-Host "-----------------------------------------"
$env:BACKPAC_JWT = $jwt
$poiJson = & $cairn poi create --chain solana --network devnet --ttl 600 --key-file $walletFile --api-url $apiUrl
$poiObj = $poiJson | ConvertFrom-Json
$poiId = $poiObj.id
if (-not $poiId) {
$poiId = $poiObj.poi_id
}
if (-not $poiId) {
Write-Error "Failed to retrieve PoI ID."
exit 1
}
Write-Host "PoI Local ID: $poiId" -ForegroundColor Green
Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host "4. Executing Intent (solana-devnet.backpac.xyz)" -ForegroundColor Cyan
Write-Host "-----------------------------------------"
# Solana sendTransaction params: [ "BASE64_STRING", { "encoding": "base64", "skipPreflight": true } ]
$trimmedTx = $base64Tx.Trim()
$paramsJson = '["{0}", {{"encoding": "base64", "skipPreflight": true}}]' -f $trimmedTx
$paramsPath = Join-Path $PSScriptRoot "params_temp.json"
[System.IO.File]::WriteAllText($paramsPath, $paramsJson, (New-Object System.Text.UTF8Encoding $false))
$ChainDomain = "solana-devnet.backpac.xyz" # Assuming this is the intended value for $ChainDomain
$txBase64 = $trimmedTx # Assuming $txBase64 should be $trimmedTx
$intentJson = & $cairn intent send --method sendTransaction --params-file $paramsPath --poi-id $poiId --api-url $apiUrl --worker-url "http://localhost:3000" --x-backpac-hostname "solana-devnet.backpac.xyz"
Write-Host $intentJson -ForegroundColor Yellow
Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host "5. Waiting for Settlement (cairn intent wait)" -ForegroundColor Cyan
Write-Host "-----------------------------------------"
# The resulting intent JSON contains the execution intent ID
$intentObj = $intentJson | ConvertFrom-Json
$intentId = $intentObj.intent_id
if (-not $intentId) {
Write-Host "Could not automatically parse execution intent ID, skipping wait."
} else {
& $cairn intent wait $intentId --timeout 60 --interval 2 --api-url $apiUrl
}
Write-Host "Done!" -ForegroundColor Green