@relaycore/sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/relay-rwa.ts ADDED
@@ -0,0 +1,128 @@
1
+ import { ethers } from 'ethers';
2
+ import { Facilitator } from '@crypto.com/facilitator-client';
3
+
4
+ export interface RWAConfig {
5
+ apiUrl: string;
6
+ facilitatorAddress?: string;
7
+ provider?: ethers.BrowserProvider | ethers.JsonRpcProvider;
8
+ }
9
+
10
+ export interface RWAStateMachine {
11
+ id: string;
12
+ rwaId: string;
13
+ currentState: string;
14
+ previousState: string | null;
15
+ metadata: Record<string, unknown>;
16
+ createdAt: string;
17
+ updatedAt: string;
18
+ }
19
+
20
+ export interface TransitionParams {
21
+ rwaId: string;
22
+ toState: string;
23
+ agentAddress: string;
24
+ agentRole: string;
25
+ signer: ethers.Signer;
26
+ }
27
+
28
+ export interface TransitionResult {
29
+ success: boolean;
30
+ txHash?: string;
31
+ newState?: string;
32
+ error?: string;
33
+ }
34
+
35
+ /**
36
+ * RelayRWASDK - SDK for managing Real-World Asset (RWA) state machines
37
+ * with x402 payment enforcement and agent coordination.
38
+ */
39
+ export class RelayRWASDK {
40
+ private config: RWAConfig;
41
+
42
+ constructor(config: RWAConfig) {
43
+ this.config = config;
44
+ }
45
+
46
+ /**
47
+ * Create a new RWA state machine
48
+ */
49
+ async create(rwaId: string, metadata: Record<string, unknown> = {}): Promise<RWAStateMachine> {
50
+ const response = await fetch(`${this.config.apiUrl}/api/rwa/state-machine/create`, {
51
+ method: 'POST',
52
+ headers: { 'Content-Type': 'application/json' },
53
+ body: JSON.stringify({ rwaId, metadata })
54
+ });
55
+
56
+ if (!response.ok) {
57
+ throw new Error(`Failed to create RWA: ${response.statusText}`);
58
+ }
59
+
60
+ return await response.json();
61
+ }
62
+
63
+ /**
64
+ * Get RWA state by ID
65
+ */
66
+ async getState(rwaId: string): Promise<RWAStateMachine> {
67
+ const response = await fetch(`${this.config.apiUrl}/api/rwa/state-machine/${rwaId}/state`);
68
+
69
+ if (!response.ok) {
70
+ throw new Error(`Failed to get RWA state: ${response.statusText}`);
71
+ }
72
+
73
+ return await response.json();
74
+ }
75
+
76
+ /**
77
+ * Transition RWA state with x402 payment enforcement
78
+ */
79
+ async transition(params: TransitionParams): Promise<TransitionResult> {
80
+ const { rwaId, toState, agentAddress, agentRole, signer } = params;
81
+
82
+ // Step 1: Request transition (expecting 402)
83
+ const initResponse = await fetch(`${this.config.apiUrl}/api/rwa/state-machine/${rwaId}/transition`, {
84
+ method: 'POST',
85
+ headers: { 'Content-Type': 'application/json' },
86
+ body: JSON.stringify({ rwaId, toState, agentAddress, agentRole })
87
+ });
88
+
89
+ if (initResponse.status !== 402) {
90
+ const data = await initResponse.json();
91
+ return { success: true, newState: data.newState, txHash: data.txHash };
92
+ }
93
+
94
+ // Step 2: Handle x402 Payment
95
+ const paymentRequirements = initResponse.headers.get('XXX-Payment-Config');
96
+ if (!paymentRequirements) {
97
+ throw new Error('No payment requirements received');
98
+ }
99
+
100
+ const requirements = JSON.parse(paymentRequirements);
101
+
102
+ // Generate EIP-3009 signature using Facilitator Client logic
103
+ // Note: In a real SDK bundle, this would use a properly instantiated Facilitator
104
+ // For now, we simulate the client-side signing flow
105
+
106
+ const signerAddress = await signer.getAddress();
107
+ const facilitator = new Facilitator(requirements.payTo); // simplified
108
+
109
+ // This is a placeholder for the actual SDK signing logic which might vary
110
+ // depending on the environment (browser vs node)
111
+ // In a real implementation, we'd reuse the logic from verify-payment.ts
112
+
113
+ // For production use, we assume the user provides a payment header
114
+ // generated by the Facilitator client separately if needed,
115
+ // or we implement the full signing here.
116
+
117
+ // TO DO: Full client-side signing implementation
118
+ // For now, we return specific instructions
119
+ throw new Error('SDK requires a fully initialized Facilitator instance for automatic payment signing. Please use the UI or CLI.');
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Create a new RWA SDK instance
125
+ */
126
+ export function createRWASDK(config: RWAConfig): RelayRWASDK {
127
+ return new RelayRWASDK(config);
128
+ }