-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnames.go
126 lines (98 loc) · 3.66 KB
/
names.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2014-2017 The btcsuite developers
// Copyright (c) 2019 The Namecoin developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ncrpcclient
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/rpcclient"
"github.com/namecoin/ncbtcjson"
)
// *********************
// Name Lookup Functions
// *********************
// FutureNameShowResult is a future promise to deliver the result
// of a NameShowAsync RPC invocation (or an applicable error).
type FutureNameShowResult chan *rpcclient.Response
// Receive waits for the Response promised by the future and returns detailed
// information about a name.
func (r FutureNameShowResult) Receive() (*ncbtcjson.NameShowResult, error) {
res, err := rpcclient.ReceiveFuture(r)
if err != nil {
return nil, fmt.Errorf("receive future: %w", err)
}
// Unmarshal result as a name_show result object
var nameShow ncbtcjson.NameShowResult
err = json.Unmarshal(res, &nameShow)
if err != nil {
return nil, fmt.Errorf("unmarshal result: %w", err)
}
if nameShow.NameEncoding == ncbtcjson.Hex {
var nameBytes []byte
nameBytes, err = hex.DecodeString(nameShow.Name)
if err != nil {
return nil, fmt.Errorf("decode hex name: %w", err)
}
nameShow.Name = string(nameBytes)
}
if nameShow.ValueEncoding == ncbtcjson.Hex {
var valueBytes []byte
valueBytes, err = hex.DecodeString(nameShow.Value)
if err != nil {
return nil, fmt.Errorf("decode hex value: %w", err)
}
nameShow.Value = string(valueBytes)
}
return &nameShow, nil
}
// NameShowAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on
// the returned instance.
//
// See NameShow for the blocking version and more details.
func (c *Client) NameShowAsync(name string, options *ncbtcjson.NameShowOptions) FutureNameShowResult {
if options == nil {
options = &ncbtcjson.NameShowOptions{}
}
options.NameEncoding, options.ValueEncoding = ncbtcjson.Hex, ncbtcjson.Hex
name = hex.EncodeToString([]byte(name))
cmd := ncbtcjson.NewNameShowCmd(name, options)
return c.SendCmd(cmd)
}
// NameShow returns detailed information about a name.
func (c *Client) NameShow(name string, options *ncbtcjson.NameShowOptions) (*ncbtcjson.NameShowResult, error) {
return c.NameShowAsync(name, options).Receive()
}
// FutureNameScanResult is a future promise to deliver the result
// of a NameScanAsync RPC invocation (or an applicable error).
type FutureNameScanResult chan *rpcclient.Response
// Receive waits for the Response promised by the future and returns detailed
// information about a list of names.
func (r FutureNameScanResult) Receive() (ncbtcjson.NameScanResult, error) {
res, err := rpcclient.ReceiveFuture(r)
if err != nil {
return nil, fmt.Errorf("receive future: %w", err)
}
// Unmarshal result as a name_scan result object
var nameScan ncbtcjson.NameScanResult
err = json.Unmarshal(res, &nameScan)
if err != nil {
return nil, fmt.Errorf("unmarshal result: %w", err)
}
return nameScan, nil
}
// NameScanAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on
// the returned instance.
//
// See NameScan for the blocking version and more details.
func (c *Client) NameScanAsync(start string, count uint32) FutureNameScanResult {
cmd := ncbtcjson.NewNameScanCmd(start, &count, nil)
return c.SendCmd(cmd)
}
// NameScan returns detailed information about a list of names.
func (c *Client) NameScan(start string, count uint32) (ncbtcjson.NameScanResult, error) {
return c.NameScanAsync(start, count).Receive()
}