openraas
Version:
Open Robot-as-a-Service Protocol - A comprehensive TypeScript library for building and consuming RaaS applications with X402 payment support on Solana
45 lines (44 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RaaSClient = void 0;
const websocket_1 = require("../transport/websocket");
const x402_1 = require("../core/x402");
class RaaSClient {
constructor(wallet) {
this.wallet = wallet;
this.transport = new websocket_1.WebSocketAdapter();
}
async connect(url) {
await this.transport.connect(url);
this.transport.on('message', (data) => this.handleMessage(data));
}
async sendCommand(command) {
this.transport.send(command);
}
async handleMessage(data) {
if (data.status === 'payment_required' && data.paymentRequest) {
console.log('Payment required, processing...');
await this.handlePayment(data.paymentRequest);
}
else {
console.log('Received:', data);
}
}
async handlePayment(request) {
try {
const payload = await x402_1.X402Payment.signRequest(this.wallet.getKeypair(), request);
// Retry the request with payment payload
// In a real app, we'd need to know *what* request triggered this to retry it.
// For now, we just send the payment payload as a standalone message or header.
this.transport.send({
type: 'payment_submission',
payment: payload
});
console.log('Payment submitted');
}
catch (err) {
console.error('Failed to process payment:', err);
}
}
}
exports.RaaSClient = RaaSClient;