diff --git a/block.go b/block.go index 2c70cb5..3e90cae 100644 --- a/block.go +++ b/block.go @@ -3,7 +3,10 @@ package bc import ( "bytes" "encoding/gob" + "fmt" "log" + "strconv" + "strings" "time" ) @@ -71,3 +74,20 @@ func DeserializeBlock(d []byte) *Block { return &block } + +func (b *Block) PrintHTML(detail bool) string { + var lines []string + lines = append(lines, fmt.Sprintf("

Block %x

", b.Hash, b.Hash)) + lines = append(lines, fmt.Sprintf("Height: %d
", b.Height)) + lines = append(lines, fmt.Sprintf("Prev. block: %x
", b.PrevBlockHash, b.PrevBlockHash)) + lines = append(lines, fmt.Sprintf("Created at : %s
", time.Unix(b.Timestamp, 0))) + pow := NewProofOfWork(b) + lines = append(lines, fmt.Sprintf("PoW: %s

", strconv.FormatBool(pow.Validate()))) + if detail { + for _, tx := range b.Transactions { + lines = append(lines, tx.PrintHTML()) + } + } + lines = append(lines, fmt.Sprintf("

")) + return strings.Join(lines, "\n") +} diff --git a/blockchain.go b/blockchain.go index 7135732..60b1235 100644 --- a/blockchain.go +++ b/blockchain.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "os" + "strings" "github.com/boltdb/bolt" ) @@ -358,3 +359,20 @@ func dbExists(dbFile string) bool { return true } + +func (bc *Blockchain) PrintHTML() string { + var lines []string + bci := bc.Iterator() + + for { + block := bci.Next() + + lines = append(lines, block.PrintHTML(false)) + + if len(block.PrevBlockHash) == 0 { + break + } + } + return strings.Join(lines, "\n") + +} diff --git a/transaction.go b/transaction.go index 84a31a1..bf1b1bc 100644 --- a/transaction.go +++ b/transaction.go @@ -249,3 +249,32 @@ func DeserializeTransaction(data []byte) Transaction { return transaction } + +func (tx Transaction) PrintHTML() string { + var lines []string + + lines = append(lines, fmt.Sprintf("

Transaction %x:

", tx.ID)) + + for i, input := range tx.Vin { + + pubKeyHash := HashPubKey(input.PubKey) + versionedPayload := append([]byte{version}, pubKeyHash...) + fullPayload := append(versionedPayload, checksum(versionedPayload)...) + + lines = append(lines, fmt.Sprintf("
Input %d:
", i)) + lines = append(lines, fmt.Sprintf("
TXID: %x
", input.Txid)) + lines = append(lines, fmt.Sprintf("
Out: %d
", input.Vout)) + lines = append(lines, fmt.Sprintf("
Signature: %x
", input.Signature)) + lines = append(lines, fmt.Sprintf("
PubKey: %x
", input.PubKey)) + lines = append(lines, fmt.Sprintf("
Addr : %s
", Base58Encode(fullPayload))) + } + + for i, output := range tx.Vout { + lines = append(lines, fmt.Sprintf("
Output %d:
", i)) + lines = append(lines, fmt.Sprintf("
Value: %d
", output.Value)) + lines = append(lines, fmt.Sprintf("
Script: %x
", output.PubKeyHash)) + lines = append(lines, fmt.Sprintf("
Addr : %s
", output.Address)) + } + + return strings.Join(lines, "") +}