Skip to content

Commit

Permalink
fix: edge case + optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
BastienFaivre committed Dec 23, 2024
1 parent c9a2855 commit ec77558
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 106 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ The library is documented using GoDoc, you can find the documentation [here](htt

## Features

The library is still in development and not all features are implemented yet

| Method | Implemented |
| --- | --- |
| `GetAddressFromStarkName` ||
Expand Down
169 changes: 76 additions & 93 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,10 +1496,12 @@ func (p *Provider) GetProfileData(
if len(result) < i+int(length)+1 {
return types.StarkProfile{}, fmt.Errorf("unexpected result")
}
if length < 2 {
if length == 0 {
return types.StarkProfile{}, fmt.Errorf("unexpected result")
}
profile.Name = utils.DecodeDomain(result[i+2 : i+1+int(length)])
if length >= 2 {
profile.Name = utils.DecodeDomain(result[i+2 : i+1+int(length)])
}
i += int(length) + 1

// id
Expand Down Expand Up @@ -1611,59 +1613,22 @@ func (p *Provider) GetProfileData(
url := fmt.Sprintf("https://identicon.starknet.id/%d", profile.Id)
profile.ProfilePicture = &url
} else if strings.Contains(metadata, "base64") {
parts := strings.Split(metadata, ",")
if len(parts) < 2 {
return types.StarkProfile{}, fmt.Errorf("invalid metadata format")
}
base64Data := strings.TrimSuffix(parts[1], ",")
decodedData, err := base64.StdEncoding.DecodeString(base64Data)
image, err := getImageFromBase64Metadata(metadata)
if err != nil {
return types.StarkProfile{}, fmt.Errorf(
"error decoding base64: %w",
err,
)
}
var result map[string]interface{}
if err := json.Unmarshal(decodedData, &result); err != nil {
return types.StarkProfile{}, fmt.Errorf(
"error parsing JSON: %w",
"failed to get image from base64 metadata: %w",
err,
)
}
image, ok := result["image"].(string)
if !ok {
return types.StarkProfile{}, fmt.Errorf(
"image field not found or not a string",
)
}
profile.ProfilePicture = &image
} else {
metadata = strings.Replace(
metadata,
"ipfs://", "https://gateway.pinata.cloud/ipfs/",
1,
)
resp, err := http.Get(metadata)
image, err := fetchImage(metadata)
if err != nil {
return types.StarkProfile{}, fmt.Errorf(
"failed to fetch metadata: %w",
err,
)
}
defer resp.Body.Close()
var content interface{}
if err := json.NewDecoder(resp.Body).Decode(&content); err != nil {
return types.StarkProfile{}, fmt.Errorf(
"failed to decode metadata: %w",
"failed to get image from url metadata: %w",
err,
)
}
image, ok := content.(map[string]interface{})["image"].(string)
if !ok {
return types.StarkProfile{}, fmt.Errorf(
"image field not found or not a string",
)
}
profile.ProfilePicture = &image
}

Expand Down Expand Up @@ -1850,13 +1815,6 @@ func (p *Provider) GetStarkProfiles(
)
}

_ = identityContractAddress
_ = pfpVerifierContractAddress
_ = utilsMulticallContractAddress
_ = blobbertContractAddress
_ = nftPpContractFelt
_ = nftPpIdFelt

const NB_INSTRUCTIONS = 5

callData := []*felt.Felt{
Expand Down Expand Up @@ -2020,10 +1978,12 @@ func (p *Provider) GetStarkProfiles(
if len(result) < i+int(length)+1 {
return nil, fmt.Errorf("unexpected result")
}
if length < 2 {
if length == 0 {
return nil, fmt.Errorf("unexpected result")
}
profiles[j].Name = utils.DecodeDomain(result[i+2 : i+1+int(length)])
if length >= 2 {
profiles[j].Name = utils.DecodeDomain(result[i+2 : i+1+int(length)])
}
i += int(length) + 1

// id
Expand Down Expand Up @@ -2092,59 +2052,22 @@ func (p *Provider) GetStarkProfiles(
url := fmt.Sprintf("https://identicon.starknet.id/%d", profiles[j].Id)
profiles[j].ProfilePicture = &url
} else if strings.Contains(metadata, "base64") {
parts := strings.Split(metadata, ",")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid metadata format")
}
base64Data := strings.TrimSuffix(parts[1], ",")
decodedData, err := base64.StdEncoding.DecodeString(base64Data)
image, err := getImageFromBase64Metadata(metadata)
if err != nil {
return nil, fmt.Errorf(
"error decoding base64: %w",
"failed to get image from base64 metadata: %w",
err,
)
}
var result map[string]interface{}
if err := json.Unmarshal(decodedData, &result); err != nil {
return nil, fmt.Errorf(
"error parsing JSON: %w",
err,
)
}
image, ok := result["image"].(string)
if !ok {
return nil, fmt.Errorf(
"image field not found or not a string",
)
}
profiles[j].ProfilePicture = &image
} else {
metadata = strings.Replace(
metadata,
"ipfs://", "https://gateway.pinata.cloud/ipfs/",
1,
)
resp, err := http.Get(metadata)
image, err := fetchImage(metadata)
if err != nil {
return nil, fmt.Errorf(
"failed to fetch metadata: %w",
"failed to get image from url metadata: %w",
err,
)
}
defer resp.Body.Close()
var content interface{}
if err := json.NewDecoder(resp.Body).Decode(&content); err != nil {
return nil, fmt.Errorf(
"failed to decode metadata: %w",
err,
)
}
image, ok := content.(map[string]interface{})["image"].(string)
if !ok {
return nil, fmt.Errorf(
"image field not found or not a string",
)
}
profiles[j].ProfilePicture = &image
}
}
Expand Down Expand Up @@ -2304,3 +2227,63 @@ func (p *Provider) checkArguments(
return nil, fmt.Errorf("invalid idDomainOrAddr")
}
}

// getImageFromBase64Metadata gets the image from a base64 metadata.
//
// Parameters:
// - metadata: the base64 metadata.
//
// Returns:
// - string: the image.
// - error: an error if the image could not be fetched.
func getImageFromBase64Metadata(metadata string) (string, error) {
parts := strings.Split(metadata, ",")
if len(parts) < 2 {
return "", fmt.Errorf("invalid metadata format")
}
base64Data := strings.TrimSuffix(parts[1], ",")
decodedData, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return "", fmt.Errorf("error decoding base64: %w", err)
}
var result map[string]interface{}
if err := json.Unmarshal(decodedData, &result); err != nil {
return "", fmt.Errorf("error parsing JSON: %w", err)
}
image, ok := result["image"].(string)
if !ok {
return "", fmt.Errorf("image field not found or not a string")
}
return image, nil
}

// fetchImage fetches an image from a URL.
//
// Parameters:
// - url: the URL.
//
// Returns:
// - string: the image.
// - error: an error if the image could not be fetched.
func fetchImage(url string) (string, error) {
url = strings.Replace(
url,
"ipfs://",
"https://gateway.pinata.cloud/ipfs/",
1,
)
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("failed to fetch image: %w", err)
}
defer resp.Body.Close()
var content interface{}
if err := json.NewDecoder(resp.Body).Decode(&content); err != nil {
return "", fmt.Errorf("failed to decode image: %w", err)
}
image, ok := content.(map[string]interface{})["image"].(string)
if !ok {
return "", fmt.Errorf("image field not found or not a string")
}
return image, nil
}
64 changes: 53 additions & 11 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,39 @@ func TestGetProfileData(t *testing.T) {
*profileData.ProfilePicture,
)
}

