-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
151 lines (135 loc) · 3.35 KB
/
client.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
package opplasmairys
import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/Ja7ad/irys"
"github.com/Ja7ad/irys/currency"
"github.com/Ja7ad/irys/types"
ghl "github.com/Khan/genqlient/graphql"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"io"
"log/slog"
"net/http"
)
const (
Matic = "matic"
Bnb = "bnb"
Avalanche = "avalanche"
Arbitrum = "arbitrum"
Fantom = "fantom"
Ethereum = "ethereum"
)
const (
GraphqlMainNet = "https://arweave.mainnet.irys.xyz/graphql"
GraphqlMainNet2 = "https://arweave.net/graphql"
)
type Config struct {
NetworkName string
PrivateKey string
NetWorkRpc string
FreeUpload bool
}
type IrysClient struct {
c irys.Irys
key string
graphql ghl.Client
enableFreeUpload bool
ethereumAddr string
arweaveAddr string
}
// NewIrysClient
// support tokens ethereum,matic,bnb,avalanche,arbitrum,fantom
func NewIrysClient(networkName, rpc, privateKey string, enableFreeUpload bool) (*IrysClient, error) {
prKey, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
pubkBytes := crypto.FromECDSAPub(&prKey.PublicKey)
key := base64.RawURLEncoding.EncodeToString(pubkBytes)
ethereumAddress := crypto.PubkeyToAddress(prKey.PublicKey).String()
addr := sha256.Sum256(pubkBytes)
arweaveAddr := base64.RawURLEncoding.EncodeToString(addr[:])
currencyFunc := newProvider(networkName)
if currencyFunc == nil {
return nil, fmt.Errorf("unsupport network: %s", networkName)
}
matic, err := currencyFunc(privateKey, rpc)
if err != nil {
return nil, err
}
c, err := irys.New(irys.DefaultNode1, matic, false)
if err != nil {
return nil, err
}
graphql := ghl.NewClient(GraphqlMainNet, http.DefaultClient)
return &IrysClient{
c: c,
key: key,
graphql: graphql,
enableFreeUpload: enableFreeUpload,
ethereumAddr: ethereumAddress,
arweaveAddr: arweaveAddr,
}, nil
}
func (c *IrysClient) Upload(ctx context.Context, id []byte, data []byte) error {
var (
tx types.Transaction
err error
)
if c.enableFreeUpload && len(data) > 1024*1024*100 {
tx, err = c.c.BasicUpload(ctx, data, types.Tag{
Name: "OP-PLASMA-KEY",
Value: hexutil.Encode(id),
})
} else {
tx, err = c.c.Upload(ctx, data, types.Tag{
Name: "OP-PLASMA-KEY",
Value: hexutil.Encode(id),
})
}
if err != nil {
return err
}
slog.Info("irys uploaded, ", "tx", tx.ID)
return nil
}
func (c *IrysClient) Download(ctx context.Context, id []byte) (io.ReadCloser, error) {
//query txid by op plasma key
// filter owners:
//use ethereum address for https://arweave.mainnet.irys.xyz/graphql
//use arweave address for https://arweave.net/graphql
tx, err := QueryTx(ctx, c.graphql, []string{c.ethereumAddr}, hexutil.Encode(id))
if err != nil {
return nil, err
}
edges := tx.Transactions.Edges
if len(edges) == 0 {
return nil, fmt.Errorf("not found")
}
txId := edges[0].Node.Id
f, err := c.c.Download(ctx, txId)
if err != nil {
return nil, err
}
return f.Data, nil
}
func newProvider(name string) func(privateKey, rpc string) (currency.Currency, error) {
switch name {
case Bnb:
return currency.NewBNB
case Arbitrum:
return currency.NewArbitrum
case Avalanche:
return currency.NewAvalanche
case Fantom:
return currency.NewFantom
case Ethereum:
return currency.NewEthereum
case Matic:
return currency.NewMatic
}
return nil
}