mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-10 23:14:13 +02:00
Documents OTS methods
This commit is contained in:
@@ -61,17 +61,30 @@ class OtsEvent(
|
|||||||
const val KIND = 1040
|
const val KIND = 1040
|
||||||
const val ALT = "Opentimestamps Attestation"
|
const val ALT = "Opentimestamps Attestation"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stamp is used to save OTS requests locally while we want for the bitcoin
|
||||||
|
* blockchain to include the block with the proof.
|
||||||
|
*/
|
||||||
fun stamp(
|
fun stamp(
|
||||||
eventId: HexKey,
|
eventId: HexKey,
|
||||||
resolver: OtsResolver,
|
resolver: OtsResolver,
|
||||||
) = resolver.stamp(eventId.hexToByteArray())
|
) = resolver.stamp(eventId.hexToByteArray())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a stamp into an attestation as soon as the bitcoin chain confirms
|
||||||
|
* the block with it. If the return is not null, the attestation can be sent
|
||||||
|
* to Nostr.
|
||||||
|
*/
|
||||||
fun upgrade(
|
fun upgrade(
|
||||||
otsState: ByteArray,
|
otsState: ByteArray,
|
||||||
eventId: HexKey,
|
eventId: HexKey,
|
||||||
resolver: OtsResolver,
|
resolver: OtsResolver,
|
||||||
) = resolver.upgrade(otsState, eventId.hexToByteArray())
|
) = resolver.upgrade(otsState, eventId.hexToByteArray())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies if the attestation contained in a Nostr Event is valid by checking
|
||||||
|
* with the blockchain.
|
||||||
|
*/
|
||||||
fun verify(
|
fun verify(
|
||||||
otsState: ByteArray,
|
otsState: ByteArray,
|
||||||
eventId: HexKey,
|
eventId: HexKey,
|
||||||
|
@@ -33,14 +33,35 @@ import com.vitorpamplona.quartz.nip03Timestamp.ots.http.CalendarPureJavaBuilder
|
|||||||
import com.vitorpamplona.quartz.nip03Timestamp.ots.op.OpSHA256
|
import com.vitorpamplona.quartz.nip03Timestamp.ots.op.OpSHA256
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolver class for OpenTimestamps operations.
|
||||||
|
*
|
||||||
|
* This class provides functionality to stamp data with OpenTimestamps,
|
||||||
|
* upgrade existing timestamps, and verify timestamp proofs against data.
|
||||||
|
*
|
||||||
|
* @property explorer The Bitcoin explorer used for verification operations.
|
||||||
|
* @property calendarBuilder The calendar builder used for stamping operations.
|
||||||
|
*/
|
||||||
class OtsResolver(
|
class OtsResolver(
|
||||||
explorer: BitcoinExplorer = BlockstreamExplorer(),
|
explorer: BitcoinExplorer = BlockstreamExplorer(),
|
||||||
calendarBuilder: CalendarBuilder = CalendarPureJavaBuilder(),
|
calendarBuilder: CalendarBuilder = CalendarPureJavaBuilder(),
|
||||||
) {
|
) {
|
||||||
val ots: OpenTimestamps = OpenTimestamps(explorer, calendarBuilder)
|
val ots: OpenTimestamps = OpenTimestamps(explorer, calendarBuilder)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a human-readable information string about the given OTS file.
|
||||||
|
*
|
||||||
|
* @param otsState The serialized OpenTimestamps file data.
|
||||||
|
* @return A string containing information about the timestamp.
|
||||||
|
*/
|
||||||
fun info(otsState: ByteArray): String = ots.info(DetachedTimestampFile.deserialize(otsState))
|
fun info(otsState: ByteArray): String = ots.info(DetachedTimestampFile.deserialize(otsState))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a local timestamp proof for the given data.
|
||||||
|
*
|
||||||
|
* @param data The data to be timestamped.
|
||||||
|
* @return A serialized DetachedTimestampFile containing the timestamp proof.
|
||||||
|
*/
|
||||||
fun stamp(data: ByteArray): ByteArray {
|
fun stamp(data: ByteArray): ByteArray {
|
||||||
val hash = Hash(data, OpSHA256.TAG)
|
val hash = Hash(data, OpSHA256.TAG)
|
||||||
val file = DetachedTimestampFile.from(hash)
|
val file = DetachedTimestampFile.from(hash)
|
||||||
@@ -49,6 +70,13 @@ class OtsResolver(
|
|||||||
return detachedToSerialize.serialize()
|
return detachedToSerialize.serialize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to upgrade an existing timestamp to a verified proof.
|
||||||
|
*
|
||||||
|
* @param otsState The serialized OpenTimestamps file data to upgrade.
|
||||||
|
* @param data The original data that was timestamped.
|
||||||
|
* @return A serialized DetachedTimestampFile with upgraded proof if successful, null otherwise.
|
||||||
|
*/
|
||||||
fun upgrade(
|
fun upgrade(
|
||||||
otsState: ByteArray,
|
otsState: ByteArray,
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
@@ -67,11 +95,25 @@ class OtsResolver(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies an OpenTimestamps proof against the original data.
|
||||||
|
*
|
||||||
|
* @param otsFile The serialized OpenTimestamps file data.
|
||||||
|
* @param data The original data to verify against.
|
||||||
|
* @return VerificationState indicating the result of the verification.
|
||||||
|
*/
|
||||||
fun verify(
|
fun verify(
|
||||||
otsFile: ByteArray,
|
otsFile: ByteArray,
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
): VerificationState = verify(DetachedTimestampFile.deserialize(otsFile), data)
|
): VerificationState = verify(DetachedTimestampFile.deserialize(otsFile), data)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies an OpenTimestamps proof against the original data.
|
||||||
|
*
|
||||||
|
* @param detachedOts The DetachedTimestampFile containing the proof.
|
||||||
|
* @param data The original data to verify against.
|
||||||
|
* @return VerificationState indicating the result of the verification.
|
||||||
|
*/
|
||||||
fun verify(
|
fun verify(
|
||||||
detachedOts: DetachedTimestampFile,
|
detachedOts: DetachedTimestampFile,
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
|
Reference in New Issue
Block a user