// hash_nexa.go
package nexa

/*
#cgo CFLAGS : -I../include
#cgo LDFLAGS: -L../lib -lnexa

#include <stdio.h>
#include <stdlib.h>
#include "nexaapi.h"
*/
import "C"
import (
	//"encoding/hex"
	//"log"
	"unsafe"
)

func BuildPowHash(h NexaBlockHeader) []byte {
	outputs := make([]byte, 32)
	inb := NexaBlockHeaderToBytes(h)
	//log.Println("[nexa]in", hex.EncodeToString(inb))
	in := (*C.uchar)(C.CBytes(inb))
	output := (*C.uchar)(C.malloc(32))
	C.nexa_hash(output, in)
	p := uintptr(unsafe.Pointer(output))
	for i := 0; i < 32; i++ {
		j := *(*byte)(unsafe.Pointer(p))
		outputs[i] = j
		p += unsafe.Sizeof(j)
	}
	C.free(unsafe.Pointer(output))
	C.free(unsafe.Pointer(in))
	outputs32 := make([]byte, 32)
	for i := 0; i < 32; i++ {
		outputs32[i] = outputs[i]
	}
	return outputs32
}

func BuildPowHash12(h NexaBlockHeader12) []byte {
	outputs := make([]byte, 32)
	inb := NexaBlockHeaderToBytes12(h)
	//log.Println("[nexa]in", hex.EncodeToString(inb))
	in := (*C.uchar)(C.CBytes(inb))
	output := (*C.uchar)(C.malloc(32))
	C.nexa_hash12(output, in)
	p := uintptr(unsafe.Pointer(output))
	for i := 0; i < 32; i++ {
		j := *(*byte)(unsafe.Pointer(p))
		outputs[i] = j
		p += unsafe.Sizeof(j)
	}
	C.free(unsafe.Pointer(output))
	C.free(unsafe.Pointer(in))
	outputs32 := make([]byte, 32)
	for i := 0; i < 32; i++ {
		outputs32[i] = outputs[i]
	}
	return outputs32
}
