Skip to content
Merged
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
34 changes: 34 additions & 0 deletions .github/workflows/managed-agent-guides.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Managed agent guides

on:
pull_request:
push:
branches: [develop, main]

permissions:
contents: read

jobs:
packed-examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
repository: wraith-protocol/sdk
ref: af34393a7676a11876703a9e5e4ce5381bd08700
path: sdk-source
persist-credentials: false
- uses: pnpm/action-setup@v4
with:
version: 10.34.4
- uses: actions/setup-node@v4
with:
node-version: 22
- name: Build SDK packages
working-directory: sdk-source
run: |
pnpm install --frozen-lockfile
pnpm run build
- name: Test the guides as an external package consumer
run: bash scripts/managed-agent/check.sh sdk-source
18 changes: 18 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,21 @@ GitHub also requires a `workflow_dispatch` workflow to exist on the default
branch before it can be manually dispatched, so this PR uses the documented
command against the preview URL instead of adding a workflow that cannot yet
be run for this PR.

### Managed agent examples

The managed-agent workflow builds core 1.4.5 and sdk-agent 0.1.0 from a pinned
SDK commit. To reproduce it with Node 22 and pnpm 10.34.4, run these commands
from the docs root with a fresh SDK checkout:

```sh
git clone https://github.com/wraith-protocol/sdk.git /tmp/wraith-guide-sdk
git -C /tmp/wraith-guide-sdk checkout af34393a7676a11876703a9e5e4ce5381bd08700
pnpm --dir /tmp/wraith-guide-sdk install --frozen-lockfile
pnpm --dir /tmp/wraith-guide-sdk run build
bash scripts/managed-agent/check.sh /tmp/wraith-guide-sdk
```

The check installs both tarballs in a temporary consumer, compiles the
TypeScript fences in the client, single-chain and Stellar cookbook pages,
and runs their request/response fixtures. It needs no platform credentials.
14 changes: 8 additions & 6 deletions guides/single-chain-agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ description: "Create an agent on one blockchain"

Create an agent on one chain and interact with it. This guide walks through the full lifecycle: creation, funding, payments, scanning, withdrawals, invoicing, and privacy.

Use `@wraith-protocol/sdk-agent@0.1.0` with core SDK `1.4.5`. Run these examples on a server with a dedicated testnet wallet. Set `WRAITH_API_KEY` for this agent and obtain ownership signatures from the connected wallet. Balance and status checks do not need a private key export.

## Create the Agent

```typescript
import { Wraith, Chain } from "@wraith-protocol/sdk";
import { Wraith, Chain } from "@wraith-protocol/sdk-agent";

const wraith = new Wraith({ apiKey: "wraith_live_abc123" });
const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });

// Sign a message to prove wallet ownership
const message = "Sign to create Wraith agent";
Expand Down Expand Up @@ -183,7 +185,7 @@ const { secret } = await agent.exportKey(
exportSig,
"Export private key for agent " + agent.info.id
);
console.log(secret); // "0x..." — the agent's private key
// Store the secret securely. Never log it.
```

Requires a fresh wallet signature from the owner.
Expand All @@ -192,11 +194,11 @@ Requires a fresh wallet signature from the owner.

```typescript
// By agent ID
const agent = wraith.agent("agent-uuid");
const byId = wraith.agent("agent-uuid");

// By owner wallet
const agent = await wraith.getAgentByWallet("0xMyWallet");
const byWallet = await wraith.getAgentByWallet("0xMyWallet");

// By .wraith name
const agent = await wraith.getAgentByName("alice");
const byName = await wraith.getAgentByName("alice");
```
99 changes: 63 additions & 36 deletions guides/spectre-stellar-cookbook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ keywords: "Stellar, soroban, freighter, xlm, lumen, friendbot, stellar.expert, p

Three complete recipes for building Stellar agents in production. Each is copy-pasteable and covers the full lifecycle: agent creation, funding, payments, webhooks, and privacy automation.

The examples target core SDK 1.4.5 and managed-agent SDK 0.1.0. Run them on a server. Use a dedicated testnet agent and provide only its credentials to that job. Keep the owner signer in the provisioning step. Monitoring jobs should never hold it or call payment or key-export methods.

> These recipes use `Chain.Stellar` throughout. For multichain variants, see the [Multichain Agent guide](/guides/multichain-agent).

---
Expand All @@ -24,7 +26,8 @@ A SaaS product that receives USDC payments on Stellar, webhooks on every incomin
### Step 1 — Create the agent

```typescript
import { Wraith, Chain } from "@wraith-protocol/sdk";
// create-payments.ts
import { Wraith, Chain } from "@wraith-protocol/sdk-agent";
import { Keypair } from "@stellar/stellar-sdk";

const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
Expand All @@ -34,7 +37,7 @@ const ownerKeypair = Keypair.fromSecret(process.env.OWNER_SECRET!);
const message = "Sign to create Wraith agent";
const signature = ownerKeypair.sign(Buffer.from(message));

const agent = await wraith.createAgent({
export const agent = await wraith.createAgent({
name: "payments", // → payments.wraith
chain: Chain.Stellar,
wallet: ownerKeypair.publicKey(),
Expand Down Expand Up @@ -69,9 +72,9 @@ curl -X POST https://api.usewraith.xyz/agent/create \
{
"id": "a1b2c3d4-...",
"name": "payments",
"chain": "stellar",
"address": "GABC...agent-address",
"metaAddress": "st:xlm:abc123...def456..."
"chains": ["stellar"],
"addresses": { "stellar": "GABC...agent-address" },
"metaAddresses": { "stellar": "st:xlm:abc123...def456..." }
}
```

