-
Notifications
You must be signed in to change notification settings - Fork 2
/
daash.go
181 lines (166 loc) Β· 4.43 KB
/
daash.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package daash
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/Layr-Labs/eigenda/api/clients"
"github.com/cenkalti/backoff"
"github.com/stackrlabs/go-daash/avail"
"github.com/stackrlabs/go-daash/celestia"
"github.com/stackrlabs/go-daash/da"
"github.com/stackrlabs/go-daash/eigen"
"github.com/stackrlabs/go-daash/mock"
)
type DALayer string
const (
Avail DALayer = "avail"
Eigen DALayer = "eigen"
Celestia DALayer = "celestia"
Mock DALayer = "mock"
)
func IsValidDA(layer DALayer) bool {
for _, validLayer := range []DALayer{Avail, Eigen, Celestia, Mock} {
if layer == validLayer {
return true
}
}
return false
}
type ClientBuilder struct {
Clients map[DALayer]da.Client
}
func NewClientBuilder() *ClientBuilder {
return &ClientBuilder{
Clients: make(map[DALayer]da.Client),
}
}
// Initiates a new DAManager with clients from the sepcified DA layers
func (d *ClientBuilder) InitClients(
ctx context.Context,
layers []DALayer,
availConfigPath string,
celestiaAuthToken string,
celestiaLightClientUrl string,
celestiaNodeUrl string,
eigenConfig clients.EigenDAClientConfig,
) (*ClientBuilder, error) {
if len(layers) == 0 {
return nil, fmt.Errorf("no da layers provided")
}
for _, layer := range layers {
switch layer {
case Avail:
var availClient da.Client
var err error
err = backoff.Retry(func() error {
availClient, err = avail.NewClient(availConfigPath)
return err //nolint: wrapcheck
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5))
if err != nil {
log.Println("β Failed to initialise Avail DA client")
return nil, fmt.Errorf(" Failed to create avail client: %v", err)
}
log.Println("π’ Avail DA client initialised")
d.Clients[Avail] = availClient
case Celestia:
if celestiaAuthToken == "" {
fmt.Println("AUTH_TOKEN is not set")
return nil, fmt.Errorf("celestia auth token is not set")
}
// We use a random pre-set hex string for namespace rn
namespace := "9cb73e106b03d1050a13"
celestia, err := celestia.NewClient(ctx, celestiaLightClientUrl, celestiaNodeUrl, celestiaAuthToken, namespace, -1)
if err != nil {
return nil, err
}
log.Println("π’ Celestia DA client initialised")
d.Clients[Celestia] = celestia
case Eigen:
eigen, err := eigen.NewClient(eigenConfig)
if err != nil {
return nil, err
}
d.Clients[Eigen] = eigen
log.Println("π’ Eigen DA client initialised")
case Mock:
d.Clients[Mock] = mock.NewDummyDA()
log.Println("π’ Mock DA client initialised")
default:
return nil, fmt.Errorf("invalid da layer provided: %s", layer)
}
}
return d, nil
}
func GetHumanReadableID(id da.ID, daLayer DALayer) any {
switch daLayer {
case Avail:
availID, ok := id.(avail.ID)
if !ok {
return ""
}
return availID
case Celestia:
celestiaID, ok := id.(celestia.ID)
if !ok {
return ""
}
return struct {
BlockHeight uint64 `json:"blockHeight"`
TxHash string `json:"txHash"`
Commitment da.Commitment `json:"commitment"`
}{
BlockHeight: celestiaID.Height,
TxHash: celestiaID.TxHash,
Commitment: celestiaID.ShareCommitment,
}
case Eigen:
eigenID, ok := id.(eigen.ID)
if !ok {
return ""
}
return struct {
BatchHeaderHash []byte
BlobIndex uint32
RequestID string
}{
BatchHeaderHash: eigenID.BlobInfo.BlobVerificationProof.BatchMetadata.BatchHeaderHash,
BlobIndex: eigenID.BlobInfo.BlobVerificationProof.BlobIndex,
RequestID: eigenID.RequestID,
}
default:
return ""
}
}
func GetExplorerLink(client da.Client, id da.ID) (string, error) {
switch daClient := client.(type) {
case *celestia.Client:
id, ok := id.(celestia.ID)
if !ok {
return "", fmt.Errorf("invalid ID")
}
return fmt.Sprintf("https://mocha-4.celenium.io/tx/%s", id.TxHash), nil
case *avail.Client:
ext, err := daClient.GetExtrinsic(id)
if err != nil {
return "", err
}
extBytes, err := json.Marshal(ext)
if err != nil {
return "", err
}
// Strip string of any leading or following quotes
extString := strings.Trim(string(extBytes), "\"")
fmt.Println(extString)
return fmt.Sprintf("https://goldberg.avail.tools/#/extrinsics/decode/%s", extString), nil
case *eigen.Client:
eigenID, ok := id.(eigen.ID)
if !ok {
return "", fmt.Errorf("invalid ID")
}
return fmt.Sprintf("https://blobs-holesky.eigenda.xyz/blobs/%s", eigenID.RequestID), nil
default:
return "", nil
}
}