mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-06 14:16:37 +00:00
docs: Document the properties of an entry.
This commit is contained in:
parent
438a8d6d64
commit
a7b611550f
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @namespace module:Log~Entry
|
* @namespace module:Log~Entry
|
||||||
* @memberof module:Log
|
* @memberof module:Log
|
||||||
* @description Log Entry
|
* @description A log entry.
|
||||||
*/
|
*/
|
||||||
import Clock from './clock.js'
|
import Clock from './clock.js'
|
||||||
import * as Block from 'multiformats/block'
|
import * as Block from 'multiformats/block'
|
||||||
@ -14,17 +14,32 @@ const hasher = sha256
|
|||||||
const hashStringEncoding = base58btc
|
const hashStringEncoding = base58btc
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an Entry
|
* Creates an Entry.
|
||||||
* @param {Identity} identity The identity instance
|
* @param {module:Identities~Identity} identity The identity instance
|
||||||
* @param {string} logId The unique identifier for this log
|
* @param {string} logId The unique identifier for this log
|
||||||
* @param {*} data Data of the entry to be added. Can be any JSON.stringifyable
|
* @param {*} data Data of the entry to be added. Can be any JSON.stringifyable
|
||||||
* data.
|
* data.
|
||||||
* @param {module:Clock} [clock] The clock
|
* @param {module:Log~Clock} [clock] The clock
|
||||||
* @param {Array<string|Entry>} [next=[]] An array of CIDs as base58btc encoded
|
* @param {Array<string|Entry>} [next=[]] An array of CIDs as base58btc encoded
|
||||||
* strings.
|
* strings which point to the next entries in a chain of entries.
|
||||||
* @param {Array<string|Entry>} [refs=[]] An array of CIDs as base58btc encoded
|
* @param {Array<string|module:Log~Entry>} [refs=[]] An array of CIDs as
|
||||||
* strings.
|
* base58btc encoded strings pointing to various entries which come before
|
||||||
* @return {Promise<Entry>}
|
* this entry.
|
||||||
|
* @return {Promise<module:Log~Entry>} A promise which contains an instance of
|
||||||
|
* Entry.
|
||||||
|
* Entry consists of the following properties:
|
||||||
|
*
|
||||||
|
* - id: A string linking multiple entries together,
|
||||||
|
* - payload: An arbitrary chunk of data,
|
||||||
|
* - next: One or more hashes pointing to the next entries in a chain of
|
||||||
|
* entries,
|
||||||
|
* - refs: One or more hashes which reference other entries in the chain,
|
||||||
|
* - clock: A logical clock. See {@link module:Log~Clock},
|
||||||
|
* - v: The version of the entry,
|
||||||
|
* - key: The public key of the identity,
|
||||||
|
* - identity: The identity of the entry's owner,
|
||||||
|
* - sig: The signature of the entry signed by the owner.
|
||||||
|
* @memberof module:Log~Entry
|
||||||
* @example
|
* @example
|
||||||
* const entry = await Entry.create(identity, 'log1', 'hello')
|
* const entry = await Entry.create(identity, 'log1', 'hello')
|
||||||
* console.log(entry)
|
* console.log(entry)
|
||||||
@ -59,11 +74,11 @@ const create = async (identity, id, payload, clock = null, next = [], refs = [])
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies an entry signature.
|
* Verifies an entry signature.
|
||||||
*
|
|
||||||
* @param {Identities} identities Identities system to use
|
* @param {Identities} identities Identities system to use
|
||||||
* @param {Entry} entry The entry being verified
|
* @param {module:Log~Entry} entry The entry being verified
|
||||||
* @return {Promise} A promise that resolves to a boolean value indicating if
|
* @return {Promise<boolean>} A promise that resolves to a boolean value indicating if
|
||||||
* the signature is valid.
|
* the signature is valid.
|
||||||
|
* @memberof module:Log~Entry
|
||||||
*/
|
*/
|
||||||
const verify = async (identities, entry) => {
|
const verify = async (identities, entry) => {
|
||||||
if (!identities) throw new Error('Identities is required, cannot verify entry')
|
if (!identities) throw new Error('Identities is required, cannot verify entry')
|
||||||
@ -86,9 +101,10 @@ const verify = async (identities, entry) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if an object is an Entry.
|
* Checks if an object is an Entry.
|
||||||
* @param {Entry} obj
|
* @param {module:Log~Entry} obj
|
||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
|
* @memberof module:Log~Entry
|
||||||
*/
|
*/
|
||||||
const isEntry = (obj) => {
|
const isEntry = (obj) => {
|
||||||
return obj && obj.id !== undefined &&
|
return obj && obj.id !== undefined &&
|
||||||
@ -99,25 +115,28 @@ const isEntry = (obj) => {
|
|||||||
obj.refs !== undefined
|
obj.refs !== undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether two entries are equal.
|
||||||
|
* @param {module:Log~Entry} a An entry to compare.
|
||||||
|
* @param {module:Log~Entry} b An entry to compare.
|
||||||
|
* @return {boolean} True if a and b are equal, false otherwise.
|
||||||
|
* @memberof module:Log~Entry
|
||||||
|
*/
|
||||||
const isEqual = (a, b) => {
|
const isEqual = (a, b) => {
|
||||||
return a && b && a.hash === b.hash
|
return a && b && a.hash === b.hash
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode a serialized Entry from bytes
|
* Decodes a serialized Entry from bytes
|
||||||
* @param {Uint8Array} bytes
|
* @param {Uint8Array} bytes
|
||||||
* @return {Entry}
|
* @return {module:Log~Entry}
|
||||||
|
* @memberof module:Log~Entry
|
||||||
*/
|
*/
|
||||||
const decode = async (bytes) => {
|
const decode = async (bytes) => {
|
||||||
const { value } = await Block.decode({ bytes, codec, hasher })
|
const { value } = await Block.decode({ bytes, codec, hasher })
|
||||||
return _encodeEntry(value)
|
return _encodeEntry(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode an Entry to a serializable form
|
|
||||||
* @param {Entry} entry
|
|
||||||
* @return {TODO}
|
|
||||||
*/
|
|
||||||
const _encodeEntry = async (entry) => {
|
const _encodeEntry = async (entry) => {
|
||||||
const { cid, bytes } = await Block.encode({ value: entry, codec, hasher })
|
const { cid, bytes } = await Block.encode({ value: entry, codec, hasher })
|
||||||
const hash = cid.toString(hashStringEncoding)
|
const hash = cid.toString(hashStringEncoding)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user