The indy library provides functions needed to use the HyperLedger Indy wire protocol. Messages in this protocol (whose mime type is "application/ssi-agent-wire") are sent in a "packed" format, which includes specified encryption and signing. Since it is not possible to produce and consume these formats directly from KRL, the indy library fills in the missing functionality.

Packing and Unpacking wire messages


Requires a message, the public key of the intended recipient, and our ECI (event channel identifier), and returns a map which is suitable for an http:post to another Indy Agent. Shown here are a sample Indy message, a call to indy:pack to produce the packed format of that message, and what the packed format looks like:

    message = {
    pm = indy:pack(message,[publicKey],meta:eci)
    // { "protected": "eyJlbmMiOiJ4Y2hhY2hhMjBwb2x5MTMwNV9pZXRmIiwidHlwIjoiSldNLzEuMCIsImFsZyI6IkF1d...ZTUlfYVAifX1dfQ==",
    //   "iv": "C3KCoNmLCJ35z0K-",
    //   "ciphertext": "1Nj2jTTzT_ztIAPUgD7hqkupb2t3_FEjIVbepL0jK-ohGOuOY7lT6oJA1QyItAjusk5XLgIrqRI0...28e9OafW7iph2Fm8Ij-FieU=",
    //   "tag": "lwblT6uFSRwEJz3odAwYmQ=="
    // }

The original message is signed and encrypted using the private key associated with the ECI (aka DID (Distributed IDentifier)).

In practice, once a message has been packed, it will be sent to the service endpoint of the other Indy Agent:

    ) setting(http_response)

The content-type should be "application/ssi-agent-wire".


Requires a packed message, and our ECI. A packed message, when received by an event, includes "protected", "iv", "ciphertext", and "tag" as event attributes, and the attributes are suitable for direct use in calling the unpack function. Again, our ECI is a distributed identifier, which includes its private key, used to decrypt the incoming message.

      all = indy:unpack(event:attrs,meta:eci)
      their_key = all{"sender_key"}
      my_key = all{"recipient_key"}
      msg = all{"message"}.decode()

An unpacked message might look like this:

    all == {
      "message":"{\"@type\": \"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message\", ...\"content\": \"Reply with: cupcake\"}",

Notice the use of the String operator decode() to produce the original message as a map:

  "@type": "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message",
  "~l10n": {"locale": "en"},
  "sent_time": "2019-03-16 17:22:23.285831+00:00",
  "content": "Reply with: cupcake"

Signing and verifying message fields

Indy Agent messages can be produced and verified using the org.sovrin.agent_message ruleset. It is available here on GitHub, and excerpts from its code are included below to show how the remaining functions in the indy library are used.

Verifying a signed message field


Some incoming messages have signed fields. These need to be verified and reconstituted. A message with a signed field looks like this:

message = {

The ruleset org.sovrin.agent_message uses the indy:verify_signed_field function to replace the signed field with a verified field of (nearly) the same name:

    verify_signed_field = function(signed_field){
      answer = indy:verify_signed_field(signed_field).klog("answer");
      timestamp = answer{"timestamp"}
        => answer{"field"}.decode().put("timestamp",time:new(timestamp))
        | null
    verify_signatures = function(map){
      map >< "connection~sig"
        => map.put("connection",verify_signed_field(map{"connection~sig"}))
         | map

The indy:verify_signed_field function takes in the signed field, and returns a map like the following one:

  "field":"{\"DID\": \"B58xq3cynkFtm3cFrF8GuA\", \"DIDDoc\": {\"@context\": \"\", ...}}",

It is recommended to use the org.sovrin.agent_message ruleset rather than using this function directly. The ruleset can be used as a module, and provides the verify_signatures function, which can be used on incoming messages.

Signing a message field

Producing a signed field is also done in the org.sovrin.agent_message ruleset, by this function:

    sign_field = function(my_did,my_vk,field){
      timestamp_bytes = toByteArray(time:now().time:strftime("%s"));
      sig_data_bytes = timestamp_bytes
        "@type": t_sign_single,
        "signature": indy:crypto_sign(sig_data_bytes,my_did),
        "signer": my_vk,
        "sig_data": indy:sig_data(sig_data_bytes)


This indy library function is used in line 9 of the sign_field function, to convert the KRL byte array (produced in lines 4-5) into a Base64 encoded string, as required by the wire protocol. It is recommended to use the org.sovrin.agent_message ruleset rather than using this function directly.


This indy library function requires a byte array and our ECI. It produces the signature required in a signed field, based on the data to be signed and our private key (associated with our DID (aka ECI)). This signature is incorporated into the signed field in line 7 of the sign_field function. It is recommended to use the org.sovrin.agent_message ruleset rather than using this function directly.

Producing the data byte array

The first eight bytes of the data are reserved for a timestamp. We produce this as an eight byte (big-endian) number of seconds since the start of the current epoch. 

These bytes are produced by line 2 of the sign_field function. The toByteArray function this line uses is described in more detail in the page Looping using the reduce operator.

Copyright Picolabs | Licensed under Creative Commons.