Skip to content

Commit

Permalink
intial serialization and coinbase txn
Browse files Browse the repository at this point in the history
  • Loading branch information
OsauravO committed Apr 24, 2024
1 parent 8701272 commit dc6c780
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 71 deletions.
66 changes: 0 additions & 66 deletions README.md

This file was deleted.

192 changes: 192 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package main

import (
"encoding/hex"
"encoding/json"
"encoding/binary"
"crypto/sha256"
"os"
"fmt"
"time"

)

var blockHeader = BlockHeader{
Version: 7,
PrevBlockHash: "0000000000000000000000000000000000000000000000000000000000000000",
MerkleRoot: "",
Time: time.Now().Unix(),
Bits: 0x1f00ffff,
Nonce: 0,
}

// // MineBlock mines a new block and writes its data to an output file
// func MineBlock() {
// // Obtain network reward, transaction IDs, and other data
// }

type BlockHeader struct {
Version uint32
PrevBlockHash string
MerkleRoot string
Time int64
Bits uint32
Nonce uint32
}

type Input struct {
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
Prevout Prevout `json:"prevout"`
Scriptsig string `json:"scriptsig"`
ScriptsigAsm string `json:"scriptsig_asm"`
IsCoinbase bool `json:"is_coinbase"`
}

type Prevout struct {
Scriptpubkey string `json:"scriptpubkey"`
ScriptpubkeyAsm string `json:"scriptpubkey_asm"`
ScriptpubkeyType string `json:"scriptpubkey_type"`
ScriptpubkeyAddress string `json:"scriptpubkey_address"`
Value uint64 `json:"value"`
}

type Transaction struct {
Version uint32 `json:"version"`
Locktime uint32 `json:"locktime"`
Vin []Input `json:"vin"`
Vout []Prevout `json:"vout"`
}

type TxInfo struct {
TxID string
WTxID string
Fee uint64
Weight uint64
}
type TxWeight struct {
BaseSize int `json:"base_size"` // Size of non-witness data in bytes
WitnessSize int `json:"witness_size"` // Size of witness data in bytes
Weight int `json:"weight"` // Total weight in weight units
}


// TargetValue represents the target value for proof-of-work mining
const TargetValue string = "0000ffff00000000000000000000000000000000000000000000000000000000"

// arrayVector compares two byte arrays lexicographically
func arrayVector(a, b []byte) int {
minLength := len(a)
if len(b) < minLength {
minLength = len(b)
}

for i := 0; i < minLength; i++ {
if a[i] < b[i] {
return -1
} else if a[i] > b[i] {
return 1
}
}

// for i := range a {
// if a[i] < b[i] {
// return -1
// } else if a[i] > b[i] {
// return 1
// }
// }

if len(a) < len(b) {
return -1
} else if len(a) > len(b) {
return 1
}

return 0
}

// ProofOfWork performs the proof-of-work mining process for a given block header
func ProofOfWork(blockHeader *BlockHeader) bool {
targetBytes, _ := hex.DecodeString(TargetValue)

for {
serzHeader := SerializeBlockHeader(blockHeader)
hash := ReverseBytes(To_sha(To_sha(serzHeader)))

if blockHeader.Nonce < 0 || blockHeader.Nonce > 0xffffffff {
return false
}

blockHeader.Nonce++
}
}

func CreateCoinbase(netReward uint64) *Transaction {
witnessCommitment := CreateWitnessMerkle()
coinbaseTx := Transaction{
Version: 1,
Vin: []Input{
{
TxID: "0000000000000000000000000000000000000000000000000000000000000000",
Vout: 0xffffffff,
Prevout: Prevout{
Scriptpubkey: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyAsm: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyType: "p2pkh",
ScriptpubkeyAddress: "bc1qma9lnumzzpejq2l9ntjepa2lg2re5gdqn3nf0c",
Value: uint64(netReward),
},
IsCoinbase: true,
Sequence: 0xffffffff,
Scriptsig: "03951a0604f15ccf5609013803062b9b5a0100072f425443432f20",
Witness: []string{"0000000000000000000000000000000000000000000000000000000000000000"},
},
},
Vout: []Prevout{
{
Scriptpubkey: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyAsm: "0014df4bf9f3621073202be59ae590f55f42879a21a0",
ScriptpubkeyType: "p2pkh",
ScriptpubkeyAddress: "bc1qma9lnumzzpejq2l9ntjepa2lg2re5gdqn3nf0c",
Value: uint64(netReward),
},
{
Scriptpubkey: "6a24" + "aa21a9ed" + witnessCommitment, //OPRETURN +OP_PUSHBYTES_36+ commitment header + witnessCommitment
ScriptpubkeyAsm: "OP_RETURN" + "OP_PUSHBYTES_36" + "aa21a9ed" + witnessCommitment,
ScriptpubkeyType: "op_return",
ScriptpubkeyAddress: "bc1qma9lnumzzpejq2l9ntjepa2lg2re5gdqn3nf0c",
Value: uint64(0),
},
},
Locktime: 0,
}
return &coinbaseTx
}



func main() {
networkReward, transactionIDs, _ := Prioritize()

// Create a coinbase transaction for the network reward
coinbaseTx := CreateCoinbase(networkReward)
serializedCoinbaseTx, _ := SerializeTransaction(coinbaseTx)

// Perform proof-of-work mining
if ProofOfWork(&blockHeader) {
// Create an output file and write block data
outputFile, _ := os.Create("output.txt")
defer outputFile.Close()

serializedBlockHeader := SerializeBlockHeader(&blockHeader)
segWitCoinbaseTx, _ := SegWitSerialize(coinbaseTx)

outputFile.WriteString(hex.EncodeToString(serializedBlockHeader) + "\n")
outputFile.WriteString(hex.EncodeToString(segWitCoinbaseTx) + "\n")

for _, txID := range transactionIDs {
outputFile.WriteString(txID + "\n")
}
}
}
Empty file added output.txt
Empty file.
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Update this file to run your own code
go run main.go
4 changes: 0 additions & 4 deletions test.sh

This file was deleted.

0 comments on commit dc6c780

Please sign in to comment.