-
Notifications
You must be signed in to change notification settings - Fork 3
/
txoutput.go
48 lines (39 loc) · 1.1 KB
/
txoutput.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
package simpleBlockchain
import "encoding/json"
const coinbaseReward = 5000000000
type TxOut struct {
Value int `json:"value"`
ScriptPubKey Hashes `json:"scriptpubkey"` // put public key hash
}
func CreateCoinbaseTxOut(address string) *TxOut{
return &TxOut{
Value: coinbaseReward,
ScriptPubKey: AddressToPubkeyHash(address),
}
}
func (txOut *TxOut) isCoinbaseTxOut() bool {
return txOut.Value == coinbaseReward
}
// Public_K=G Private_K=(x,y)
// Address=(Network Version) & Ripemd160(sha256(x&y) & checksum
// Checksum=First four bytes of sha256(sha256((Network Version)&Ripemd160(sha256(x&y))
// address base58((0x00||pubkeyHash||checksum(4bytes)))
func AddressToPubkeyHash(address string) []byte {
decodeAddr := Base58Decode([]byte(address))
return decodeAddr[1:len(decodeAddr)-4]
}
func (txOut *TxOut) Serialize() ([]byte, error) {
res, err := json.Marshal(txOut)
if err != nil {
return nil, err
}
return res, nil
}
func DeserializeTxOut(data []byte) (*TxOut, error) {
var txOut TxOut
err := json.Unmarshal(data, &txOut)
if err != nil {
return nil, err
}
return &txOut, nil
}