fix nonce

This commit is contained in:
2025-07-27 23:33:37 +03:00
parent d081605cf5
commit 2a5271b9b2

View File

@@ -13,35 +13,16 @@ export enum MessageType {
HELLO = 0, HELLO = 0,
CHALLENGE = 2, CHALLENGE = 2,
RESPONSE = 3, RESPONSE = 3,
} PING = 11,
async function loadWebAssembly(memory) {
const importObject = {
env: {
memory: memory,
},
};
const filename = path.join(__dirname, './memory-pow.wasm.full');
try {
const buffer = await readFile(filename);
const module = await WebAssembly.compile(buffer);
const instance = new WebAssembly.Instance(module, importObject);
return instance;
} catch (error) {
console.error('Error loading WebAssembly module:', error);
throw error;
}
} }
const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 }); const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
const heap = new Uint8Array(memory.buffer); const heap = new Uint8Array(memory.buffer);
const initialBrk = 512 * 1024; const initialBrk = 512 * 1024;
let brk = initialBrk; let brk = initialBrk;
const waitingQueue = []; const waitingQueue: any[] = [];
function sbrk(size) { function sbrk(size: number) {
const oldBrk = brk; const oldBrk = brk;
if (brk + size > heap.length) { if (brk + size > heap.length) {
console.log('Not enough memory available, adding to waiting queue'); console.log('Not enough memory available, adding to waiting queue');
@@ -66,8 +47,8 @@ function processWaitingQueue() {
} }
} }
function requestMemory(size) { function requestMemory(size: number) {
return new Promise((resolve, reject) => { return new Promise<number>((resolve, reject) => {
const ptr = sbrk(size); const ptr = sbrk(size);
if (ptr !== null) { if (ptr !== null) {
resolve(ptr); resolve(ptr);
@@ -77,43 +58,40 @@ function requestMemory(size) {
}); });
} }
interface WasmModule { let wasmInstance: any = null;
exports: WasmExports; let computeLock = false;
async function getWasmInstance(memory: WebAssembly.Memory) {
if (wasmInstance) return wasmInstance;
const filename = path.join(__dirname, './memory-pow.wasm.full');
const buffer = await readFile(filename);
const module = await WebAssembly.compile(buffer);
wasmInstance = new WebAssembly.Instance(module, { env: { memory } });
return wasmInstance;
} }
interface WasmExports { async function computePow(
compute2: ( memory: WebAssembly.Memory,
hashPtr: number, hashPtr: number,
workBufferPtr: number, workBufferPtr: number,
workBufferLength: number, workBufferLength: number,
difficulty: number difficulty: number
) => number; ) {
if (computeLock) throw new Error('Concurrent compute2 call detected');
computeLock = true;
try {
const wasm = await getWasmInstance(memory);
return wasm.exports.compute2(
hashPtr,
workBufferPtr,
workBufferLength,
difficulty
);
} finally {
computeLock = false;
}
} }
const computePow = async (
memory,
hashPtr,
workBufferPtr,
workBufferLength,
difficulty
) => {
let response = null;
await new Promise<void>((resolve) => {
loadWebAssembly(memory).then((wasmModule: any) => {
response = wasmModule.exports.compute2(
hashPtr,
workBufferPtr,
workBufferLength,
difficulty
);
resolve();
});
});
return response;
};
function resetMemory() { function resetMemory() {
brk = initialBrk; brk = initialBrk;
processWaitingQueue(); processWaitingQueue();
@@ -179,9 +157,8 @@ function encodeFramedMessage(type: number, payload: Buffer): Buffer {
return Buffer.concat([header, typeBuf, hasId, length, checksum, payload]); return Buffer.concat([header, typeBuf, hasId, length, checksum, payload]);
} }
function ed25519ToX25519Private(edPrivateKey: Uint8Array): Uint8Array { function ed25519ToX25519Private(edSeed: Uint8Array): Uint8Array {
const seed = edPrivateKey.slice(0, 32); const hash = sha512(edSeed);
const hash = sha512(seed);
const h = new Uint8Array(hash); const h = new Uint8Array(hash);
h[0] &= 248; h[0] &= 248;
h[31] &= 127; h[31] &= 127;
@@ -207,6 +184,8 @@ export class LiteNodeClient {
private theirChallenge: Uint8Array | null = null; private theirChallenge: Uint8Array | null = null;
private ourChallenge = crypto.randomBytes(32); private ourChallenge = crypto.randomBytes(32);
private alreadyResponded = false;
constructor( constructor(
private host: string, private host: string,
private port: number = 12392 private port: number = 12392
@@ -216,38 +195,39 @@ export class LiteNodeClient {
const edSeed = ed25519.utils.randomPrivateKey(); const edSeed = ed25519.utils.randomPrivateKey();
const edPublicKey = ed25519.getPublicKey(edSeed); const edPublicKey = ed25519.getPublicKey(edSeed);
const edPrivateKey = new Uint8Array(64); this.edPrivateKey = new Uint8Array(64);
edPrivateKey.set(edSeed); this.edPrivateKey.set(edSeed);
edPrivateKey.set(edPublicKey, 32); this.edPrivateKey.set(edPublicKey, 32);
this.edPrivateKey = edPrivateKey;
this.edPublicKey = edPublicKey; this.edPublicKey = edPublicKey;
this.xPrivateKey = ed25519ToX25519Private(this.edPrivateKey); this.xPrivateKey = ed25519ToX25519Private(edSeed);
this.xPublicKey = x25519.getPublicKey(this.xPrivateKey); this.xPublicKey = x25519.getPublicKey(this.xPrivateKey);
} }
private async computePoWNonceWasmSafe( private async computePoWNonceWasmSafe(
responseHash: Uint8Array, input: Uint8Array,
difficulty: number, difficulty: number,
workBufferLength: number = 2 * 1024 * 1024 workBufferLength = 2 * 1024 * 1024
): Promise<number> { ): Promise<number> {
try { try {
if (responseHash.length !== 32) resetMemory();
throw new Error('Invalid responseHash length');
const isHashed = input.length === 32;
const hash = isHashed
? input
: crypto.createHash('sha256').update(input).digest();
const hashPtr = sbrk(32); const hashPtr = sbrk(32);
if (hashPtr === null) if (hashPtr === null)
throw new Error('Unable to allocate memory for hash'); throw new Error('Unable to allocate memory for hash');
const hashView = new Uint8Array(memory.buffer, hashPtr, 32); const hashView = new Uint8Array(memory.buffer, hashPtr, 32);
hashView.set(responseHash); hashView.set(hash);
const workBufferPtr = await requestMemory(workBufferLength); const workBufferPtr = await requestMemory(workBufferLength);
if (workBufferPtr === null) if (workBufferPtr === null)
throw new Error('Unable to allocate memory for work buffer'); throw new Error('Unable to allocate memory for work buffer');
const result = await computePow( const nonceValue = await computePow(
memory, memory,
hashPtr, hashPtr,
workBufferPtr, workBufferPtr,
@@ -256,22 +236,26 @@ export class LiteNodeClient {
); );
if ( if (
typeof result !== 'number' || typeof nonceValue !== 'number' ||
result < 0 || nonceValue < 0 ||
!Number.isInteger(result) !Number.isInteger(nonceValue)
) { ) {
throw new Error(`Invalid nonce computed: ${result}`); throw new Error(`Invalid nonce computed: ${nonceValue}`);
} }
return result; return nonceValue;
} catch (error) { } catch (error) {
console.error('❌ PoW nonce computation failed:', error); console.error('❌ PoW nonce computation failed:', error);
resetMemory();
throw error; throw error;
} finally {
resetMemory();
} }
} }
private async handleChallenge(payload: Buffer) { private async handleChallenge(payload: Buffer) {
if (this.alreadyResponded) return;
this.alreadyResponded = true;
this.theirEdPublicKey = payload.subarray(0, 32); this.theirEdPublicKey = payload.subarray(0, 32);
this.theirXPublicKey = ed25519ToX25519Public(this.theirEdPublicKey); this.theirXPublicKey = ed25519ToX25519Public(this.theirEdPublicKey);
this.theirChallenge = payload.subarray(32, 64); this.theirChallenge = payload.subarray(32, 64);
@@ -280,35 +264,44 @@ export class LiteNodeClient {
this.xPrivateKey, this.xPrivateKey,
this.theirXPublicKey this.theirXPublicKey
); );
const combined = Buffer.concat([ const combined = Buffer.concat([
Buffer.from(sharedSecret), Buffer.from(sharedSecret),
this.theirChallenge, this.theirChallenge,
]); ]);
const responseHash = crypto.createHash('sha256').update(combined).digest(); const responseHash = crypto.createHash('sha256').update(combined).digest();
const hashPtr = sbrk(32);
const hashView = new Uint8Array(memory.buffer, hashPtr, 32);
hashView.set(responseHash);
const difficulty = 2;
const nonceValue = await this.computePoWNonceWasmSafe(combined, difficulty);
const powDifficulty = 2;
const nonceValue = await this.computePoWNonceWasmSafe(
responseHash,
powDifficulty
);
const nonce = Buffer.alloc(4); const nonce = Buffer.alloc(4);
nonce.writeUInt32BE(nonceValue); nonce.writeUInt32BE(nonceValue);
const responsePayload = Buffer.concat([nonce, responseHash]); const responsePayload = Buffer.concat([nonce, responseHash]);
console.log('📤 Sending RESPONSE with nonce:', nonceValue);
console.log('🔨 PoW nonce computed:', nonceValue); console.log('🔐 Response hash:', responseHash.toString('hex'));
console.log(
'🔐 Shared secret (hex):',
Buffer.from(sharedSecret).toString('hex')
);
this.sendMessage(MessageType.RESPONSE, responsePayload); this.sendMessage(MessageType.RESPONSE, responsePayload);
resetMemory();
const testNonce = await this.computePoWNonceWasmSafe(
responseHash,
difficulty
);
if (testNonce !== nonceValue) {
console.error('❌ Nonce mismatch on recomputation. Internal bug!');
}
} }
private async handleResponse(payload: Buffer) { private async handleResponse(payload: Buffer) {
console.log('payload', payload); this.startPinging();
const nonce = payload.readUInt32BE(0);
const responseHash = payload.subarray(4, 36);
console.log('📩 Received RESPONSE from core:');
console.log(' Nonce:', nonce);
console.log(' Hash:', responseHash.toString('hex'));
resetMemory();
} }
async connect(): Promise<void> { async connect(): Promise<void> {
@@ -369,6 +362,17 @@ export class LiteNodeClient {
this.socket.write(framed); this.socket.write(framed);
} }
startPinging(intervalMs: number = 5000) {
setInterval(() => {
try {
this.sendMessage(MessageType.PING, Buffer.alloc(0));
console.log('📡 Sent PING');
} catch (err) {
console.error('❌ Failed to send PING:', err);
}
}, intervalMs);
}
close() { close() {
this.socket?.end(); this.socket?.end();
} }