multi: add privacy mapper conversion helper

This commit is contained in:
Elle Mouton 2022-09-08 13:00:31 +02:00
parent f3d2aeb00a
commit ab347dd6f1
No known key found for this signature in database
GPG key ID: D7D916376026F177
10 changed files with 522 additions and 98 deletions

View file

@ -86,6 +86,7 @@ func main() {
app.Commands = append(app.Commands, sessionCommands...)
app.Commands = append(app.Commands, accountsCommands...)
app.Commands = append(app.Commands, listActionsCommand)
app.Commands = append(app.Commands, privacyMapCommands)
app.Commands = append(app.Commands, autopilotCommands)
err := app.Run(os.Args)

134
cmd/litcli/privacy_map.go Normal file
View file

@ -0,0 +1,134 @@
package main
import (
"context"
"encoding/hex"
"fmt"
"github.com/lightninglabs/lightning-terminal/firewalldb"
"github.com/lightninglabs/lightning-terminal/litrpc"
"github.com/urfave/cli"
)
var privacyMapCommands = cli.Command{
Name: "privacy",
ShortName: "p",
Usage: "Access the real-pseudo string pairs of the " +
"privacy mapper",
Category: "Privacy",
Flags: []cli.Flag{
cli.StringFlag{
Name: "session_id",
Usage: "The id of the session in question",
Required: true,
},
cli.BoolFlag{
Name: "realtopseudo",
Usage: "set to true if the input should be " +
"mapped to its pseudo counterpart. " +
"Otherwise the input will be taken " +
"as the pseudo value that should be " +
"mapped to its real counterpart.",
},
},
Subcommands: []cli.Command{
privacyMapConvertStrCommand,
privacyMapConvertUint64Command,
},
}
var privacyMapConvertStrCommand = cli.Command{
Name: "str",
ShortName: "s",
Usage: "convert a string to its real or pseudo counter part",
Action: privacyMapConvertStr,
Flags: []cli.Flag{
cli.StringFlag{
Name: "input",
Usage: "the string to convert",
Required: true,
},
},
}
func privacyMapConvertStr(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
if err != nil {
return err
}
defer cleanup()
client := litrpc.NewFirewallClient(clientConn)
id, err := hex.DecodeString(ctx.GlobalString("session_id"))
if err != nil {
return err
}
resp, err := client.PrivacyMapConversion(
ctxb, &litrpc.PrivacyMapConversionRequest{
SessionId: id,
RealToPseudo: ctx.GlobalBool("realtopseudo"),
Input: ctx.String("input"),
},
)
if err != nil {
return err
}
printRespJSON(resp)
return nil
}
var privacyMapConvertUint64Command = cli.Command{
Name: "uint64",
ShortName: "u",
Usage: "convert a uint64 to its real or pseudo counter part",
Action: privacyMapConvertUint64,
Flags: []cli.Flag{
cli.Uint64Flag{
Name: "input",
Usage: "the uint64 to convert",
Required: true,
},
},
}
func privacyMapConvertUint64(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
if err != nil {
return err
}
defer cleanup()
client := litrpc.NewFirewallClient(clientConn)
id, err := hex.DecodeString(ctx.GlobalString("session_id"))
if err != nil {
return err
}
input := firewalldb.Uint64ToStr(ctx.Uint64("input"))
resp, err := client.PrivacyMapConversion(
ctxb, &litrpc.PrivacyMapConversionRequest{
SessionId: id,
RealToPseudo: ctx.GlobalBool("realtopseudo"),
Input: input,
},
)
if err != nil {
return err
}
output, err := firewalldb.StrToUint64(resp.Output)
if err != nil {
return err
}
printRespJSON(&litrpc.PrivacyMapConversionResponse{
Output: fmt.Sprintf("%d", output),
})
return nil
}