Search code examples
javablockchainbitcoin

Opentimestamps - Java NullPointerException when trying to deserialize OTS file


When I try to verify a timestamp using the Java library, I am running into a Java NullPointerException.

I'm following the examples in the README of https://github.com/opentimestamps/java-opentimestamps

This is my code:

byte[] originalFile = Files.readAllBytes(fileData.get("absoluteFilePath"));     
byte[] originalBytes = new String(originalFile).getBytes("UTF-8");
String originalHex = DatatypeConverter.printHexBinary(originalBytes);

byte[] otsFile = Files.readAllBytes(fileData.get("otsFilePath"));
byte[] otsBytes = new String(otsFile).getBytes("UTF-8");
String otsHex = DatatypeConverter.printHexBinary(otsBytes);

// below is from the examples...

DetachedTimestampFile detached = DetachedTimestampFile.from( new OpSHA256(), originalFile );
DetachedTimestampFile detachedOts = DetachedTimestampFile.deserialize(ots);

HashMap<Chains, VerifyResult> result = OpenTimestamps.verify(detachedOts,detached);

And this the exception:

java.lang.NullPointerException: null
    at com.eternitywall.ots.Timestamp.doTagOrAttestation(Timestamp.java:101) ~[java-opentimestamps-1.16.jar:na]
    at com.eternitywall.ots.Timestamp.deserialize(Timestamp.java:89) ~[java-opentimestamps-1.16.jar:na]
    at com.eternitywall.ots.DetachedTimestampFile.deserialize(DetachedTimestampFile.java:107) ~[java-opentimestamps-1.16.jar:na]
    at com.eternitywall.ots.DetachedTimestampFile.deserialize(DetachedTimestampFile.java:120) ~[java-opentimestamps-1.16.jar:na]
    at com.fmr.ots.FileTimestampController.verifyDocument(FileTimestampController.java:110) ~[classes/:na]

Does anyone know how to correctly read the original file and ots file in order to correctly verify the file?


Solution

  • Ots file can be read as follows

    Path pathOts = Paths.get(otsfilename);
    byte[] byteOts = Files.readAllBytes(pathOts);
    DetachedTimestampFile detachedOts = DetachedTimestampFile.deserialize(byteOts);
    

    The normal file can be read and converted to DetachedTimeStamp as,

    File file = new File(originalfilename);
    DetachedTimestampFile detached = DetachedTimestampFile.from(new OpSHA256(), file);
    

    Finally the verify method can be called using the detached and detachedOts file

    HashMap<Chains, VerifyResult> result = OpenTimestamps.verify(detachedOts,detached);