token

NUC tokens.

class Did(public_key: bytes)[source]

Bases: object

A class representing a Decentralized Identifier (DID).

public_key: bytes
static nil(public_key: bytes) Did[source]

Construct a new DID for the “nil” method.

Parameters:

public_key – The public key in compressed form.

static parse(data: str) Did[source]

Parse a DID from a string.

__str__() str[source]

Return str(self).

__delattr__(name)

Implement delattr(self, name).

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

__setattr__(name, value)

Implement setattr(self, name, value).

class Command(segments: List[str])[source]

Bases: object

A command to be invoked.

segments: List[str]
static parse(data: str) Command[source]

Parse a command from a string.

Example

from nuc.token import Command

command = Command.parse("/nil/db/read")
is_attenuation_of(other: Command) bool[source]

Check if this command is an attenuation of another one.

Example

from nuc.token import Command

parent = Command.parse("/nil/db")
child = Command.parse("/nil/db/read")

assert child.is_attenuation_of(parent)
__str__() str[source]

Return str(self).

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class InvocationBody(args: Dict[str, Any])[source]

Bases: object

The body of an invocation.

args: Dict[str, Any]
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class DelegationBody(policies: List[Policy])[source]

Bases: object

The body of a delegation.

policies: List[Policy]
__eq__(other)

Return self==value.

__repr__()

Return repr(self).

class NucToken(issuer: Did, audience: Did, subject: Did, not_before: datetime | None, expires_at: datetime | None, command: Command, body: InvocationBody | DelegationBody, meta: Dict[str, Any] | None, nonce: bytes, proofs: List[bytes])[source]

Bases: object

A class representing a NUC token.

issuer: Did
audience: Did
subject: Did
not_before: datetime | None
expires_at: datetime | None
command: Command
body: InvocationBody | DelegationBody
meta: Dict[str, Any] | None
nonce: bytes
proofs: List[bytes]
static parse(raw_json: str | bytes) NucToken[source]

Parse a NUC token from a string.

Parameters:

raw_json – The raw JSON to be parsed.

Example

from nuc.token import NucToken

token = NucToken.parse(
    {
        "iss": "did:nil:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        "aud": "did:nil:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
        "sub": "did:nil:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
        "cmd": "/nil/db/read",
        "pol": [["==", ".foo", 42]],
        "nonce": "beef",
    }
)
to_json() Dict[str, Any][source]

Convert this token into JSON.

__str__() str[source]

Return str(self).

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

exception MalformedDidException[source]

Bases: Exception

An exception raised when a malformed DID is parsed.

exception MalformedCommandException[source]

Bases: Exception

An exception raised when a malformed command is parsed.

exception MalformedNucTokenException[source]

Bases: Exception

An exception raised when a malformed NUC token is parsed.