mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
save work
This commit is contained in:
@@ -1,84 +1,31 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { validate, ValidatorOptions } from 'class-validator';
|
||||
import { Server, Socket } from 'net';
|
||||
|
||||
import { eMethod } from './models/enums/eMethod';
|
||||
import { SubscriptionMessage } from './models/SubscriptionMessage';
|
||||
import { StratumV1Client } from './models/StratumV1Client';
|
||||
|
||||
@Injectable()
|
||||
export class BitcoinStratumProvider {
|
||||
|
||||
public clients: StratumV1Client[] = [];
|
||||
|
||||
private server: Server;
|
||||
|
||||
constructor() {
|
||||
this.server = new Server((socket: Socket) => {
|
||||
console.log('New client connected:', socket.remoteAddress);
|
||||
|
||||
socket.on('data', this.handleData.bind(this, socket));
|
||||
const client = new StratumV1Client(socket);
|
||||
this.clients.push(client);
|
||||
|
||||
console.log('Number of Clients:', this.clients.length);
|
||||
|
||||
socket.on('end', () => {
|
||||
// Handle socket disconnection
|
||||
console.log('Client disconnected:', socket.remoteAddress);
|
||||
});
|
||||
|
||||
socket.on('error', (error: Error) => {
|
||||
// Handle socket error
|
||||
console.error('Socket error:', error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private async handleData(socket: Socket, data: Buffer) {
|
||||
const message = data.toString();
|
||||
console.log('Received:', message);
|
||||
|
||||
// Parse the message and check if it's the initial subscription message
|
||||
const parsedMessage = JSON.parse(message);
|
||||
|
||||
if (parsedMessage.method === eMethod.SUBSCRIBE) {
|
||||
const subscriptionMessage = plainToInstance(
|
||||
SubscriptionMessage,
|
||||
parsedMessage,
|
||||
);
|
||||
|
||||
const validatorOptions: ValidatorOptions = {
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
};
|
||||
|
||||
const errors = await validate(subscriptionMessage, validatorOptions);
|
||||
|
||||
if (errors.length === 0) {
|
||||
const response = this.buildSubscriptionResponse(subscriptionMessage.id);
|
||||
socket.write(JSON.stringify(response) + '\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private buildSubscriptionResponse(requestId: number): any {
|
||||
const subscriptionResponse = {
|
||||
id: requestId,
|
||||
result: [
|
||||
[
|
||||
[
|
||||
'mining.set_difficulty',
|
||||
'0000000c9c7a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9',
|
||||
],
|
||||
[
|
||||
'mining.notify',
|
||||
'0000000c9c7a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9',
|
||||
],
|
||||
],
|
||||
'a843cfc2',
|
||||
4,
|
||||
],
|
||||
};
|
||||
|
||||
return subscriptionResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user