/
target_ps_pinata.go
167 lines (136 loc) · 3.85 KB
/
target_ps_pinata.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package start
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/host"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/dennis-tra/antares/pkg/utils"
)
const PinataTargetName = "pinata"
type Pinata struct {
h host.Host
auth string
}
func NewPinata(h host.Host, auth string) (PinTarget, error) {
return &Pinata{h: h, auth: auth}, nil
}
var _ PinTarget = (*Pinata)(nil)
func (p *Pinata) Operation(ctx context.Context, c cid.Cid) error {
logEntry := p.logEntry().WithField("cid", c)
logEntry.Infoln("Pinning cid to Pinata...")
var publicMaddr ma.Multiaddr
for _, maddr := range p.h.Addrs() {
if manet.IsPublicAddr(maddr) {
publicMaddr = maddr
break
}
}
var popts *PinataOptions
if publicMaddr != nil {
popts = &PinataOptions{HostNodes: []string{
publicMaddr.String() + "/p2p/" + p.h.ID().String(),
}}
}
payload := PinataRequest{
HashToPin: c.String(),
PinataMetadata: &PinataMetadata{
Name: "Antares " + time.Now().String(),
},
PinataOptions: popts,
}
data, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "marshal request payload")
}
req, err := http.NewRequest(http.MethodPost, "https://api.pinata.cloud/pinning/pinByHash", bytes.NewBuffer(data))
if err != nil {
return errors.Wrap(err, "new request")
}
req.Header.Add("Authorization", "Bearer "+p.auth)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "pin file to pinata")
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "read response body")
}
logEntry.Debugln("Pin response:", string(respBody))
if !utils.IsSuccessStatusCode(resp) {
return fmt.Errorf("status code %d", resp.StatusCode)
}
return nil
}
type PinataRequest struct {
HashToPin string `json:"hashToPin"`
PinataMetadata *PinataMetadata `json:"pinataMetadata,omitempty"`
PinataOptions *PinataOptions `json:"pinataOptions,omitempty"`
}
type PinataMetadata struct {
Name string `json:"name"`
}
type PinataOptions struct {
HostNodes []string `json:"hostNodes"`
}
func (p *Pinata) Backoff(ctx context.Context) backoff.BackOff {
bo := &backoff.ExponentialBackOff{
InitialInterval: time.Minute,
RandomizationFactor: 0.5,
Multiplier: 1.2,
MaxInterval: 5 * time.Minute,
MaxElapsedTime: 10 * time.Minute,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
return backoff.WithContext(bo, ctx)
}
func (p *Pinata) Rate() time.Duration {
return 5 * time.Minute
}
func (p *Pinata) Timeout() time.Duration {
return 10 * time.Minute
}
func (p *Pinata) Name() string {
return "pinata"
}
func (p *Pinata) Type() string {
return "pinning-service"
}
func (p *Pinata) CleanUp(ctx context.Context, c cid.Cid) error {
logEntry := p.logEntry().WithField("cid", c)
logEntry.Infoln("Unpinning cid from Pinata...")
req, err := http.NewRequest(http.MethodDelete, "https://api.pinata.cloud/pinning/unpin/"+c.String(), nil)
if err != nil {
return errors.Wrap(err, "new request")
}
req.Header.Add("Authorization", "Bearer "+p.auth)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "unpin cid from pinata")
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "read response body")
}
logEntry.Debugln("Unpin response:", string(respBody))
if !utils.IsSuccessStatusCode(resp) {
return fmt.Errorf("status code %d", resp.StatusCode)
}
return nil
}
func (p *Pinata) logEntry() *log.Entry {
return log.WithField("type", p.Type()).WithField("name", p.Name())
}