Hyperledger Composer
Introduction to Hyperledger Composer
Updated: 03 September 2023
Overview
A business Network, defined in a Business Network Archive .bna
File
- Models
- Logic
- Queries
- Access Control
In order to update a network, we simply upload the Archive .bna
file to the network, this can either be done from the command line or from the Blockchain UI if using IBM Cloud
Models
Models are files that define Assets, Participants and Transactions using the Hyperledger Composer Modelling Language
Models are defined in .cto
files, and are written using the Hyperledger Composer Modelling Language
CTO Language
A .cto
file is composed of the following elements
- A single namespace, all resource definitions are part of this namespace
- A set of resource definitions
- Assets
- Transactions
- Participants
- Events
- Optional import declarations than import resources from other namespaces
Namespaces
Resources are organized by namespaces, this is defined by first line in a .cto
file which can be as follows
All other resources defined in the same file will be part of this namespace
Classes
A resource definition has the following properties
- A namespace (defined by the namespace of its parent file)
- A name and identifying field
- Optional supertype
- Any additional properties
- An optional Abstract declaration to state that the type cannot be created but can only be extended
- A set of relationships to other Composer types
Enums
Enumerables can be defined as follows
And can be used in another class as a type
Concepts
Concepts are abstract classes that are not assets, participants or transactions. They are contained by another resource and do not have an identifier and cannot be directly stored in registries or referenced in relationships
For example we can define a concept as follows
And then use it in a class definition
Primitive Types
Composer has a few primitive types, namely
- String
- Double
- Integer
- Long
- DateTime
- Boolean
Arrays
Arrays can simply be defined with []
Relationships
A relationship is a tuple composed of
- Namespace being referenced
- Type being referenced
- Identifier instance being referenced
For example
Field Validation
Attributes may include a default value, string fields may include a regex validation, numerical values may include a range, these can be seen below
Imports
We can also import a type from a different namespace with the following
Example
We can define a trading network consisting of the following models
- Asset
- Name: Commodity
- ID: Trading Symbol
- Attributes
- Trading Symbol
- Description
- Main Exchange
- Quantity
- Owner (Trader Participant)
- Participant
- Name: Trader
- ID: TradeID
- First Name
- Last Name
- Transaction
- Name: Trade
- Commodity (Commodity Asset)
- New Owner (Trader Participant)
The model file for the above network can be defined as follows
models.cto
Logic
Logic is defined in Script Files .js
which define transaction logic
Example
The logic for a transaction can be defined by a javascript function, in this example, for example, if a transaction occurs in which a Commodity changes Ownership from one Owner to a New Owner, the function can be defined as follows
trade.js
Queries
Queries are defined in Query File .qry
file, note that a single .bna
file can only have one query
Access Control
Access Control Files .acl
define what permissions different users have, a single network can only have one access control file
Example
Access control files look like the following
Deployment
We can package our code into a .bna
file by running the following command in our directory
We then make use of a PeerAdmin
identity card that contains the necessary credential and use this to install the .bna
to the network