Skip to content

Commit

Permalink
Simplify getting data from ipns mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Jan 31, 2025
1 parent 5f6960b commit 9d40be0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/peergos/net/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ public void handleCallToAPI(HttpExchange httpExchange) {
if (records.isEmpty())
throw new IllegalStateException("Couldn't resolve " + signer);
IpnsRecord latest = records.get(records.size() - 1);
Ipns.IpnsEntry entry = Ipns.IpnsEntry.parseFrom(ByteBuffer.wrap(latest.raw));
IpnsMapping mapping = new IpnsMapping(signer, latest);
Map<String, Object> res = new HashMap<>();
res.put("sig", ArrayOps.bytesToHex(entry.getSignatureV2().toByteArray()));
res.put("data", ArrayOps.bytesToHex(entry.getData().toByteArray()));
res.put("sig", ArrayOps.bytesToHex(mapping.getSignature()));
res.put("data", ArrayOps.bytesToHex(mapping.getData()));
String json = JSONParser.toString(res);
replyJson(httpExchange, json);
break;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/peergos/protocol/ipns/IpnsMapping.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.peergos.protocol.ipns;

import com.google.protobuf.InvalidProtocolBufferException;
import io.ipfs.multihash.*;
import org.peergos.protocol.ipns.pb.Ipns;

import java.nio.ByteBuffer;

public class IpnsMapping {
public final Multihash publisher;
Expand All @@ -10,4 +14,22 @@ public IpnsMapping(Multihash publisher, IpnsRecord value) {
this.publisher = publisher;
this.value = value;
}

public byte[] getData() {
try {
Ipns.IpnsEntry entry = Ipns.IpnsEntry.parseFrom(ByteBuffer.wrap(value.raw));
return entry.getData().toByteArray();
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
}

public byte[] getSignature() {
try {
Ipns.IpnsEntry entry = Ipns.IpnsEntry.parseFrom(ByteBuffer.wrap(value.raw));
return entry.getSignatureV2().toByteArray();
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 9d40be0

Please sign in to comment.