Skip to content

Commit 846870e

Browse files
committed
wip
1 parent 19192c6 commit 846870e

File tree

12 files changed

+1361
-7
lines changed

12 files changed

+1361
-7
lines changed

kms/auth-eth/.env.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Example environment configuration for local testing
2+
3+
# Server configuration
4+
PORT=8000
5+
HOST=127.0.0.1
6+
7+
# Ethereum configuration
8+
ETH_RPC_URL=http://127.0.0.1:8545
9+
KMS_CONTRACT_ADDR=0x0000000000000000000000000000000000000000
10+
11+
# For testing with local Anvil node (Foundry):
12+
# ETH_RPC_URL=http://127.0.0.1:8545
13+
# KMS_CONTRACT_ADDR=<deployed_contract_address>
14+
15+
# For testing with testnet:
16+
# ETH_RPC_URL=https://rpc.sepolia.org
17+
# KMS_CONTRACT_ADDR=<your_deployed_contract_address>

kms/auth-eth/TESTING.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Testing Guide for DStack KMS Auth
2+
3+
Complete testing with **Foundry only** - no Hardhat dependencies.
4+
5+
## 🏗️ Smart Contract Testing
6+
7+
```bash
8+
npm run test:foundry:all # All smart contract tests (36 tests)
9+
```
10+
11+
Covers: Core logic, upgrades, authorization, gas optimization
12+
13+
## 🌐 API Server Testing
14+
15+
### Unit Tests (Fast)
16+
```bash
17+
npm test # Jest with mocked blockchain (4 tests)
18+
```
19+
20+
### Integration Tests (Real Blockchain)
21+
```bash
22+
npm run test:full # Complete: Anvil + Deploy + API tests + Cleanup
23+
```
24+
25+
This automatically:
26+
1. Starts Anvil node
27+
2. Deploys contracts
28+
3. Starts API server
29+
4. Tests all endpoints
30+
5. Cleans up
31+
32+
## 🔧 Manual Testing
33+
34+
### Start Services
35+
```bash
36+
npm run test:full:keep # Keep Anvil + API server running
37+
```
38+
39+
### Test Endpoints
40+
```bash
41+
curl http://127.0.0.1:8000/ # Health check
42+
curl -X POST http://127.0.0.1:8000/bootAuth/app \
43+
-H "Content-Type: application/json" \
44+
-d '{"tcbStatus":"UpToDate","advisoryIds":[],"mrAggregated":"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef","osImageHash":"0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd","mrSystem":"0x9012901290129012901290129012901290129012901290129012901290129012","appId":"0x9012345678901234567890123456789012345678","composeHash":"0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd","instanceId":"0x3456789012345678901234567890123456789012","deviceId":"0xef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12"}'
45+
```
46+
47+
## 🚀 CI/CD Pipeline
48+
49+
```bash
50+
npm test # Fast unit tests
51+
npm run test:foundry:all # Complete contract tests
52+
npm run build # Build check
53+
```

