aboutsummaryrefslogtreecommitdiffstats
path: root/common/types.go
blob: e0963a7c5d34d2c02d206dd4063fb64d56e45d72 (plain) (blame)
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
package common

type (
    Hash    [32]byte
    Address [20]byte
)

var (
    zeroHash    Hash
    zeroAddress Address
)

func BytesToHash(b []byte) Hash {
    var h Hash
    h.SetBytes(b)
    return h
}
func StringToHash(s string) Hash { return BytesToHash([]byte(s)) }

func BytesToAddress(b []byte) Address {
    var a Address
    a.SetBytes(b)
    return a
}
func StringToAddress(s string) Address { return BytesToAddress([]byte(s)) }

// Don't use the default 'String' method in case we want to overwrite

// Get the string representation of the underlying hash
func (h Hash) Str() string {
    return string(h[:])
}

// Sets the hash to the value of b. If b is larger than len(h) it will panic
func (h Hash) SetBytes(b []byte) {
    if len(b) > len(h) {
        panic("unable to set bytes. too big")
    }

    // reverse loop
    for i := len(b); i >= 0; i-- {
        h[i] = b[i]
    }
}

// Set string `s` to h. If s is larger than len(h) it will panic
func (h Hash) SetString(s string) { h.SetBytes([]byte(s)) }

// Sets h to other
func (h Hash) Set(other Hash) {
    for i, v := range other {
        h[i] = v
    }
}

// Get the string representation of the underlying address
func (a Address) Str() string {
    return string(a[:])
}

// Sets the address to the value of b. If b is larger than len(a) it will panic
func (a Address) SetBytes(b []byte) {
    if len(b) > len(a) {
        panic("unable to set bytes. too big")
    }

    // reverse loop
    for i := len(b); i >= 0; i-- {
        a[i] = b[i]
    }
}

// Set string `s` to a. If s is larger than len(a) it will panic
func (a Address) SetString(s string) { a.SetBytes([]byte(s)) }

// Sets a to other
func (a Address) Set(other Address) {
    for i, v := range other {
        a[i] = v
    }
}