-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
411 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/iden3/go-iden3-core/v2/w3c" | ||
) | ||
|
||
// ChainID is alias for int32 that represents ChainID | ||
type ChainID int32 | ||
|
||
// ChainIDs Object containing chain IDs for various blockchains and networks. | ||
var chainIDs = map[string]ChainID{ | ||
"eth:main": 1, | ||
"eth:goerli": 5, | ||
"eth:sepolia": 11155111, | ||
"polygon:main": 137, | ||
"polygon:mumbai": 80001, | ||
"zkevm:main": 1101, | ||
"zkevm:test": 1442, | ||
} | ||
|
||
// ChainIDfromDID returns chain name from w3c.DID | ||
func ChainIDfromDID(did w3c.DID) (ChainID, error) { | ||
|
||
id, err := IDFromDID(did) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
blockchain, err := BlockchainFromID(id) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
networkID, err := NetworkIDFromID(id) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
chainID, ok := chainIDs[fmt.Sprintf("%s:%s", blockchain, networkID)] | ||
if !ok { | ||
return 0, fmt.Errorf("chainID not found for %s:%s", blockchain, networkID) | ||
} | ||
|
||
return chainID, nil | ||
} | ||
|
||
// RegisterChainID registers chainID for blockchain and network | ||
func RegisterChainID(blockchain Blockchain, network NetworkID, chainID int) error { | ||
k := fmt.Sprintf("%s:%s", blockchain, network) | ||
existingChainID, ok := chainIDs[k] | ||
if ok && existingChainID != ChainID(chainID) { | ||
return fmt.Errorf("chainID '%s:%s' already registered with value %d", blockchain, network, existingChainID) | ||
} | ||
chainIDs[k] = ChainID(chainID) | ||
|
||
return nil | ||
} | ||
|
||
// GetChainID returns chainID for blockchain and network | ||
func GetChainID(blockchain Blockchain, network NetworkID) (ChainID, error) { | ||
k := fmt.Sprintf("%s:%s", blockchain, network) | ||
if _, ok := chainIDs[k]; !ok { | ||
return 0, fmt.Errorf("chainID not registered for %s:%s", blockchain, network) | ||
} | ||
|
||
return chainIDs[k], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.