Commit b51cef87 authored by Krystian Gaus's avatar Krystian Gaus

trackntrace model added

parents
/deployment/admin@trackntrace-network.card
/deployment/REST_SERVER_PID
\ No newline at end of file
# Hyperledger trackntrace-network
\ No newline at end of file
prerequisites:
- installed 'jq'. this can be installed in a terminal via 'sudo apt install jq'
\ No newline at end of file
#!/bin/bash
echo "Start creating new trackntrace-network.bna..."
composer archive create -a ../trackntrace-network.bna -t dir -n ../
\ No newline at end of file
#!/bin/bash
NEW_REST_SERVER_PID=$(ps -o pid= | head -n 1)
installedNetworkVersion=$(composer network ping -c admin@trackntrace-network | head -n 2 | tail -1 | cut -d':' -f2 | tr -d ' ')
version=$(cat ../package.json | jq .version | tr -d \")
echo "Installed Business Network version:" $installedNetworkVersion
echo "Latest found Business Network version:" $version
if [ $installedNetworkVersion == 'Commandfailed' ]
then
# The Business Network must be installed and started for the first time
composer network install -a ../trackntrace-network.bna -c PeerAdmin@hlfv1
composer network start -n trackntrace-network -V $version -c PeerAdmin@hlfv1 -A admin -S adminpw
else
if [ $installedNetworkVersion == $version ]
then
echo "Skipping installation and start/upgrade of Business Network because latest version is already installed."
else
# The installedNetworkVersion and the latest found version differ, thus the Business Network has to be upgraded
composer network install -a ../trackntrace-network.bna -c PeerAdmin@hlfv1
composer network upgrade -c PeerAdmin@hlfv1 -n trackntrace-network -V $version
fi
echo "Killing process of running Composer REST Server..."
kill -9 $(head -1 REST_SERVER_PID)
fi
echo "Writing new process number" $NEW_REST_SERVER_PID "of terminal to REST_SERVER_PID file..."
echo $NEW_REST_SERVER_PID > REST_SERVER_PID
echo "Process number saved successfully."
echo "Starting Composer REST Server..."
composer-rest-server -c admin@trackntrace-network -n required -w true
#use the following line if a api key is needed
#composer-rest-server -c admin@trackntrace-network -n required -y u4nyxpcrc3frPjszkjgxunu3wqmiBiyz -w true
\ No newline at end of file
/**
* createDelivery function.
* @param {org.trackntrace.models.assets.delivery.createDelivery} tx - The createDelivery transaction instance.
* @transaction
*/
async function createDelivery(tx)
{
const deliveryRegistry = await getAssetRegistry(fqtDelivery);
let location = await getLocation(tx);
let delivery = factory.newResource(nsDelivery, tDelivery, tx.shippingId);
if(tx.statusMessage != undefined)
{
delivery.statusMessage = tx.statusMessage;
}
if(tx.sender != undefined)
{
delivery.sender = await getPerson(tx.sender);
}
if(tx.recipient != undefined)
{
delivery.recipient = await getPerson(tx.recipient);
}
if (tx.product != undefined)
{
delivery.product = tx.product;
}
if(tx.unit != undefined)
{
delivery.unit = tx.unit;
}
if(tx.quantity != undefined)
{
delivery.quantity = tx.quantity;
}
delivery.locations = location == null ? [] : [location];
await deliveryRegistry.add(delivery);
}
/**
* updateDelivery transaction processor function.
* @param {org.trackntrace.models.assets.delivery.updateDelivery} tx - The updateDelivery transaction instance.
* @transaction
*/
async function updateDelivery(tx)
{
const deliveryRegistry = await getAssetRegistry(fqtDelivery);
let location = await getLocation(tx);
let delivery = await deliveryRegistry.get(tx.shippingId);
if(tx.statusMessage != undefined)
{
delivery.statusMessage = tx.statusMessage;
}
if(delivery.sender == undefined && tx.sender != undefined)
{
delivery.sender = await getPerson(tx.sender);
}
if(delivery.recipient == undefined && tx.recipient != undefined)
{
delivery.recipient = await getPerson(tx.recipient);
}
if (delivery.product == undefined && tx.product != undefined)
{
delivery.product = tx.product;
}
if(delivery.unit == undefined && tx.unit != undefined)
{
delivery.unit = tx.unit;
}
if(delivery.quantity == undefined && tx.quantity != undefined)
{
delivery.quantity = tx.quantity;
}
if(location != null)
{
delivery.locations.push(location);
}
await deliveryRegistry.update(delivery);
}
\ No newline at end of file
/**
* getHash function.
*
* The returned value is always an integer casted to string.
* Probably there are better hash functions, but for short term use it's okay,
* source: https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript
*
* @param {string} str - The string to hash
*/
function getHash(str)
{
let hash = 0, i, chr, len;
if(str.length > 0)
{
for (i = 0, len = str.length; i < len; i++)
{
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
}
return String(hash);
}
/**
* getLocation function.
* @param {org.trackntrace.models.assets.delivery.createDelivery} tx - The createDelivery transaction instance.
*/
async function getLocation(tx)
{
if(tx.latitude != undefined && tx.longitude != undefined)
{
const id = getHash(String(tx.latitude) + String(tx.longitude) + String(Date.now()));
return getAssetRegistry(fqtLocation)
.then(function(locationRegistry)
{
let location = factory.newResource(nsLocation, tLocation, id)
location.latitude = tx.latitude;
location.longitude = tx.longitude;
locationRegistry.add(location)
.catch(function(error)
{
// location already exists (because of the used timestamp this should not happen!)
});
})
.then(function()
{
return factory.newRelationship(nsLocation, tLocation, id);
});
}
else
{
return null;
}
}
\ No newline at end of file
const factory = getFactory();
const nsDelivery = 'org.trackntrace.models.assets.delivery';
const nsLocation = 'org.trackntrace.models.assets.location';
const nsPerson = 'org.trackntrace.models.participants.person';
const tDelivery = 'Delivery';
const tLocation = 'Location';
const tPerson = 'Person';
const fqtDelivery = nsDelivery + '.' + tDelivery;
const fqtLocation = nsLocation + '.' + tLocation;
const fqtPerson = nsPerson + '.' + tPerson;
\ No newline at end of file
/**
* createPerson function.
* @param {org.trackntrace.models.participants.person.createPerson} tx - The createPerson transaction instance.
* @transaction
*/
async function createPerson(tx)
{
const personRegistry = await getParticipantRegistry(fqtPerson);
let person = factory.newResource(nsPerson, tPerson, tx.name);
person.name = tx.name;
await personRegistry.add(person);
}
/**
* getPerson function.
* @param {string} name - The name of the person.
*/
async function getPerson(name)
{
const personRegistry = await getParticipantRegistry(fqtPerson);
let person = await personRegistry.get(name);
return person;
}
\ No newline at end of file
namespace org.trackntrace.models.assets.delivery
import org.trackntrace.models.assets.location.Location
import org.trackntrace.models.participants.person.Person
asset Delivery identified by shippingId
{
o String shippingId
o String statusMessage optional
--> Person sender optional
--> Person recipient optional
o String product optional
o String unit optional
o Double quantity optional
--> Location[] locations
}
transaction createDelivery
{
o String shippingId
o String statusMessage optional
o String sender optional
o String recipient optional
o String product optional
o String unit optional
o Double quantity optional
o Double latitude optional
o Double longitude optional
}
transaction updateDelivery
{
o String shippingId
o String statusMessage optional
o String sender optional
o String recipient optional
o String product optional
o String unit optional
o Double quantity optional
o Double latitude optional
o Double longitude optional
}
\ No newline at end of file
namespace org.trackntrace.models.assets.location
asset Location identified by id
{
o String id
o Double latitude
o Double longitude
}
namespace org.trackntrace.models.participants.person
participant Person identified by name
{
o String name
}
transaction createPerson
{
o String name
}
\ No newline at end of file
{"name":"trackntrace-network","author":"author","description":"Start from scratch with a blank business network","version":"0.0.112","devDependencies":{"browserfs":"^1.2.0","chai":"^3.5.0","composer-admin":"latest","composer-cli":"latest","composer-client":"latest","composer-connector-embedded":"latest","eslint":"^3.6.1","istanbul":"^0.4.5","jsdoc":"^3.4.1","mkdirp":"^0.5.1","mocha":"^3.2.0","moment":"^2.19.3"},"keywords":[],"license":"Apache 2.0","repository":{"type":"e.g. git","url":"URL"},"scripts":{"deploy":"./scripts/deploy.sh","doc":"jsdoc --pedantic --recurse -c jsdoc.conf","lint":"eslint .","postlicchk":"npm run doc","postlint":"npm run licchk","prepublish":"mkdirp ./dist && composer archive create --sourceType dir --sourceName . -a ./dist/unnamed-network.bna","pretest":"npm run lint","test":"mocha --recursive"}}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Access control rules for trackntrace-network
*/
rule Default
{
description: "Allow all participants access to all resources"
participant: "ANY"
operation: ALL
resource: "org.trackntrace.models.participants.person.*"
action: ALLOW
}
rule SystemACL
{
description: "System ACL to permit all access"
participant: "ANY"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
rule NetworkAdminUser
{
description: "Grant business network administrators full access to user resources"
participant: "org.hyperledger.composer.system.NetworkAdmin"
operation: ALL
resource: "**"
action: ALLOW
}
rule NetworkAdminSystem
{
description: "Grant business network administrators full access to system resources"
participant: "org.hyperledger.composer.system.NetworkAdmin"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment