feat(opentimestamps): updates utils and adds comments
This commit is contained in:
parent
85bf907f54
commit
a2138f1de1
@ -1,13 +1,8 @@
|
|||||||
import {
|
import { OpenTimestamp, OpenTimestampUpgradeVerifyResponse } from '../types'
|
||||||
Timestamp,
|
import { retry } from './retry.ts'
|
||||||
TimestampUpgradeResponse,
|
|
||||||
TimestampUpgradeVerifyResponse
|
|
||||||
} from '../types'
|
|
||||||
import { retry, retryAll } from './retry.ts'
|
|
||||||
import { bytesToHex } from '@noble/hashes/utils'
|
import { bytesToHex } from '@noble/hashes/utils'
|
||||||
import { utf8Encoder } from 'nostr-tools/utils'
|
import { utf8Encoder } from 'nostr-tools/utils'
|
||||||
import { hexStringToUint8Array } from './string.ts'
|
import { hexStringToUint8Array } from './string.ts'
|
||||||
import { isPromiseFulfilled } from './utils.ts'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a timestamp for the provided nostr event ID.
|
* Generates a timestamp for the provided nostr event ID.
|
||||||
@ -15,10 +10,10 @@ import { isPromiseFulfilled } from './utils.ts'
|
|||||||
*/
|
*/
|
||||||
export const generateTimestamp = async (
|
export const generateTimestamp = async (
|
||||||
nostrId: string
|
nostrId: string
|
||||||
): Promise<Timestamp | undefined> => {
|
): Promise<OpenTimestamp | undefined> => {
|
||||||
try {
|
try {
|
||||||
return {
|
return {
|
||||||
timestamp: await retry(() => timestamp(nostrId)),
|
value: await retry(() => timestamp(nostrId)),
|
||||||
nostrId: nostrId
|
nostrId: nostrId
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -27,37 +22,31 @@ export const generateTimestamp = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to upgrade (i.e. add Bitcoin blockchain attestations) and verify the provided timestamp.
|
||||||
|
* Returns the same timestamp, alongside additional information required to decide if any further
|
||||||
|
* timestamp updates are required.
|
||||||
|
* @param timestamp
|
||||||
|
*/
|
||||||
export const upgradeAndVerifyTimestamp = async (
|
export const upgradeAndVerifyTimestamp = async (
|
||||||
timestamp: Timestamp
|
timestamp: OpenTimestamp
|
||||||
): Promise<TimestampUpgradeVerifyResponse> => {
|
): Promise<OpenTimestampUpgradeVerifyResponse> => {
|
||||||
const upgradedResult = await retry(() => upgrade(timestamp))
|
const upgradedResult = await retry(() => upgrade(timestamp))
|
||||||
const verifiedResult = await verify(upgradedResult)
|
return await verify(upgradedResult)
|
||||||
|
|
||||||
console.log('verifiedResult: ', verifiedResult)
|
|
||||||
|
|
||||||
return verifiedResult
|
|
||||||
}
|
|
||||||
|
|
||||||
export const upgradeTimestamps = async (
|
|
||||||
timestamps: Timestamp[]
|
|
||||||
): Promise<TimestampUpgradeResponse[]> => {
|
|
||||||
const resolved = await retryAll(timestamps.map((t) => () => upgrade(t)))
|
|
||||||
return resolved.filter(isPromiseFulfilled).map((res) => res.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const verifyTimestamps = async (timestamps: Timestamp[]) => {
|
|
||||||
const settledResults = await Promise.allSettled(
|
|
||||||
timestamps.map(async (timestamp) => verify(timestamp))
|
|
||||||
)
|
|
||||||
return settledResults.filter(isPromiseFulfilled).map((res) => res.value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to upgrade a timestamp. If an upgrade is available,
|
||||||
|
* it will add new data to detachedTimestamp.
|
||||||
|
* The upgraded flag indicates if an upgrade has been performed.
|
||||||
|
* @param t - timestamp
|
||||||
|
*/
|
||||||
const upgrade = async (
|
const upgrade = async (
|
||||||
t: Timestamp
|
t: OpenTimestamp
|
||||||
): Promise<TimestampUpgradeVerifyResponse> => {
|
): Promise<OpenTimestampUpgradeVerifyResponse> => {
|
||||||
let detachedTimestamp =
|
const detachedTimestamp =
|
||||||
window.OpenTimestamps.DetachedTimestampFile.deserialize(
|
window.OpenTimestamps.DetachedTimestampFile.deserialize(
|
||||||
hexStringToUint8Array(t.timestamp)
|
hexStringToUint8Array(t.value)
|
||||||
)
|
)
|
||||||
|
|
||||||
const changed: boolean =
|
const changed: boolean =
|
||||||
@ -71,29 +60,32 @@ const upgrade = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
value,
|
timestamp: value,
|
||||||
upgraded: true,
|
upgraded: true
|
||||||
isComplete: detachedTimestamp.timestamp.isTimestampComplete()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
value: t,
|
timestamp: t,
|
||||||
upgraded: false,
|
upgraded: false
|
||||||
isComplete: detachedTimestamp.timestamp.isTimestampComplete()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to verify a timestamp. If verification is available,
|
||||||
|
* it will be included in the returned object.
|
||||||
|
* @param t - timestamp
|
||||||
|
*/
|
||||||
export const verify = async (
|
export const verify = async (
|
||||||
t: TimestampUpgradeVerifyResponse
|
t: OpenTimestampUpgradeVerifyResponse
|
||||||
): Promise<TimestampUpgradeVerifyResponse> => {
|
): Promise<OpenTimestampUpgradeVerifyResponse> => {
|
||||||
const detachedNostrId = window.OpenTimestamps.DetachedTimestampFile.fromBytes(
|
const detachedNostrId = window.OpenTimestamps.DetachedTimestampFile.fromBytes(
|
||||||
new window.OpenTimestamps.Ops.OpSHA256(),
|
new window.OpenTimestamps.Ops.OpSHA256(),
|
||||||
utf8Encoder.encode(t.value.nostrId)
|
utf8Encoder.encode(t.timestamp.nostrId)
|
||||||
)
|
)
|
||||||
|
|
||||||
let detachedTimestamp =
|
const detachedTimestamp =
|
||||||
window.OpenTimestamps.DetachedTimestampFile.deserialize(
|
window.OpenTimestamps.DetachedTimestampFile.deserialize(
|
||||||
hexStringToUint8Array(t.value.timestamp)
|
hexStringToUint8Array(t.timestamp.value)
|
||||||
)
|
)
|
||||||
|
|
||||||
const res = await window.OpenTimestamps.verify(
|
const res = await window.OpenTimestamps.verify(
|
||||||
@ -108,6 +100,10 @@ export const verify = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timestamps a nostrId.
|
||||||
|
* @param nostrId
|
||||||
|
*/
|
||||||
const timestamp = async (nostrId: string): Promise<string> => {
|
const timestamp = async (nostrId: string): Promise<string> => {
|
||||||
const detachedTimestamp =
|
const detachedTimestamp =
|
||||||
window.OpenTimestamps.DetachedTimestampFile.fromBytes(
|
window.OpenTimestamps.DetachedTimestampFile.fromBytes(
|
||||||
|
Loading…
Reference in New Issue
Block a user