2
0
mirror of https://github.com/namecoin/ncdns synced 2024-11-18 03:26:00 +00:00
ncdns/namecoin.go

119 lines
2.4 KiB
Go
Raw Normal View History

2014-10-13 22:26:02 +00:00
package main
import "github.com/conformal/btcjson"
import "encoding/json"
import "sync/atomic"
import "fmt"
2014-10-17 16:00:13 +00:00
//import "github.com/hlandau/degoutils/log"
2014-10-13 22:26:02 +00:00
var idCounter int32 = 0
func newID() int32 {
return atomic.AddInt32(&idCounter, 1)
}
type NameShowCmd struct {
id interface{}
Name string `json:"name"`
}
func NewNameShowCmd(id interface{}, name string) (*NameShowCmd, error) {
return &NameShowCmd {
id: id,
Name: name,
}, nil
}
func (c *NameShowCmd) Id() interface{} {
return c.id
}
func (c *NameShowCmd) Method() string {
return "name_show"
}
func (c *NameShowCmd) MarshalJSON() ([]byte, error) {
params := []interface{}{
c.Name,
}
raw, err := btcjson.NewRawCmd(c.id, c.Method(), params)
if err != nil {
return nil, err
}
return json.Marshal(raw)
}
func (c *NameShowCmd) UnmarshalJSON(b []byte) error {
// We don't need to implement this as we are only ever the client.
panic("not implemented")
return nil
}
type NameShowReply struct {
Name string `json:"name"`
Value string `json:"value"`
ExpiresIn int `json:"expires_in"`
}
func replyParser(m json.RawMessage) (interface{}, error) {
nsr := &NameShowReply{}
err := json.Unmarshal(m, nsr)
if err != nil {
return nil, err
}
2014-10-17 16:00:13 +00:00
2014-10-13 22:26:02 +00:00
return nsr, nil
}
func init() {
btcjson.RegisterCustomCmd("name_show", nil, replyParser, "name_show <name>")
}
type NamecoinConn struct {
Username string
Password string
Server string
}
func (nc *NamecoinConn) Query(name string) (v string, err error) {
2014-10-17 16:00:13 +00:00
if name == "d/badger" {
v = `{"ns":["ns1.badger.bit","ns2.badger.bit"],"map":{"ns1":{"ip":["1.2.3.4"]},"ns2":{"ip":["2.3.4.5"]}},"ds":[[12345,8,2,"lu6y/9mwDNRpTngni179qwqARGVntp9jTaB48NkPAbo="]]}`
return
}
2014-10-13 22:26:02 +00:00
cmd, err := NewNameShowCmd(newID(), name)
if err != nil {
2014-10-17 16:00:13 +00:00
//log.Info("NC NEWCMD ", err)
2014-10-13 22:26:02 +00:00
return "", err
}
r, err := btcjson.RpcSend(nc.Username, nc.Password, nc.Server, cmd)
if err != nil {
2014-10-17 16:00:13 +00:00
return "", err
}
if r.Error != nil {
//log.Info("RPC error: ", r.Error)
if r.Error.Code == -4 {
2014-10-13 22:26:02 +00:00
return "", ErrNoSuchDomain
}
2014-10-17 16:00:13 +00:00
return "", r.Error
2014-10-13 22:26:02 +00:00
}
if r.Result == nil {
2014-10-17 16:00:13 +00:00
//log.Info("NC NILRESULT")
2014-10-13 22:26:02 +00:00
return "", fmt.Errorf("got nil result")
}
if nsr, ok := r.Result.(*NameShowReply); ok {
2014-10-17 16:00:13 +00:00
//log.Info("NC OK")
2014-10-13 22:26:02 +00:00
return nsr.Value, nil
} else {
2014-10-17 16:00:13 +00:00
//log.Info("NC BADREPLY")
2014-10-13 22:26:02 +00:00
return "", fmt.Errorf("bad reply")
}
}
2014-10-17 16:08:53 +00:00
// © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later