Expand All @@ -80,7 +83,8 @@ curl -X POST https://api.usewraith.xyz/agent/create \
Stellar testnet funding uses Friendbot. The agent handles this automatically:

```typescript
const res = await agent.chat("fund my wallet");
// create-payments.ts (continued)
export const funding = await agent.chat("fund my wallet");
// "Wallet funded via Friendbot. Balance: 10,000 XLM"
```

Expand Down Expand Up @@ -127,27 +131,23 @@ curl https://api.usewraith.xyz/agent/info/payments \
Poll notifications on a schedule, or wire up a background worker:

```typescript
// poll-payments.ts — run every 60 seconds
async function pollIncoming(agentId: string) {
const res = await fetch(
`https://api.usewraith.xyz/agent/${agentId}/notifications`,
{ headers: { Authorization: `Bearer ${process.env.WRAITH_API_KEY}` } }
);
const { notifications, unreadCount } = await res.json();
// poll-payments.ts (run every 60 seconds)
import { Wraith, type Notification } from "@wraith-protocol/sdk-agent";

export async function pollIncoming(agentId: string) {
const client = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
const agent = client.agent(agentId);
const { notifications, unreadCount } = await agent.getNotifications();

if (unreadCount === 0) return;

for (const n of notifications.filter((n: any) => !n.read)) {
for (const n of notifications.filter((n) => !n.read)) {
if (n.type === "payment_received") {
await handleIncomingPayment(n);
}
}

// Mark all read
await fetch(
`https://api.usewraith.xyz/agent/${agentId}/notifications/read`,
{ method: "POST", headers: { Authorization: `Bearer ${process.env.WRAITH_API_KEY}` } }
);
await agent.markNotificationsRead();
}
```

Expand All @@ -167,7 +167,8 @@ async function pollIncoming(agentId: string) {
Your handler extracts amount, token, and stealth address from the `body` field:

```typescript
async function handleIncomingPayment(notification: any) {
// poll-payments.ts (continued)
async function handleIncomingPayment(notification: Notification) {
// Parse: "50 USDC received at stealth address GABC...xyz via payments.wraith"
const match = notification.body.match(
/^(\S+)\s+(\S+)\s+received at stealth address\s+(\S+)/
Expand All @@ -194,7 +195,7 @@ Schedule this with your platform's cron (AWS EventBridge, GitHub Actions, Vercel

```typescript
// weekly-withdraw.ts
import { Wraith, Chain } from "@wraith-protocol/sdk";
import { Wraith } from "@wraith-protocol/sdk-agent";

export async function weeklyWithdraw() {
const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
Expand Down Expand Up @@ -244,6 +245,8 @@ Enable privacy autopilot to get weekly analysis and preemptive warnings built in

```typescript
// privacy-autopilot.ts — run weekly, or after large payment batches
import { Wraith } from "@wraith-protocol/sdk-agent";

export async function privacyAutopilot(agentId: string) {
const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
const agent = wraith.agent(agentId);
Expand All @@ -268,12 +271,11 @@ export async function privacyAutopilot(agentId: string) {

```typescript
// agent-setup.ts
import { Wraith, Chain } from "@wraith-protocol/sdk";
import { pollIncoming } from "./poll-payments.js";
import { weeklyWithdraw } from "./weekly-withdraw.js";
import { privacyAutopilot } from "./privacy-autopilot.js";
import cron from "node-cron";

const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
const agent = wraith.agent(process.env.AGENT_ID!);

// Poll for payments every 60s
setInterval(() => pollIncoming(process.env.AGENT_ID!), 60_000);

Expand All @@ -300,7 +302,8 @@ A DAO pays contributors weekly in USDC via `.wraith` stealth addresses on Stella
### Step 1 — Create the payroll agent

```typescript
import { Wraith, Chain } from "@wraith-protocol/sdk";
// create-payroll.ts
import { Wraith, Chain } from "@wraith-protocol/sdk-agent";
import { Keypair } from "@stellar/stellar-sdk";

const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
Expand Down Expand Up @@ -362,10 +365,10 @@ export const contributors: Contributor[] = [

```typescript
// payroll-run.ts
import { Wraith } from "@wraith-protocol/sdk";
import { contributors } from "./payroll-registry";
import { Wraith } from "@wraith-protocol/sdk-agent";
import { contributors } from "./payroll-registry.js";

interface PayrollResult {
export interface PayrollResult {
contributorId: string;
wraithName: string;
amount: number;
Expand Down Expand Up @@ -447,6 +450,9 @@ curl -X POST https://api.usewraith.xyz/agent/$AGENT_ID/chat \

```typescript
// payroll-monitor.ts
import { Wraith } from "@wraith-protocol/sdk-agent";
import type { PayrollResult } from "./payroll-run.js";

export async function monitorAndRetry(
results: PayrollResult[],
agentId: string
Expand Down Expand Up @@ -485,7 +491,7 @@ export async function monitorAndRetry(
}

// Alert function — wire to Slack, PagerDuty, email, etc.
async function alertOps(info: {
export async function alertOps(info: {
contributor: string;
amount: number;
error: string;
Expand All @@ -506,6 +512,8 @@ Contributors update their `.wraith` destination monthly to prevent pattern analy

```typescript
// rotation.ts
import { contributors } from "./payroll-registry.js";

export async function rotateDestinations(
updates: Array<{ contributorId: string; newWraithName: string }>
) {
Expand Down Expand Up @@ -533,6 +541,10 @@ To rotate, contributors call `wraith.getAgentByName("alice")` on a new agent and

```typescript
// scheduler.ts
import { Wraith } from "@wraith-protocol/sdk-agent";
import { alertOps, monitorAndRetry } from "./payroll-monitor.js";
import { runWeeklyPayroll } from "./payroll-run.js";
import { contributors } from "./payroll-registry.js";
import cron from "node-cron";

// Every Friday at 16:00 UTC
Expand Down Expand Up @@ -599,7 +611,8 @@ A weekly background agent that analyzes stealth payment patterns, sends Slack/em
### Step 1 — Create the monitoring agent

```typescript
import { Wraith, Chain } from "@wraith-protocol/sdk";
// create-monitor.ts
import { Wraith, Chain } from "@wraith-protocol/sdk-agent";
import { Keypair } from "@stellar/stellar-sdk";

const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });
Expand Down Expand Up @@ -638,7 +651,7 @@ curl -X POST https://api.usewraith.xyz/agent/create \

```tsx
// privacy-analysis.ts
import { Wraith } from "@wraith-protocol/sdk";
import { Wraith } from "@wraith-protocol/sdk-agent";

export interface PrivacyReport {
agentId: string;
Expand Down Expand Up @@ -725,6 +738,7 @@ Best Practices:

```tsx
// reporters.ts
import type { PrivacyReport } from "./privacy-analysis.js";

// Slack report
export async function sendSlackReport(report: PrivacyReport) {
Expand Down Expand Up @@ -822,6 +836,8 @@ When the privacy score drops below a threshold, the agent suggests new `.wraith`

```typescript
// auto-rotate.ts
import { Wraith } from "@wraith-protocol/sdk-agent";
import type { PrivacyReport } from "./privacy-analysis.js";
export async function generateRotationPlan(
agentId: string,
report: PrivacyReport
Expand Down Expand Up @@ -865,6 +881,9 @@ export async function applyRotation(

```typescript
// weekly-privacy-job.ts
import { analyzePrivacy } from "./privacy-analysis.js";
import { generateRotationPlan } from "./auto-rotate.js";
import { sendEmailReport, sendSlackReport } from "./reporters.js";
import cron from "node-cron";

// Every Monday at 08:00 UTC
Expand Down Expand Up @@ -1024,11 +1043,15 @@ Or a name-transfer endpoint that moves ownership to a new agent without requirin
All three recipes reconnect to a pre-created agent by ID. Store the agent ID in your environment after the first `createAgent` call:

```typescript
// Reconnect — no new agent is created
// reconnect.ts
import { Wraith } from "@wraith-protocol/sdk-agent";
const wraith = new Wraith({ apiKey: process.env.WRAITH_API_KEY! });

// Reconnect by ID
const agent = wraith.agent(process.env.AGENT_ID!);

// Or by name
const agent = await wraith.getAgentByName("payments");
const byName = await wraith.getAgentByName("payments");
```

```bash
Expand Down Expand Up @@ -1058,6 +1081,7 @@ curl https://api.usewraith.xyz/agent/$AGENT_ID/status \
### Error handling

```typescript
// reconnect.ts (continued)
try {
const res = await agent.chat("send 500 USDC to alice.wraith on stellar");
} catch (err: any) {
Expand Down Expand Up @@ -1086,10 +1110,13 @@ All API errors follow:
New stealth addresses on Stellar require a `createAccount` operation (not `payment`) to activate the account with the 1 XLM minimum balance. The Wraith agent handles this automatically. If you're using the low-level SDK:

```typescript
// activate-account.ts
import { generateStealthAddress } from "@wraith-protocol/sdk/chains/stellar";

const stealth = generateStealthAddress(spendingPubKey, viewingPubKey);
// stealth.stealthAddress is a G... address with no on-chain existence yet
export function receivingAddress(spendingPubKey: Uint8Array, viewingPubKey: Uint8Array) {
return generateStealthAddress(spendingPubKey, viewingPubKey).stealthAddress;
}
// The returned G... address is an account with no on-chain existence yet
// Use Operation.createAccount to fund it for the first time
// Use stealth-sender contract for subsequent USDC transfers
```
Expand Down
Loading
Loading