import { promises } from "fs" ; import path from "path" ; import { parentPort , workerData } from "worker_threads" ; param powDigest param targetZeros param startIndex returns export async function doPow ( powDigest , targetZeros , startIndex ) { const wasmData = await promises . readFile ( path . join ( __dirname , "../wasm/build/release.wasm" ) ) ; const wasmInstance = await WebAssembly . instantiate ( wasmData , buildImports ( ) ) ; const module = wasmInstance . instance . exports as { setDigest : ( index : number , value : number ) => void ; powWorker : ( target : number , startIndexLo : number , startIndexHi : number ) => void ; getNonceLo : ( ) => number ; getNonceHi : ( ) => number ; } ; for ( let i = 0 ; i < powDigest . length ; i ++ ) { module . setDigest ( i , powDigest [ i ] ) ; } const startIndexNum = BigInt ( startIndex ) ; const startIndexLo = startIndexNum & BigInt ( 0xffffffff ) ; const startIndexHigh = ( startIndexNum >> BigInt ( 32 ) ) & BigInt ( 0xffffffff ) ; module . powWorker ( targetZeros , Number ( startIndexLo ) , Number ( startIndexHigh ) ) ; const nonceLo = module . getNonceLo ( ) ; const nonceHigh = module . getNonceHi ( ) ; return ( BigInt ( nonceLo ) | ( BigInt ( nonceHigh ) << BigInt ( 32 ) ) ) . toString ( ) ; } returns function buildImports ( ) { const env = { abort : ( msg , file , line , colm ) => { } , trace : ( msg , n , ... args ) => { } , seed : Date . now } ; return { Math , Date , env } ; } if ( workerData && parentPort ) { doPow ( workerData . powDigest as Uint8Array , workerData . targetZeros as number , workerData . startIndex as string ) . then ( nonce => { if ( parentPort ) { parentPort . postMessage ( nonce ) ; } } ) . catch ( ( ) => { process . exit ( 1 ) ; } ) ; }