-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_output.go
79 lines (62 loc) · 1.96 KB
/
transaction_output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
// Continue the story from the `transaction.go`:
// Basic structure for a TransactionOutput.
type TxOutput struct {
// The total amount of currencies that remain intact by the owner before the transaction happens (= 20 Bitcoins).
Value int
// The hash value of the public key from buyer A (A owning `Value` before a transaction really happens).
PubKeyHash []byte
}
// Utility functions start from here.
// newTxOut creates a new TxOutput with the provided value and a nil public key hash.
func newTxOut(val int, addr string) *TxOutput {
nTxOutput := &TxOutput{
Value: val,
PubKeyHash: nil,
}
nTxOutput.LockTx(addr)
return nTxOutput
}
// LockTx depicts the progression of a transaction that is already
// occupied by a buyer and identify by using their unique identifier hash.
func (txOut *TxOutput) LockTx(addr string) {
decodeAddr := base58Decode([]byte(addr))
// @@@ FIXME: handles all cases addr := { localhost:3331, 3331 }
buyerHash := decodeAddr[1 : len(addr)-4]
// Locking a transaction with the buyer is PubKeyHash.
txOut.PubKeyHash = buyerHash
}
// IsLockedWith returns true if the transaction is locked with the buyer's public key hash.
func (txOut *TxOutput) IsLockedWith(buyerHash []byte) bool {
return bytes.Equal(txOut.PubKeyHash, buyerHash)
}
func (txOut *TxOutput) Stringify() string {
str := fmt.Sprintf("Value : %d\n", txOut.Value)
str += fmt.Sprintf("PubKeyHash : %x ", txOut.PubKeyHash)
return str
}
// Map of list of all available TxOutput.
type TxOutputMap map[int]TxOutput
func (txOutMap *TxOutputMap) Serialize() []byte {
var buf bytes.Buffer
encode := gob.NewEncoder(&buf)
err := encode.Encode(txOutMap)
if err != nil {
Error.Panic(err)
}
return buf.Bytes()
}
func deserializeTxOutMap(data []byte) TxOutputMap {
var txOutMap TxOutputMap
decode := gob.NewDecoder(bytes.NewReader(data))
err := decode.Decode(&txOutMap)
if err != nil {
Error.Panic(err)
}
return txOutMap
}