kms/auth-eth/anvil-test.log

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
_ _
4+
(_) | |
5+
__ _ _ __ __ __ _ | |
6+
/ _` | | '_ \ \ \ / / | | | |
7+
| (_| | | | | | \ V / | | | |
8+
\__,_| |_| |_| \_/ |_| |_|
9+
10+
1.2.3-stable (a813a2cee7 2025-06-08T15:42:40.147013149Z)
11+
https://github.com/foundry-rs/foundry
12+
13+
Available Accounts
14+
==================
15+
16+
(0) 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (100.000000000000000000 ETH)
17+
(1) 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (100.000000000000000000 ETH)
18+
(2) 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC (100.000000000000000000 ETH)
19+
20+
Private Keys
21+
==================
22+
23+
(0) 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
24+
(1) 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
25+
(2) 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
26+
27+
Wallet
28+
==================
29+
Mnemonic: test test test test test test test test test test test junk
30+
Derivation path: m/44'/60'/0'/0/
31+
32+
33+
Chain ID
34+
==================
35+
36+
31337
37+
38+
Base Fee
39+
==================
40+
41+
1000000000
42+
43+
Gas Limit
44+
==================
45+
46+
30000000
47+
48+
Genesis Timestamp
49+
==================
50+
51+
1753701293
52+
53+
Genesis Number
54+
==================
55+
56+
0
57+
58+
Listening on 127.0.0.1:8545

kms/auth-eth/broadcast/Deploy.s.sol/31337/run-1753703030.json

Lines changed: 210 additions & 0 deletions
Large diffs are not rendered by default.

kms/auth-eth/broadcast/Deploy.s.sol/31337/run-1753703062.json

Lines changed: 210 additions & 0 deletions
Large diffs are not rendered by default.

kms/auth-eth/broadcast/Deploy.s.sol/31337/run-1753715759.json

Lines changed: 210 additions & 0 deletions
Large diffs are not rendered by default.

kms/auth-eth/broadcast/Deploy.s.sol/31337/run-1753715799.json

Lines changed: 210 additions & 0 deletions
Large diffs are not rendered by default.

kms/auth-eth/broadcast/Deploy.s.sol/31337/run-latest.json

Lines changed: 210 additions & 0 deletions
Large diffs are not rendered by default.

kms/auth-eth/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"test:watch": "jest --watch",
1212
"test:coverage": "jest --coverage",
1313
"test:foundry": "forge test --ffi",
14-
"test:foundry:core": "forge test --ffi --match-path \"test/DstackApp.t.sol\" && forge test --ffi --match-path \"test/DstackKms.t.sol\" && forge test --ffi --match-path \"test/UpgradesBasic.t.sol\""
14+
"test:foundry:all": "forge clean && forge build && forge test --ffi",
15+
"test:full": "./scripts/test-with-anvil.sh",
16+
"test:full:keep": "./scripts/test-with-anvil.sh --keep-running"
1517
},
1618
"dependencies": {
1719
"@fastify/swagger": "^8.12.0",
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/bin/bash
2+
# Complete local testing workflow with Anvil
3+
4+
set -e
5+
6+
echo "🔧 Starting complete local testing workflow..."
7+
8+
# Check if anvil is available
9+
if ! command -v anvil &> /dev/null; then
10+
echo "❌ Error: anvil not found. Install Foundry first:"
11+
echo "curl -L https://foundry.paradigm.xyz | bash"
12+
echo "foundryup"
13+
exit 1
14+
fi
15+
16+
# Clean up any existing processes
17+
echo "🧹 Cleaning up existing processes..."
18+
pkill -f "anvil" || true
19+
pkill -f "ts-node src/main.ts" || true
20+
sleep 1
21+
22+
# Start Anvil in the background
23+
echo "🚀 Starting Anvil local node..."
24+
anvil --host 0.0.0.0 --port 8545 --accounts 10 --balance 1000 > anvil.log 2>&1 &
25+
ANVIL_PID=$!
26+
27+
# Wait for Anvil to start
28+
echo "⏳ Waiting for Anvil to start..."
29+
sleep 3
30+
31+
# Deploy contracts
32+
echo "📦 Deploying contracts..."
33+
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
34+
ETHERSCAN_API_KEY=dummy \
35+
forge script script/Deploy.s.sol:DeployScript --broadcast --rpc-url http://127.0.0.1:8545 > deploy.log 2>&1
36+
37+
if [ $? -ne 0 ]; then
38+
echo "❌ Contract deployment failed. Check deploy.log"
39+
kill $ANVIL_PID
40+
exit 1
41+
fi
42+
43+
# Extract contract addresses from deployment logs
44+
KMS_ADDR=$(grep "DstackKms proxy deployed to:" deploy.log | awk '{print $NF}' || echo "")
45+
46+
if [ -z "$KMS_ADDR" ]; then
47+
echo "❌ Could not extract KMS contract address from deployment"
48+
echo "💡 Check deploy.log for deployment details"
49+
kill $ANVIL_PID
50+
exit 1
51+
fi
52+
53+
echo "✅ Contracts deployed!"
54+
echo " KMS Address: $KMS_ADDR"
55+
56+
# Set environment variables
57+
export ETH_RPC_URL=http://127.0.0.1:8545
58+
export KMS_CONTRACT_ADDR=$KMS_ADDR
59+
60+
# Build TypeScript
61+
echo "🔨 Building TypeScript..."
62+
npm run build
63+
64+
# Start the API server in background
65+
echo "🌐 Starting API server..."
66+
npm run dev > server.log 2>&1 &
67+
SERVER_PID=$!
68+
69+
# Wait for server to start
70+
echo "⏳ Waiting for API server to start..."
71+
sleep 5
72+
73+
# Test the API
74+
echo "🧪 Testing API endpoints..."
75+
76+
# Inline API test
77+
node -e "
78+
const mockBootInfo = {
79+
tcbStatus: 'UpToDate',
80+
advisoryIds: [],
81+
mrAggregated: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
82+
osImageHash: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd',
83+
mrSystem: '0x9012901290129012901290129012901290129012901290129012901290129012',
84+
appId: '0x9012345678901234567890123456789012345678',
85+
composeHash: '0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd',
86+
instanceId: '0x3456789012345678901234567890123456789012',
87+
deviceId: '0xef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12ef12'
88+
};
89+
90+
async function testAPI() {
91+
const baseUrl = 'http://127.0.0.1:8000';
92+
console.log('1️⃣ Health check...');
93+
const health = await fetch(baseUrl + '/');
94+
console.log(' Status:', health.status);
95+
96+
console.log('2️⃣ App authorization...');
97+
const app = await fetch(baseUrl + '/bootAuth/app', {
98+
method: 'POST',
99+
headers: { 'Content-Type': 'application/json' },
100+
body: JSON.stringify(mockBootInfo)
101+
});
102+
console.log(' Status:', app.status);
103+
104+
console.log('3️⃣ KMS authorization...');
105+
const kms = await fetch(baseUrl + '/bootAuth/kms', {
106+
method: 'POST',
107+
headers: { 'Content-Type': 'application/json' },
108+
body: JSON.stringify(mockBootInfo)
109+
});
110+
console.log(' Status:', kms.status);
111+
112+
console.log('4️⃣ Invalid request...');
113+
const invalid = await fetch(baseUrl + '/bootAuth/app', {
114+
method: 'POST',
115+
headers: { 'Content-Type': 'application/json' },
116+
body: JSON.stringify({ mrAggregated: '0x1234' })
117+
});
118+
console.log(' Status:', invalid.status);
119+
}
120+
121+
testAPI().then(() => console.log('✅ API tests completed!')).catch(console.error);
122+
""
123+
124+
echo ""
125+
echo "✅ All tests completed successfully!"
126+
echo ""
127+
echo "📊 Services running:"
128+
echo " Anvil: PID $ANVIL_PID (http://127.0.0.1:8545)"
129+
echo " API Server: PID $SERVER_PID (http://127.0.0.1:8000)"
130+
echo " KMS Contract: $KMS_ADDR"
131+
echo ""
132+
echo "🔍 Logs available in:"
133+
echo " Anvil: anvil.log"
134+
echo " Deployment: deploy.log"
135+
echo " Server: server.log"
136+
echo ""
137+
echo "🛑 To stop services:"
138+
echo " kill $ANVIL_PID $SERVER_PID"
139+
140+
# Function to cleanup on exit
141+
cleanup() {
142+
echo ""
143+
echo "🛑 Stopping services..."
144+
kill $ANVIL_PID $SERVER_PID 2>/dev/null || true
145+
echo "✅ Cleanup completed"
146+
}
147+
148+
# Set trap to cleanup on script exit
149+
trap cleanup EXIT
150+
151+
# Keep script running or exit based on argument
152+
if [ "$1" = "--keep-running" ]; then
153+
echo "🔄 Services will keep running. Press Ctrl+C to stop."
154+
wait
155+
else
156+
echo "ℹ️ Use '--keep-running' to keep services running"
157+
echo "🛑 Stopping services in 10 seconds..."
158+
sleep 10
159+
fi

0 commit comments

Comments
 (0)