-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
136 lines (109 loc) · 2.68 KB
/
types.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package common
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"github.com/libp2p/go-libp2p-peer"
"github.com/tinychain/tinychain/p2p/pb"
"math/big"
)
const (
HashLength = 32
AddressLength = 20
)
type Hash [HashLength]byte
// BigToHash sets byte representation of b to hash.
// If b is larger than len(h), b will be cropped from the left.
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
// HexToHash sets byte representation of s to hash.
// If b is larger than len(h), b will be cropped from the left.
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
func (h Hash) String() string {
return string(h[:])
}
func (h Hash) Bytes() []byte {
return h[:]
}
func (h Hash) Hex() string {
return string(Hex(h[:]))
}
func (h Hash) Big() *big.Int {
return new(big.Int).SetBytes(h.Bytes())
}
// Decode hash string with "0x...." format to Hash type
func DecodeHash(data []byte) Hash {
dec := make([]byte, HashLength)
hex.Decode(dec, data[2:])
return BytesToHash(dec)
}
func BytesToHash(d []byte) Hash {
var h Hash
if len(d) > HashLength {
d = d[:HashLength]
}
copy(h[:], d)
return h
}
func (h Hash) Nil() bool {
return h == Hash{}
}
func Sha256(d []byte) Hash {
return sha256.Sum256(d)
}
type Address [AddressLength]byte
func (addr Address) String() string {
return string(addr[:])
}
func (addr Address) Bytes() []byte {
return addr[:]
}
func (addr Address) Hex() string {
enc := make([]byte, len(addr)*2)
hex.Encode(enc, addr[:])
return "0x" + string(enc)
}
func (addr Address) Big() *big.Int {
return new(big.Int).SetBytes(addr.Bytes())
}
func (addr Address) Nil() bool {
return addr == Address{}
}
func BytesToAddress(b []byte) Address {
var addr Address
if len(b) > AddressLength {
b = b[:AddressLength]
}
copy(addr[:], b)
return addr
}
func BigToAddress(b *big.Int) Address {
return BytesToAddress(b.Bytes())
}
func CreateAddress(addr Address, nonce uint64) Address {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, nonce)
return BytesToAddress(Sha256(append(addr.Bytes(), buf...)).Bytes())
}
func HashToAddr(hash Hash) Address {
return BytesToAddress(hash[:AddressLength])
}
// Decode address in hex format to common.Address
func HexToAddress(d string) Address {
h := []byte(d)
dec := make([]byte, AddressLength)
if bytes.Compare(h[:2], []byte("0x")) == 0 {
h = h[2:]
}
hex.Decode(dec, h)
return BytesToAddress(dec)
}
// Protocol represents the callback handler
type Protocol interface {
// Typ should match the message type
Type() string
// Run func handles the message from the stream
Run(pid peer.ID, message *pb.Message) error
// Error func handles the error returned from the stream
Error(error)
}