/
init.go
219 lines (193 loc) 路 6.3 KB
/
init.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package stealcamoor
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/0xmichalis/stealcamoor/pkg/abis"
email "github.com/0xmichalis/stealcamoor/pkg/client/email"
"github.com/0xmichalis/stealcamoor/pkg/client/etherscan"
"github.com/0xmichalis/stealcamoor/pkg/client/stealcam"
)
func New() (*Stealcamoor, error) {
sc := &Stealcamoor{}
if err := sc.initApiClient(); err != nil {
return nil, fmt.Errorf("failed to initialize api client: %w", err)
}
if err := sc.initMisc(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
if err := sc.initEmailClient(); err != nil {
return nil, fmt.Errorf("failed to initialize email client: %w", err)
}
if err := sc.initBlockchainClient(); err != nil {
return nil, fmt.Errorf("failed to initialize blockchain client: %w", err)
}
return sc, nil
}
func (sc *Stealcamoor) initApiClient() error {
apiURL := os.Getenv("STEALCAM_API_URL")
if apiURL == "" {
return errors.New("STEALCAM_API_URL cannot be empty")
}
sc.apiClient = stealcam.New(apiURL)
if os.Getenv("STEALCAM_API_REQUEST_INTERVAL") == "" {
return errors.New("STEALCAM_API_REQUEST_INTERVAL cannot be empty")
}
apiRequestInterval, err := time.ParseDuration(os.Getenv("STEALCAM_API_REQUEST_INTERVAL"))
if err != nil {
return err
}
sc.apiRequestInterval = apiRequestInterval
return nil
}
func (sc *Stealcamoor) initEmailClient() error {
host := os.Getenv("SMTP_HOST")
if host == "" {
return errors.New("SMTP_HOST cannot be empty")
}
port := os.Getenv("SMTP_PORT")
if port == "" {
return errors.New("SMTP_PORT cannot be empty")
}
username := os.Getenv("SMTP_USERNAME")
if username == "" {
return errors.New("SMTP_USERNAME cannot be empty")
}
password := os.Getenv("SMTP_PASSWORD")
if password == "" {
return errors.New("SMTP_PASSWORD cannot be empty")
}
from := os.Getenv("SMTP_FROM")
if from == "" {
return errors.New("SMTP_FROM cannot be empty")
}
to := os.Getenv("SMTP_TO")
if to == "" {
return errors.New("SMTP_TO cannot be empty")
}
sc.emailClient = email.New(host, port, username, password, from, strings.Split(to, ","))
return nil
}
func (sc *Stealcamoor) initBlockchainClient() error {
stealcamAddress := os.Getenv("STEALCAM_ADDRESS")
if stealcamAddress == "" {
return errors.New("STEALCAM_ADDRESS cannot be empty")
}
sc.stealcamAddress = common.HexToAddress(stealcamAddress)
log.Println("Stealcam address:", etherscan.GetEtherscanAddress(sc.explorerURL, sc.stealcamAddress))
if os.Getenv("PRIVATE_KEY") == "" {
return errors.New("PRIVATE_KEY cannot be empty")
}
nodeURL := os.Getenv("NODE_API_URL")
if nodeURL == "" {
return errors.New("NODE_API_URL cannot be empty")
}
if !strings.HasPrefix(nodeURL, "wss://") {
return errors.New("NODE_API_URL needs to be a Websocket RPC URL")
}
log.Println("Initializing node connection...")
client, err := ethclient.Dial(nodeURL)
if err != nil {
return fmt.Errorf("cannot connect to node: %w", err)
}
sc.client = client
chainID, err := client.NetworkID(context.Background())
if err != nil {
return fmt.Errorf("cannot get chain id: %w", err)
}
log.Println("Chain ID:", chainID)
// Load private key
privateKey, err := crypto.HexToECDSA(os.Getenv("PRIVATE_KEY"))
if err != nil {
return fmt.Errorf("cannot load private key: %w", err)
}
// Extract address
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf("cannot cast public key to ECDSA")
}
address := crypto.PubkeyToAddress(*publicKeyECDSA)
sc.ourAddress = address
log.Println("Our address:", etherscan.GetEtherscanAddress(sc.explorerURL, address))
// Sign message and store signature once to be used during reveals
message := "Confirming my ETH address to reveal a Stealcam photo"
signature, err := signMessage(message, privateKey)
if err != nil {
return fmt.Errorf("cannot sign message: %w", err)
}
sc.ourSignature = signature
txOpts, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
return fmt.Errorf("cannot create authorized transactor: %w", err)
}
sc.txOpts = txOpts
sc.txOpts.GasLimit = 10000000
stealcam, err := abis.NewStealcam(sc.stealcamAddress, client)
if err != nil {
return fmt.Errorf("cannot instantiate stealcam contract client: %w", err)
}
sc.stealcamContract = stealcam
return nil
}
func signMessage(message string, privateKey *ecdsa.PrivateKey) (string, error) {
fullMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)
hash := crypto.Keccak256Hash([]byte(fullMessage))
signatureBytes, err := crypto.Sign(hash.Bytes(), privateKey)
if err != nil {
return "", err
}
signatureBytes[64] += 27
return hexutil.Encode(signatureBytes), nil
}
func (sc *Stealcamoor) initMisc() error {
explorerURL := os.Getenv("BLOCKCHAIN_EXPLORER_URL")
if explorerURL == "" {
return errors.New("BLOCKCHAIN_EXPLORER_URL cannot be empty")
}
sc.explorerURL = explorerURL
creatorStrings := strings.Split(os.Getenv("CREATORS"), ",")
creators := make([]common.Address, 0)
isDuplicate := make(map[string]bool)
addressToTwitter := make(map[string]string)
for _, creator := range creatorStrings {
if isDuplicate[creator] {
return fmt.Errorf("duplicate creator %s", creator)
}
isDuplicate[creator] = true
c := common.HexToAddress(creator)
creators = append(creators, c)
profile, err := sc.apiClient.GetProfile(c)
if err != nil {
return fmt.Errorf("cannot get profile for creator %s: %w", creator, err)
}
addressToTwitter[c.String()] = profile.Username
logCreator := etherscan.GetEtherscanAddress(sc.explorerURL, c)
if profile.Username != "" {
logCreator = fmt.Sprintf("https://twitter.com/%s (%s)", profile.Username, creator)
}
log.Printf("Tracking creator %s", logCreator)
}
if len(creators) == 0 {
return errors.New("Need at least one creator provided in CREATORS (comma-separated list)")
}
if len(creators) == 1 {
log.Print("Tracking 1 creator.")
} else {
log.Printf("Tracking %d creators.", len(creatorStrings))
}
sc.creators = creators
sc.addressToTwitter = addressToTwitter
sc.backupDir = os.Getenv("BACKUP_DIR")
return nil
}