class-transformer work

This commit is contained in:
Ben Wilson
2023-06-17 10:10:14 -04:00
parent f0dd027302
commit 00cb33c43d
7 changed files with 72 additions and 27 deletions
@@ -1,4 +1,5 @@
import { ArrayMaxSize, ArrayMinSize, IsArray } from 'class-validator';
import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsString } from 'class-validator';
import { eRequestMethod } from '../enums/eRequestMethod';
import { StratumBaseMessage } from './StratumBaseMessage';
@@ -10,8 +11,18 @@ export class AuthorizationMessage extends StratumBaseMessage {
@ArrayMaxSize(2)
params: string[];
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[0];
})
public username: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[1];
})
public password: string;
constructor() {
@@ -20,11 +31,6 @@ export class AuthorizationMessage extends StratumBaseMessage {
}
public parse() {
this.username = this.params[0];
this.password = this.params[1];
console.log(`Username ${this.username}, Password: ${this.password}`);
}
public response() {
return {
@@ -1,5 +1,6 @@
import Big from 'big.js';
import { ArrayMaxSize, ArrayMinSize, IsArray } from 'class-validator';
import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsString } from 'class-validator';
import * as crypto from 'crypto';
import { eRequestMethod } from '../enums/eRequestMethod';
@@ -13,11 +14,41 @@ export class MiningSubmitMessage extends StratumBaseMessage {
@ArrayMaxSize(6)
public params: string[];
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[0];
})
public userId: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[1];
})
public jobId: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[2];
})
public extraNonce2: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[3];
})
public ntime: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[4];
})
public nonce: string
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[5];
})
public versionMask: string;
constructor() {
super();
@@ -25,15 +56,6 @@ export class MiningSubmitMessage extends StratumBaseMessage {
}
public parse() {
this.userId = this.params[0];
this.jobId = this.params[1];
this.extraNonce2 = this.params[2];
this.ntime = this.params[3];
this.nonce = this.params[4];
this.versionMask = this.params[5];
}
public response() {
return {
id: null,
@@ -22,7 +22,7 @@ export class SubscriptionMessage extends StratumBaseMessage {
error: null,
result: [
[
['mining.notify', '64d8c004']
// ['mining.notify', '64d8c004']
], //subscription details
clientId, //Extranonce1 - Hex-encoded, per-connection unique string which will be used for coinbase serialization later. Keep it safe!
8 //Extranonce2_size - Represents expected length of extranonce2 which will be generated by the miner.
@@ -1,3 +1,4 @@
import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsNumber } from 'class-validator';
import { eRequestMethod } from '../enums/eRequestMethod';
@@ -9,18 +10,31 @@ export class SuggestDifficulty extends StratumBaseMessage {
@ArrayMinSize(1)
@ArrayMaxSize(1)
@IsNumber({}, { each: true })
params: string[];
params: string | number[];
@Expose()
@IsNumber()
@Transform(({ value, key, obj, type }) => {
return Number(obj.params[0]);
})
public suggestedDifficulty: number;
constructor() {
super();
this.method = eRequestMethod.SUGGEST_DIFFICULTY;
}
public response() {
public response(difficulty: number) {
return {
id: null,
method: eResponseMethod.SET_DIFFICULTY,
params: [256]
params: [difficulty]
}
}
}
}