func (handler *ConsensusHandler) doChainQuery(msg *pb.OpenchainMessage) error { var response *pb.Response tx := &pb.Transaction{} err := proto.Unmarshal(msg.Payload, tx) if err != nil { response = &pb.Response{Status: pb.Response_FAILURE, Msg: []byte(fmt.Sprintf("Error unmarshalling payload of received OpenchainMessage:%s.", msg.Type))} } else { // Verify transaction signature if security is enabled secHelper := handler.coordinator.GetSecHelper() if nil != secHelper { if logger.IsEnabledFor(logging.DEBUG) { logger.Debug("Verifying transaction signature %s", tx.Uuid) } if tx, err = secHelper.TransactionPreValidation(tx); nil != err { response = &pb.Response{Status: pb.Response_FAILURE, Msg: []byte(err.Error())} logger.Debug("Failed to verify transaction %v", err) } } // execute if response nil (ie, no error) if nil == response { result, err := chaincode.Execute(context.Background(), chaincode.GetChain(chaincode.DefaultChain), tx, secHelper) if err != nil { response = &pb.Response{Status: pb.Response_FAILURE, Msg: []byte(fmt.Sprintf("Error:%s", err))} } else { response = &pb.Response{Status: pb.Response_SUCCESS, Msg: result} } } } payload, _ := proto.Marshal(response) handler.SendMessage(&pb.OpenchainMessage{Type: pb.OpenchainMessage_RESPONSE, Payload: payload}) return nil }
func whoIsTheOwner(asset string) ([]byte, error) { chaincodeInput := &pb.ChaincodeInput{Function: "query", Args: []string{asset}} // Prepare spec and submit spec := &pb.ChaincodeSpec{ Type: 1, ChaincodeID: &pb.ChaincodeID{Name: "mycc"}, CtorMsg: chaincodeInput, ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC, } var ctx = context.Background() chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec} tid := chaincodeInvocationSpec.ChaincodeSpec.ChaincodeID.Name // Now create the Transactions message and send to Peer. transaction, err := administrator.NewChaincodeQuery(chaincodeInvocationSpec, tid) if err != nil { return nil, fmt.Errorf("Error deploying chaincode: %s ", err) } ledger, err := ledger.GetLedger() ledger.BeginTxBatch("1") result, err := chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction) if err != nil { return nil, fmt.Errorf("Error deploying chaincode: %s", err) } ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil) return result, err }
func transferOwnership(owner crypto.Client, ownerCert crypto.CertificateHandler, asset string, newOwnerCert crypto.CertificateHandler) error { // Get a transaction handler to be used to submit the execute transaction // and bind the chaincode access control logic using the binding submittingCertHandler, err := owner.GetTCertificateHandlerNext() if err != nil { return err } txHandler, err := submittingCertHandler.GetTransactionHandler() if err != nil { return err } binding, err := txHandler.GetBinding() if err != nil { return err } chaincodeInput := &pb.ChaincodeInput{Function: "transfer", Args: []string{asset, string(newOwnerCert.GetCertificate())}} chaincodeInputRaw, err := proto.Marshal(chaincodeInput) if err != nil { return err } // Access control. Owner signs chaincodeInputRaw || binding to confirm his identity sigma, err := ownerCert.Sign(append(chaincodeInputRaw, binding...)) if err != nil { return err } // Prepare spec and submit spec := &pb.ChaincodeSpec{ Type: 1, ChaincodeID: &pb.ChaincodeID{Name: "mycc"}, CtorMsg: chaincodeInput, Metadata: sigma, // Proof of identity ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC, } var ctx = context.Background() chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec} tid := chaincodeInvocationSpec.ChaincodeSpec.ChaincodeID.Name // Now create the Transactions message and send to Peer. transaction, err := txHandler.NewChaincodeExecute(chaincodeInvocationSpec, tid) if err != nil { return fmt.Errorf("Error deploying chaincode: %s ", err) } ledger, err := ledger.GetLedger() ledger.BeginTxBatch("1") _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction) if err != nil { return fmt.Errorf("Error deploying chaincode: %s", err) } ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil) return err }
func deploy(admCert crypto.CertificateHandler) error { // Prepare the spec. The metadata includes the identity of the administrator spec := &pb.ChaincodeSpec{ Type: 1, ChaincodeID: &pb.ChaincodeID{Name: "mycc"}, CtorMsg: &pb.ChaincodeInput{Function: "init", Args: []string{}}, Metadata: admCert.GetCertificate(), ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC, } // First build and get the deployment spec var ctx = context.Background() chaincodeDeploymentSpec, err := getDeploymentSpec(ctx, spec) if err != nil { return err } tid := chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name // Now create the Transactions message and send to Peer. transaction, err := administrator.NewChaincodeDeployTransaction(chaincodeDeploymentSpec, tid) if err != nil { return fmt.Errorf("Error deploying chaincode: %s ", err) } ledger, err := ledger.GetLedger() ledger.BeginTxBatch("1") _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction) if err != nil { return fmt.Errorf("Error deploying chaincode: %s", err) } ledger.CommitTxBatch("1", []*pb.Transaction{transaction}, nil, nil) return err }
// DeployLocal deploys the supplied chaincode image to the local peer func DeployLocal(ctx context.Context, spec *protos.ChaincodeSpec, secCxt crypto.Peer) (*protos.Transaction, []byte, error) { // First build and get the deployment spec chaincodeDeploymentSpec, err := BuildLocal(ctx, spec) if err != nil { genesisLogger.Error(fmt.Sprintf("Error deploying chaincode spec: %v\n\n error: %s", spec, err)) return nil, nil, err } transaction, err := protos.NewChaincodeDeployTransaction(chaincodeDeploymentSpec, chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name) if err != nil { return nil, nil, fmt.Errorf("Error deploying chaincode: %s ", err) } //chaincode.NewChaincodeSupport(chaincode.DefaultChain, peer.GetPeerEndpoint, false, 120000) result, err := chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction, secCxt) return transaction, result, err }
// DeployLocal deploys the supplied chaincode image to the local peer func DeployLocal(ctx context.Context, spec *protos.ChaincodeSpec) (*protos.Transaction, []byte, error) { // First build and get the deployment spec chaincodeDeploymentSpec, err := BuildLocal(ctx, spec) if err != nil { genesisLogger.Error(fmt.Sprintf("Error deploying chaincode spec: %v\n\n error: %s", spec, err)) return nil, nil, err } transaction, err := protos.NewChaincodeDeployTransaction(chaincodeDeploymentSpec, chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name) if err != nil { return nil, nil, fmt.Errorf("Error deploying chaincode: %s ", err) } //chaincode.NewChaincodeSupport(chaincode.DefaultChain, peer.GetPeerEndpoint, false, 120000) // The secHelper is set during creat ChaincodeSupport, so we don't need this step //ctx = context.WithValue(ctx, "security", secCxt) result, err := chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction) return transaction, result, err }