Skip to content

Commit ab347dd

Browse files
committed
multi: add privacy mapper conversion helper
1 parent f3d2aeb commit ab347dd

File tree

10 files changed

+522
-98
lines changed

10 files changed

+522
-98
lines changed

cmd/litcli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func main() {
8686
app.Commands = append(app.Commands, sessionCommands...)
8787
app.Commands = append(app.Commands, accountsCommands...)
8888
app.Commands = append(app.Commands, listActionsCommand)
89+
app.Commands = append(app.Commands, privacyMapCommands)
8990
app.Commands = append(app.Commands, autopilotCommands)
9091

9192
err := app.Run(os.Args)

cmd/litcli/privacy_map.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"fmt"
7+
8+
"github.com/lightninglabs/lightning-terminal/firewalldb"
9+
"github.com/lightninglabs/lightning-terminal/litrpc"
10+
"github.com/urfave/cli"
11+
)
12+
13+
var privacyMapCommands = cli.Command{
14+
Name: "privacy",
15+
ShortName: "p",
16+
Usage: "Access the real-pseudo string pairs of the " +
17+
"privacy mapper",
18+
Category: "Privacy",
19+
Flags: []cli.Flag{
20+
cli.StringFlag{
21+
Name: "session_id",
22+
Usage: "The id of the session in question",
23+
Required: true,
24+
},
25+
cli.BoolFlag{
26+
Name: "realtopseudo",
27+
Usage: "set to true if the input should be " +
28+
"mapped to its pseudo counterpart. " +
29+
"Otherwise the input will be taken " +
30+
"as the pseudo value that should be " +
31+
"mapped to its real counterpart.",
32+
},
33+
},
34+
Subcommands: []cli.Command{
35+
privacyMapConvertStrCommand,
36+
privacyMapConvertUint64Command,
37+
},
38+
}
39+
40+
var privacyMapConvertStrCommand = cli.Command{
41+
Name: "str",
42+
ShortName: "s",
43+
Usage: "convert a string to its real or pseudo counter part",
44+
Action: privacyMapConvertStr,
45+
Flags: []cli.Flag{
46+
cli.StringFlag{
47+
Name: "input",
48+
Usage: "the string to convert",
49+
Required: true,
50+
},
51+
},
52+
}
53+
54+
func privacyMapConvertStr(ctx *cli.Context) error {
55+
ctxb := context.Background()
56+
clientConn, cleanup, err := connectClient(ctx)
57+
if err != nil {
58+
return err
59+
}
60+
defer cleanup()
61+
client := litrpc.NewFirewallClient(clientConn)
62+
63+
id, err := hex.DecodeString(ctx.GlobalString("session_id"))
64+
if err != nil {
65+
return err
66+
}
67+
68+
resp, err := client.PrivacyMapConversion(
69+
ctxb, &litrpc.PrivacyMapConversionRequest{
70+
SessionId: id,
71+
RealToPseudo: ctx.GlobalBool("realtopseudo"),
72+
Input: ctx.String("input"),
73+
},
74+
)
75+
if err != nil {
76+
return err
77+
}
78+
79+
printRespJSON(resp)
80+
81+
return nil
82+
}
83+
84+
var privacyMapConvertUint64Command = cli.Command{
85+
Name: "uint64",
86+
ShortName: "u",
87+
Usage: "convert a uint64 to its real or pseudo counter part",
88+
Action: privacyMapConvertUint64,
89+
Flags: []cli.Flag{
90+
cli.Uint64Flag{
91+
Name: "input",
92+
Usage: "the uint64 to convert",
93+
Required: true,
94+
},
95+
},
96+
}
97+
98+
func privacyMapConvertUint64(ctx *cli.Context) error {
99+
ctxb := context.Background()
100+
clientConn, cleanup, err := connectClient(ctx)
101+
if err != nil {
102+
return err
103+
}
104+
defer cleanup()
105+
client := litrpc.NewFirewallClient(clientConn)
106+
107+
id, err := hex.DecodeString(ctx.GlobalString("session_id"))
108+
if err != nil {
109+
return err
110+
}
111+
112+
input := firewalldb.Uint64ToStr(ctx.Uint64("input"))
113+
114+
resp, err := client.PrivacyMapConversion(
115+
ctxb, &litrpc.PrivacyMapConversionRequest{
116+
SessionId: id,
117+
RealToPseudo: ctx.GlobalBool("realtopseudo"),
118+
Input: input,
119+
},
120+
)
121+
if err != nil {
122+
return err
123+
}
124+
125+
output, err := firewalldb.StrToUint64(resp.Output)
126+
if err != nil {
127+
return err
128+
}
129+
130+
printRespJSON(&litrpc.PrivacyMapConversionResponse{
131+
Output: fmt.Sprintf("%d", output),
132+
})
133+
return nil
134+
}

0 commit comments

Comments
 (0)