forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
address.go
133 lines (110 loc) · 3.35 KB
/
address.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package common
import (
"crypto/sha256"
"errors"
"fmt"
"io"
"math/big"
"github.com/itchyny/base58-go"
"golang.org/x/crypto/ripemd160"
)
const ADDR_LEN = 20
type Address [ADDR_LEN]byte
var ADDRESS_EMPTY = Address{}
// ToHexString returns hex string representation of Address
func (self *Address) ToHexString() string {
return fmt.Sprintf("%x", ToArrayReverse(self[:]))
}
// Serialize serialize Address into io.Writer
func (self *Address) Serialize(w io.Writer) error {
_, err := w.Write(self[:])
return err
}
// Deserialize deserialize Address from io.Reader
func (self *Address) Deserialize(r io.Reader) error {
_, err := io.ReadFull(r, self[:])
if err != nil {
return errors.New("deserialize Address error")
}
return nil
}
// ToBase58 returns base58 encoded address string
func (f *Address) ToBase58() string {
data := append([]byte{23}, f[:]...)
temp := sha256.Sum256(data)
temps := sha256.Sum256(temp[:])
data = append(data, temps[0:4]...)
bi := new(big.Int).SetBytes(data).String()
encoded, _ := base58.BitcoinEncoding.Encode([]byte(bi))
return string(encoded)
}
// AddressParseFromBytes returns parsed Address
func AddressParseFromBytes(f []byte) (Address, error) {
if len(f) != ADDR_LEN {
return ADDRESS_EMPTY, errors.New("[Common]: AddressParseFromBytes err, len != 20")
}
var addr Address
copy(addr[:], f)
return addr, nil
}
// AddressParseFromHexString returns parsed Address
func AddressFromHexString(s string) (Address, error) {
hx, err := HexToBytes(s)
if err != nil {
return ADDRESS_EMPTY, err
}
return AddressParseFromBytes(ToArrayReverse(hx))
}
const MaxBase58AddrLen = 2048 // just to avoid dos
// AddressFromBase58 returns Address from encoded base58 string
func AddressFromBase58(encoded string) (Address, error) {
if encoded == "" || len(encoded) > MaxBase58AddrLen {
return ADDRESS_EMPTY, errors.New("invalid address")
}
decoded, err := base58.BitcoinEncoding.Decode([]byte(encoded))
if err != nil {
return ADDRESS_EMPTY, err
}
x, ok := new(big.Int).SetString(string(decoded), 10)
if !ok {
return ADDRESS_EMPTY, errors.New("invalid address")
}
buf := x.Bytes()
if len(buf) != 1+ADDR_LEN+4 || buf[0] != byte(23) {
return ADDRESS_EMPTY, errors.New("wrong encoded address")
}
ph, err := AddressParseFromBytes(buf[1:21])
if err != nil {
return ADDRESS_EMPTY, err
}
addr := ph.ToBase58()
if addr != encoded {
return ADDRESS_EMPTY, errors.New("[AddressFromBase58]: decode encoded verify failed.")
}
return ph, nil
}
func AddressFromVmCode(code []byte) Address {
var addr Address
temp := sha256.Sum256(code)
md := ripemd160.New()
md.Write(temp[:])
md.Sum(addr[:0])
return addr
}