get channel IDs for channels with peer

This commit is contained in:
Carsten Otto
2021-11-11 20:14:04 +01:00
parent 6ad011696b
commit 07d9c7c5f7
12 changed files with 220 additions and 35 deletions

View File

@@ -1,23 +1,34 @@
package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.grpc.GrpcNodeInfo;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.NodeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/node/")
public class NodeController {
private final GrpcNodeInfo grpcNodeInfo;
import java.util.List;
import java.util.stream.Collectors;
public NodeController(GrpcNodeInfo grpcNodeInfo) {
this.grpcNodeInfo = grpcNodeInfo;
@RestController
@RequestMapping("/api/node/{pubkey}/")
public class NodeController {
private final NodeService nodeService;
public NodeController(NodeService nodeService) {
this.nodeService = nodeService;
}
@GetMapping("/{pubkey}/alias")
@GetMapping("/alias")
public String getAlias(@PathVariable Pubkey pubkey) {
return grpcNodeInfo.getNode(pubkey).alias();
return nodeService.getAlias(pubkey);
}
@GetMapping("/open-channels")
public List<Long> getOpenChannelIds(@PathVariable Pubkey pubkey) {
return nodeService.getOpenChannelIds(pubkey).stream()
.map(ChannelId::shortChannelId)
.collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,40 @@
package de.cotto.lndmanagej.service;
import de.cotto.lndmanagej.grpc.GrpcChannels;
import de.cotto.lndmanagej.grpc.GrpcNodeInfo;
import de.cotto.lndmanagej.model.Channel;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class NodeService {
private final GrpcNodeInfo grpcNodeInfo;
private final GrpcChannels grpcChannels;
public NodeService(GrpcNodeInfo grpcNodeInfo, GrpcChannels grpcChannels) {
this.grpcNodeInfo = grpcNodeInfo;
this.grpcChannels = grpcChannels;
}
public List<ChannelId> getOpenChannelIds(Pubkey pubkey) {
Node node = getNode(pubkey);
return grpcChannels.getChannels().stream()
.filter(c -> c.getNodes().contains(node))
.map(Channel::getId)
.sorted()
.collect(Collectors.toList());
}
public String getAlias(Pubkey pubkey) {
return getNode(pubkey).alias();
}
private Node getNode(Pubkey pubkey) {
return grpcNodeInfo.getNode(pubkey);
}
}