check checksum in NewFromBase58
This commit is contained in:
parent
b3a47c5c7b
commit
54bdad99f1
@ -1,6 +1,7 @@
|
|||||||
package address
|
package address
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
@ -77,16 +78,22 @@ func NewFromHex(s string) (Address, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewFromBase58(s string) (Address, error) {
|
func NewFromBase58(s string) (Address, error) {
|
||||||
bytes, err := base58.Decode(s)
|
b, err := base58.Decode(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Address{}, err
|
return Address{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bytes) != 25 {
|
if len(b) != 25 {
|
||||||
return Address{}, fmt.Errorf("tron address is not 25 bytes but %d", len(bytes))
|
return Address{}, fmt.Errorf("tron address is not 25 bytes but %d", len(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
return *(*[21]byte)(bytes[:21]), nil
|
hash := SHA256(SHA256(b[:21]))
|
||||||
|
checksum := hash[:4]
|
||||||
|
if !bytes.Equal(checksum, b[21:]) {
|
||||||
|
return Address{}, fmt.Errorf("invalid checksum")
|
||||||
|
}
|
||||||
|
|
||||||
|
return *(*[21]byte)(b[:21]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFromString(s string) (Address, error) {
|
func NewFromString(s string) (Address, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user