profileData, err = p.GetProfileData(
context.Background(),
"0x123",
true,
nil,
nil,
nil,
)
if err != nil {
t.Error(err)
}
if profileData.Name != "" {
t.Errorf("Expected empty string but got %s", profileData.Name)
}
if profileData.Id != 0 {
t.Errorf("Expected 0 but got %d", profileData.Id)
}
if profileData.ProfilePicture != nil {
t.Error("Expected nil but got profile picture")
}
if profileData.Discord != nil {
t.Error("Expected nil but got discord")
}
if profileData.Twitter != nil {
t.Error("Expected nil but got twitter")
}
if profileData.Github != nil {
t.Error("Expected nil but got github")
}
if profileData.ProofOfPersonhood {
t.Error("Expected false but got true")
}
}

func TestGetStarkProfiles(t *testing.T) {
Expand All @@ -475,15 +508,15 @@ func TestGetStarkProfiles(t *testing.T) {

starkProfiles, err := p.GetStarkProfiles(
context.Background(),
[]string{BASTVRE_ADDRESS, METACUBE_ADDRESS, FRICOBEN_ADDRESS},
[]string{BASTVRE_ADDRESS, "0x123", METACUBE_ADDRESS, FRICOBEN_ADDRESS},
true,
nil,
)
if err != nil {
t.Error(err)
}
if len(starkProfiles) != 3 {
t.Errorf("Expected 3 but got %d", len(starkProfiles))
if len(starkProfiles) != 4 {
t.Errorf("Expected 4 but got %d", len(starkProfiles))
}
if starkProfiles[0].Name != BASTVRE_DOMAIN {
t.Errorf("Expected %s but got %s", BASTVRE_DOMAIN, starkProfiles[0].Name)
Expand All @@ -494,28 +527,37 @@ func TestGetStarkProfiles(t *testing.T) {
if starkProfiles[0].ProfilePicture != nil {
t.Errorf("Expected nil but got %s", *starkProfiles[0].ProfilePicture)
}
if starkProfiles[1].Name != METACUBE_DOMAIN {
if starkProfiles[1].Name != "" {
t.Errorf("Expected empty string but got %s", starkProfiles[1].Name)
}
if starkProfiles[1].Id != 0 {
t.Errorf("Expected 0 but got %d", starkProfiles[1].Id)
}
if starkProfiles[1].ProfilePicture != nil {
t.Errorf("Expected nil but got %s", *starkProfiles[1].ProfilePicture)
}
if starkProfiles[2].Name != METACUBE_DOMAIN {
t.Errorf("Expected %s but got %s", METACUBE_DOMAIN, starkProfiles[1].Name)
}
if starkProfiles[1].Id != 899148099505 {
if starkProfiles[2].Id != 899148099505 {
t.Errorf("Expected 899148099505 but got %d", starkProfiles[1].Id)
}
if starkProfiles[1].ProfilePicture != nil {
if starkProfiles[2].ProfilePicture != nil {
t.Errorf("Expected nil but got %s", *starkProfiles[1].ProfilePicture)
}
if starkProfiles[2].Name != FRICOBEN_DOMAIN {
if starkProfiles[3].Name != FRICOBEN_DOMAIN {
t.Errorf("Expected %s but got %s", FRICOBEN_DOMAIN, starkProfiles[2].Name)
}
if starkProfiles[2].Id != 8 {
if starkProfiles[3].Id != 8 {
t.Errorf("Expected 8 but got %d", starkProfiles[2].Id)
}
if starkProfiles[2].ProfilePicture == nil {
if starkProfiles[3].ProfilePicture == nil {
t.Error("Expected profile picture but got nil")
}
if *starkProfiles[2].ProfilePicture != "https://img.starkurabu.com/41584010289889200780326579696828426.png" {
if *starkProfiles[3].ProfilePicture != "https://img.starkurabu.com/41584010289889200780326579696828426.png" {
t.Errorf(
"Expected https://img.starkurabu.com/41584010289889200780326579696828426.png but got %s",
*starkProfiles[2].ProfilePicture,
*starkProfiles[3].ProfilePicture,
)
}
}

0 comments on commit ec77558

Please sign in to comment.