Skip to content
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 256 additions & 0 deletions sdk/sdk-go/common/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package common

type Country3LetterCode string

const (
AFG Country3LetterCode = "AFG" // Afghanistan
ALA Country3LetterCode = "ALA" // Aland Islands
ALB Country3LetterCode = "ALB" // Albania
DZA Country3LetterCode = "DZA" // Algeria
ASM Country3LetterCode = "ASM" // American Samoa
AND Country3LetterCode = "AND" // Andorra
AGO Country3LetterCode = "AGO" // Angola
AIA Country3LetterCode = "AIA" // Anguilla
ATA Country3LetterCode = "ATA" // Antarctica
ATG Country3LetterCode = "ATG" // Antigua and Barbuda
ARG Country3LetterCode = "ARG" // Argentina
ARM Country3LetterCode = "ARM" // Armenia
ABW Country3LetterCode = "ABW" // Aruba
AUS Country3LetterCode = "AUS" // Australia
AUT Country3LetterCode = "AUT" // Austria
AZE Country3LetterCode = "AZE" // Azerbaijan
BHS Country3LetterCode = "BHS" // Bahamas
BHR Country3LetterCode = "BHR" // Bahrain
BGD Country3LetterCode = "BGD" // Bangladesh
BRB Country3LetterCode = "BRB" // Barbados
BLR Country3LetterCode = "BLR" // Belarus
BEL Country3LetterCode = "BEL" // Belgium
BLZ Country3LetterCode = "BLZ" // Belize
BEN Country3LetterCode = "BEN" // Benin
BMU Country3LetterCode = "BMU" // Bermuda
BTN Country3LetterCode = "BTN" // Bhutan
BOL Country3LetterCode = "BOL" // Bolivia (Plurinational State of)
BES Country3LetterCode = "BES" // Bonaire, Sint Eustatius and Saba
BIH Country3LetterCode = "BIH" // Bosnia and Herzegovina
BWA Country3LetterCode = "BWA" // Botswana
BVT Country3LetterCode = "BVT" // Bouvet Island
BRA Country3LetterCode = "BRA" // Brazil
IOT Country3LetterCode = "IOT" // British Indian Ocean Territory
BRN Country3LetterCode = "BRN" // Brunei Darussalam
BGR Country3LetterCode = "BGR" // Bulgaria
BFA Country3LetterCode = "BFA" // Burkina Faso
BDI Country3LetterCode = "BDI" // Burundi
CPV Country3LetterCode = "CPV" // Cabo Verde
KHM Country3LetterCode = "KHM" // Cambodia
CMR Country3LetterCode = "CMR" // Cameroon
CAN Country3LetterCode = "CAN" // Canada
CYM Country3LetterCode = "CYM" // Cayman Islands
CAF Country3LetterCode = "CAF" // Central African Republic
TCD Country3LetterCode = "TCD" // Chad
CHL Country3LetterCode = "CHL" // Chile
CHN Country3LetterCode = "CHN" // China
CXR Country3LetterCode = "CXR" // Christmas Island
CCK Country3LetterCode = "CCK" // Cocos (Keeling) Islands
COL Country3LetterCode = "COL" // Colombia
COM Country3LetterCode = "COM" // Comoros
COG Country3LetterCode = "COG" // Congo
COD Country3LetterCode = "COD" // Congo, Democratic Republic of the
COK Country3LetterCode = "COK" // Cook Islands
CRI Country3LetterCode = "CRI" // Costa Rica
CIV Country3LetterCode = "CIV" // Cote d'Ivoire
HRV Country3LetterCode = "HRV" // Croatia
CUB Country3LetterCode = "CUB" // Cuba
CUW Country3LetterCode = "CUW" // Curacao
CYP Country3LetterCode = "CYP" // Cyprus
CZE Country3LetterCode = "CZE" // Czechia
DNK Country3LetterCode = "DNK" // Denmark
DJI Country3LetterCode = "DJI" // Djibouti
DMA Country3LetterCode = "DMA" // Dominica
DOM Country3LetterCode = "DOM" // Dominican Republic
ECU Country3LetterCode = "ECU" // Ecuador
EGY Country3LetterCode = "EGY" // Egypt
SLV Country3LetterCode = "SLV" // El Salvador
GNQ Country3LetterCode = "GNQ" // Equatorial Guinea
ERI Country3LetterCode = "ERI" // Eritrea
EST Country3LetterCode = "EST" // Estonia
SWZ Country3LetterCode = "SWZ" // Eswatini
ETH Country3LetterCode = "ETH" // Ethiopia
FLK Country3LetterCode = "FLK" // Falkland Islands (Malvinas)
FRO Country3LetterCode = "FRO" // Faroe Islands
FJI Country3LetterCode = "FJI" // Fiji
FIN Country3LetterCode = "FIN" // Finland
FRA Country3LetterCode = "FRA" // France
GUF Country3LetterCode = "GUF" // French Guiana
PYF Country3LetterCode = "PYF" // French Polynesia
ATF Country3LetterCode = "ATF" // French Southern Territories
GAB Country3LetterCode = "GAB" // Gabon
GMB Country3LetterCode = "GMB" // Gambia
GEO Country3LetterCode = "GEO" // Georgia
DEU Country3LetterCode = "DEU" // Germany
D Country3LetterCode = "D<<" // Germany (Bundesrepublik Deutschland passport format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Remove anomalous non-ISO code D = "D<<" to prevent misuse and confusion

This exported constant deviates from ISO 3166-1 alpha-3 (three uppercase letters), introduces ambiguous naming, and could leak into UI/templating paths where angle brackets become problematic. If MRZ-specific tokens are needed, model them with a separate type in a dedicated MRZ module rather than mixing them into ISO country codes.

Apply this diff to remove the invalid constant:

-	D   Country3LetterCode = "D<<" // Germany (Bundesrepublik Deutschland passport format)

To proactively catch similar issues project-wide, you can verify no usages exist:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Searching for anomalous MRZ token and symbol usage..."
# 1) Direct string usage
rg -nP -C2 '"D<<"' || true
# 2) Symbol usage if imported as common.D
rg -nP -C2 '\bcommon\.D\b' || true
# 3) Sanity check: find any Country3LetterCode const values not matching ^[A-Z]{3}$
python - << 'PY'
import re, sys, pathlib
p = pathlib.Path("sdk/sdk-go/common/constants.go")
src = p.read_text(encoding="utf-8")
bad = []
for i, line in enumerate(src.splitlines(), 1):
    m = re.match(r'^\s*([A-Z0-9_]+)\s+Country3LetterCode\s*=\s*"([^"]+)"', line)
    if m:
        name, val = m.group(1), m.group(2)
        if not re.fullmatch(r'[A-Z]{3}', name) or not re.fullmatch(r'[A-Z]{3}', val):
            bad.append((i, name, val))
if bad:
    print("Found non-ISO alpha-3 entries (line, name, value):")
    for b in bad:
        print(b)
    sys.exit(1)
else:
    print("OK: All constants conform to ^[A-Z]{3}$")
PY

Length of output: 702


Remove anomalous non‑ISO constant D = "D<<" from sdk/sdk-go/common/constants.go

This constant is not a valid ISO 3166‑1 alpha‑3 code (it's an MRZ token) and should not live in the exported Country3LetterCode set — it risks confusion and templating/escaping issues.

Findings

  • File: sdk/sdk-go/common/constants.go — line 90: D Country3LetterCode = "D<<" (only occurrence found).
  • Verification: ripgrep + sanity check flagged only that line; no other references to "D<<" or common.D were found.

Apply this removal diff:

-	D   Country3LetterCode = "D<<" // Germany (Bundesrepublik Deutschland passport format)

Recommended fixes (pick one)

  • Remove the line (safe: no usages found).
  • OR move MRZ tokens into a dedicated MRZ package (e.g., sdk/sdk-go/mrz) and model them with a distinct type (e.g., type MRZToken string) and clear names like MRZ_GER = "D<<".

Quick note: since no references were found, removing or relocating this constant is a low‑risk, high‑clarity fix.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
D Country3LetterCode = "D<<" // Germany (Bundesrepublik Deutschland passport format)
🤖 Prompt for AI Agents
In sdk/sdk-go/common/constants.go around line 90, the exported
Country3LetterCode set contains an anomalous non‑ISO constant `D  
Country3LetterCode = "D<<"` which is an MRZ token and should be removed; delete
that line from the file, run go fmt/golangci-lint or your repo’s
formatting/linting, run unit tests/compile to verify no references break, and if
you prefer to keep MRZ tokens later, create a separate package (e.g.,
sdk/sdk-go/mrz) with a distinct type (type MRZToken string) and clearly named
constants like MRZ_GER = "D<<" instead of adding them to Country3LetterCode.

GHA Country3LetterCode = "GHA" // Ghana
GIB Country3LetterCode = "GIB" // Gibraltar
GRC Country3LetterCode = "GRC" // Greece
GRL Country3LetterCode = "GRL" // Greenland
GRD Country3LetterCode = "GRD" // Grenada
GLP Country3LetterCode = "GLP" // Guadeloupe
GUM Country3LetterCode = "GUM" // Guam
GTM Country3LetterCode = "GTM" // Guatemala
GGY Country3LetterCode = "GGY" // Guernsey
GIN Country3LetterCode = "GIN" // Guinea
GNB Country3LetterCode = "GNB" // Guinea-Bissau
GUY Country3LetterCode = "GUY" // Guyana
HTI Country3LetterCode = "HTI" // Haiti
HMD Country3LetterCode = "HMD" // Heard Island and McDonald Islands
VAT Country3LetterCode = "VAT" // Holy See
HND Country3LetterCode = "HND" // Honduras
HKG Country3LetterCode = "HKG" // Hong Kong
HUN Country3LetterCode = "HUN" // Hungary
ISL Country3LetterCode = "ISL" // Iceland
IND Country3LetterCode = "IND" // India
IDN Country3LetterCode = "IDN" // Indonesia
IRN Country3LetterCode = "IRN" // Iran (Islamic Republic of)
IRQ Country3LetterCode = "IRQ" // Iraq
IRL Country3LetterCode = "IRL" // Ireland
IMN Country3LetterCode = "IMN" // Isle of Man
ISR Country3LetterCode = "ISR" // Israel
ITA Country3LetterCode = "ITA" // Italy
JAM Country3LetterCode = "JAM" // Jamaica
JPN Country3LetterCode = "JPN" // Japan
JEY Country3LetterCode = "JEY" // Jersey
JOR Country3LetterCode = "JOR" // Jordan
KAZ Country3LetterCode = "KAZ" // Kazakhstan
KEN Country3LetterCode = "KEN" // Kenya
KIR Country3LetterCode = "KIR" // Kiribati
PRK Country3LetterCode = "PRK" // Korea (Democratic People's Republic of)
KOR Country3LetterCode = "KOR" // Korea, Republic of
KWT Country3LetterCode = "KWT" // Kuwait
KGZ Country3LetterCode = "KGZ" // Kyrgyzstan
LAO Country3LetterCode = "LAO" // Lao People's Democratic Republic
LVA Country3LetterCode = "LVA" // Latvia
LBN Country3LetterCode = "LBN" // Lebanon
LSO Country3LetterCode = "LSO" // Lesotho
LBR Country3LetterCode = "LBR" // Liberia
LBY Country3LetterCode = "LBY" // Libya
LIE Country3LetterCode = "LIE" // Liechtenstein
LTU Country3LetterCode = "LTU" // Lithuania
LUX Country3LetterCode = "LUX" // Luxembourg
MAC Country3LetterCode = "MAC" // Macao
MDG Country3LetterCode = "MDG" // Madagascar
MWI Country3LetterCode = "MWI" // Malawi
MYS Country3LetterCode = "MYS" // Malaysia
MDV Country3LetterCode = "MDV" // Maldives
MLI Country3LetterCode = "MLI" // Mali
MLT Country3LetterCode = "MLT" // Malta
MHL Country3LetterCode = "MHL" // Marshall Islands
MTQ Country3LetterCode = "MTQ" // Martinique
MRT Country3LetterCode = "MRT" // Mauritania
MUS Country3LetterCode = "MUS" // Mauritius
MYT Country3LetterCode = "MYT" // Mayotte
MEX Country3LetterCode = "MEX" // Mexico
FSM Country3LetterCode = "FSM" // Micronesia (Federated States of)
MDA Country3LetterCode = "MDA" // Moldova, Republic of
MCO Country3LetterCode = "MCO" // Monaco
MNG Country3LetterCode = "MNG" // Mongolia
MNE Country3LetterCode = "MNE" // Montenegro
MSR Country3LetterCode = "MSR" // Montserrat
MAR Country3LetterCode = "MAR" // Morocco
MOZ Country3LetterCode = "MOZ" // Mozambique
MMR Country3LetterCode = "MMR" // Myanmar
NAM Country3LetterCode = "NAM" // Namibia
NRU Country3LetterCode = "NRU" // Nauru
NPL Country3LetterCode = "NPL" // Nepal
NLD Country3LetterCode = "NLD" // Netherlands
NCL Country3LetterCode = "NCL" // New Caledonia
NZL Country3LetterCode = "NZL" // New Zealand
NIC Country3LetterCode = "NIC" // Nicaragua
NER Country3LetterCode = "NER" // Niger
NGA Country3LetterCode = "NGA" // Nigeria
NIU Country3LetterCode = "NIU" // Niue
NFK Country3LetterCode = "NFK" // Norfolk Island
MKD Country3LetterCode = "MKD" // North Macedonia
MNP Country3LetterCode = "MNP" // Northern Mariana Islands
NOR Country3LetterCode = "NOR" // Norway
OMN Country3LetterCode = "OMN" // Oman
PAK Country3LetterCode = "PAK" // Pakistan
PLW Country3LetterCode = "PLW" // Palau
PSE Country3LetterCode = "PSE" // Palestine, State of
PAN Country3LetterCode = "PAN" // Panama
PNG Country3LetterCode = "PNG" // Papua New Guinea
PRY Country3LetterCode = "PRY" // Paraguay
PER Country3LetterCode = "PER" // Peru
PHL Country3LetterCode = "PHL" // Philippines
PCN Country3LetterCode = "PCN" // Pitcairn
POL Country3LetterCode = "POL" // Poland
PRT Country3LetterCode = "PRT" // Portugal
PRI Country3LetterCode = "PRI" // Puerto Rico
QAT Country3LetterCode = "QAT" // Qatar
REU Country3LetterCode = "REU" // Reunion
ROU Country3LetterCode = "ROU" // Romania
RUS Country3LetterCode = "RUS" // Russian Federation
RWA Country3LetterCode = "RWA" // Rwanda
BLM Country3LetterCode = "BLM" // Saint Barthelemy
SHN Country3LetterCode = "SHN" // Saint Helena, Ascension and Tristan da Cunha
KNA Country3LetterCode = "KNA" // Saint Kitts and Nevis
LCA Country3LetterCode = "LCA" // Saint Lucia
MAF Country3LetterCode = "MAF" // Saint Martin (French part)
SPM Country3LetterCode = "SPM" // Saint Pierre and Miquelon
VCT Country3LetterCode = "VCT" // Saint Vincent and the Grenadines
WSM Country3LetterCode = "WSM" // Samoa
SMR Country3LetterCode = "SMR" // San Marino
STP Country3LetterCode = "STP" // Sao Tome and Principe
SAU Country3LetterCode = "SAU" // Saudi Arabia
SEN Country3LetterCode = "SEN" // Senegal
SRB Country3LetterCode = "SRB" // Serbia
SYC Country3LetterCode = "SYC" // Seychelles
SLE Country3LetterCode = "SLE" // Sierra Leone
SGP Country3LetterCode = "SGP" // Singapore
SXM Country3LetterCode = "SXM" // Sint Maarten (Dutch part)
SVK Country3LetterCode = "SVK" // Slovakia
SVN Country3LetterCode = "SVN" // Slovenia
SLB Country3LetterCode = "SLB" // Solomon Islands
SOM Country3LetterCode = "SOM" // Somalia
ZAF Country3LetterCode = "ZAF" // South Africa
SGS Country3LetterCode = "SGS" // South Georgia and the South Sandwich Islands
SSD Country3LetterCode = "SSD" // South Sudan
ESP Country3LetterCode = "ESP" // Spain
LKA Country3LetterCode = "LKA" // Sri Lanka
SDN Country3LetterCode = "SDN" // Sudan
SUR Country3LetterCode = "SUR" // Suriname
SJM Country3LetterCode = "SJM" // Svalbard and Jan Mayen
SWE Country3LetterCode = "SWE" // Sweden
CHE Country3LetterCode = "CHE" // Switzerland
SYR Country3LetterCode = "SYR" // Syrian Arab Republic
TWN Country3LetterCode = "TWN" // Taiwan, Province of China
TJK Country3LetterCode = "TJK" // Tajikistan
TZA Country3LetterCode = "TZA" // Tanzania, United Republic of
THA Country3LetterCode = "THA" // Thailand
TLS Country3LetterCode = "TLS" // Timor-Leste
TGO Country3LetterCode = "TGO" // Togo
TKL Country3LetterCode = "TKL" // Tokelau
TON Country3LetterCode = "TON" // Tonga
TTO Country3LetterCode = "TTO" // Trinidad and Tobago
TUN Country3LetterCode = "TUN" // Tunisia
TUR Country3LetterCode = "TUR" // Turkey
TKM Country3LetterCode = "TKM" // Turkmenistan
TCA Country3LetterCode = "TCA" // Turks and Caicos Islands
TUV Country3LetterCode = "TUV" // Tuvalu
UGA Country3LetterCode = "UGA" // Uganda
UKR Country3LetterCode = "UKR" // Ukraine
ARE Country3LetterCode = "ARE" // United Arab Emirates
GBR Country3LetterCode = "GBR" // United Kingdom of Great Britain and Northern Ireland
USA Country3LetterCode = "USA" // United States of America
UMI Country3LetterCode = "UMI" // United States Minor Outlying Islands
URY Country3LetterCode = "URY" // Uruguay
UZB Country3LetterCode = "UZB" // Uzbekistan
VUT Country3LetterCode = "VUT" // Vanuatu
VEN Country3LetterCode = "VEN" // Venezuela (Bolivarian Republic of)
VNM Country3LetterCode = "VNM" // Viet Nam
VGB Country3LetterCode = "VGB" // Virgin Islands (British)
VIR Country3LetterCode = "VIR" // Virgin Islands (U.S.)
WLF Country3LetterCode = "WLF" // Wallis and Futuna
ESH Country3LetterCode = "ESH" // Western Sahara
YEM Country3LetterCode = "YEM" // Yemen
ZMB Country3LetterCode = "ZMB" // Zambia
ZWE Country3LetterCode = "ZWE" // Zimbabwe
)
122 changes: 122 additions & 0 deletions sdk/sdk-go/common/customHash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package common

import (
"fmt"
"math/big"
"regexp"
"strings"

"github.com/iden3/go-iden3-crypto/poseidon"
)

func FlexiblePoseidon(inputs []*big.Int) (*big.Int, error) {
if len(inputs) == 0 {
return nil, fmt.Errorf("no inputs provided")
}

if len(inputs) > 16 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be > 17?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we decided not to change the circuits right? so kept it same like the circuit algo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no if you look at the typescript code it considers the switch case of 16.

see:

export function flexiblePoseidon(inputs: bigint[]): bigint {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tru tru , will change it

return nil, fmt.Errorf("unsupported number of inputs: %d", len(inputs))
}

result, err := poseidon.Hash(inputs)
if err != nil {
return nil, fmt.Errorf("poseidon hash failed: %w", err)
}

return result, nil
}

// StringToBigInt converts a string to a big.Int by treating each character as a byte
// Validates input contains only ASCII characters and doesn't exceed 31 bytes
func StringToBigInt(str string) (*big.Int, error) {
// Validate input contains only ASCII characters (0-127)
for _, char := range str {
if char > 127 {
return nil, fmt.Errorf("input must contain only ASCII characters (0-127)")
}
}

result := big.NewInt(0)
for i := 0; i < len(str); i++ {
// Shift left by 8 bits and add the character code
result.Lsh(result, 8)
result.Add(result, big.NewInt(int64(str[i])))
}

// Check size limit (31 bytes = 248 bits)
maxValue := new(big.Int)
maxValue.Lsh(big.NewInt(1), 248)
maxValue.Sub(maxValue, big.NewInt(1))

if result.Cmp(maxValue) > 0 {
return nil, fmt.Errorf("resulting BigInt exceeds maximum size of 31 bytes")
}

return result, nil
}

// FormatEndpoint removes protocol and path from URL, keeping only the domain
func FormatEndpoint(endpoint string) string {
if endpoint == "" {
return ""
}

// Remove protocol (http:// or https://)
re := regexp.MustCompile(`^https?://`)
formatted := re.ReplaceAllString(endpoint, "")

// Split by '/' and take only the first part (domain)
parts := strings.Split(formatted, "/")
return parts[0]
}
Comment on lines +59 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use url.Parse for robust endpoint normalization (remove ports, handle case-insensitive schemes), and align behavior with comment

The current regex-based approach:

  • Misses uppercase schemes (e.g., HTTPS://).
  • Retains ports despite the comment saying “domain”.
  • Doesn’t trim spaces or handle odd inputs as reliably as url.Parse.

Refactor to use net/url and trim whitespace. This also removes the need for regexp here.

Apply this diff:

-import (
-	"fmt"
-	"math/big"
-	"regexp"
-	"strings"
-
-	"github.com/iden3/go-iden3-crypto/poseidon"
-)
+import (
+	"fmt"
+	"math/big"
+	"net/url"
+	"strings"
+
+	"github.com/iden3/go-iden3-crypto/poseidon"
+)
-// FormatEndpoint removes protocol and path from URL, keeping only the domain
+// FormatEndpoint removes protocol and path from URL, keeping only the domain (without port)
 func FormatEndpoint(endpoint string) string {
-	if endpoint == "" {
-		return ""
-	}
-
-	// Remove protocol (http:// or https://)
-	re := regexp.MustCompile(`^https?://`)
-	formatted := re.ReplaceAllString(endpoint, "")
-
-	// Split by '/' and take only the first part (domain)
-	parts := strings.Split(formatted, "/")
-	return parts[0]
+	s := strings.TrimSpace(endpoint)
+	if s == "" {
+		return ""
+	}
+	// Ensure a scheme for proper parsing when the input is a bare host
+	if !strings.Contains(s, "://") {
+		s = "https://" + s
+	}
+	u, err := url.Parse(s)
+	if err != nil {
+		return ""
+	}
+	// Hostname strips the port if present
+	return u.Hostname()
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func FormatEndpoint(endpoint string) string {
if endpoint == "" {
return ""
}
// Remove protocol (http:// or https://)
re := regexp.MustCompile(`^https?://`)
formatted := re.ReplaceAllString(endpoint, "")
// Split by '/' and take only the first part (domain)
parts := strings.Split(formatted, "/")
return parts[0]
}
import (
"fmt"
"math/big"
"net/url"
"strings"
"github.com/iden3/go-iden3-crypto/poseidon"
)
// FormatEndpoint removes protocol and path from URL, keeping only the domain (without port)
func FormatEndpoint(endpoint string) string {
s := strings.TrimSpace(endpoint)
if s == "" {
return ""
}
// Ensure a scheme for proper parsing when the input is a bare host
if !strings.Contains(s, "://") {
s = "https://" + s
}
u, err := url.Parse(s)
if err != nil {
return ""
}
// Hostname strips the port if present
return u.Hostname()
}
🤖 Prompt for AI Agents
In sdk/sdk-go/common/customHash.go around lines 59-71: the current regex-based
FormatEndpoint fails on uppercase schemes, leaves ports, and doesn't trim
spaces; replace it to trim whitespace, ensure a scheme exists (if missing,
prepend "http://"), parse with net/url.Parse, and return the hostname via
URL.Hostname() to drop ports; on parse error fall back to taking the trimmed
string before the first '/' and stripping any ":port" suffix; remove the regexp
usage and update imports accordingly.


// HashEndpointWithScope implements the hashEndpointWithScope function from TypeScript
func HashEndpointWithScope(endpoint, scope string) (string, error) {
formattedEndpoint := FormatEndpoint(endpoint)

// Split endpoint into 31-character chunks (different from ts)
var endpointChunks []string
remaining := formattedEndpoint
for len(remaining) > 0 {
// Take up to 31 characters (safe slicing)
if len(remaining) > 31 {
chunk := remaining[:31]
endpointChunks = append(endpointChunks, chunk)
remaining = remaining[31:]
} else {
endpointChunks = append(endpointChunks, remaining)
remaining = ""
}
}

if len(endpointChunks) > 16 {
return "", fmt.Errorf("endpoint must be less than 496 characters")
}

chunkedEndpointBigInts := make([]*big.Int, len(endpointChunks))
for i, chunk := range endpointChunks {
bigInt, err := StringToBigInt(chunk)
if err != nil {
return "", fmt.Errorf("failed to convert chunk to BigInt: %w", err)
}
chunkedEndpointBigInts[i] = bigInt
}

endpointHash, err := FlexiblePoseidon(chunkedEndpointBigInts)
if err != nil {
return "", fmt.Errorf("failed to hash endpoint chunks: %w", err)
}


scopeBigInt, err := StringToBigInt(scope)
if err != nil {
return "", fmt.Errorf("failed to convert scope to BigInt: %w", err)
}

finalResult, err := poseidon.Hash([]*big.Int{endpointHash, scopeBigInt})
if err != nil {
return "", fmt.Errorf("failed to hash endpoint with scope: %w", err)
}

return finalResult.String(), nil
}
Loading
Loading