github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/invoice_messaging.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package pingpong
    19  
    20  import (
    21  	"context"
    22  	"time"
    23  
    24  	"github.com/mysteriumnetwork/node/p2p"
    25  	"github.com/mysteriumnetwork/node/pb"
    26  	"github.com/mysteriumnetwork/payments/crypto"
    27  	"github.com/rs/zerolog/log"
    28  )
    29  
    30  // InvoiceRequest structure represents the invoice message that the provider sends to the consumer.
    31  type InvoiceRequest struct {
    32  	Invoice crypto.Invoice `json:"invoice"`
    33  }
    34  
    35  // InvoiceSender is responsible for sending the invoice messages.
    36  type InvoiceSender struct {
    37  	ch p2p.ChannelSender
    38  }
    39  
    40  // NewInvoiceSender returns a new instance of the invoice sender.
    41  func NewInvoiceSender(ch p2p.ChannelSender) *InvoiceSender {
    42  	return &InvoiceSender{
    43  		ch: ch,
    44  	}
    45  }
    46  
    47  // Send sends the given invoice.
    48  func (is *InvoiceSender) Send(invoice crypto.Invoice) error {
    49  	pInvoice := &pb.Invoice{
    50  		AgreementID:    invoice.AgreementID.Text(bigIntBase),
    51  		AgreementTotal: invoice.AgreementTotal.Text(bigIntBase),
    52  		TransactorFee:  invoice.TransactorFee.Text(bigIntBase),
    53  		Hashlock:       invoice.Hashlock,
    54  		Provider:       invoice.Provider,
    55  		ChainID:        invoice.ChainID,
    56  	}
    57  	log.Debug().Msgf("Sending P2P message to %q: %s", p2p.TopicPaymentInvoice, pInvoice.String())
    58  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    59  	defer cancel()
    60  	_, err := is.ch.Send(ctx, p2p.TopicPaymentInvoice, p2p.ProtoMessage(pInvoice))
    61  	return err
    62  }