resolve channel ID from channel point

This commit is contained in:
Carsten Otto
2021-11-28 18:49:25 +01:00
parent 8a520895ed
commit 2760238756
7 changed files with 84 additions and 8 deletions

View File

@@ -1,6 +1,8 @@
package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.ChannelIdResolver;
import de.cotto.lndmanagej.model.ChannelPoint;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@@ -8,17 +10,36 @@ import javax.annotation.Nonnull;
@Component
public class ChannelIdConverter implements Converter<String, ChannelId> {
public ChannelIdConverter() {
// default constructor
private final ChannelIdResolver channelIdResolver;
public ChannelIdConverter(ChannelIdResolver channelIdResolver) {
this.channelIdResolver = channelIdResolver;
}
@Override
public ChannelId convert(@Nonnull String source) {
try {
long shortChannelId = Long.parseLong(source);
return ChannelId.fromShortChannelId(shortChannelId);
return fromShortChannelId(source);
} catch (NumberFormatException numberFormatException) {
return ChannelId.fromCompactForm(source);
return fromCompactFormOrChannelPoint(source);
}
}
private ChannelId fromCompactFormOrChannelPoint(String source) {
try {
return ChannelId.fromCompactForm(source);
} catch (IllegalArgumentException e) {
return fromChannelPoint(source);
}
}
private ChannelId fromChannelPoint(String source) {
return channelIdResolver.resolveFromChannelPoint(ChannelPoint.create(source))
.orElseThrow(IllegalArgumentException::new);
}
private ChannelId fromShortChannelId(String source) {
long shortChannelId = Long.parseLong(source);
return ChannelId.fromShortChannelId(shortChannelId);
}
}