beetlebug▾
/

ztcl::meter record

Gives the usage record of a meter: one line of JSON in a hash chain.

ztcl::meter record NAME ?-tenant TEXT? ?-script TEXT? ?-sign? ?-key SEED?

Description

Gives one line of JSON with the fields version, meter, tenant, seq, start_ms, end_ms, the counts of get, and prev:

With -key or -sign, the record ends with key, the hex public key, and sig, the hex Ed25519 signature. The signed text is the record with ,"sig":"..." removed: everything before ,"sig", then a closing brace. A customer checks a record in any language: cut off the last field, add a closing brace, and check the signature with the public key of the host.

Arguments

ArgumentTypeDefaultDescription
NAMEstringrequiredThe name of a meter.

Options

OptionTypeDefaultDescription
-tenantstringnoneGives the tenant of this record.
-scriptstringnoneAdds the SHA-256 of a script to the record.
-signflagnoneSigns the record with the key of the host.
-key64 hex digitsnoneSigns the record with the Ed25519 key of a seed.

-tenant TEXT

Gives the tenant of this record.

The field tenant of this record is TEXT, in place of the tenant of the meter. The meter keeps its tenant.

% ztcl::meter create h -tenant acme
> set a [ztcl::json get [ztcl::meter record h -tenant x] tenant]
> list $a [ztcl::json get [ztcl::meter record h] tenant]
x acme

Each text is a valid tenant.

-script TEXT

Adds the SHA-256 of a script to the record.

The record has the field script_sha256, the hex SHA-256 of the UTF-8 bytes of TEXT, after end_ms.

% ztcl::meter create d
> ztcl::json get [ztcl::meter record d -script {puts hi}] script_sha256
3c7a71eb5dc3bdec4b2c935ae8e8fe52a9d6b2af64c1ac7c7319fb8d4750af8f

Each text has a hash.

-sign

Signs the record with the key of the host.

Signs with the Ed25519 key that the embedder sets in Interp.meter_key, so the script never has the key. The ztcl program sets no key.

% ztcl::meter create s
> catch {ztcl::meter record s -sign} msg
> set msg
the host gave no key: use -key, or set Interp.meter_key
% ztcl::meter create s
> ztcl::meter record s -sign
the host gave no key: use -key, or set Interp.meter_key
-errorcode ZTCL METER KEY

-key SEED

Signs the record with the Ed25519 key of a seed.

Signs with the key pair of the 32-byte seed SEED, given as 64 hex digits. It serves tests and a host script that holds its own key.

% ztcl::meter create k
> set seed [string repeat 0 63]1
> set r [ztcl::meter record k -key $seed]
> list [string equal [ztcl::json get $r key] [ztcl::meter pubkey $seed]] [ztcl::meter verify $r [ztcl::meter pubkey $seed]]
1 1
% ztcl::meter create k
> ztcl::meter record k -key xyz
a seed is 64 hex digits
-errorcode ZTCL METER KEY

Returns

A line of JSON.

Examples

Each example of this page runs after package require ztcl::json.

% ztcl::meter create c
> set r [ztcl::meter record c]
> list [ztcl::json get $r version] [ztcl::json get $r meter] [ztcl::json get $r seq] [ztcl::json get $r prev] [ztcl::json exists $r sig]
1 c 1 0000000000000000000000000000000000000000000000000000000000000000 0

% ztcl::meter create c2
> ztcl::meter record c2
> ztcl::json get [ztcl::meter record c2] seq
2

Errors

% ztcl::meter record nope
no meter "nope"
-errorcode ZTCL METER NAME

% ztcl::meter record
wrong # args: should be "ztcl::meter record name ?-tenant text? ?-script text? ?-sign? ?-key seed?"
-errorcode TCL WRONGARGS

% ztcl::meter create c
> ztcl::meter record c -bogus 1
bad option "-bogus": must be -key, -script, -sign, or -tenant
-errorcode TCL LOOKUP INDEX option

Facts

Capability
meter. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also