Skip to content

Commit 31a6b1d

Browse files
authored
Fixes for check by address and metadata (#8)
* Fixes for check by address and metadata * More documentation
1 parent 74f9e3a commit 31a6b1d

5 files changed

Lines changed: 149 additions & 27 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/ethereum/go-ethereum v1.12.0
77
github.com/stretchr/testify v1.8.4
8+
go.mongodb.org/mongo-driver v1.12.0
89
)
910

1011
require (

go.sum

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,55 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/ethereum/go-ethereum v1.12.0 h1:bdnhLPtqETd4m3mS8BGMNvBTf36bO5bx/hxE2zljOa0=
44
github.com/ethereum/go-ethereum v1.12.0/go.mod h1:/oo2X/dZLJjf2mJ6YT9wcWxa4nNJDBKDBU6sFIpx1Gs=
5+
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
6+
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
7+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
8+
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
9+
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
510
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
611
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
712
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
813
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
14+
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
15+
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
16+
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
17+
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
18+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
19+
go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE=
20+
go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0=
21+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
22+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
23+
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
924
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
1025
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
26+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
27+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
28+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
29+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
30+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
31+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
32+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
33+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
34+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
35+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
36+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
38+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1139
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
1240
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
42+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
43+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
44+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
45+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
46+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
47+
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
48+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
49+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
50+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
51+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
52+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
53+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1354
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1455
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1556
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

methods_check_by_address.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package sourcify
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"net/http"
8+
"strings"
79

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

68+
// CheckContractAddressMore represents the contract address and associated chain IDs and statuses.
69+
type CheckContractAddressMore struct {
70+
Address common.Address `json:"address"` // The contract address.
71+
Info []CheckContractAddressMoreInfo `json:"chainIds"` // The chain ID.
72+
}
73+
74+
// CheckContractAddressMoreInfo represents the contract address and associated chain IDs and statuses.
75+
type CheckContractAddressMoreInfo struct {
76+
Status string `json:"status"` // The status of the contract.
77+
ChainID string `json:"chainId"` // The chain ID.
78+
}
79+
6680
// CheckContractByAddresses retrieves the available verified contract addresses for the given chain ID.
6781
func CheckContractByAddresses(client *Client, addresses []string, chainIds []int, matchType MethodMatchType) ([]*CheckContractAddress, error) {
6882
var method Method
@@ -101,8 +115,31 @@ func CheckContractByAddresses(client *Client, addresses []string, chainIds []int
101115
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
102116
}
103117

118+
body, err := io.ReadAll(response)
119+
if err != nil {
120+
return nil, err
121+
}
122+
104123
var toReturn []*CheckContractAddress
105-
if err := json.NewDecoder(response).Decode(&toReturn); err != nil {
124+
if err := json.Unmarshal(body, &toReturn); err != nil {
125+
if strings.Contains(err.Error(), "cannot unmarshal object into Go struct field CheckContractAddress.chainIds") {
126+
var toReturnMore []*CheckContractAddressMore
127+
if err := json.Unmarshal(body, &toReturnMore); err != nil {
128+
return nil, err
129+
}
130+
131+
for _, v := range toReturnMore {
132+
for _, info := range v.Info {
133+
toReturn = append(toReturn, &CheckContractAddress{
134+
Address: v.Address,
135+
Status: info.Status,
136+
ChainIDs: []string{info.ChainID},
137+
})
138+
}
139+
}
140+
141+
return toReturn, nil
142+
}
106143
return nil, err
107144
}
108145

methods_metadata.go

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sourcify
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"net/http"
78

89
"github.com/ethereum/go-ethereum/common"
@@ -74,9 +75,7 @@ type Settings struct {
7475
}
7576

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

8180
// Libraries represent the libraries used in the source code.
8281
type Libraries struct {
@@ -94,13 +93,12 @@ type Optimizer struct {
9493
}
9594

9695
// Sources provides details about the source code.
97-
type Sources struct {
98-
BrowserStakehavensSol BrowserStakehavensSol `json:"browser/Stakehavens.sol"` // Details about the main contract source code
99-
}
96+
type Sources map[string]SourceDetails
10097

101-
// BrowserStakehavensSol holds the details of the main contract source code.
102-
type BrowserStakehavensSol struct {
98+
// SourceDetails holds the details of the main contract source code.
99+
type SourceDetails struct {
103100
Keccak256 string `json:"keccak256"` // Hash of the source code
101+
License string `json:"license"` // License of the source code
104102
Urls []string `json:"urls"` // URLs of the source code
105103
}
106104

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

154152
return &toReturn, nil
155153
}
154+
155+
// GetContractMetadataAsBytes retrieves the metadata of a smart contract as a byte slice.
156+
// The client parameter is a pointer to a Client instance used to make the API call.
157+
// The chainId parameter is the ID of the blockchain network where the smart contract resides.
158+
// The contract parameter is the address of the smart contract.
159+
// The matchType parameter determines the type of method matching to use when retrieving the contract metadata.
160+
// 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.
161+
//
162+
// The MethodMatchType enum is used to determine the type of method matching to use:
163+
// - MethodMatchTypeFull: Use full method matching. This will only return a match if the entire method signature matches.
164+
// - MethodMatchTypePartial: Use partial method matching. This will return a match if any part of the method signature matches.
165+
// - MethodMatchTypeAny: This match type is not implemented and will return an error.
166+
//
167+
// This function will send a request to the API endpoint specified in the client parameter, using the method determined by the matchType parameter.
168+
// The method will be set with the chainId, contract address, and file path parameters.
169+
// If the method fails verification, an error will be returned.
170+
// If the API call is successful, the response body will be read and returned as a byte slice.
171+
// If the status code of the response is not 200 OK, an error will be returned.
172+
func GetContractMetadataAsBytes(client *Client, chainId int, contract common.Address, matchType MethodMatchType) ([]byte, error) {
173+
var method Method
174+
175+
switch matchType {
176+
case MethodMatchTypeFull:
177+
method = MethodGetFileFromRepositoryFullMatch
178+
case MethodMatchTypePartial:
179+
method = MethodGetFileFromRepositoryPartialMatch
180+
case MethodMatchTypeAny:
181+
return nil, fmt.Errorf("type: %s is not implemented", matchType)
182+
default:
183+
return nil, fmt.Errorf("invalid match type: %s", matchType)
184+
}
185+
186+
method.SetParams(
187+
MethodParam{Key: ":chain", Value: chainId},
188+
MethodParam{Key: ":address", Value: contract.Hex()},
189+
MethodParam{Key: ":filePath", Value: "metadata.json"},
190+
)
191+
192+
if err := method.Verify(); err != nil {
193+
return nil, err
194+
}
195+
196+
response, statusCode, err := client.CallMethod(method)
197+
if err != nil {
198+
return nil, err
199+
}
200+
201+
// Close the io.ReadCloser interface.
202+
// This is important as CallMethod is NOT closing the response body!
203+
// You'll have memory leaks if you don't do this!
204+
defer response.Close()
205+
206+
if statusCode != http.StatusOK {
207+
return nil, fmt.Errorf("unexpected status code: %d", statusCode)
208+
}
209+
210+
body, err := io.ReadAll(response)
211+
if err != nil {
212+
return nil, err
213+
}
214+
215+
return body, nil
216+
}

methods_metadata_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ func TestGetContractMetadata(t *testing.T) {
3333
},
3434
},
3535
},
36-
Settings: Settings{
37-
CompilationTarget: CompilationTarget{BrowserStakehavensSol: "browser/Stakehavens.sol"},
38-
},
39-
Sources: Sources{
40-
BrowserStakehavensSol: BrowserStakehavensSol{
41-
Keccak256: "0xabcdef123456",
42-
Urls: []string{"https://example.com/browser/Stakehavens.sol"},
43-
},
44-
},
4536
Version: 1,
4637
}
4738

@@ -87,15 +78,6 @@ func TestGetContractMetadata(t *testing.T) {
8778
},
8879
},
8980
},
90-
Settings: Settings{
91-
CompilationTarget: CompilationTarget{BrowserStakehavensSol: "browser/Stakehavens.sol"},
92-
},
93-
Sources: Sources{
94-
BrowserStakehavensSol: BrowserStakehavensSol{
95-
Keccak256: "0xabcdef123456",
96-
Urls: []string{"https://example.com/browser/Stakehavens.sol"},
97-
},
98-
},
9981
Version: 1,
10082
}
10183

0 commit comments

Comments
 (0)