This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
types.go
120 lines (101 loc) · 3.1 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
// Copyright (C) 2018 MediBloc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package common
import (
"bytes"
"math/big"
"github.com/medibloc/go-medibloc/crypto/signature"
"github.com/medibloc/go-medibloc/crypto/signature/algorithm"
"github.com/medibloc/go-medibloc/util/byteutils"
)
// Types' length.
const (
AddressLength = 33
HashLength = 32
)
// Address represents Address.
type Address [AddressLength]byte
// BytesToAddress gets Address from bytes.
func BytesToAddress(b []byte) (Address, error) {
var a Address
if err := a.FromBytes(b); err != nil {
return Address{}, err
}
return a, nil
}
// HexToAddress gets Address from hex string.
func HexToAddress(s string) (Address, error) {
addr, err := byteutils.FromHex(s)
if err != nil {
return Address{}, err
}
return BytesToAddress(addr)
}
// IsHexAddress checks hex address.
func IsHexAddress(s string) bool {
if byteutils.HasHexPrefix(s) {
s = s[2:]
}
return len(s) == 2*AddressLength && byteutils.IsHex(s)
}
// IsHash checks hash string.
func IsHash(s string) bool {
return len(s) == 2*HashLength && byteutils.IsHex(s)
}
// PublicKeyToAddress gets Address from PublicKey.
func PublicKeyToAddress(p signature.PublicKey) (Address, error) {
switch p.Algorithm() {
case algorithm.SECP256K1:
buf, err := p.Compressed()
if err != nil {
return Address{}, err
}
return BytesToAddress(buf)
default:
return Address{}, algorithm.ErrInvalidCryptoAlgorithm
}
}
// Str returns Address in string.
func (a Address) Str() string { return string(a[:]) }
// Bytes returns Address in bytes.
func (a Address) Bytes() []byte { return a[:] }
// Big returns Address in big Int.
func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) }
// Hex returns Address in hex string
func (a Address) Hex() string { return byteutils.Bytes2Hex(a[:]) }
// SetBytes 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) {
b = b[len(b)-AddressLength:]
}
copy(a[AddressLength-len(b):], b)
}
// Equals compare Address.
func (a Address) Equals(b Address) bool {
return bytes.Compare(a[:], b[:]) == 0
}
// String is a stringer interface of Address.
func (a Address) String() string {
return byteutils.Bytes2Hex(a.Bytes())
}
// ToBytes convert address to byte slice (for trie.Serializable)
func (a Address) ToBytes() ([]byte, error) {
return a.Bytes(), nil
}
// FromBytes convert byte slice to slice (for trie.Serializable)
func (a *Address) FromBytes(b []byte) error {
a.SetBytes(b)
return nil
}