keva-stratum/util/util.go

85 lines
1.7 KiB
Go
Raw Normal View History

2015-07-05 14:49:07 +05:00
package util
import (
"encoding/hex"
"encoding/json"
2015-07-05 14:49:07 +05:00
"math/big"
"time"
"unicode/utf8"
"../cnutil"
"../rpc"
2015-07-05 14:49:07 +05:00
)
2016-12-08 02:57:24 +05:00
var Diff1 = StringToBig("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
2015-07-05 14:49:07 +05:00
2016-12-08 02:57:24 +05:00
func StringToBig(h string) *big.Int {
n := new(big.Int)
n.SetString(h, 0)
return n
2015-07-05 14:49:07 +05:00
}
func MakeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
2016-12-07 13:57:02 +05:00
func GetTargetHex(diff int64) string {
2015-07-05 14:49:07 +05:00
padded := make([]byte, 32)
2016-12-07 13:57:02 +05:00
diffBuff := new(big.Int).Div(Diff1, big.NewInt(diff)).Bytes()
2015-07-05 14:49:07 +05:00
copy(padded[32-len(diffBuff):], diffBuff)
buff := padded[0:4]
2021-11-23 11:00:45 -07:00
targetHex := hex.EncodeToString(ReverseBytes(buff))
2016-12-07 13:57:02 +05:00
return targetHex
2015-07-05 14:49:07 +05:00
}
2018-02-10 05:08:43 +05:00
func GetHashDifficulty(hashBytes []byte) (*big.Int, bool) {
2015-07-05 14:49:07 +05:00
diff := new(big.Int)
2021-11-23 11:00:45 -07:00
diff.SetBytes(ReverseBytes(hashBytes))
2018-02-10 05:08:43 +05:00
// Check for broken result, empty string or zero hex value
if diff.Cmp(new(big.Int)) == 0 {
return nil, false
}
return diff.Div(Diff1, diff), true
2015-07-05 14:49:07 +05:00
}
func ValidateAddress_Keva(r *rpc.RPCClient, addr string, checkIsMine bool) bool {
rpcResp, err := r.ValidateAddress(addr)
if err != nil {
2015-07-05 14:49:07 +05:00
return false
}
var reply *rpc.ValidateAddressReply
if rpcResp.Result != nil {
err = json.Unmarshal(*rpcResp.Result, &reply)
if err != nil {
return false
}
if checkIsMine {
return reply.IsMine
}
return reply.IsValid
2015-07-05 14:49:07 +05:00
}
return false
2015-07-05 14:49:07 +05:00
}
func ValidateAddress(addy string, poolAddy string) bool {
if len(addy) != len(poolAddy) {
return false
}
prefix, _ := utf8.DecodeRuneInString(addy)
poolPrefix, _ := utf8.DecodeRuneInString(poolAddy)
if prefix != poolPrefix {
return false
}
return cnutil.ValidateAddress(addy)
}
2021-11-23 11:00:45 -07:00
func ReverseBytes(src []byte) []byte {
2015-07-05 14:49:07 +05:00
dst := make([]byte, len(src))
for i := len(src); i > 0; i-- {
dst[len(src)-i] = src[i-1]
}
return dst
}