added process tx

This commit is contained in:
2025-07-31 09:00:23 +03:00
parent f05626ef2c
commit b14ec24c28
7 changed files with 118 additions and 6 deletions

View File

@@ -15,6 +15,7 @@
"@stablelib/ed25519": "^2.0.2",
"@stablelib/x25519": "^2.0.1",
"adm-zip": "^0.5.16",
"body-parser": "^2.2.0",
"bs58": "^6.0.0",
"chokidar": "^3.6.0",
"decimal.js": "^10.6.0",
@@ -24,6 +25,7 @@
"electron-updater": "^5.3.0",
"electron-window-state": "^5.0.3",
"express": "^5.1.0",
"fast-xml-parser": "^5.2.5",
"tweetnacl": "^1.0.3",
"ws": "^8.18.3"
},
@@ -3701,6 +3703,24 @@
"dev": true,
"license": "MIT"
},
"node_modules/fast-xml-parser": {
"version": "5.2.5",
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-5.2.5.tgz",
"integrity": "sha512-pfX9uG9Ki0yekDHx2SiuRIyFdyAr1kMIMitPvb0YBo8SUfKvia7w7FIyd/l6av85pFYRhZscS75MwMnbvY+hcQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/NaturalIntelligence"
}
],
"license": "MIT",
"dependencies": {
"strnum": "^2.1.0"
},
"bin": {
"fxparser": "src/cli/cli.js"
}
},
"node_modules/fd-slicer": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz",
@@ -6620,6 +6640,18 @@
"node": ">=8"
}
},
"node_modules/strnum": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/strnum/-/strnum-2.1.1.tgz",
"integrity": "sha512-7ZvoFTiCnGxBtDqJ//Cu6fWtZtc7Y3x+QOirG15wztbdngGSkht27o2pyGWrVy0b4WAy3jbKmnoK6g5VlVNUUw==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/NaturalIntelligence"
}
],
"license": "MIT"
},
"node_modules/sumchecker": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/sumchecker/-/sumchecker-3.0.1.tgz",

View File

@@ -44,6 +44,7 @@
"@stablelib/ed25519": "^2.0.2",
"@stablelib/x25519": "^2.0.1",
"adm-zip": "^0.5.16",
"body-parser": "^2.2.0",
"bs58": "^6.0.0",
"chokidar": "^3.6.0",
"decimal.js": "^10.6.0",
@@ -53,6 +54,7 @@
"electron-updater": "^5.3.0",
"electron-window-state": "^5.0.3",
"express": "^5.1.0",
"fast-xml-parser": "^5.2.5",
"tweetnacl": "^1.0.3",
"ws": "^8.18.3"
},

View File

@@ -4,6 +4,7 @@ import {
handleAccount,
handleAccountBalance,
handleActiveChat,
handleProcessTransactionResponseMessage,
} from '../messages/handlers';
import { getRandomClient } from '../peerService';
import { MessageType } from '../protocol/messageTypes';
@@ -11,6 +12,7 @@ import {
createGetAccountBalancePayload,
createGetAccountMessagePayload,
createGetActiveChatPayload,
createProcessTransactionMessagePayload,
Encoding,
} from '../protocol/payloads';
@@ -37,3 +39,15 @@ export async function getAccount(address: string): Promise<any> {
return handleAccount(res);
}
export async function processTransaction(signedBytes: string): Promise<any> {
const client = getRandomClient();
if (!client) throw new Error('No available peers');
const res: Buffer = await client.sendRequest(
MessageType.PROCESS_TRANSACTION,
createProcessTransactionMessagePayload(signedBytes)
);
console.log('res2', res);
return handleProcessTransactionResponseMessage(res);
}

View File

