Created
May 19, 2019 12:05
-
-
Save S3bb1/9433b31e4c3c3dc874194f58c9328f81 to your computer and use it in GitHub Desktop.
Create a digital twin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// require blockchain-core dependencies | |
const IpfsApi = require('ipfs-api'); | |
const Web3 = require('web3'); | |
// require blockchain-core | |
const { Ipfs, createDefaultRuntime, PropertyType, ModificationType } = require('@evan.network/api-blockchain-core'); | |
// ipfs configuration for evan.network testnet storage | |
const ipfsConfig = {host: 'ipfs.test.evan.network', port: '443', protocol: 'https'}; | |
// web3 provider config (currently evan.network testcore) | |
const web3Provider = 'wss://testcore.evan.network/ws' | |
const runtimeConfig = { | |
// account map to blockchain accounts with their private key | |
accountMap: { | |
'0x4D15A20Fb7337776a8009e274C5fe9f2433E284B': | |
'820ac7018f3a352f18528026880de11894f807cb9b4ea3c838d780a123ba6a17', | |
}, | |
// key configuration for private data handling | |
keyConfig: { | |
'0x4D15A20Fb7337776a8009e274C5fe9f2433E284B': 'Test1234', | |
}, | |
// ipfs configuration for evan.network storage | |
ipfs: {host: 'ipfs.test.evan.network', port: '443', protocol: 'https'}, | |
// web3 provider config (currently evan.network testcore) | |
web3Provider: 'wss://testcore.evan.network/ws', | |
}; | |
async function init() { | |
// initialize dependencies | |
const provider = new Web3.providers.WebsocketProvider( | |
runtimeConfig.web3Provider, | |
{ clientConfig: { keepalive: true, keepaliveInterval: 5000 } }); | |
const web3 = new Web3(provider, { transactionConfirmationBlocks: 1 }); | |
const dfs = new Ipfs({ remoteNode: new IpfsApi(runtimeConfig.ipfs), }); | |
// create runtime | |
const runtime = await createDefaultRuntime(web3, dfs, { accountMap: runtimeConfig.accountMap, keyConfig: runtimeConfig.keyConfig }); | |
return runtime; | |
} | |
async function createTwin(runtime) { | |
const contract = await runtime.dataContract.create('testdatacontract', runtime.activeAccount); | |
console.log('contract created'); | |
await runtime.rightsAndRoles.setOperationPermission( | |
contract, // contract to be updated | |
runtime.activeAccount, // account, that can change permissions | |
0, // role id, uint8 value | |
'metadata', // name of the object | |
PropertyType.Entry, // what type of element is modified | |
ModificationType.Set, // type of the modification | |
true, // grant this capability | |
); | |
console.log('rights set for field metadata') | |
const metadata = { | |
"weight": "100t", | |
"height": "2,5m", | |
"type": "exvacator" | |
}; | |
await runtime.dataContract.setEntry(contract, 'metadata', metadata, runtime.activeAccount); | |
} | |
async function updateTwin(runtime, address) { | |
const contract = runtime.contractLoader.loadContract('DataContract', address); | |
await runtime.rightsAndRoles.setOperationPermission( | |
contract, // contract to be updated | |
runtime.activeAccount, // account, that can change permissions | |
0, // role id, uint8 value | |
'rentalCalender', // name of the object | |
PropertyType.ListEntry, // what type of element is modified | |
ModificationType.Set, // type of the modification | |
true, // grant this capability | |
); | |
const rentalEntry = { | |
from: Date.now(), | |
to: Date.now() + 100 * 60 * 60 * 24 | |
}; | |
await runtime.dataContract.addListEntries(contract, 'rentalCalender', [rentalEntry], runtime.activeAccount); | |
} | |
async function updateTwin2(runtime, address) { | |
const contract = runtime.contractLoader.loadContract('DataContract', address); | |
await runtime.rightsAndRoles.setOperationPermission( | |
contract, // contract to be updated | |
runtime.activeAccount, // account, that can change permissions | |
0, // role id, uint8 value | |
'workingHours', // name of the object | |
PropertyType.Entry, // what type of element is modified | |
ModificationType.Set, // type of the modification | |
true, // grant this capability | |
); | |
await runtime.dataContract.setEntry(contract, 'workingHours', '0x1', runtime.activeAccount, false, false, 'unencrypted'); | |
} | |
async function getValue(runtime, address) { | |
const contract = runtime.contractLoader.loadContract('DataContract', address); | |
const metadata = await runtime.dataContract.getEntry(contract, 'metadata', runtime.activeAccount) | |
console.dir(metadata) | |
} | |
init().then(async (runtime) => { | |
const twinAdress = await createTwin(runtime) | |
await updateTwin(runtime, twinAdress); | |
await updateTwin2(runtime, twinAdress) | |
await getValue(runtime, twinAdress); | |
console.log('Twin created ' + twinAdress); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment