trucoshi 7.0.0 → 7.1.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/dist/lib/classes/Random.d.ts +10 -6
- package/dist/lib/classes/Random.js +66 -17
- package/dist/types.d.ts +5 -0
- package/package.json +2 -2
- package/prisma/client/index.d.ts +309 -241
- package/prisma/client/index.js +4 -2
- package/prisma/client/schema.prisma +7 -8
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { IRandom } from "../../types";
|
|
2
2
|
export declare const Random: () => IRandom;
|
|
3
3
|
export interface IRng {
|
|
4
|
-
combine(client: string, server: string, nonce: number): string;
|
|
4
|
+
combine(client: string, server: string, bitcoinHash: string, nonce: number): string;
|
|
5
5
|
sha512(string: string): string;
|
|
6
6
|
generateServerSeed(): string;
|
|
7
|
+
getBitcoinLatestBlockHash(): Promise<{
|
|
8
|
+
hash: string;
|
|
9
|
+
height: number;
|
|
10
|
+
}>;
|
|
7
11
|
hexToBytes(hex: string): Uint8Array;
|
|
8
|
-
byteGenerator(clientseed: string, serverseed: string, nonce: number): Uint8Array;
|
|
9
|
-
generateInteger(clientSeed: string, serverSeed: string, nonce: number, min: number, max: number): number;
|
|
10
|
-
generateFloat(clientSeed: string, serverSeed: string, nonce: number, precision?: number): number;
|
|
11
|
-
generateBool(clientSeed: string, serverSeed: string, nonce: number): boolean;
|
|
12
|
+
byteGenerator(clientseed: string, serverseed: string, bitcoinHash: string, nonce: number): Uint8Array;
|
|
13
|
+
generateInteger(clientSeed: string, serverSeed: string, bitcoinHash: string, nonce: number, min: number, max: number): number;
|
|
14
|
+
generateFloat(clientSeed: string, serverSeed: string, bitcoinHash: string, nonce: number, precision?: number): number;
|
|
15
|
+
generateBool(clientSeed: string, serverSeed: string, bitcoinHash: string, nonce: number): boolean;
|
|
12
16
|
selectRandomObject<T extends {
|
|
13
17
|
probability: number;
|
|
14
18
|
} = {
|
|
15
19
|
probability: number;
|
|
16
20
|
[x: string]: any;
|
|
17
|
-
}>(clientSeed: string, serverSeed: string, nonce: number, objects: Array<T>): T | null;
|
|
21
|
+
}>(clientSeed: string, serverSeed: string, bitcoinHash: string, nonce: number, objects: Array<T>): T | null;
|
|
18
22
|
}
|
|
@@ -1,18 +1,42 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { accountsApi } from "../../accounts/client";
|
|
1
11
|
import forge from "node-forge";
|
|
2
12
|
const rnd = Rng();
|
|
3
13
|
export const Random = () => {
|
|
4
14
|
const random = {
|
|
5
15
|
secret: rnd.generateServerSeed(),
|
|
6
16
|
clients: [],
|
|
17
|
+
bitcoinHash: "",
|
|
18
|
+
bitcoinHeight: 0,
|
|
7
19
|
nonce: 0,
|
|
20
|
+
getLatestBitcoinBlock() {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const { hash, height } = yield rnd.getBitcoinLatestBlockHash();
|
|
23
|
+
random.bitcoinHash = hash;
|
|
24
|
+
random.bitcoinHeight = height;
|
|
25
|
+
});
|
|
26
|
+
},
|
|
8
27
|
pick(key, max) {
|
|
9
|
-
return rnd.generateInteger(random.clients[key], random.secret, random.nonce, 0, max);
|
|
28
|
+
return rnd.generateInteger(random.clients[key], random.secret, random.bitcoinHash, random.nonce, 0, max);
|
|
10
29
|
},
|
|
11
30
|
next() {
|
|
12
31
|
random.nonce++;
|
|
13
32
|
},
|
|
14
33
|
reveal() {
|
|
15
|
-
return {
|
|
34
|
+
return {
|
|
35
|
+
secret: random.secret,
|
|
36
|
+
clients: [...random.clients],
|
|
37
|
+
bitcoinHash: random.bitcoinHash,
|
|
38
|
+
bitcoinHeight: random.bitcoinHeight,
|
|
39
|
+
};
|
|
16
40
|
},
|
|
17
41
|
};
|
|
18
42
|
return random;
|
|
@@ -22,12 +46,13 @@ function Rng() {
|
|
|
22
46
|
/**
|
|
23
47
|
* Generates a random 256 long hex hash
|
|
24
48
|
*
|
|
25
|
-
* @param {string} clientSeed
|
|
26
|
-
* @param {string} serverSeed
|
|
27
|
-
* @param {
|
|
49
|
+
* @param {string} clientSeed - the clientSeed
|
|
50
|
+
* @param {string} serverSeed - the serverSeed
|
|
51
|
+
* @param {string} bitcoinHash - The latest bitcoin block hash.
|
|
52
|
+
* @param {number} nonce - the nonce
|
|
28
53
|
* @returns {string} combined string
|
|
29
54
|
*/
|
|
30
|
-
combine: (clientSeed, serverSeed, nonce) => clientSeed + serverSeed + nonce,
|
|
55
|
+
combine: (clientSeed, serverSeed, bitcoinHash, nonce) => clientSeed + serverSeed + bitcoinHash + nonce,
|
|
31
56
|
/**
|
|
32
57
|
* Generates a sha512 hash from a string
|
|
33
58
|
*
|
|
@@ -62,11 +87,12 @@ function Rng() {
|
|
|
62
87
|
*
|
|
63
88
|
* @param {string} clientseed - The clientseed to use.
|
|
64
89
|
* @param {string} serverseed - The serverseed to use.
|
|
90
|
+
* @param {string} bitcoinHash - The latest bitcoin block hash.
|
|
65
91
|
* @param {number} nonce - The nonce to use.
|
|
66
92
|
* @returns {Uint8Array} - The generated bytes as a Uint8Array.
|
|
67
93
|
*/
|
|
68
|
-
byteGenerator: function (clientseed, serverseed, nonce) {
|
|
69
|
-
const preHash = this.combine(clientseed, serverseed, nonce);
|
|
94
|
+
byteGenerator: function (clientseed, serverseed, bitcoinHash, nonce) {
|
|
95
|
+
const preHash = this.combine(clientseed, serverseed, bitcoinHash, nonce);
|
|
70
96
|
const hash = this.sha512(preHash);
|
|
71
97
|
return this.hexToBytes(hash.slice(0, 64));
|
|
72
98
|
},
|
|
@@ -75,27 +101,29 @@ function Rng() {
|
|
|
75
101
|
*
|
|
76
102
|
* @param {string} clientSeed - The client seed.
|
|
77
103
|
* @param {string} serverSeed - The server seed.
|
|
104
|
+
* @param {string} bitcoinHash - The latest bitcoin block hash.
|
|
78
105
|
* @param {number} nonce - The nonce.
|
|
79
106
|
* @param {number} min - The minimum value of the range.
|
|
80
107
|
* @param {number} max - The maximum value of the range.
|
|
81
108
|
* @returns {number} A random integer between min and max (inclusive).
|
|
82
109
|
*/
|
|
83
|
-
generateInteger: function (clientSeed, serverSeed, nonce, min, max) {
|
|
84
|
-
const preHash = this.combine(clientSeed, serverSeed, nonce);
|
|
110
|
+
generateInteger: function (clientSeed, serverSeed, bitcoinHash, nonce, min, max) {
|
|
111
|
+
const preHash = this.combine(clientSeed, serverSeed, bitcoinHash, nonce);
|
|
85
112
|
const hash = this.sha512(preHash);
|
|
86
113
|
const range = max - min + 1;
|
|
87
|
-
return (parseInt(hash.slice(0,
|
|
114
|
+
return (parseInt(hash.slice(0, 16), 16) % range) + min;
|
|
88
115
|
},
|
|
89
116
|
/**
|
|
90
117
|
* Generates a random float between 0 and 1 using the fairjs library.
|
|
91
118
|
* @param {string} clientSeed - The client seed to use for the random number generation.
|
|
92
119
|
* @param {string} serverSeed - The server seed to use for the random number generation.
|
|
120
|
+
* @param {string} bitcoinHash - The latest bitcoin block hash.
|
|
93
121
|
* @param {number} nonce - The nonce to use for the random number generation.
|
|
94
122
|
* @param {number} [precision=2] - The number of decimal places to include in the result.
|
|
95
123
|
* @returns {number} - The random float between 0 and 1.
|
|
96
124
|
*/
|
|
97
|
-
generateFloat: function (clientSeed, serverSeed, nonce, precision = 2) {
|
|
98
|
-
const bytes = this.byteGenerator(clientSeed, serverSeed, nonce);
|
|
125
|
+
generateFloat: function (clientSeed, serverSeed, bitcoinHash, nonce, precision = 2) {
|
|
126
|
+
const bytes = this.byteGenerator(clientSeed, serverSeed, bitcoinHash, nonce);
|
|
99
127
|
const float = parseFloat("0." + bytes.join(""));
|
|
100
128
|
return parseFloat(float.toFixed(precision));
|
|
101
129
|
},
|
|
@@ -107,25 +135,28 @@ function Rng() {
|
|
|
107
135
|
* @param {number} nonce - The nonce.
|
|
108
136
|
* @returns {float, boolean} random boolean true/false
|
|
109
137
|
*/
|
|
110
|
-
generateBool: function (clientSeed, serverSeed, nonce) {
|
|
111
|
-
return this.generateFloat(clientSeed, serverSeed, nonce, 10) <= 0.5
|
|
138
|
+
generateBool: function (clientSeed, serverSeed, bitcoinHash, nonce) {
|
|
139
|
+
return this.generateFloat(clientSeed, serverSeed, bitcoinHash, nonce, 10) <= 0.5
|
|
140
|
+
? true
|
|
141
|
+
: false;
|
|
112
142
|
},
|
|
113
143
|
/**
|
|
114
144
|
* Selects a random object from an array of objects based on their probabilities.
|
|
115
145
|
*
|
|
116
146
|
* @param {string} clientSeed - The client seed.
|
|
117
147
|
* @param {string} serverSeed - The server seed.
|
|
148
|
+
* @param {string} bitcoinHash - The latest bitcoin block hash.
|
|
118
149
|
* @param {number} nonce - The nonce.
|
|
119
150
|
* @param {Array} objects - An array of objects with an ID and a probability property.
|
|
120
151
|
* @returns {String} The ID of the randomly selected object.
|
|
121
152
|
*/
|
|
122
|
-
selectRandomObject: function (clientSeed, serverSeed, nonce, objects) {
|
|
153
|
+
selectRandomObject: function (clientSeed, serverSeed, bitcoinHash, nonce, objects) {
|
|
123
154
|
let totalProbability = 0;
|
|
124
155
|
for (const obj of objects) {
|
|
125
156
|
totalProbability += obj.probability;
|
|
126
157
|
}
|
|
127
158
|
const normalizedProbabilities = objects.map((obj) => obj.probability / totalProbability);
|
|
128
|
-
const randomFloat = this.generateFloat(clientSeed, serverSeed, nonce, 10);
|
|
159
|
+
const randomFloat = this.generateFloat(clientSeed, serverSeed, bitcoinHash, nonce, 10);
|
|
129
160
|
let index = 0;
|
|
130
161
|
for (let i = 0; i < normalizedProbabilities.length; i++) {
|
|
131
162
|
index += normalizedProbabilities[i];
|
|
@@ -136,5 +167,23 @@ function Rng() {
|
|
|
136
167
|
// If no object is selected, return null
|
|
137
168
|
return null;
|
|
138
169
|
},
|
|
170
|
+
/**
|
|
171
|
+
* Gets the latest Bitcoin block hash from the LND node
|
|
172
|
+
*
|
|
173
|
+
* @returns {Promise<string>} The latest Bitcoin block hash
|
|
174
|
+
*/
|
|
175
|
+
getBitcoinLatestBlockHash: function () {
|
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
try {
|
|
178
|
+
if (!accountsApi.instance.defaults.baseURL) {
|
|
179
|
+
return { hash: "", height: 0 };
|
|
180
|
+
}
|
|
181
|
+
return (yield accountsApi.wallet.getLatestBitcoinBlock()).data || { hash: "", height: 0 };
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
return { hash: "", height: 0 };
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
},
|
|
139
188
|
};
|
|
140
189
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -237,12 +237,17 @@ export interface IHandPoints {
|
|
|
237
237
|
export interface IRandom {
|
|
238
238
|
secret: string;
|
|
239
239
|
clients: string[];
|
|
240
|
+
bitcoinHash: string;
|
|
241
|
+
bitcoinHeight: number;
|
|
240
242
|
nonce: number;
|
|
243
|
+
getLatestBitcoinBlock(): Promise<void>;
|
|
241
244
|
next(): void;
|
|
242
245
|
pick(idx: number, max: number): number;
|
|
243
246
|
reveal(): {
|
|
244
247
|
secret: string;
|
|
245
248
|
clients: string[];
|
|
249
|
+
bitcoinHash: string;
|
|
250
|
+
bitcoinHeight: number;
|
|
246
251
|
};
|
|
247
252
|
}
|
|
248
253
|
export type IPublicPlayer = Pick<IPlayer, "idx" | "key" | "name" | "abandonedTime" | "accountId" | "avatarUrl" | "disabled" | "abandoned" | "ready" | "hand" | "usedHand" | "prevHand" | "teamIdx" | "isTurn" | "turnExpiresAt" | "turnExtensionExpiresAt" | "isEnvidoTurn" | "isOwner"> & ({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trucoshi",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"description": "Lightning Truco Server",
|
|
5
5
|
"main": "dist/types.js",
|
|
6
6
|
"license": "GPL-3.0",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"dotenv-cli": "^7.3.0",
|
|
77
77
|
"form-data": "^4.0.0",
|
|
78
78
|
"jsonwebtoken": "^9.0.2",
|
|
79
|
-
"lightning-accounts": "
|
|
79
|
+
"lightning-accounts": "4.3.0",
|
|
80
80
|
"node-forge": "^1.3.1",
|
|
81
81
|
"path-scurry": "^1.9.2",
|
|
82
82
|
"pino": "^9.3.2",
|