You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ncdns/namecoin/namecoin.go

149 lines
3.3 KiB
Go

10 years ago
package namecoin
// btcjson had to be modified a bit to get correct error reporting.
import "github.com/hlandauf/btcjson"
10 years ago
import "github.com/hlandau/madns/merr"
10 years ago
import "github.com/hlandau/ncdns/namecoin/extratypes"
10 years ago
import "sync/atomic"
import "fmt"
10 years ago
// Used for generating IDs for JSON-RPC requests.
10 years ago
var idCounter int32
func newID() int32 {
10 years ago
return atomic.AddInt32(&idCounter, 1)
}
10 years ago
// Used to query a Namecoin JSON-RPC interface. Initialize the struct with a
// username, password, and address (hostname:port).
10 years ago
type Conn struct {
10 years ago
Username string
Password string
Server string
}
10 years ago
// Query the Namecoin daemon for a Namecoin domain (e.g. d/example).
// If the domain exists, returns the value stored in Namecoin, which should be JSON.
// Note that this will return domain data even if the domain is expired.
10 years ago
func (nc *Conn) Query(name string) (v string, err error) {
10 years ago
cmd, err := extratypes.NewNameShowCmd(newID(), name)
if err != nil {
//log.Info("NC NEWCMD ", err)
return "", err
}
r, err := btcjson.RpcSend(nc.Username, nc.Password, nc.Server, cmd)
if err != nil {
return "", err
}
if r.Error != nil {
//log.Info("RPC error: ", r.Error)
if r.Error.Code == -4 {
return "", merr.ErrNoSuchDomain
}
return "", r.Error
}
if r.Result == nil {
//log.Info("NC NILRESULT")
return "", fmt.Errorf("got nil result")
}
if nsr, ok := r.Result.(*extratypes.NameShowReply); ok {
//log.Info("NC OK")
return nsr.Value, nil
}
10 years ago
//log.Info("NC BADREPLY")
return "", fmt.Errorf("bad reply")
}
10 years ago
10 years ago
var ErrSyncNoSuchBlock = fmt.Errorf("no block exists with given hash")
const rpcInvalidAddressOrKey = -5
func (nc *Conn) Sync(hash string, count int, wait bool) ([]extratypes.NameSyncEvent, error) {
cmd, err := extratypes.NewNameSyncCmd(newID(), hash, count, wait)
if err != nil {
return nil, err
}
r, err := btcjson.RpcSend(nc.Username, nc.Password, nc.Server, cmd)
if err != nil {
return nil, err
}
if r.Error != nil {
if r.Error.Code == rpcInvalidAddressOrKey {
return nil, ErrSyncNoSuchBlock
}
return nil, r.Error
}
if r.Result == nil {
return nil, fmt.Errorf("got nil result")
}
if nsr, ok := r.Result.(extratypes.NameSyncReply); ok {
return []extratypes.NameSyncEvent(nsr), nil
}
return nil, fmt.Errorf("bad reply")
}
func (nc *Conn) CurHeight() (int, error) {
cmd, err := btcjson.NewGetInfoCmd(newID())
if err != nil {
return 0, err
}
r, err := btcjson.RpcSend(nc.Username, nc.Password, nc.Server, cmd)
if err != nil {
return 0, err
}
if r.Error != nil {
return 0, r.Error
}
if r.Result == nil {
return 0, fmt.Errorf("got nil result")
}
if rep, ok := r.Result.(*btcjson.InfoResult); ok {
return int(rep.Blocks), nil
}
return 0, fmt.Errorf("bad reply")
}
10 years ago
func (nc *Conn) Filter(regexp string, maxage, from, count int) (names []extratypes.NameFilterItem, err error) {
cmd, err := extratypes.NewNameFilterCmd(newID(), regexp, maxage, from, count)
if err != nil {
return nil, err
}
r, err := btcjson.RpcSend(nc.Username, nc.Password, nc.Server, cmd)
if err != nil {
return nil, err
}
if r.Error != nil {
return nil, r.Error
}
if r.Result == nil {
return nil, fmt.Errorf("got nil result")
}
if nsr, ok := r.Result.(extratypes.NameFilterReply); ok {
return []extratypes.NameFilterItem(nsr), nil
}
return nil, fmt.Errorf("bad reply")
}
10 years ago
// © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later