Skip to content

Commit

Permalink
encoding and byteArray conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
OsauravO committed Apr 24, 2024
1 parent dc6c780 commit 6325760
Showing 1 changed file with 91 additions and 9 deletions.
100 changes: 91 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ import (
"time"

)
func Uint16ToBytes(n uint16) []byte {
bytes := make([]byte, 2)
binary.LittleEndian.PutUint16(bytes, n)
return bytes
}
func Uint32ToBytes(n uint32) []byte {
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, n)
return bytes
}

func Uint64ToBytes(n uint64) []byte {
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, n)
return bytes
}

func ReverseBytes(data []byte) []byte {
length := len(data)
for i := 0; i < length/2; i++ {
data[i], data[length-i-1] = data[length-i-1], data[i]
}
return data
}

var blockHeader = BlockHeader{
Version: 7,
Expand Down Expand Up @@ -40,7 +64,9 @@ type Input struct {
Prevout Prevout `json:"prevout"`
Scriptsig string `json:"scriptsig"`
ScriptsigAsm string `json:"scriptsig_asm"`
Witness []string `json:"witness"`
IsCoinbase bool `json:"is_coinbase"`
Sequence uint32 `json:"sequence"`
}

type Prevout struct {
Expand Down Expand Up @@ -70,6 +96,15 @@ type TxWeight struct {
Weight int `json:"weight"` // Total weight in weight units
}

type MerkleNode struct {
Left *MerkleNode
Data []byte
Right *MerkleNode
}

type MerkleTree struct {
MerkleRoot *MerkleNode
}

// TargetValue represents the target value for proof-of-work mining
const TargetValue string = "0000ffff00000000000000000000000000000000000000000000000000000000"
Expand Down Expand Up @@ -108,20 +143,67 @@ func arrayVector(a, b []byte) int {

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

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

if checkByteArray(hash, targetBytes) == -1 {
return true
}

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

blockHeader.Nonce++
}
}

if blockHeader.Nonce < 0 || blockHeader.Nonce > 0xffffffff {
return false
}
func ExtractHexFromScriptpubkeyAsm(str []string) string {
for i := 0; i < len(str); i++ {
if str[i] == "OP_PUSHBYTES_20" || str[i] == "OP_PUSHBYTES_32" {
return str[i+1]
}
}
return ""
}

blockHeader.Nonce++
}
func Base58Encode(input []byte) []byte {
var encoded string = base58.Encode(input)
return []byte(encoded)
}

func To_sha(data []byte) []byte {
hash := sha256.Sum256(data)
return hash[:]
}

func JsonData(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}

func Handle(err error) {
if err != nil {
fmt.Println(err)
}
}

func CheckSegWit(tx *Transaction) bool {
for _, vin := range tx.Vin {
if len(vin.Witness) > 0 {
return true
}
}
return false
}


func CreateCoinbase(netReward uint64) *Transaction {
witnessCommitment := CreateWitnessMerkle()
coinbaseTx := Transaction{
Expand Down

0 comments on commit 6325760

Please sign in to comment.