Kevacoin stratum server for solo-mining
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.7 KiB

9 years ago
package util
import (
"encoding/hex"
"encoding/json"
9 years ago
"math/big"
"time"
"unicode/utf8"
"../cnutil"
"../rpc"
9 years ago
)
var Diff1 = StringToBig("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
9 years ago
func StringToBig(h string) *big.Int {
n := new(big.Int)
n.SetString(h, 0)
return n
9 years ago
}
func MakeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func GetTargetHex(diff int64) string {
9 years ago
padded := make([]byte, 32)
diffBuff := new(big.Int).Div(Diff1, big.NewInt(diff)).Bytes()
9 years ago
copy(padded[32-len(diffBuff):], diffBuff)
buff := padded[0:4]
targetHex := hex.EncodeToString(reverse(buff))
return targetHex
9 years ago
}
func GetHashDifficulty(hashBytes []byte) (*big.Int, bool) {
9 years ago
diff := new(big.Int)
diff.SetBytes(reverse(hashBytes))
// 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
9 years ago
}
func ValidateAddress_Keva(r *rpc.RPCClient, addr string, checkIsMine bool) bool {
rpcResp, err := r.ValidateAddress(addr)
if err != nil {
9 years ago
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
9 years ago
}
return false
9 years ago
}
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)
}
9 years ago
func reverse(src []byte) []byte {
dst := make([]byte, len(src))
for i := len(src); i > 0; i-- {
dst[len(src)-i] = src[i-1]
}
return dst
}