-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-item-parser.go
131 lines (114 loc) · 3.3 KB
/
data-item-parser.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
package warp
import (
"crypto/sha256"
"encoding/json"
"github.com/jackc/pgtype"
"github.com/warp-contracts/syncer/src/utils/arweave"
"github.com/warp-contracts/syncer/src/utils/bundlr"
"github.com/warp-contracts/syncer/src/utils/config"
"github.com/warp-contracts/syncer/src/utils/logger"
"github.com/warp-contracts/syncer/src/utils/model"
"github.com/warp-contracts/syncer/src/utils/smartweave"
"github.com/dvsekhvalnov/jose2go/base64url"
"github.com/sirupsen/logrus"
)
type DataItemParser struct {
log *logrus.Entry
}
func NewDataItemParser(config *config.Config) (self *DataItemParser) {
self = new(DataItemParser)
self.log = logger.NewSublogger("interaction-parser")
return
}
func (self *DataItemParser) Parse(tx *bundlr.BundleItem, blockHeight int64, blockId arweave.Base64String, blockTimestamp int64, sortKey, lastSortKey string, random arweave.Base64String, sequencerBlock *smartweave.SequencerBlock) (out *model.Interaction, err error) {
out = &model.Interaction{
InteractionId: tx.Id,
BlockHeight: blockHeight,
BlockId: blockId,
ConfirmationStatus: "confirmed",
Source: "redstone-sequencer",
BlockTimestamp: blockTimestamp,
SortKey: sortKey,
}
// Fill last sort key
if lastSortKey == "" {
out.LastSortKey.Status = pgtype.Null
} else {
out.LastSortKey.Status = pgtype.Present
out.LastSortKey.String = lastSortKey
}
// Fill Sort Key
out.SortKey = sortKey
// Fill tags, already decoded from base64
err = self.fillTags(tx, out)
if err != nil {
return
}
out.Owner, err = GetWalletAddressFromBundle(tx)
if err != nil {
return
}
// Save decoded tags
parsedTags := self.parseTags(tx.Tags)
swInteraction := smartweave.Interaction{
Id: tx.Id,
Owner: smartweave.Owner{
Address: out.Owner,
},
Recipient: tx.Target,
Tags: parsedTags,
Block: smartweave.Block{
Height: blockHeight,
Id: blockId,
Timestamp: blockTimestamp,
},
Random: random,
SequencerBlock: sequencerBlock,
// Fee: smartweave.Amount{
// Winston: tx.Reward,
// },
// Quantity: smartweave.Amount{
// Winston: tx.Quantity,
// },
}
swInteractionJson, err := json.Marshal(swInteraction)
if err != nil {
self.log.Error("Failed to marshal interaction")
return
}
err = out.Interaction.Set(swInteractionJson)
if err != nil {
self.log.Error("Failed set interaction JSON")
return
}
return
}
func GetWalletAddressFromBundle(tx *bundlr.BundleItem) (owner string, err error) {
// The n value is the public modulus and is used as the transaction owner field,
// and the address of a wallet is a Base64URL encoded SHA-256 hash of the n value from the JWK.
// https://docs.arweave.org/developers/server/http-api#addressing
h := sha256.New()
h.Write([]byte(tx.Owner))
owner = base64url.Encode(h.Sum(nil))
return
}
func (self *DataItemParser) parseTags(tags []bundlr.Tag) (out []smartweave.Tag) {
out = make([]smartweave.Tag, len(tags))
for i, t := range tags {
out[i] = smartweave.Tag{
Name: string(t.Name),
Value: string(t.Value),
}
}
return
}
func (self *DataItemParser) fillTags(tx *bundlr.BundleItem, out *model.Interaction) (err error) {
// Fill data from tags
for _, t := range tx.Tags {
err = AddTagToInteraction(out, string(t.Name), string(t.Value))
if err != nil {
return
}
}
return nil
}