walletrpc: add ListLeases

This commit is contained in:
Joost Jager
2021-03-12 09:26:24 +01:00
parent b8e54fffbf
commit 9398220568
10 changed files with 411 additions and 123 deletions

View File

@@ -47,6 +47,7 @@ func walletCommands() []cli.Command {
listSweepsCommand,
labelTxCommand,
releaseOutputCommand,
listLeasesCommand,
psbtCommand,
},
},
@@ -599,14 +600,7 @@ func fundPsbt(ctx *cli.Context) error {
return err
}
jsonLocks := make([]*utxoLease, len(response.LockedUtxos))
for idx, lock := range response.LockedUtxos {
jsonLocks[idx] = &utxoLease{
ID: hex.EncodeToString(lock.Id),
OutPoint: NewOutPointFromProto(lock.Outpoint),
Expiration: lock.Expiration,
}
}
jsonLocks := marshallLocks(response.LockedUtxos)
printJSON(&fundPsbtResponse{
Psbt: base64.StdEncoding.EncodeToString(
@@ -619,6 +613,21 @@ func fundPsbt(ctx *cli.Context) error {
return nil
}
// marshallLocks converts the rpc lease information to a more json-friendly
// format.
func marshallLocks(lockedUtxos []*walletrpc.UtxoLease) []*utxoLease {
jsonLocks := make([]*utxoLease, len(lockedUtxos))
for idx, lock := range lockedUtxos {
jsonLocks[idx] = &utxoLease{
ID: hex.EncodeToString(lock.Id),
OutPoint: NewOutPointFromProto(lock.Outpoint),
Expiration: lock.Expiration,
}
}
return jsonLocks
}
// finalizePsbtResponse is a struct that contains JSON annotations for nice
// result serialization.
type finalizePsbtResponse struct {
@@ -757,3 +766,25 @@ func releaseOutput(ctx *cli.Context) error {
return nil
}
var listLeasesCommand = cli.Command{
Name: "listleases",
Usage: "Return a list of currently held leases.",
Action: actionDecorator(listLeases),
}
func listLeases(ctx *cli.Context) error {
req := &walletrpc.ListLeasesRequest{}
walletClient, cleanUp := getWalletClient(ctx)
defer cleanUp()
response, err := walletClient.ListLeases(context.Background(), req)
if err != nil {
return err
}
printJSON(marshallLocks(response.LockedUtxos))
return nil
}