Skip to content
Snippets Groups Projects
  • Luca Moser's avatar
    08b51ce5
    Refactor message layer packages (#331) · 08b51ce5
    Luca Moser authored
    * replace 'transaction' with 'message' in message layer pkg
    
    * remove more occurrences of 'transaction'
    
    * fixes ReferencedMessageId() returning approverMessageId
    
    * renames ApprovingMessageId to ApproverMessageId
    
    * use ApproverMessageId in deleteFureCone()
    
    * refactors message pkg
    
    * renames payloadId in Message to contentId
    
    * finalize comment on what a Message is
    
    * refactors additional message sub pkgs
    
    * renames 'transaction' to 'message' in gossip plugin
    
    * removes occurrences of 'transaction' in graph plugin
    
    * removes occurrences of 'transaction' in metrics plugin
    
    * refactors dashboard plugin for messages
    
    * removes further occurrences where tx is used instead of msg
    
    * fixes TestMessageRequest in manager_test.go
    
    * go fmt explorer_routes.go
    
    * fix review comments
    
    * fix Errorf order
    
    * throw around bones
    Refactor message layer packages (#331)
    Luca Moser authored
    * replace 'transaction' with 'message' in message layer pkg
    
    * remove more occurrences of 'transaction'
    
    * fixes ReferencedMessageId() returning approverMessageId
    
    * renames ApprovingMessageId to ApproverMessageId
    
    * use ApproverMessageId in deleteFureCone()
    
    * refactors message pkg
    
    * renames payloadId in Message to contentId
    
    * finalize comment on what a Message is
    
    * refactors additional message sub pkgs
    
    * renames 'transaction' to 'message' in gossip plugin
    
    * removes occurrences of 'transaction' in graph plugin
    
    * removes occurrences of 'transaction' in metrics plugin
    
    * refactors dashboard plugin for messages
    
    * removes further occurrences where tx is used instead of msg
    
    * fixes TestMessageRequest in manager_test.go
    
    * go fmt explorer_routes.go
    
    * fix review comments
    
    * fix Errorf order
    
    * throw around bones
plugin.go 2.46 KiB
package getMessageByHash

import (
	"net/http"

	"github.com/iotaledger/goshimmer/packages/binary/messagelayer/message"
	"github.com/iotaledger/goshimmer/plugins/messagelayer"
	"github.com/labstack/echo"

	"github.com/iotaledger/goshimmer/plugins/webapi"

	"github.com/iotaledger/hive.go/logger"
	"github.com/iotaledger/hive.go/node"
)

var PLUGIN = node.NewPlugin("WebAPI getMessageByHash Endpoint", node.Enabled, configure)
var log *logger.Logger

func configure(plugin *node.Plugin) {
	log = logger.NewLogger("API-getMessageByHash")
	webapi.Server.POST("getMessageByHash", getMessageByHash)
}

// getMessageByHash returns the array of messages for the
// given message hashes (in the same order as the parameters).
// If a node doesn't have the message for a given message hash in its ledger,
// the value at the index of that message hash is empty.
func getMessageByHash(c echo.Context) error {
	var request Request
	if err := c.Bind(&request); err != nil {
		log.Info(err.Error())
		return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
	}

	var result []Message
	for _, hash := range request.Hashes {
		log.Info("Received:", hash)

		msgId, err := message.NewId(hash)
		if err != nil {
			log.Info(err)
			continue
		}

		msgObject := messagelayer.Tangle.Message(msgId)
		if !msgObject.Exists() {
			continue
		}

		msg := msgObject.Unwrap()
		msgResp := Message{
			MessageId:       msg.Id().String(),
			TrunkMessageId:  msg.TrunkId().String(),
			BranchMessageId: msg.BranchId().String(),
			IssuerPublicKey: msg.IssuerPublicKey().String(),
			IssuingTime:     msg.IssuingTime().String(),
			SequenceNumber:  msg.SequenceNumber(),
			Payload:         msg.Payload().String(),
			Signature:       msg.Signature().String(),
		}
		result = append(result, msgResp)
		msgObject.Release()
	}

	return c.JSON(http.StatusOK, Response{Messages: result})
}

type Response struct {
	Messages []Message `json:"messages,omitempty"`
	Error    string    `json:"error,omitempty"`
}

type Request struct {
	Hashes []string `json:"hashes"`
}

type Message struct {
	MessageId       string `json:"messageId,omitempty"`
	TrunkMessageId  string `json:"trunkMessageId,omitempty"`
	BranchMessageId string `json:"branchMessageId,omitempty"`
	IssuerPublicKey string `json:"issuerPublicKey,omitempty"`
	IssuingTime     string `json:"issuingTime,omitempty"`
	SequenceNumber  uint64 `json:"sequenceNumber,omitempty"`
	Payload         string `json:"payload,omitempty"`
	Signature       string `json:"signature,omitempty"`
}