首页 > 解决方案 > Opentimestamps - 尝试反序列化 OTS 文件时出现 Java NullPointerException

问题描述

当我尝试使用 Java 库验证时间戳时,我遇到了 Java NullPointerException。

我正在关注https://github.com/opentimestamps/java-opentimestamps的自述文件中的示例

这是我的代码:

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);

这是一个例外:

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]

有谁知道如何正确读取原始文件和 ots 文件以正确验证文件?

标签: javablockchainbitcoin

解决方案


ots文件可以读取如下

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

可以读取普通文件并将其转换为 DetachedTimeStamp 为,

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

最后,可以使用 detached 和 detachedOts 文件调用 verify 方法

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

推荐阅读