github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/server/scripts/seed_sample_data.js (about) 1 /** 2 * Copyright 2017 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ---------------------------------------------------------------------------- 16 */ 17 'use strict' 18 19 const _ = require('lodash') 20 const request = require('request-promise-native') 21 const protos = require('../blockchain/protos') 22 const { 23 awaitServerPubkey, 24 getTxnCreator, 25 submitTxns, 26 encodeTimestampedPayload 27 } = require('../system/submit_utils') 28 29 const SERVER = process.env.SERVER || 'http://localhost:3000' 30 const DATA = process.env.DATA 31 if (DATA.indexOf('.json') === -1) { 32 throw new Error('Use the "DATA" environment variable to specify a JSON file') 33 } 34 35 const { records, agents } = require(`./${DATA}`) 36 let createTxn = null 37 38 const createProposal = (privateKey, action) => { 39 return createTxn(privateKey, encodeTimestampedPayload({ 40 action: protos.SCPayload.Action.CREATE_PROPOSAL, 41 createProposal: protos.CreateProposalAction.create(action) 42 })) 43 } 44 45 const answerProposal = (privateKey, action) => { 46 return createTxn(privateKey, encodeTimestampedPayload({ 47 action: protos.SCPayload.Action.ANSWER_PROPOSAL, 48 answerProposal: protos.AnswerProposalAction.create(action) 49 })) 50 } 51 52 protos.compile() 53 .then(awaitServerPubkey) 54 .then(batcherPublicKey => { 55 const txnCreators = {} 56 57 createTxn = (privateKey, payload) => { 58 if (!txnCreators[privateKey]) { 59 txnCreators[privateKey] = getTxnCreator(privateKey, batcherPublicKey) 60 } 61 return txnCreators[privateKey](payload) 62 } 63 }) 64 65 // Create Agents 66 .then(() => { 67 console.log('Creating Agents . . .') 68 const agentAdditions = agents.map(agent => { 69 return createTxn(agent.privateKey, encodeTimestampedPayload({ 70 action: protos.SCPayload.Action.CREATE_AGENT, 71 createAgent: protos.CreateAgentAction.create({ name: agent.name }) 72 })) 73 }) 74 75 return submitTxns(agentAdditions) 76 }) 77 78 // Create Users 79 .then(() => { 80 console.log('Creating Users . . .') 81 const userRequests = agents.map(agent => { 82 const user = _.omit(agent, 'name', 'privateKey', 'hashedPassword') 83 user.password = agent.hashedPassword 84 return request({ 85 method: 'POST', 86 url: `${SERVER}/users`, 87 headers: { 'Content-Type': 'application/json' }, 88 body: JSON.stringify(user) 89 }) 90 }) 91 92 return Promise.all(userRequests) 93 }) 94 95 // Create Records 96 .then(() => { 97 console.log('Creating Records . . .') 98 const recordAdditions = records.map(record => { 99 const properties = record.properties.map(property => { 100 if (property.dataType === protos.PropertySchema.DataType.LOCATION) { 101 property.locationValue = protos.Location.create(property.locationValue) 102 } 103 return protos.PropertyValue.create(property) 104 }) 105 106 return createTxn(agents[record.ownerIndex || 0].privateKey, encodeTimestampedPayload({ 107 action: protos.SCPayload.Action.CREATE_RECORD, 108 createRecord: protos.CreateRecordAction.create({ 109 recordId: record.recordId, 110 recordType: record.recordType, 111 properties 112 }) 113 })) 114 }) 115 116 return submitTxns(recordAdditions) 117 }) 118 119 // Transfer Custodianship 120 .then(() => { 121 console.log('Transferring Custodianship . . .') 122 const custodianProposals = records 123 .filter(record => record.custodianIndex !== undefined) 124 .map(record => { 125 return createProposal(agents[record.ownerIndex || 0].privateKey, { 126 recordId: record.recordId, 127 receivingAgent: agents[record.custodianIndex].publicKey, 128 role: protos.Proposal.Role.CUSTODIAN 129 }) 130 }) 131 132 return submitTxns(custodianProposals) 133 }) 134 .then(() => { 135 const custodianAnswers = records 136 .filter(record => record.custodianIndex !== undefined) 137 .map(record => { 138 return answerProposal(agents[record.custodianIndex].privateKey, { 139 recordId: record.recordId, 140 receivingAgent: agents[record.custodianIndex].publicKey, 141 role: protos.Proposal.Role.CUSTODIAN, 142 response: protos.AnswerProposalAction.Response.ACCEPT 143 }) 144 }) 145 146 return submitTxns(custodianAnswers) 147 }) 148 149 // Authorize New Reporters 150 .then(() => { 151 console.log('Authorizing New Reporters . . .') 152 const reporterProposals = records 153 .filter(record => record.reporterIndex !== undefined) 154 .map(record => { 155 return createProposal(agents[record.ownerIndex || 0].privateKey, { 156 recordId: record.recordId, 157 receivingAgent: agents[record.reporterIndex].publicKey, 158 role: protos.Proposal.Role.REPORTER, 159 properties: record.reportableProperties 160 }) 161 }) 162 163 return submitTxns(reporterProposals) 164 }) 165 .then(() => { 166 const reporterAnswers = records 167 .filter(record => record.reporterIndex !== undefined) 168 .map(record => { 169 return answerProposal(agents[record.reporterIndex].privateKey, { 170 recordId: record.recordId, 171 receivingAgent: agents[record.reporterIndex].publicKey, 172 role: protos.Proposal.Role.REPORTER, 173 response: protos.AnswerProposalAction.Response.ACCEPT 174 }) 175 }) 176 177 return submitTxns(reporterAnswers) 178 }) 179 .catch(err => { 180 console.error(err.toString()) 181 process.exit() 182 })