Skip to main content

Module: codecs

Common codecs for DID packages.

Purpose

Codecs for encoding, decoding and validating types used by the DID packages.

Installation

npm install @didtools/codecs

Usage

Validate a string is a valid DID:

import { didString } from '@didtools/codecs'
import { isRight, validate } from 'codeco'

const result = validate(didString, 'did:key:...')
const isValid = isRight(result)

Encode and decode a Uint8Array as base64-encoded string:

import { uint8ArrayAsBase64 } from '@didtools/codecs'
import { decode } from 'codeco'

const bytes = new Uint8Array(...)
const encoded = uint8ArrayAsBase64.encode(bytes)
const decoded = decode(uint8ArrayAsBase64, encoded)

Type Aliases

Cacao

Ƭ Cacao: Object

Type declaration

NameType
hCacaoHeader
pCacaoPayload
s?CacaoSignature

CacaoHeader

Ƭ CacaoHeader: Object

Type declaration

NameType
t"eip4361" | "caip122"

CacaoPayload

Ƭ CacaoPayload: Object

Type declaration

NameType
audstring
domainstring
exp?string
iatstring
issstring
nbf?string
noncestring
requestId?string
resources?string[]
statement?string
versionstring

CacaoSignature

Ƭ CacaoSignature: Object

Type declaration

NameType
m?SignatureMeta
sstring
t"eip191" | "eip1271" | "solana:ed25519" | "tezos:ed25519" | "stacks:secp256k1" | "webauthn:p256"

DIDString

Ƭ DIDString: Opaque<string, "DIDString">


DagJWS

Ƭ DagJWS: Object

Type declaration

NameType
link?CID
payloadstring
signaturesJWSSignature[]

GeneralJWS

Ƭ GeneralJWS: Object

Type declaration

NameType
payloadstring
signaturesJWSSignature[]

JWSSignature

Ƭ JWSSignature: Object

Type declaration

NameType
protectedstring
signaturestring

SignatureMeta

Ƭ SignatureMeta: any


SignedCacao

Ƭ SignedCacao: Object

Type declaration

NameType
hCacaoHeader
pCacaoPayload
sCacaoSignature

Variables

Cacao

Const Cacao: SparseCodec<{ h: ExactCodec<TypeCodec<{ t: KeyOfCodec<{ caip122: null = null; eip4361: null = null }> }>> = CacaoHeader; p: SparseCodec<{ aud: TrivialCodec<string> = string; domain: TrivialCodec<string> = string; exp: OptionalCodec<TrivialCodec<string>> ; iat: TrivialCodec<string> = string; iss: TrivialCodec<string> = string; nbf: OptionalCodec<TrivialCodec<string>> ; nonce: TrivialCodec<string> = string; requestId: OptionalCodec<TrivialCodec<string>> ; resources: OptionalCodec<Codec<string[], string[], unknown> & {}> ; statement: OptionalCodec<TrivialCodec<string>> ; version: TrivialCodec<string> = string }> = CacaoPayload; s: OptionalCodec<ExactCodec<TypeCodec<{ s: TrivialCodec<string> = string; t: KeyOfCodec<{ eip1271: null = null; eip191: null = null; solana:ed25519: null = null; stacks:secp256k1: null = null; tezos:ed25519: null = null }> }>>> }>


CacaoHeader

Const CacaoHeader: ExactCodec<TypeCodec<{ t: KeyOfCodec<{ caip122: null = null; eip4361: null = null }> }>>


CacaoPayload

Const CacaoPayload: SparseCodec<{ aud: TrivialCodec<string> = string; domain: TrivialCodec<string> = string; exp: OptionalCodec<TrivialCodec<string>> ; iat: TrivialCodec<string> = string; iss: TrivialCodec<string> = string; nbf: OptionalCodec<TrivialCodec<string>> ; nonce: TrivialCodec<string> = string; requestId: OptionalCodec<TrivialCodec<string>> ; resources: OptionalCodec<Codec<string[], string[], unknown> & {}> ; statement: OptionalCodec<TrivialCodec<string>> ; version: TrivialCodec<string> = string }>


CacaoSignature

Const CacaoSignature: ExactCodec<TypeCodec<{ s: TrivialCodec<string> = string; t: KeyOfCodec<{ eip1271: null = null; eip191: null = null; solana:ed25519: null = null; stacks:secp256k1: null = null; tezos:ed25519: null = null }> }>>