@@ -1,13 +1,33 @@
import express from 'express';
import { createServer } from 'http';
import { WebSocketServer } from 'ws';
import { getAccount, getAccountBalance } from './account';
import { getAccount, getAccountBalance, processTransaction } from './account';
import url from 'url';
import { Encoding } from '../protocol/payloads';
import { getActiveChat } from './chat';
import bodyParser from 'body-parser';
export async function createHttpServer() {
const app = express();
// First route that needs raw body (do not put JSON parser above this)
app.post(
'/transactions/process',
bodyParser.raw({ type: '*/*' }),
async (req, res) => {
try {
const rawBase58 = req.body.toString('utf8').trim();
console.log('📨 Raw Transaction (base58):', rawBase58);
const result = await processTransaction(rawBase58);
res.json(result);
} catch (err: any) {
res.status(500).type('text').send(`Error: ${err.message}`);
}
}
);
// Now apply JSON parser for actual JSON endpoints
app.use(express.json());
app.get('/addresses/balance/:address', async (req, res) => {
@@ -46,6 +66,26 @@ export async function createHttpServer() {
}
});
app.get('/transactions/unitfee', async (req, res) => {
try {
res.type('text').send(1000000);
} catch (err: any) {
res.status(500).type('text').send(`Error: ${err.message}`);
}
});
app.get('/addresses/lastreference/:address', async (req, res) => {
try {
res
.type('text')
.send(
'61EoUF6XwNksjQ2WVcDyMG4dhmRkKHoiBfh6HpUCXh8swsGg8paZaWjhVPE5sbRRdumJBkrRB45iRGv9sBsyDuom'
);
} catch (err: any) {
res.status(500).type('text').send(`Error: ${err.message}`);
}
});
const server = createServer(app);
const wss = new WebSocketServer({ noServer: true });
@@ -53,13 +93,12 @@ export async function createHttpServer() {
const parsedUrl = url.parse(request.url!, true);
const pathname = parsedUrl.pathname || '';
// Basic router for WebSocket endpoints
if (pathname.startsWith('/websockets/chat/active/')) {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request, parsedUrl);
});
} else {
socket.destroy(); // Reject unknown paths
socket.destroy();
}
});
@@ -77,7 +116,6 @@ export async function createHttpServer() {
const encoding = Encoding.BASE64;
// Setup polling
const interval = setInterval(async () => {
try {
const response = await getActiveChat(
@@ -87,7 +125,7 @@ export async function createHttpServer() {
);
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify(response)); // Send to frontend
ws.send(JSON.stringify(response));
}
} catch (err) {
console.error('Failed to fetch active chats:', err);
@@ -108,7 +146,6 @@ export async function createHttpServer() {
clearInterval(interval);
});
} else {
// Optional: handle other routes if needed
ws.close();
}
});

View File

@@ -1,4 +1,5 @@
import bs58 from 'bs58';
import { XMLParser } from 'fast-xml-parser';
import Decimal from 'decimal.js';
@@ -224,3 +225,19 @@ export function handleActiveChat(buffer) {
}
return { groups, direct };
}
export function handleProcessTransactionResponseMessage(payload: Buffer) {
const jsonString = payload.toString('utf-8');
try {
const parsed = JSON.parse(jsonString);
console.log('📬 Received ProcessTransactionResponseMessage:');
console.dir(parsed, { depth: null });
return parsed;
} catch (err) {
console.error('❌ Failed to parse JSON:', jsonString);
return null;
}
}

View File

@@ -12,4 +12,6 @@ export enum MessageType {
GET_ACCOUNT_BALANCE = 171,
ACTIVE_CHAT = 210,
GET_ACTIVE_CHAT = 211,
PROCESS_TRANSACTION_RESPONSE = 300,
PROCESS_TRANSACTION = 301,
}

View File

@@ -55,6 +55,14 @@ export function createGetAccountMessagePayload(address: string): Buffer {
return Buffer.from(addressBytes); // ✅ Just raw payload
}
export function createProcessTransactionMessagePayload(
signedBytes: string
): Buffer {
const signedBytesToBytes = bs58.decode(signedBytes);
return Buffer.from(signedBytesToBytes); // ✅ Just raw payload
}
export enum Encoding {
BASE58 = 0,
BASE64 = 1,