-
Notifications
You must be signed in to change notification settings - Fork 136
/
transaction.go
100 lines (83 loc) · 2.67 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package http
import (
"encoding/hex"
"net/http"
"github.com/gorilla/mux"
"github.com/pactus-project/pactus/types/amount"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
)
func (s *Server) GetTransactionHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if s.enableAuth {
user, password, ok := r.BasicAuth()
if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
ctx = s.basicAuth(ctx, user, password)
}
vars := mux.Vars(r)
id, err := hex.DecodeString(vars["id"])
if err != nil {
s.writeError(w, err)
return
}
res, err := s.transaction.GetTransaction(ctx,
&pactus.GetTransactionRequest{
Id: id,
Verbosity: pactus.TransactionVerbosity_TRANSACTION_INFO,
},
)
if err != nil {
s.writeError(w, err)
return
}
tm := newTableMaker()
txToTable(res.Transaction, tm)
s.writeHTML(w, tm.html())
}
func txToTable(trx *pactus.TransactionInfo, tm *tableMaker) {
if trx == nil {
return
}
tm.addRowTxID("ID", trx.Id)
tm.addRowBytes("Data", trx.Data)
tm.addRowInt("Version", int(trx.Version))
tm.addRowInt("LockTime", int(trx.LockTime))
tm.addRowAmount("Fee", amount.Amount(trx.Fee))
tm.addRowString("Memo", trx.Memo)
tm.addRowString("Payload type", trx.PayloadType.String())
switch trx.PayloadType {
case pactus.PayloadType_TRANSFER_PAYLOAD:
pld := trx.Payload.(*pactus.TransactionInfo_Transfer).Transfer
tm.addRowAccAddress("Sender", pld.Sender)
tm.addRowAccAddress("Receiver", pld.Receiver)
tm.addRowAmount("Amount", amount.Amount(pld.Amount))
case pactus.PayloadType_BOND_PAYLOAD:
pld := trx.Payload.(*pactus.TransactionInfo_Bond).Bond
tm.addRowAccAddress("Sender", pld.Sender)
tm.addRowValAddress("Receiver", pld.Receiver)
tm.addRowAmount("Stake", amount.Amount(pld.Stake))
case pactus.PayloadType_SORTITION_PAYLOAD:
pld := trx.Payload.(*pactus.TransactionInfo_Sortition).Sortition
tm.addRowValAddress("Address", pld.Address)
tm.addRowBytes("Proof", pld.Proof)
case pactus.PayloadType_UNBOND_PAYLOAD:
pld := trx.Payload.(*pactus.TransactionInfo_Unbond).Unbond
tm.addRowValAddress("Validator", pld.Validator)
case pactus.PayloadType_WITHDRAW_PAYLOAD:
pld := trx.Payload.(*pactus.TransactionInfo_Withdraw).Withdraw
tm.addRowValAddress("Sender", pld.From)
tm.addRowAccAddress("Receiver", pld.To)
tm.addRowAmount("Amount", amount.Amount(pld.Amount))
case pactus.PayloadType_UNKNOWN:
tm.addRowValAddress("error", "unknown payload type")
}
if trx.PublicKey != "" {
tm.addRowString("PublicKey", trx.PublicKey)
}
if trx.Signature != nil {
tm.addRowBytes("Signature", trx.Signature)
}
}