DagJWS

Const DagJWS: SparseCodec<{ link: OptionalCodec<Type<CID<unknown, number, number, Version>, string, string>> ; payload: TrivialCodec<string> = string; signatures: Codec<MapIn<{ protected: TrivialCodec<string> = string; signature: TrivialCodec<string> = string }, $TypeOf>[], MapIn<{ protected: TrivialCodec<string> = string; signature: TrivialCodec<string> = string }, $OutputOf>[], unknown> & {} }>


GeneralJWS

Const GeneralJWS: ExactCodec<TypeCodec<{ payload: TrivialCodec<string> = string; signatures: Codec<MapIn<{ protected: TrivialCodec<string> = string; signature: TrivialCodec<string> = string }, $TypeOf>[], MapIn<{ protected: TrivialCodec<string> = string; signature: TrivialCodec<string> = string }, $OutputOf>[], unknown> & {} }>>


JWSSignature

Const JWSSignature: ExactCodec<TypeCodec<{ protected: TrivialCodec<string> = string; signature: TrivialCodec<string> = string }>>


SignedCacao

Const SignedCacao: ExactCodec<TypeCodec<{ h: ExactCodec<TypeCodec<{ t: KeyOfCodec<{ caip122: null = null; eip4361: null = null }> }>> = CacaoHeader; p: SparseCodec<{ aud: TrivialCodec<string> = string; domain: TrivialCodec<string> = string; exp: OptionalCodec<TrivialCodec<string>> ; iat: TrivialCodec<string> = string; iss: TrivialCodec<string> = string; nbf: OptionalCodec<TrivialCodec<string>> ; nonce: TrivialCodec<string> = string; requestId: OptionalCodec<TrivialCodec<string>> ; resources: OptionalCodec<Codec<string[], string[], unknown> & {}> ; statement: OptionalCodec<TrivialCodec<string>> ; version: TrivialCodec<string> = string }> = CacaoPayload; s: ExactCodec<TypeCodec<{ s: TrivialCodec<string> = string; t: KeyOfCodec<{ eip1271: null = null; eip191: null = null; solana:ed25519: null = null; stacks:secp256k1: null = null; tezos:ed25519: null = null }> }>> = CacaoSignature }>>


cid

Const cid: Type<CID<unknown, number, number, Version>, CID<unknown, number, number, Version>, unknown>

Passthrough codeco codec for CID.


cidAsString

Const cidAsString: Type<CID<unknown, number, number, Version>, string, string>

codeco codec for CID encoded as string.


didString

Const didString: RefinementCodec<TrivialCodec<string>, string & WithOpaque<"DIDString">>

codeco codec for a vanilla DID string, i.e. did:method:id.


uint8ArrayAsBase64

Const uint8ArrayAsBase64: Type<Uint8Array, string, string>

codeco codec for Uint8Array as base64-encoded string.


uint8ArrayAsBase64pad

Const uint8ArrayAsBase64pad: Type<Uint8Array, string, string>

codeco codec for Uint8Array as base64pad-encoded string.


uint8ArrayAsBase64url

Const uint8ArrayAsBase64url: Type<Uint8Array, string, string>

codeco codec for Uint8Array as base64url-encoded string.


uint8array

Const uint8array: TrivialCodec<Uint8Array>

codeco codec for JS Uint8Array.

Functions

asDIDString

asDIDString(input): DIDString

Type cast input as DIDString.

Parameters

NameType
inputstring

Returns

DIDString


createUint8ArrayAsString

createUint8ArrayAsString(encoding): Type<Uint8Array, string, string>

Factory for codeco codec for Uint8Array as encoded string.

Parameters

NameType
encodingSupportedEncodings

Returns

Type<Uint8Array, string, string>


isDIDString

isDIDString(input): input is string & WithOpaque<"DIDString">

Verify if input is DID string, i.e. conforms to did:method:id format.

Parameters

NameType
inputstring

Returns

input is string & WithOpaque<"DIDString">


isUint8Array

isUint8Array(input): input is Uint8Array

Check if the input is a JS Uint8Array.

Parameters

NameType
inputunknown

Returns

input is Uint8Array