From 6325760d1647b8e9cdfd8aae57dd711501b77991 Mon Sep 17 00:00:00 2001 From: OsauravO Date: Thu, 25 Apr 2024 01:06:58 +0530 Subject: [PATCH] encoding and byteArray conversion --- main.go | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 087e3d2..6feee88 100644 --- a/main.go +++ b/main.go @@ -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, @@ -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 { @@ -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" @@ -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{