-
Notifications
You must be signed in to change notification settings - Fork 102
/
addrs.go
259 lines (224 loc) · 5.59 KB
/
addrs.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"encoding/hex"
"fmt"
"math"
"time"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/urfave/cli"
)
var addrCommands = []cli.Command{
{
Name: "addrs",
ShortName: "ad",
Usage: "Interact with Taproot Asset addresses.",
Category: "Addresses",
Subcommands: []cli.Command{
newAddrCommand,
queryAddrsCommand,
decodeAddrCommand,
receivesAddrCommand,
},
},
}
const (
groupKeyName = "group_key"
amtName = "amt"
assetVersionName = "asset_version"
proofCourierAddrName = "proof_courier_addr"
)
var newAddrCommand = cli.Command{
Name: "new",
ShortName: "n",
Usage: "Create a Taproot Asset address",
Description: "Create a new Taproot Asset address to receive an asset " +
"on-chain",
Flags: []cli.Flag{
cli.StringFlag{
Name: assetIDName,
Usage: "the asset genesis ID of the asset to receive",
},
cli.Uint64Flag{
Name: amtName,
Usage: "the amt of the asset to receive",
},
cli.Uint64Flag{
Name: assetVersionName,
Usage: "the asset version of the asset to receive",
},
cli.StringFlag{
Name: proofCourierAddrName,
Usage: "(optional) the address of the proof courier " +
"to use for this specific address, if the " +
"default proof courier should be " +
"overwritten; format: protocol://host:port",
},
},
Action: newAddr,
}
func newAddr(ctx *cli.Context) error {
switch {
case ctx.String(assetIDName) == "":
return cli.ShowSubcommandHelp(ctx)
}
assetID, err := hex.DecodeString(ctx.String(assetIDName))
if err != nil {
return fmt.Errorf("unable to decode assetID: %v", err)
}
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
assetVersion, err := taprpc.MarshalAssetVersion(
asset.Version(ctx.Uint64(assetVersionName)),
)
if err != nil {
return err
}
addr, err := client.NewAddr(ctxc, &taprpc.NewAddrRequest{
AssetId: assetID,
Amt: ctx.Uint64(amtName),
AssetVersion: assetVersion,
ProofCourierAddr: ctx.String(proofCourierAddrName),
})
if err != nil {
return fmt.Errorf("unable to make addr: %w", err)
}
printRespJSON(addr)
return nil
}
const (
createdAfterName = "created_after"
createdBeforeName = "created_before"
limitName = "limit"
offsetName = "offset"
)
var queryAddrsCommand = cli.Command{
Name: "query",
ShortName: "q",
Usage: "Query for the set of created addresses",
Description: "Query for the set of created addresses, supports pagination",
Flags: []cli.Flag{
cli.StringFlag{
Name: createdAfterName,
Usage: "a duration short hand (-1h, 2d, 2w, etc)",
},
cli.StringFlag{
Name: createdBeforeName,
Usage: "a duration short hand (-2h, 3d, 2w, etc)",
},
cli.Int64Flag{
Name: limitName,
Usage: "the max number of addrs to returned",
},
cli.Int64Flag{
Name: offsetName,
Usage: "the number of addrs to skip before returning the first addr",
},
},
Action: queryAddr,
}
func queryAddr(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
var start int64
if ctx.IsSet(createdAfterName) {
startOffset, err := time.ParseDuration(ctx.String(createdAfterName))
if err != nil {
return fmt.Errorf("unable to parse start: %w", err)
}
startTime := time.Now().Add(startOffset)
start = startTime.Unix()
}
// Wrap with int64() so math.MaxInt64 will cross-compile on 32-bit arch.
end := int64(math.MaxInt64)
if ctx.IsSet(createdBeforeName) {
endOffset, err := time.ParseDuration(ctx.String(createdBeforeName))
if err != nil {
return fmt.Errorf("unable to parse end: %w", err)
}
endTime := time.Now().Add(endOffset)
start = endTime.Unix()
}
addrs, err := client.QueryAddrs(ctxc, &taprpc.QueryAddrRequest{
CreatedAfter: start,
CreatedBefore: int64(end),
Limit: int32(ctx.Int64(limitName)),
Offset: int32(ctx.Int64(offsetName)),
})
if err != nil {
return fmt.Errorf("unable to make addrs: %v", err)
}
printRespJSON(addrs)
return nil
}
const addrName = "addr"
var decodeAddrCommand = cli.Command{
Name: "decode",
ShortName: "d",
ArgsUsage: "[--addr | addr]",
Usage: "Attempt to decode a taproot asset addr",
Flags: []cli.Flag{
cli.StringFlag{
Name: addrName,
Usage: "the address to decode",
},
},
Action: decodeAddr,
}
func decodeAddr(ctx *cli.Context) error {
var addr string
switch {
case ctx.String(addrName) != "":
addr = ctx.String(addrName)
case len(ctx.Args()) > 0:
addr = ctx.Args().First()
default:
return cli.ShowSubcommandHelp(ctx)
}
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
resp, err := client.DecodeAddr(ctxc, &taprpc.DecodeAddrRequest{
Addr: addr,
})
if err != nil {
return fmt.Errorf("unable to decode addr: %w", err)
}
printRespJSON(resp)
return nil
}
var receivesAddrCommand = cli.Command{
Name: "receives",
ShortName: "r",
ArgsUsage: "[--addr | addr]",
Usage: "Show all inbound asset transfers",
Flags: []cli.Flag{
cli.StringFlag{
Name: addrName,
Usage: "show transfers of a single address only",
},
},
Action: addrReceives,
}
func addrReceives(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
var addr string
switch {
case ctx.String(addrName) != "":
addr = ctx.String(addrName)
case len(ctx.Args()) > 0:
addr = ctx.Args().First()
}
resp, err := client.AddrReceives(ctxc, &taprpc.AddrReceivesRequest{
FilterAddr: addr,
})
if err != nil {
return fmt.Errorf("unable to query addr receives: %w", err)
}
printRespJSON(resp)
return nil
}