Guest User

Untitled

a guest
Apr 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. pragma solidity ^0.4.0;
  2.  
  3. import "./strings.sol";
  4. import "./safemath.sol";
  5. contract PublishService
  6. {
  7.  
  8. using strings for *;
  9. using SafeMath for uint;
  10. struct ServiceListStruct {
  11. string cloudID;
  12. address gatewayID;
  13. string serviceProducerID;
  14. mapping (string => string) serviceProducerMetadata;
  15. string serviceConsumerMetaAttr;
  16. string accessControlModel;
  17. }
  18. ServiceListStruct[] public BCServiceList;
  19.  
  20. uint BCServiceListIndex;
  21.  
  22. function PublishService()
  23. {
  24. BCServiceListIndex = 0;
  25. }
  26.  
  27. modifier onlyServiceProducerOwner(address _gatewayID) {
  28. require(msg.sender == _gatewayID);
  29. _;
  30. }
  31.  
  32. ServiceListStruct sls;
  33. uint public maxParams = 0;
  34.  
  35.  
  36. function addEntry(string _cloudID, address _gatewayID, string _serviceProducerID, string _serviceProducerMetadata, string _serviceConsumerMetaAttr, string _accessControlModel) public onlyServiceProducerOwner(_gatewayID) returns (uint)
  37. {
  38. sls.cloudID = _cloudID;
  39. sls.gatewayID = _gatewayID;
  40. sls.serviceProducerID = _serviceProducerID;
  41. sls.serviceConsumerMetaAttr = _serviceConsumerMetaAttr;
  42. sls.accessControlModel = _accessControlModel;
  43. BCServiceList.push(sls);
  44. //
  45. string memory s1;
  46. string memory s2 = _serviceProducerMetadata;
  47. string memory s3;
  48. bytes memory s2bytes = bytes(_serviceProducerMetadata);
  49. uint paramCount = 0;
  50. while(s2bytes.length != 0)
  51. {
  52. (s1,s2) = splitString(s2,";");
  53. (s1,s3) = splitString(s1,":");
  54. BCServiceList[BCServiceListIndex].serviceProducerMetadata[s1] =s3;
  55. paramCount = paramCount.add(1);
  56. s2bytes = bytes(s2);
  57. }
  58. if(maxParams < paramCount)
  59. {
  60. maxParams = paramCount;
  61. }
  62. BCServiceListIndex = BCServiceListIndex.add(1);
  63. return 1;
  64. }
  65.  
  66. function deleteEntry(string _cloudID, address _gatewayID, string _serviceProducerID) public onlyServiceProducerOwner(_gatewayID) returns (uint)
  67. {
  68. require(msg.sender==_gatewayID);
  69. int pos = -1;
  70. for(uint index = 0; index< BCServiceList.length; index++)
  71. {
  72. if(compareStringsbyBytes(_cloudID, BCServiceList[index].cloudID))
  73. {
  74. if(_gatewayID == BCServiceList[index].gatewayID)
  75. {
  76. if(compareStringsbyBytes(_serviceProducerID, BCServiceList[index].serviceProducerID))
  77. {
  78. pos = int(index);
  79. }
  80. }
  81. }
  82. }
  83. if(pos > -1)
  84. {
  85. BCServiceList[index] = BCServiceList[BCServiceList.length -1];
  86. delete BCServiceList[BCServiceList.length-1];
  87. BCServiceList.length--;
  88. return 1;
  89. }
  90. else
  91. return 0;
  92. }
  93.  
  94. function compareStringsbyBytes(string s1, string s2) internal pure returns(bool)
  95. {
  96. bytes memory s1bytes = bytes(s1);
  97. bytes memory s2bytes = bytes(s2);
  98. if(s1bytes.length!=s2bytes.length) {
  99. return false;
  100. }
  101. else{
  102. for(uint i = 0;i<s1bytes.length;i++)
  103. {
  104. if(s1bytes[i] != s2bytes[i])
  105. return false;
  106. }
  107. return true;
  108. }
  109. }
  110.  
  111. function splitString(string _s, string _seperator) internal returns(string, string)
  112. {
  113. var s_slice = _s.toSlice();
  114. var seperator_slice = _seperator.toSlice();
  115. string memory result = "";
  116. var result_slice = result.toSlice();
  117. result_slice = s_slice.split(seperator_slice);
  118. return (result_slice.toString(), s_slice.toString());
  119. }
  120. }
  121.  
  122. var Web3 = require('web3');
  123. var web3Provider = null;
  124. var PublishService;
  125. var contract = require('./PublishService_abi.js');
  126.  
  127. function init() {
  128. //initializing web3 to access blockchain
  129. initweb3();
  130. }
  131.  
  132. function initweb3() {
  133. //To make sure not to overwrite the already set provider when in mist, check first if the web3 is available
  134. if (typeof web3 !== 'undefined') {
  135. web3 = new Web3(web3.currentProvider);
  136. } else {
  137. // create an instance of web3 using the HTTP provider
  138. web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
  139. }
  140. web3.eth.defaultAccount = web3.eth.accounts[1];
  141. var PublishServiceContractAddress = "0x6c68d153b9709283e3900e944f1c6677273987c5";
  142. var PublishServiceContract = new web3.eth.Contract(contract,PublishServiceContractAddress );
  143. PublishService.addEntry("LC1",web3.eth.defaultAccount,"SP1","location:inside;reading:degree","scattr","ngac");
  144. }
  145.  
  146. init();
  147.  
  148. TypeError: PublishServiceContract.addEntry is not a function
  149.  
  150. var Web3 = require('web3');
  151. var web3Provider = null;
  152. var contract = require('./PublishService_abi.js');
  153. var PublishServiceContract;
  154.  
  155. function init() {
  156. //initializing web3 to access blockchain
  157. initweb3();
  158. }
  159.  
  160. function initweb3() {
  161. //To make sure not to overwrite the already set provider when in mist, check first if the web3 is available
  162. if (typeof web3 !== 'undefined') {
  163. web3 = new Web3(web3.currentProvider);
  164. } else {
  165. // create an instance of web3 using the HTTP provider
  166. web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
  167. }
  168. web3.eth.defaultAccount = web3.eth.accounts[1];
  169. var PublishServiceContractAddress = "0x6c68d153b9709283e3900e944f1c6677273987c5";
  170. PublishServiceContract = new web3.eth.Contract(contract,PublishServiceContractAddress);
  171. addServiceProducer("LC1","SP1","location:inside;reading:degree","scattr","ngac");
  172. }
  173.  
  174. function addServiceProducer(s1,s2,s3,s4,s5) {
  175. var psinstance;
  176. PublishServiceContract.deployed().then(function(instance) {
  177. psInstance = instance;
  178. psinstance.addEntry(s1,web3.eth.defaultAccount,s2,s3,s4,s5);
  179. }).then(function(print) {
  180. console.log("sp details added successfully")
  181. }).catch(function(err) {
  182. console.log(err.message);
  183. });
  184. }
  185.  
  186. init();
  187.  
  188. initContract: function() {
  189. $.getJSON('Adoption.json', function(data) {
  190. // Get the necessary contract artifact file and instantiate it with truffle-contract
  191. var AdoptionArtifact = data;
  192. App.contracts.Adoption = TruffleContract(AdoptionArtifact);
  193.  
  194. // Set the provider for our contract
  195. App.contracts.Adoption.setProvider(App.web3Provider);
  196.  
  197. // Use our contract to retrieve and mark the adopted pets
  198. return App.markAdopted();
  199. });
  200.  
  201. return App.bindEvents();
  202. },
Add Comment
Please, Sign In to add comment