Objects with hex values - for nmostr.
Description
Public and Private keys, Signatures, and Event IDs implemented as objects with data/hex pairs.
See also:
§ Hex utils
Primitives for working with hexadecimal strings. Implemented using StackStrings as our hexes each have a constant length.
§ Objects
The objects themselves, access the bytes and hex using obj.toBytes and obj.toHex. Use Object.fromHex or Object.fromBytes for initialization.
Types
EventID = object raw*: array[32, byte] hex*: StackString[64]
PublicKey = object raw*: array[64, byte] hex*: StackString[64]
- A Nostr public key is a secp256k1 x-only public key. An x-only public key encodes a point whose Y coordinate is even. It is serialized and transmitted using only its X coordinate (32 bytes).
SchnorrSignature = object raw*: array[64, byte] hex*: StackString[128]
Procs
func dumpHook(s: var string; v: PublicKey | EventID | SchnorrSignature) {.inline.}
- Serialize id, pubkey, and sig into hexadecimal.
proc dumpHook(s: var string; v: StackString) {.inline.}
func fromHex(T: typedesc[PublicKey | EventID | SchnorrSignature]; hex: auto): T:type {. inline.}
func fromHex(T: typedesc[seq[byte]]; hex: auto): T:type {.inline.}
func fromHex[N, N2](T: typedesc[array[N, byte]]; hex: array[N2, char]): T:type {. inline.}
func fromHex[N](T: typedesc[array[N, byte]]; hex: openArray[char]): T:type {. inline.}
func fromHex[N](T: typedesc[array[N, byte]]; hex: StackString): T:type {.inline.}
func hexToRaw(v: EventID | SchnorrSignature): auto {.inline.}
- Parse raw data from hex
func init(T: typedesc[PublicKey]; raw: sink array[64, byte]): T:type {.inline.}
- Note that raw != toBytes for PublicKey. Use fromBytes to get a PublicKey from an array[32, byte] x-only public key.
func init(T: typedesc[SchnorrSignature]; raw: sink array[64, byte]): T:type {. inline.}
func parseHook(s: string; i: var int; v: var (PublicKey | EventID | SchnorrSignature)) {.inline.}
- Parse id, pubkey, and sig as a hexadecimal encoding (of a sha256 hash).
func parseHook(s: string; i: var int; v: var StackString) {.inline.}
func populate(v: var (PublicKey | EventID | SchnorrSignature)) {.inline.}
- Makes sure both the hex and raw fields are populated, raises an exception if v is an empty object.
func populateHex(v: var (PublicKey | EventID | SchnorrSignature)) {.inline.}
- Update the hex of v based on its raw data
func populateRaw(v: var (PublicKey | EventID | SchnorrSignature)) {.inline.}
- Update the raw data of v based on its hex
func readHexChar(c: char): byte {.inline, ...raises: [ValueError], tags: [], forbids: [].}
- Converts a hex char to a byte
func toHex(bytes: openArray[byte]): string {.inline, ...raises: [], tags: [], forbids: [].}
- Convert a byte-sequence to its hex representation
func toHex[N](bytes: array[N, byte]): auto {.inline.}
- Convert a byte-array to its hex representation
func toString(v: PublicKey | EventID | SchnorrSignature): string {.inline.}
Templates
template `$`(v: PublicKey | EventID | SchnorrSignature): string
template bytes(v: EventID | SchnorrSignature): auto
template bytesLen(T: typedesc[EventID | SchnorrSignature]): Positive
template fromBytes(T: typedesc[EventID | SchnorrSignature]; bytes: openArray[byte]): T:type
template rawToHex(v: PublicKey | EventID | SchnorrSignature): auto
- Parse hex from raw data
template toBytes(v: EventID | SchnorrSignature): auto
template toHex(v: PublicKey | EventID | SchnorrSignature): auto