proc/worker/agent.go
func (w *Worker) AgentPreEndpointMsgCreate(job *data.Job) error {
	logger := w.logger.Add("method", "AgentPreEndpointMsgCreate", "job", job)

	channel, err := w.relatedChannel(logger, job, data.JobAgentPreEndpointMsgCreate)
	if err != nil {
		return err
	}

	logger = logger.Add("channel", channel)

	msg, err := w.ept.EndpointMessage(channel.ID)
	if err != nil {
		logger.Error(err.Error())
		return ErrMakeEndpointMsg
	}

	logger = logger.Add("endpointMsg", msg)

	template, err := w.templateByHash(logger, msg.TemplateHash)
	if err != nil {
		return err
	}

	logger = logger.Add("template", template)

	msgBytes, err := json.Marshal(msg)
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	client, err := w.user(logger, channel.Client)
	if err != nil {
		return err
	}

	logger = logger.Add("client", client)

	clientPub, err := data.ToBytes(client.PublicKey)
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	agent, err := w.account(logger, channel.Agent)
	if err != nil {
		return err
	}

	logger = logger.Add("agent", agent)

	agentKey, err := w.key(logger, agent.PrivateKey)
	if err != nil {
		return err
	}

	msgSealed, err := messages.AgentSeal(msgBytes, clientPub, agentKey)
	if err != nil {
		logger.Error(err.Error())
		return ErrEndpointMsgSeal
	}

	hash := crypto.Keccak256(msgSealed)

	var params []byte

	params, err = json.Marshal(msg.AdditionalParams)
	if err != nil {
		params = []byte("{}")
	}

	newEndpoint := &data.Endpoint{
		ID:                     util.NewUUID(),
		Template:               template.ID,
		Channel:                channel.ID,
		Hash:                   data.HexFromBytes(hash),
		RawMsg:                 data.FromBytes(msgSealed),
		ServiceEndpointAddress: pointer.ToString(msg.ServiceEndpointAddress),
		PaymentReceiverAddress: pointer.ToString(msg.PaymentReceiverAddress),
		Username:               pointer.ToString(msg.Username),
		Password:               pointer.ToString(msg.Password),
		AdditionalParams:       params,
	}

	tx, err := w.db.Begin()
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	if err = tx.Insert(newEndpoint); err != nil {
		logger.Error(err.Error())
		tx.Rollback()
		return err
	}

	salt, err := rand.Int(rand.Reader, big.NewInt(9*1e18))
	if err != nil {
		return err
	}

	passwordHash, err := data.HashPassword(msg.Password, string(salt.Uint64()))
	if err != nil {
		logger.Error(err.Error())
		return ErrGeneratePasswordHash
	}

	offering, err := w.offering(logger, channel.Offering)
	if err != nil {
		return err
	}

	channel.Password = passwordHash
	channel.Salt = salt.Uint64()

	if offering.BillingType == data.BillingPrepaid ||
		offering.SetupPrice > 0 {
		channel.ServiceStatus = data.ServiceSuspended

	} else {
		channel.ServiceStatus = data.ServiceActive
	}
	changedTime := time.Now().Add(time.Minute)
	channel.ServiceChangedTime = &changedTime

	if err = tx.Update(channel); err != nil {
		logger.Error(err.Error())
		tx.Rollback()
		return ErrInternal
	}

	if err = tx.Commit(); err != nil {
		logger.Error(err.Error())
		tx.Rollback()
		return ErrInternal
	}

	return nil
}
func (w *Worker) AgentPreEndpointMsgCreate(job *data.Job) error {
	logger := w.logger.Add("method", "AgentPreEndpointMsgCreate", "job", job)

	channel, err := w.relatedChannel(logger, job, data.JobAgentPreEndpointMsgCreate)
	if err != nil {
		return err
	}

	logger = logger.Add("channel", channel)

	msg, err := w.ept.EndpointMessage(channel.ID)
	if err != nil {
		logger.Error(err.Error())
		return ErrMakeEndpointMsg
	}

	logger = logger.Add("endpointMsg", msg)

	template, err := w.templateByHash(logger, msg.TemplateHash)
	if err != nil {
		return err
	}

	logger = logger.Add("template", template)

	msgBytes, err := json.Marshal(msg)
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	client, err := w.user(logger, channel.Client)
	if err != nil {
		return err
	}

	logger = logger.Add("client", client)

	clientPub, err := data.ToBytes(client.PublicKey)
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	agent, err := w.account(logger, channel.Agent)
	if err != nil {
		return err
	}

	logger = logger.Add("agent", agent)

	agentKey, err := w.key(logger, agent.PrivateKey)
	if err != nil {
		return err
	}

	msgSealed, err := messages.AgentSeal(msgBytes, clientPub, agentKey)
	if err != nil {
		logger.Error(err.Error())
		return ErrEndpointMsgSeal
	}

	hash := crypto.Keccak256(msgSealed)

	var params []byte

	params, err = json.Marshal(msg.AdditionalParams)
	if err != nil {
		params = []byte("{}")
	}

	newEndpoint := &data.Endpoint{
		ID:                     util.NewUUID(),
		Template:               template.ID,
		Channel:                channel.ID,
		Hash:                   data.HexFromBytes(hash),
		RawMsg:                 data.FromBytes(msgSealed),
		ServiceEndpointAddress: pointer.ToString(msg.ServiceEndpointAddress),
		PaymentReceiverAddress: pointer.ToString(msg.PaymentReceiverAddress),
		Username:               pointer.ToString(msg.Username),
		Password:               pointer.ToString(msg.Password),
		AdditionalParams:       params,
	}

	tx, err := w.db.Begin()
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	if err = tx.Insert(newEndpoint); err != nil {
		logger.Error(err.Error())
		tx.Rollback()
		return err
	}

	salt, err := rand.Int(rand.Reader, big.NewInt(9*1e18))
	if err != nil {
		return err
	}

	passwordHash, err := data.HashPassword(msg.Password, string(salt.Uint64()))
	if err != nil {
		logger.Error(err.Error())
		return ErrGeneratePasswordHash
	}

	offering, err := w.offering(logger, channel.Offering)
	if err != nil {
		return err
	}

	channel.Password = passwordHash
	channel.Salt = salt.Uint64()

	if offering.BillingType == data.BillingPrepaid ||
		offering.SetupPrice > 0 {
		channel.ServiceStatus = data.ServiceSuspended

	} else {
		channel.ServiceStatus = data.ServiceActive
	}
	changedTime := time.Now().Add(time.Minute)
	channel.ServiceChangedTime = &changedTime

	if err = tx.Update(channel); err != nil {
		logger.Error(err.Error())
		tx.Rollback()
		return ErrInternal
	}

	if err = tx.Commit(); err != nil {
		logger.Error(err.Error())
		tx.Rollback()
		return ErrInternal
	}

	return nil
}
// AgentPreEndpointMsgCreate prepares endpoint message.