A project written in go that uses gin-gonic/gin to expose an REST endpoint that receives a data field and hashes it to get a valid hash depending on the difficulty level provided.
Clone this repository and
go run cmd/server/main.go
this will expose the endpoint in your localhost (127.0.0.1) with 8080 port as default. If you want to address another IP or port, use HOST and PORT environment variable.
curl -X POST http://localhost:8080/hash \
-H "Content-Type: application/json" \
-d '{
"version": "1.0",
"data": [
{
"source": "Alice",
"destination": "Bob",
"value": 100,
"id": 1
},
{
"source": "Bob",
"destination": "Charlie",
"value": 50,
"id": 2
}
],
"difficultyTarget": 5
}'
{
"version": "1.0",
"previousHash": "",
"data": [
{
"source": "Alice",
"destination": "Bob",
"value": 100,
"id": 1
},
{
"source": "Bob",
"destination": "Charlie",
"value": 50,
"id": 2
}
],
"difficultyTarget": 6,
"nonce": 15924360,
"hash": "00000037c62d3ad024b26d6cbf5a19a5917744e14ae813b9403133e6d00407fa"
}Inside a block chain you feed the previous hash to the new block with the current transactions ongoing. Meanwhile, the script will try to use the compute power to find the valid has, defined by its difficulty level, so it can mine that block and pass in to the another one.
That iteration will hash the data with all transactions done in the block and will include a new field called Nonce that basically will tell how many iterations the code done to get the "valid" block.
This is a learning project from someone that likes the blockchain logic and exploring some golang capabilities.