-
Notifications
You must be signed in to change notification settings - Fork 3
/
receiver.go
137 lines (115 loc) · 3.4 KB
/
receiver.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package communications
import (
"fmt"
"sort"
"time"
"github.com/habpygo/zero-value-msg.client.go/mamutils"
"github.com/iotaledger/giota"
)
type Transaction struct {
Message string
Value int64
Timestamp time.Time
Recipient string
}
type ApiTransactionsFinder interface {
FindTransactions(giota.FindTransactionsRequest) ([]giota.Transaction, error)
}
// ReadTransactions reads all the historic transaction data from a particular address
func ReadTransactions(address string, f ApiTransactionsFinder) ([]Transaction, error) {
iotaAddress, err := giota.ToAddress(address)
if err != nil {
return nil, err
}
req := giota.FindTransactionsRequest{
Addresses: []giota.Address{iotaAddress},
}
foundTx, err := f.FindTransactions(req)
//fmt.Println("foundTx is: ", foundTx)
if err != nil {
return nil, err
}
sort.Slice(foundTx, func(i, j int) bool {
return !(foundTx[i].Timestamp.Unix() < foundTx[j].Timestamp.Unix())
})
transactions := make([]Transaction, len(foundTx))
for i, t := range foundTx {
//message, err := mamutils.FromMAMTrytes(t.SignatureMessageFragment)
message, err := mamutils.FromMAMTrytes(t.SignatureMessageFragment)
if err != nil {
return nil, err
}
transactions[i] = Transaction{
Message: message,
Value: t.Value,
Timestamp: t.Timestamp,
Recipient: string(t.Address),
}
}
fmt.Println("Total number of transactions found on tangle are: ", len(transactions))
return transactions, nil
}
type ApiTransactionsReader interface {
ReadTransactions([]giota.Trytes) ([]giota.Transaction, error)
}
func ReadTransaction(transactionID string, r ApiTransactionsReader) (Transaction, error) {
tID, err := giota.ToTrytes(transactionID)
if err != nil {
return Transaction{}, err
}
txs, err := r.ReadTransactions([]giota.Trytes{tID})
if len(txs) != 1 {
return Transaction{}, fmt.Errorf("Requested 1 Transaction but got %d", len(txs))
}
if err != nil {
return Transaction{}, err
}
tx := txs[0]
message, err := mamutils.FromMAMTrytes(tx.SignatureMessageFragment)
if err != nil {
return Transaction{}, err
}
transaction := Transaction{
Message: message,
Value: tx.Value,
Timestamp: tx.Timestamp,
Recipient: string(tx.Address),
}
return transaction, nil
}
// func ReadTransactions(address string, f ApiTransactionsFinder) ([]Transaction, error) {
// iotaAddress, err := giota.ToAddress(address)
// if err != nil {
// return nil, err
// }
// fmt.Println("iotaAddress is: ", iotaAddress)
// req := giota.FindTransactionsRequest{
// Addresses: []giota.Address{iotaAddress},
// }
// fmt.Println("req.Addresses is: ", req.Addresses)
// fmt.Println("req.Approvees is: ", req.Approvees)
// fmt.Println("req.Bundles is: ", req.Bundles)
// fmt.Println("req.Command is: ", req.Command)
// fmt.Println("req.Tags is: ", req.Tags)
// foundTx, err := f.FindTransactions(req)
// if err != nil {
// return nil, err
// }
// sort.Slice(foundTx, func(i, j int) bool {
// return !(foundTx[i].Timestamp.Unix() < foundTx[j].Timestamp.Unix())
// })
// transactions := make([]Transaction, len(foundTx))
// for i, t := range foundTx {
// message, err := mamutils.FromMAMTrytes(t.SignatureMessageFragment)
// if err != nil {
// return nil, err
// }
// transactions[i] = Transaction{
// Message: message,
// Value: t.Value,
// Timestamp: t.Timestamp,
// Recipient: string(t.Address),
// }
// }
// return transactions, nil
// }