Skip to content

Commit

Permalink
Fixes for check by address and metadata (#8)
Browse files Browse the repository at this point in the history
* Fixes for check by address and metadata

* More documentation
  • Loading branch information
0x19 authored Jun 25, 2023
1 parent 74f9e3a commit 31a6b1d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 27 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/ethereum/go-ethereum v1.12.0
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.12.0
)

require (
Expand Down
41 changes: 41 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,55 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ethereum/go-ethereum v1.12.0 h1:bdnhLPtqETd4m3mS8BGMNvBTf36bO5bx/hxE2zljOa0=
github.com/ethereum/go-ethereum v1.12.0/go.mod h1:/oo2X/dZLJjf2mJ6YT9wcWxa4nNJDBKDBU6sFIpx1Gs=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE=
go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
39 changes: 38 additions & 1 deletion methods_check_by_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package sourcify
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -63,6 +65,18 @@ type CheckContractAddress struct {
ChainIDs []string `json:"chainIds"` // The chain ID.
}

// CheckContractAddressMore represents the contract address and associated chain IDs and statuses.
type CheckContractAddressMore struct {
Address common.Address `json:"address"` // The contract address.
Info []CheckContractAddressMoreInfo `json:"chainIds"` // The chain ID.
}

// CheckContractAddressMoreInfo represents the contract address and associated chain IDs and statuses.
type CheckContractAddressMoreInfo struct {
Status string `json:"status"` // The status of the contract.
ChainID string `json:"chainId"` // The chain ID.
}

// CheckContractByAddresses retrieves the available verified contract addresses for the given chain ID.
func CheckContractByAddresses(client *Client, addresses []string, chainIds []int, matchType MethodMatchType) ([]*CheckContractAddress, error) {
var method Method
Expand Down Expand Up @@ -101,8 +115,31 @@ func CheckContractByAddresses(client *Client, addresses []string, chainIds []int
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}

body, err := io.ReadAll(response)
if err != nil {
return nil, err
}

var toReturn []*CheckContractAddress
if err := json.NewDecoder(response).Decode(&toReturn); err != nil {
if err := json.Unmarshal(body, &toReturn); err != nil {
if strings.Contains(err.Error(), "cannot unmarshal object into Go struct field CheckContractAddress.chainIds") {
var toReturnMore []*CheckContractAddressMore
if err := json.Unmarshal(body, &toReturnMore); err != nil {
return nil, err
}

for _, v := range toReturnMore {
for _, info := range v.Info {
toReturn = append(toReturn, &CheckContractAddress{
Address: v.Address,
Status: info.Status,
ChainIDs: []string{info.ChainID},
})
}
}

return toReturn, nil
}
return nil, err
}

Expand Down
77 changes: 69 additions & 8 deletions methods_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sourcify
import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -74,9 +75,7 @@ type Settings struct {
}

// CompilationTarget holds the details of the compilation target.
type CompilationTarget struct {
BrowserStakehavensSol string `json:"browser/Stakehavens.sol"` // Path of the main contract in browser
}
type CompilationTarget map[string]string

// Libraries represent the libraries used in the source code.
type Libraries struct {
Expand All @@ -94,13 +93,12 @@ type Optimizer struct {
}

// Sources provides details about the source code.
type Sources struct {
BrowserStakehavensSol BrowserStakehavensSol `json:"browser/Stakehavens.sol"` // Details about the main contract source code
}
type Sources map[string]SourceDetails

// BrowserStakehavensSol holds the details of the main contract source code.
type BrowserStakehavensSol struct {
// SourceDetails holds the details of the main contract source code.
type SourceDetails struct {
Keccak256 string `json:"keccak256"` // Hash of the source code
License string `json:"license"` // License of the source code
Urls []string `json:"urls"` // URLs of the source code
}

Expand Down Expand Up @@ -153,3 +151,66 @@ func GetContractMetadata(client *Client, chainId int, contract common.Address, m

return &toReturn, nil
}

// GetContractMetadataAsBytes retrieves the metadata of a smart contract as a byte slice.
// The client parameter is a pointer to a Client instance used to make the API call.
// The chainId parameter is the ID of the blockchain network where the smart contract resides.
// The contract parameter is the address of the smart contract.
// The matchType parameter determines the type of method matching to use when retrieving the contract metadata.
// It returns a byte slice containing the contract metadata, or an error if there was an issue sending the request, decoding the response, or if an invalid matchType was provided.
//
// The MethodMatchType enum is used to determine the type of method matching to use:
// - MethodMatchTypeFull: Use full method matching. This will only return a match if the entire method signature matches.
// - MethodMatchTypePartial: Use partial method matching. This will return a match if any part of the method signature matches.
// - MethodMatchTypeAny: This match type is not implemented and will return an error.
//
// This function will send a request to the API endpoint specified in the client parameter, using the method determined by the matchType parameter.
// The method will be set with the chainId, contract address, and file path parameters.
// If the method fails verification, an error will be returned.
// If the API call is successful, the response body will be read and returned as a byte slice.
// If the status code of the response is not 200 OK, an error will be returned.
func GetContractMetadataAsBytes(client *Client, chainId int, contract common.Address, matchType MethodMatchType) ([]byte, error) {
var method Method

switch matchType {
case MethodMatchTypeFull:
method = MethodGetFileFromRepositoryFullMatch
case MethodMatchTypePartial:
method = MethodGetFileFromRepositoryPartialMatch
case MethodMatchTypeAny:
return nil, fmt.Errorf("type: %s is not implemented", matchType)
default:
return nil, fmt.Errorf("invalid match type: %s", matchType)
}

method.SetParams(
MethodParam{Key: ":chain", Value: chainId},
MethodParam{Key: ":address", Value: contract.Hex()},
MethodParam{Key: ":filePath", Value: "metadata.json"},
)

if err := method.Verify(); err != nil {
return nil, err
}

response, statusCode, err := client.CallMethod(method)
if err != nil {
return nil, err
}

// Close the io.ReadCloser interface.
// This is important as CallMethod is NOT closing the response body!
// You'll have memory leaks if you don't do this!
defer response.Close()

if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
}

body, err := io.ReadAll(response)
if err != nil {
return nil, err
}

return body, nil
}
18 changes: 0 additions & 18 deletions methods_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ func TestGetContractMetadata(t *testing.T) {
},
},
},
Settings: Settings{
CompilationTarget: CompilationTarget{BrowserStakehavensSol: "browser/Stakehavens.sol"},
},
Sources: Sources{
BrowserStakehavensSol: BrowserStakehavensSol{
Keccak256: "0xabcdef123456",
Urls: []string{"https://example.com/browser/Stakehavens.sol"},
},
},
Version: 1,
}

Expand Down Expand Up @@ -87,15 +78,6 @@ func TestGetContractMetadata(t *testing.T) {
},
},
},
Settings: Settings{
CompilationTarget: CompilationTarget{BrowserStakehavensSol: "browser/Stakehavens.sol"},
},
Sources: Sources{
BrowserStakehavensSol: BrowserStakehavensSol{
Keccak256: "0xabcdef123456",
Urls: []string{"https://example.com/browser/Stakehavens.sol"},
},
},
Version: 1,
}

Expand Down

0 comments on commit 31a6b1d

Please sign in to comment.