-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereumapp.go
1087 lines (963 loc) · 29.9 KB
/
ethereumapp.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:generate solc --overwrite --abi --bin nft/nft.sol -o nft/build/
//go:generate abigen --abi nft/build/SampleNFT.abi --bin nft/build/SampleNFT.bin --pkg nft --type SampleNFT --out nft/nft.go
package main
import (
"log"
"fmt"
"os"
"time"
"flag"
"regexp"
"strings"
"context"
"net/http"
"html/template"
"database/sql"
"math/big"
"io/ioutil"
"strconv"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/sessions"
"github.com/shomali11/util/xhashes"
"github.com/serg666/ethereumapp/nft"
_ "github.com/mattn/go-sqlite3"
)
type Page struct {
Title string
Account *Account
Asset *Asset
Token *Token
Chunks []interface{}
}
type Asset struct {
Id string
Name string
Description string
Content []byte
ContentType string
}
type Token struct {
Id int64
}
type Account struct {
Id string
Email string
}
type HistoryResult struct {
Time time.Time
Sender string
Recipient string
Gas *big.Float
GasPrice *big.Float
Block uint64
TxHash string
Value float64
}
type Transaction struct {
From string
To string
Value string
}
type Participant struct {
Id int64
Email string
Passwd string
}
var (
err error
dataDir *string
baseAccount *string
contractAddress *string
sqliteDatabase *sql.DB
rpcClient *rpc.Client
ethClient *ethclient.Client
token *nft.SampleNFT
chainID *big.Int
ks *keystore.KeyStore
transactor *bind.TransactOpts
key = []byte("super-secret-key")
templates = template.Must(template.ParseFiles(
"templates/main.html",
"templates/login.html",
"templates/account.html",
"templates/history.html",
"templates/transfer.html",
"templates/nft.html",
"templates/asset.html",
"templates/tokens.html",
"templates/token.html",
"templates/footer.html",
))
store = sessions.NewCookieStore(key)
validPath = regexp.MustCompile("^/(history|transfer|img|asset|token)/([a-z0-9]+)$")
)
func weiToEther(wei *big.Int) *big.Float {
return new(big.Float).Quo(new(big.Float).SetInt(wei), big.NewFloat(params.Ether))
}
func etherToWei(eth *big.Float) *big.Int {
truncInt, _ := eth.Int(nil)
truncInt = new(big.Int).Mul(truncInt, big.NewInt(params.Ether))
fracStr := strings.Split(fmt.Sprintf("%.18f", eth), ".")[1]
fracStr += strings.Repeat("0", 18 - len(fracStr))
fracInt, _ := new(big.Int).SetString(fracStr, 10)
wei := new(big.Int).Add(truncInt, fracInt)
return wei
}
func ParseBigFloat(value string) (*big.Float, error) {
f := new(big.Float)
f.SetPrec(236) // IEEE 754 octuple-precision binary floating-point format: binary256
f.SetMode(big.ToNearestEven)
_, err := fmt.Sscan(value, f)
return f, err
}
func (t *Token) Symbol() string {
if symbol, err := token.Symbol(&bind.CallOpts{}); err == nil {
return symbol
}
log.Printf("token symbol err: %v", err)
return "Not available now"
}
func (t *Token) Name() string {
if name, err := token.Name(&bind.CallOpts{}); err == nil {
return name
}
log.Printf("token name err: %v", err)
return "Not available now"
}
func (t *Token) Account() *Account {
if owner, err := token.OwnerOf(&bind.CallOpts{}, big.NewInt(t.Id)); err == nil {
if acc, err := readAccount(owner.Hex()); err == nil {
return acc
}
log.Printf("read acc err: %v", err)
}
log.Printf("ownerof err: %v", err)
return nil
}
func (t *Token) Owner() string {
if acc := t.Account(); acc != nil {
return fmt.Sprintf("%s (%s)", acc.Id, acc.Email)
}
return "Not available now"
}
func (t *Token) URI() string {
if uri, err := token.TokenURI(&bind.CallOpts{}, big.NewInt(t.Id)); err == nil {
return uri
}
log.Printf("token uri err: %v", err)
return "Not available now"
}
func (a *Account) Balance() *big.Float {
account := common.HexToAddress(a.Id)
balance, err := ethClient.BalanceAt(context.Background(), account, nil)
ethBal := new(big.Float)
if err == nil {
ethBal.SetString(weiToEther(balance).String())
} else {
ethBal.SetString("0")
}
return ethBal
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err = templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func login_page(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "login", &Page{Title: "Login"})
}
func auth_page(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "cookie-name")
// Authentication goes here
if r.Method == "POST" {
err = r.ParseForm()
if err == nil {
exists, err := participantExists(r.FormValue("email"))
log.Printf("Participant %s exists: %v %v", r.FormValue("email"), exists, err)
if err == nil {
if !exists {
participant, err := createParticipant(r.FormValue("email"), r.FormValue("password"))
log.Printf("Create participant: %v", err)
session.Values["authenticated"] = err == nil
session.Values["email"] = participant.Email
session.Values["participant_id"] = participant.Id
} else {
participant, err := readParticipant(r.FormValue("email"))
log.Printf("Read participant: %v", err)
if err == nil {
session.Values["authenticated"] = participant.Passwd == xhashes.MD5(r.FormValue("password"))
session.Values["email"] = participant.Email
session.Values["participant_id"] = participant.Id
}
}
}
}
}
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
func logout_page(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "cookie-name")
// Revoke users authentication
session.Values["authenticated"] = false
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
func main_page(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
participant_id, _ := session.Values["participant_id"].(int64)
p := &Page{
Title: email,
}
accounts, err := getParticipantAccounts(participant_id)
if err == nil {
for _, acc := range accounts {
p.Chunks = append(p.Chunks, acc)
}
}
session.Save(r, w)
renderTemplate(w, "main", p)
}
func account_page(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
participant_id, _ := session.Values["participant_id"].(int64)
switch r.Method {
case "GET":
p := &Page{
Title: email,
}
session.Save(r, w)
renderTemplate(w, "account", p)
case "POST":
if err = r.ParseForm(); err != nil {
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
return
}
var newAcc hexutil.Bytes
err = rpcClient.Call(&newAcc, "personal_newAccount", r.FormValue("passwd"))
log.Printf("Create personal account for %s: %v", email, err)
if err == nil {
_, err = createAccount(newAcc.String(), email, participant_id)
log.Printf("Creating participant account for %s: %v", email, err)
if err == nil {
if amount, err := ParseBigFloat(r.FormValue("amount")); err == nil && amount.Cmp(big.NewFloat(0)) == 1 {
var txHash hexutil.Bytes
tx := Transaction{
From: *baseAccount,
To: newAcc.String(),
Value: fmt.Sprintf("0x%x", etherToWei(amount)),
}
// @note: before run the application you should set the BASE_ACC_PASSWD environment variable
err = rpcClient.Call(&txHash, "personal_sendTransaction", tx, os.Getenv("BASE_ACC_PASSWD"))
log.Printf("Buy some coins %v: %v", txHash, err)
} else {
log.Printf("Convert amount %v err: %v", amount, err)
}
}
}
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
default:
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
}
func tokenHandler(w http.ResponseWriter, r *http.Request, id string) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
session.Save(r, w)
t, err := readToken(id)
if err != nil {
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
p := &Page{
Title: email,
Token: t,
}
if accounts, err := getAllAccounts(); err == nil {
for _, acc := range accounts {
if acc.Id != t.Account().Id {
p.Chunks = append(p.Chunks, acc)
}
}
}
renderTemplate(w, "token", p)
case "POST":
if err = r.ParseForm(); err == nil {
ownerAcc := accounts.Account{
Address: common.HexToAddress(t.Account().Id),
}
if err = ks.Unlock(ownerAcc, r.FormValue("password")); err == nil {
if transferTransactor, err := bind.NewKeyStoreTransactorWithChainID(ks, ownerAcc, chainID); err == nil {
if ttx, err := token.TransferFrom(
transferTransactor,
common.HexToAddress(t.Account().Id),
common.HexToAddress(r.FormValue("new_owner")),
big.NewInt(t.Id),
); err == nil {
log.Printf("transfer token tx hash: %v", ttx.Hash())
}
log.Printf("transfer token err: %v", err)
}
log.Printf("transfer transactor err: %v", err)
err = ks.Lock(common.HexToAddress(t.Account().Id))
log.Printf("Lock owner acc err: %v", err)
}
log.Printf("Transfer unlock err: %v", err)
}
log.Printf("Parse err: %v", err)
http.Redirect(w, r, "/tokens", http.StatusFound)
default:
http.Redirect(w, r, "/", http.StatusFound)
}
}
func assetHandler(w http.ResponseWriter, r *http.Request, id string) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
asset, err := readAsset(id)
if err != nil {
http.NotFound(w, r)
return
}
p := &Page{
Title: email,
Asset: asset,
}
session.Save(r, w)
renderTemplate(w, "asset", p)
}
func imgHandler(w http.ResponseWriter, r *http.Request, id string) {
asset, err := readAsset(id)
if err != nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", asset.ContentType)
w.Header().Set("Content-Length", strconv.Itoa(len(asset.Content)))
imgLen, err := w.Write(asset.Content)
log.Printf("img len: %v", imgLen)
log.Printf("img err: %v", err)
}
func tokens_page(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
p := &Page{
Title: email,
}
err := getAllTokens(&p.Chunks)
log.Printf("getAllTokens err: %v", err)
session.Save(r, w)
renderTemplate(w, "tokens", p)
}
func nft_page(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
session.Save(r, w)
switch r.Method {
case "GET":
p := &Page{
Title: email,
}
if accounts, err := getAllAccounts(); err == nil {
for _, acc := range accounts {
p.Chunks = append(p.Chunks, acc)
}
}
renderTemplate(w, "nft", p)
case "POST":
if err := r.ParseMultipartForm(10 << 20); err == nil {
if file, handler, err := r.FormFile("image"); err == nil {
defer file.Close()
log.Printf("Uploaded File: %v", handler.Filename)
log.Printf("File Size: %v", handler.Size)
log.Printf("MIME Header: %v", handler.Header)
if strings.HasPrefix(handler.Header["Content-Type"][0], "image") {
if fileBytes, err := ioutil.ReadAll(file); err == nil {
log.Printf("MD5: %s", xhashes.MD5(string(fileBytes)))
if ntx, err := token.MintNFT(transactor, common.HexToAddress(r.FormValue("owner")), fmt.Sprintf(
"/asset/%s",
xhashes.MD5(string(fileBytes)),
)); err == nil {
log.Printf("Mint tx: %v", ntx.Hash())
insertSQL := `insert into assets (id, name, description, content, content_type) values (?, ?, ?, ?, ?)`
if stmt, err := sqliteDatabase.Prepare(insertSQL); err == nil {
defer stmt.Close()
result, err := stmt.Exec(
xhashes.MD5(string(fileBytes)),
r.FormValue("name"),
r.FormValue("description"),
fileBytes,
handler.Header["Content-Type"][0],
)
log.Printf("Insert result: %v", result)
log.Printf("Insert err: %v", err)
// @note: now we will be waiting for transaction mined and emit Transfer event with new token_id
}
log.Printf("Prepare err: %v", err)
}
log.Printf("Mint err: %v", err)
}
log.Printf("Read file err: %v", err)
}
}
log.Printf("File err: %v", err)
}
log.Printf("Parse err: %v", err)
http.Redirect(w, r, "/tokens", http.StatusFound)
default:
http.Redirect(w, r, "/", http.StatusFound)
}
}
func historyHandler(w http.ResponseWriter, r *http.Request, account string) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
acc, err := readAccount(account)
if err != nil {
session.Save(r, w)
http.NotFound(w, r)
return
}
p := &Page{
Title: email,
Account: acc,
}
err = getAccountHistory(acc, &p.Chunks)
if err != nil {
log.Printf("Error to get account history: %v", err)
}
session.Save(r, w)
renderTemplate(w, "history", p)
}
func transferHandler(w http.ResponseWriter, r *http.Request, account string) {
session, _ := store.Get(r, "cookie-name")
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
email, _ := session.Values["email"].(string)
sender, err := readAccount(account)
if err != nil {
session.Save(r, w)
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
p := &Page{
Title: email,
Account: sender,
}
accounts, err := getAllAccounts()
if err == nil {
for _, acc := range accounts {
if acc.Id != account {
p.Chunks = append(p.Chunks, acc)
}
}
} else {
log.Printf("Can not get all accounts: %v", err)
}
session.Save(r, w)
renderTemplate(w, "transfer", p)
case "POST":
if err = r.ParseForm(); err != nil {
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
return
}
if amount, err := ParseBigFloat(r.FormValue("amount")); err == nil && amount.Cmp(big.NewFloat(0)) == 1 {
var txHash hexutil.Bytes
tx := Transaction{
From: account,
To: r.FormValue("recipient"),
Value: fmt.Sprintf("0x%x", etherToWei(amount)),
}
err = rpcClient.Call(&txHash, "personal_sendTransaction", tx, r.FormValue("password"))
log.Printf("Transfer coins from %s to %s: %v, %v", account, r.FormValue("recipient"), txHash, err)
}
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
default:
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
}
func getAccountHistory(acc *Account, result *[]interface{}) error {
stmt, err := sqliteDatabase.Prepare("select * from ethtxs where lower(sender)=lower(?) or lower(recipient)=lower(?)")
if err != nil {
return fmt.Errorf("failed to get account %v history", acc.Id)
}
defer stmt.Close()
rows, err := stmt.Query(acc.Id, acc.Id)
if err != nil {
return fmt.Errorf("failed to get account %v history", acc.Id)
}
defer rows.Close()
for rows.Next() {
var res HistoryResult
var ts, gas, gasprice int64
if err := rows.Scan(&ts, &res.Sender, &res.Recipient, &gas, &gasprice, &res.Block, &res.TxHash, &res.Value); err != nil {
return fmt.Errorf("failed to get history result row: %v", err)
}
res.Time = time.Unix(ts, 0)
res.Gas = weiToEther(big.NewInt(gas))
res.GasPrice = weiToEther(big.NewInt(gasprice))
*result = append(*result, res)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("failed to iteration ober rows of %v account: %v", acc.Id, err)
}
return nil
}
func getAllTokens(tokens *[]interface {}) error {
stmt, err := sqliteDatabase.Prepare("select distinct token_id from transfer_history")
if err != nil {
return fmt.Errorf("Failed to get tokens: %v", err)
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
return fmt.Errorf("Failed to get tokens: %v", err)
}
defer rows.Close()
for rows.Next() {
var t Token
if err := rows.Scan(&t.Id); err == nil {
*tokens = append(*tokens, &t)
}
}
if err := rows.Err(); err != nil {
return fmt.Errorf("Failed to iterating over rows of tokens: %v", err)
}
return nil
}
func getAllAccounts() ([]*Account, error) {
stmt, err := sqliteDatabase.Prepare("select t1.id,t2.email from accounts as t1 left join participants as t2 on t2.id=t1.participant_id")
if err != nil {
return nil, fmt.Errorf("failed to get accounts: %v", err)
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
return nil, fmt.Errorf("failed to get accounts: %v", err)
}
defer rows.Close()
var accounts []*Account
for rows.Next() {
var acc Account
if err := rows.Scan(&acc.Id, &acc.Email); err != nil {
return nil, fmt.Errorf("failed to get accounts: %v", err)
}
accounts = append(accounts, &acc)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to iterating over rows of accounts: %v", err)
}
return accounts, nil
}
func getParticipantAccounts(participant_id int64) ([]*Account, error) {
stmt, err := sqliteDatabase.Prepare("select t1.id, t2.email from accounts as t1 left join participants as t2 on t2.id=t1.participant_id where t1.participant_id = ?")
if err != nil {
return nil, fmt.Errorf("failed to get participant %v accounts: %v", participant_id, err)
}
defer stmt.Close()
rows, err := stmt.Query(participant_id)
if err != nil {
return nil, fmt.Errorf("failed to get participant %v accounts: %v", participant_id, err)
}
defer rows.Close()
var accounts []*Account
for rows.Next() {
var acc Account
if err := rows.Scan(&acc.Id, &acc.Email); err != nil {
return nil, fmt.Errorf("failed to get participant %v accounts: %v", participant_id, err)
}
accounts = append(accounts, &acc)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to iterating over rows of participant %v accounts: %v", participant_id, err)
}
return accounts, nil
}
func participantExists(email string) (bool, error) {
stmt, err := sqliteDatabase.Prepare("select count(*) from participants where email = ?")
if err != nil {
return false, fmt.Errorf("failed to get participant %s: %v", email, err)
}
defer stmt.Close()
var cnt int
err = stmt.QueryRow(email).Scan(&cnt)
if err != nil {
return false, fmt.Errorf("failed to get participant %s: %v", email, err)
}
return cnt > 0, nil
}
func createAccount(accId, email string, participantId int64) (*Account, error) {
createSQL := `insert into accounts (id, participant_id) values (?, ?)`
stmt, err := sqliteDatabase.Prepare(createSQL)
if err != nil {
return nil, fmt.Errorf("failed to create account %s: %v", accId, err)
}
defer stmt.Close()
_, err = stmt.Exec(accId, participantId)
if err != nil {
return nil, fmt.Errorf("failed to create account %s: %v", accId, err)
}
var acc Account
acc.Id = accId
acc.Email = email
return &acc, nil
}
func createParticipant(email, passwd string) (*Participant, error) {
createSQL := `insert into participants (email, passwd) values (?, ?)`
stmt, err := sqliteDatabase.Prepare(createSQL)
if err != nil {
return nil, fmt.Errorf("failed to create participant %s: %v", email, err)
}
defer stmt.Close()
result, err := stmt.Exec(email, xhashes.MD5(passwd))
if err != nil {
return nil, fmt.Errorf("failed to create participant %s: %v", email, err)
}
last_id, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("failed to create participant %s: %v", email, err)
}
var participant Participant
participant.Id = last_id
participant.Email = email
participant.Passwd = xhashes.MD5(passwd)
return &participant, nil
}
func readToken(id string) (*Token, error) {
stmt, err := sqliteDatabase.Prepare("select token_id from transfer_history where token_id = ? limit 1")
if err != nil {
return nil, fmt.Errorf("failed to get token %s: %v", id, err)
}
defer stmt.Close()
var t Token
err = stmt.QueryRow(id).Scan(&t.Id)
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("token %s does not exists", id)
case err != nil:
return nil, fmt.Errorf("failed to get token %s: %v", id, err)
default:
return &t, nil
}
}
func readAsset(id string) (*Asset, error) {
stmt, err := sqliteDatabase.Prepare("select * from assets where id = ?")
if err != nil {
return nil, fmt.Errorf("failed to get asset %s: %v", id, err)
}
defer stmt.Close()
var asset Asset
err = stmt.QueryRow(id).Scan(&asset.Id, &asset.Name, &asset.Description, &asset.Content, &asset.ContentType)
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("asset %s does not exists", id)
case err != nil:
return nil, fmt.Errorf("failed to get asset %s: %v", id, err)
default:
return &asset, nil
}
}
func readAccount(id string) (*Account, error) {
stmt, err := sqliteDatabase.Prepare("select t1.id, t2.email from accounts as t1 left join participants as t2 on t2.id=t1.participant_id where lower(t1.id) = lower(?)")
if err != nil {
return nil, fmt.Errorf("failed to get account %s: %v", id, err)
}
defer stmt.Close()
var account Account
err = stmt.QueryRow(id).Scan(&account.Id, &account.Email)
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("account %s does not exists", id)
case err != nil:
return nil, fmt.Errorf("failed to get account %s: %v", id, err)
default:
return &account, nil
}
}
func readParticipant(email string) (*Participant, error) {
stmt, err := sqliteDatabase.Prepare("select * from participants where email = ?")
if err != nil {
return nil, fmt.Errorf("failed to get participant %s: %v", email, err)
}
defer stmt.Close()
var participant Participant
err = stmt.QueryRow(email).Scan(&participant.Id, &participant.Email, &participant.Passwd)
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("participant %s does not exists", email)
case err != nil:
return nil, fmt.Errorf("failed to get participan %s: %v", email, err)
default:
return &participant, nil
}
}
func initDB() {
initSQL := `create table participants (
id integer not null primary key autoincrement,
email varchar(255) not null unique,
passwd varchar(255)
);create table accounts (
id varchar(255) not null primary key,
participant_id integer not null,
foreign key(participant_id) references participants(id)
);create table ethtxs (
time integer,
sender text,
recipient text,
gas bigint,
gasprice bigint,
block integer,
txhash text,
value numeric
);create table assets (
id varchar(32) not null unique,
name varchar(255) not null,
description varchar(255) not null,
content blob not null,
content_type varchar(255) not null
);create table transfer_history (
ts timestamp not null default current_timestamp,
sender varchar(255) not null,
recipient varchar(255) not null,
token_id integer not null
);create index transfer_history_token_id_idx on transfer_history(token_id);`
_, err := sqliteDatabase.Exec(initSQL)
if err != nil {
log.Fatalf("Can not init database: %v", err)
}
}
func get_events() {
contract := common.HexToAddress(*contractAddress)
query := ethereum.FilterQuery{
Addresses: []common.Address{contract},
}
logs := make(chan types.Log)
sub, err := ethClient.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
log.Fatalf("Can not get subscriber: %v", err)
}
logTransferSig := []byte("Transfer(address,address,uint256)")
logTransferSigHash := crypto.Keccak256Hash(logTransferSig)
for {
select {
case err := <-sub.Err():
log.Println(err)
case vLog := <-logs:
switch vLog.Topics[0].Hex() {
case logTransferSigHash.Hex():
log.Println("Log Name: Transfer")
log.Printf("From: %s", common.HexToAddress(vLog.Topics[1].Hex()))
log.Printf("To: %s", common.HexToAddress(vLog.Topics[2].Hex()))
log.Printf("Token: %s", vLog.Topics[3].Big().String())
log.Printf("Tx Hash: %s", vLog.TxHash.Hex())
insertSQL := `insert into transfer_history (sender, recipient, token_id) values (?, ?, ?)`
if stmt, err := sqliteDatabase.Prepare(insertSQL); err == nil {
defer stmt.Close()
result, err := stmt.Exec(
common.HexToAddress(vLog.Topics[1].Hex()).Hex(),
common.HexToAddress(vLog.Topics[2].Hex()).Hex(),
vLog.Topics[3].Big().Int64(),
)
log.Printf("Insert result: %v", result)
log.Printf("Insert err: %v", err)
}
log.Printf("Prepare insert err: %v", err)
default:
log.Println(vLog)
}
}
}
}
func index_transactions() {
// @note: infinite loop
for {
stmt, err := sqliteDatabase.Prepare("select max(block) from ethtxs")
defer stmt.Close()
startblock := big.NewInt(1).Uint64()
if err == nil {
_ = stmt.QueryRow().Scan(&startblock)
}
endblock, err := ethClient.BlockNumber(context.Background())
if err != nil {
endblock = big.NewInt(1).Uint64()
}
log.Printf("Current best block in index: %v; Current best block in index: %v", startblock, endblock)
for i := startblock+1; i < endblock; i++ {
block, err := ethClient.BlockByNumber(context.Background(), big.NewInt(int64(i)))
if err == nil {
for _, transaction := range block.Transactions() {
message, err := transaction.AsMessage(types.LatestSignerForChainID(transaction.ChainId()), nil)
if err == nil {
log.Printf("mess val: %v, %T", message.Value(), message.Value())
log.Printf("mess is_fake: %v", message.IsFake())
log.Printf("mess data: %v", message.Data())
log.Printf("From: %v", message.From().Hex())
log.Printf("To: %v", message.To())
to := message.To()
var toHex string = ""
if to != nil {
toHex = to.Hex()
}
insertSQL := `insert into ethtxs (time, sender, recipient, gas, gasprice, block, txhash, value) values (?, ?, ?, ?, ?, ?, ?, ?)`
insert, err := sqliteDatabase.Prepare(insertSQL)
defer insert.Close()
if err == nil {
val, _ := weiToEther(message.Value()).Float64()
_, err = insert.Exec(
block.Time(),
message.From().Hex(),
toHex,
message.Gas(),
message.GasPrice().Uint64(),
block.NumberU64(),
transaction.Hash().Hex(),
val,
)
if err != nil {
log.Printf("Error insert record: %v", err)
}
}
}
}
}
}
time.Sleep(20 * time.Second)
}
}
func main() {
// @note: set auto logoff to 5 minutes of idle