improve dns hrn support

This commit is contained in:
Craig Raw 2025-09-29 11:53:17 +02:00
parent a896809286
commit 73acc00ab6
2 changed files with 32 additions and 0 deletions

View file

@ -4,6 +4,9 @@ import com.sparrowwallet.drongo.uri.BitcoinURI;
import org.xbill.DNS.*;
import org.xbill.DNS.Record;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import static com.sparrowwallet.drongo.dns.RecordUtils.fromWire;
public record DnsPayment(String hrn, BitcoinURI bitcoinURI, byte[] proofChain) {
@ -33,4 +36,23 @@ public record DnsPayment(String hrn, BitcoinURI bitcoinURI, byte[] proofChain) {
public boolean hasSilentPaymentAddress() {
return bitcoinURI.getSilentPaymentAddress() != null;
}
public static Optional<String> getHrn(String value) {
String hrn = value;
if(value.endsWith(".")) {
return Optional.empty();
}
if(hrn.startsWith("")) {
hrn = hrn.substring(1);
}
String[] addressParts = hrn.split("@");
if(addressParts.length == 2 && addressParts[1].indexOf('.') > -1 && addressParts[1].substring(addressParts[1].indexOf('.') + 1).length() > 1 &&
StandardCharsets.US_ASCII.newEncoder().canEncode(hrn)) {
return Optional.of(hrn);
}
return Optional.empty();
}
}

View file

@ -51,6 +51,16 @@ public class DnsPaymentCache {
}
}
public static DnsPayment getDnsPayment(String hrn) {
for(DnsPayment dnsPayment : dnsPayments.asMap().values()) {
if(dnsPayment.hrn().equals(hrn)) {
return dnsPayment;
}
}
return null;
}
public static void putDnsPayment(Address address, DnsPayment dnsPayment) {
dnsPayments.put(new DnsAddress(address), dnsPayment);
}