Skip to content

Commit

Permalink
Merge pull request #5 from spacemeshos/fix-address-casing-issue
Browse files Browse the repository at this point in the history
Fix an account address casing issue
  • Loading branch information
AndrewAR2 authored Apr 16, 2021
2 parents 61a7eb0 + 3165df1 commit 2eda184
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 39 deletions.
18 changes: 11 additions & 7 deletions api/httpserver/rest/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"bytes"
"errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/spacemeshos/explorer-backend/model"
)

func (s *Service) AccountsHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -45,7 +48,7 @@ func (s *Service) AccountsHandler(w http.ResponseWriter, r *http.Request) {
}},
})
data = append(data, bson.D{
account[0],
{"address", model.ToCheckedAddress(address)},
account[1],
account[2],
{"sent", sent},
Expand All @@ -57,7 +60,7 @@ func (s *Service) AccountsHandler(w http.ResponseWriter, r *http.Request) {
})
} else {
data = append(data, bson.D{
account[0],
{"address", model.ToCheckedAddress(address)},
account[1],
account[2],
{"sent", uint64(0)},
Expand Down Expand Up @@ -97,7 +100,7 @@ func (s *Service) AccountHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
filter := &bson.D{{"address", idStr}}

buf.WriteByte('{')
Expand Down Expand Up @@ -126,7 +129,7 @@ func (s *Service) AccountHandler(w http.ResponseWriter, r *http.Request) {
}},
})
data = append(data, bson.D{
account[0],
{"address", model.ToCheckedAddress(address)},
account[1],
account[2],
{"sent", sent},
Expand All @@ -138,7 +141,7 @@ func (s *Service) AccountHandler(w http.ResponseWriter, r *http.Request) {
})
} else {
data = append(data, bson.D{
account[0],
{"address", model.ToCheckedAddress(address)},
account[1],
account[2],
{"sent", uint64(0)},
Expand Down Expand Up @@ -178,7 +181,7 @@ func (s *Service) AccountRewardsHandler(w http.ResponseWriter, r *http.Request)
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])

filter := &bson.D{{"coinbase", idStr}}

Expand Down Expand Up @@ -217,7 +220,7 @@ func (s *Service) AccountTransactionsHandler(w http.ResponseWriter, r *http.Requ
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])

filter := &bson.D{
{"$or", bson.A{
Expand All @@ -233,6 +236,7 @@ func (s *Service) AccountTransactionsHandler(w http.ResponseWriter, r *http.Requ
data, err := s.storage.GetTransactions(s.ctx, filter, options.Find().SetSort(bson.D{{"counter", -1}}).SetLimit(pageSize).SetSkip((pageNumber - 1) * pageSize).SetProjection(bson.D{{"_id", 0}}))
if err != nil {
}
fixCheckedAddress(data, []int{17, 18})
setDataInfo(buf, data)
} else {
setDataInfo(buf, nil)
Expand Down
3 changes: 2 additions & 1 deletion api/httpserver/rest/activations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *Service) ActivationHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
filter := &bson.D{{"id", idStr}}

buf.WriteByte('{')
Expand Down
3 changes: 2 additions & 1 deletion api/httpserver/rest/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
// "errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *Service) AppHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
filter := &bson.D{{"address", idStr}}

buf.WriteByte('{')
Expand Down
3 changes: 2 additions & 1 deletion api/httpserver/rest/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *Service) BlockHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
filter := &bson.D{{"id", idStr}}

buf.WriteByte('{')
Expand Down
15 changes: 15 additions & 0 deletions api/httpserver/rest/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/spacemeshos/explorer-backend/model"
)
/*
200:
Expand Down Expand Up @@ -405,3 +407,16 @@ func setDataInfo(buf *bytes.Buffer, data []bson.D) error {
buf.WriteByte(']')
return nil
}

func fixCheckedAddress(data []bson.D, positions []int) error {
if data != nil {
for i, _ := range data {
for _, j := range positions {
if address, ok := data[i][j].Value.(string); ok {
data[i][j].Value = model.ToCheckedAddress(address)
}
}
}
}
return nil
}
3 changes: 2 additions & 1 deletion api/httpserver/rest/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (s *Service) RewardHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
id, err := primitive.ObjectIDFromHex(idStr);
if err != nil {
}
Expand Down
3 changes: 2 additions & 1 deletion api/httpserver/rest/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -18,7 +19,7 @@ func (s *Service) SearchHandler(w http.ResponseWriter, r *http.Request) {
buf.WriteByte('{')

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])

switch len(idStr) {
case 42:
Expand Down
7 changes: 4 additions & 3 deletions api/httpserver/rest/smeshers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -65,7 +66,7 @@ func (s *Service) SmesherHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
filter := &bson.D{{"id", idStr}}

buf.WriteByte('{')
Expand Down Expand Up @@ -112,7 +113,7 @@ func (s *Service) SmesherRewardsHandler(w http.ResponseWriter, r *http.Request)
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])

filter := &bson.D{{"smesher", idStr}}

Expand Down Expand Up @@ -151,7 +152,7 @@ func (s *Service) SmesherAtxsHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])
filter := &bson.D{{"smesher", idStr}}

buf.WriteByte('{')
Expand Down
3 changes: 2 additions & 1 deletion api/httpserver/rest/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"net/http"
"strings"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *Service) TransactionHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
idStr := vars["id"]
idStr := strings.ToLower(vars["id"])

filter := &bson.D{{"id", idStr}}

Expand Down
28 changes: 28 additions & 0 deletions model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spacemeshos/explorer-backend/utils"

"github.com/spacemeshos/go-spacemesh/crypto/sha3"
)

type Account struct {
Expand Down Expand Up @@ -34,3 +36,29 @@ func NewAccount(in *pb.Account) *Account {
}
}

// Hex returns an EIP55-compliant hex string representation of the address.
func ToCheckedAddress(a string) string {
if len(a) != 42 || a[0] != '0' || a[1] != 'x' {
return ""
}
unchecksummed := make([]byte, 40)
copy(unchecksummed, a[2:])
sha := sha3.NewKeccak256()
sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)

result := []byte(unchecksummed)
for i := 0; i < len(result); i++ {
hashByte := hash[i/2]
if i%2 == 0 {
hashByte = hashByte >> 4
} else {
hashByte &= 0xf
}
if result[i] > '9' && hashByte > 7 {
result[i] -= 32
}
}
return "0x" + string(result)
}

26 changes: 3 additions & 23 deletions utils/bytes-to-string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@ package utils

import (
"encoding/hex"
"github.com/spacemeshos/go-spacemesh/crypto/sha3"
"github.com/spacemeshos/go-spacemesh/common/util"
)

// Hex returns an EIP55-compliant hex string representation of the address.
func BytesToAddressString(a []byte) string {
unchecksummed := hex.EncodeToString(a[:])
sha := sha3.NewKeccak256()
sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)

result := []byte(unchecksummed)
for i := 0; i < len(result); i++ {
hashByte := hash[i/2]
if i%2 == 0 {
hashByte = hashByte >> 4
} else {
hashByte &= 0xf
}
if result[i] > '9' && hashByte > 7 {
result[i] -= 32
}
}
return "0x" + string(result)
return "0x" + hex.EncodeToString(a[:])
}

func BytesToHex(a []byte) string { return util.Encode(a[:]) }
func BytesToHex(a []byte) string { return hex.EncodeToString(a[:]) }

func NBytesToHex(a []byte, n int) string { return util.Encode(a[:n]) }
func NBytesToHex(a []byte, n int) string { return hex.EncodeToString(a[:n]) }

0 comments on commit 2eda184

Please sign in to comment.