builder

NUC builder.

class NucTokenBuilder(body: InvocationBody | DelegationBody, audience: Did | None = None, subject: Did | None = None, not_before: datetime | None = None, expires_at: datetime | None = None, command: Command | None = None, meta: Dict[str, Any] | None = None, nonce: bytes | None = None, proof: NucTokenEnvelope | None = None)[source]

Bases: object

A builder for a NUC token.

Example

from secp256k1 import PrivateKey
from nuc.builder NucTokenBuilder
from nuc.token import Did, Command
from nuc.policy import Policy

# Create a key to sign the generated token.
key = PrivateKey()

# Create a token.
token = NucTokenBuilder.delegation([Policy.equals(".args.foo", 42)])
    .audience(Did(bytes([0xBB] * 33)))
    .subject(Did(bytes([0xCC] * 33)))
    .command(Command(["nil", "db", "read"]))
    .build(key)
static delegation(policies: List[Policy]) NucTokenBuilder[source]

Create a new token builder for a delegation.

Parameters:

policies – The policies to use in the delegation.

static invocation(args: Dict[str, Any]) NucTokenBuilder[source]

Create a new token builder for an invocation.

Parameters:

args – The arguments to use in the invocation.

static extending(envelope: NucTokenEnvelope) NucTokenBuilder[source]

Create a token that pulls basic properties from another one.

This pulls the following properties from the given envelope:

  • command

  • subject

The given token will be used as a proof for this one so there’s no need to call anything else to link them.

Parameters:

envelope – The envelope to extend.

body(body: InvocationBody | DelegationBody) NucTokenBuilder[source]

Set the body for the token being built.

Parameters:

body – The body for the token.

audience(audience: Did) Self[source]

Set the audience for the token to be built.

The audience must be the entity this token is going to be sent to.

Parameters:

audience – The audience of the token.

subject(subject: Did) Self[source]

Set the subject for the token to be built.

Parameters:

subject – The subject of the token.

not_before(not_before: datetime) Self[source]

Set the not before date for the token to be built.

Parameters:

not_before – The timestamp at which the token will become valid.

expires_at(expires_at: datetime) Self[source]

Set the expires at date for the token to be built.

Parameters:

expires_at – The timestamp at which the token will expire.

command(command: Command) Self[source]

Set the command for the token to be built.

Parameters:

command – The command for the token to be built.

meta(meta: Dict[str, Any]) Self[source]

Set the metadata for the token to be built.

Parameters:

meta – The metadata for the built token.

nonce(nonce: bytes) Self[source]

Set the nonce for the token to be built.

Parameters:
  • nonce – The nonce to be set.

  • note: (..) – The nonce doesn’t have to be explicitly set and it will default to: a random 16 byte long bytestring if not set.

proof(proof: NucTokenEnvelope) Self[source]

Set the proof for the token to be built.

It’s recommended to call NucTokenBuilder.extending() which also takes care of pulling other important fields.

Parameters:

proof – The token to be used as proof.

build(key: PrivateKey) str[source]

Build the token, signing it using the given private key.

Parameters:

key – The key to use to sign the token.

__eq__(other)

Return self==value.

__repr__()

Return repr(self).

exception TokenBuildException[source]

Bases: Exception

An exception raised when building a token.