Skip to content

Commit

Permalink
Move chain id to core
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Nov 28, 2023
1 parent 17766a6 commit 357f743
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 73 deletions.
80 changes: 80 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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": 1,
"eth:main": 1,
"eth:goerli": 5,
"eth:sepolia": 11155111,
"polygon": 137,
"polygon:main": 137,
"polygon:mumbai": 80001,
"zkevm": 1101,
"zkevm:main": 1101,
"zkevm:test": 1442,
}

// ChainIDfromDID returns chain name from w3c.DID
func ChainIDfromDID(did w3c.DID) (ChainID, error) {
// TODO: fix for networks like eth / polygon / zkevm

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 {
if _, ok := blockchains[blockchain]; !ok {
return fmt.Errorf("blockchain not registered: %s", blockchain)
}

if _, ok := networks[network]; !ok {
return fmt.Errorf("network not registered: %s", network)
}

k := fmt.Sprintf("%s:%s", blockchain, network)
if _, ok := chainIDs[k]; ok {
return fmt.Errorf("chainID already registered for %s:%s", blockchain, network)
}
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
}
78 changes: 6 additions & 72 deletions did.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func RegisterDIDMethodNetwork(method DIDMethod, blockchain Blockchain, network N
return fmt.Errorf("DID method network %s:%s:%s already registered", method, blockchain, network)
}

// check chainID
_, err := GetChainID(blockchain, network)
if err != nil {
return err
}

if _, ok := DIDMethodNetwork[method]; !ok {
DIDMethodNetwork[method] = map[DIDNetworkFlag]byte{}
DIDMethodNetwork[method][flg] = b
Expand All @@ -246,78 +252,6 @@ func RegisterDIDMethodNetwork(method DIDMethod, blockchain Blockchain, network N

}

func RegisterDIDMethodNetworkImplicit(method DIDMethod, blockchain Blockchain, network NetworkID) error {
m := DIDMethod(method)
if _, ok := didMethods[m]; !ok {
didMethods[m] = m
}

b := Blockchain(blockchain)
if _, ok := blockchains[b]; !ok {
blockchains[b] = b
}

n := NetworkID(network)
if _, ok := networks[n]; !ok {
networks[n] = n
}

if _, ok := DIDMethodByte[m]; !ok {
preLatest := byte(0b00000000)
for k, v := range DIDMethodByte {
if v > preLatest && k != DIDMethodOther {
preLatest = v
}
}
preLatest++

if preLatest >= DIDMethodByte[DIDMethodOther] {
return fmt.Errorf("DID method byte %s already registered", method)
}
DIDMethodByte[m] = preLatest
}

flg := DIDNetworkFlag{Blockchain: b, NetworkID: n}

if _, ok := DIDMethodNetwork[m]; !ok {
DIDMethodNetwork[m] = map[DIDNetworkFlag]byte{}
}

if _, ok := DIDMethodNetwork[m][flg]; ok {
return fmt.Errorf("DID method network %s already registered", method)
}

flags := DIDMethodNetwork[m]

if len(flags) == 0 {
DIDMethodNetwork[m][flg] = 0b00010000 | 0b00000001
return nil
}
// find biggest flag byte
preLatest := byte(0b00000000)
for _, v := range flags {
if v > preLatest {
preLatest = v
}
}

// split flag byte to 2 parts
chainPart := (preLatest >> 4) + 1
networkPart := (preLatest & 0b00001111) + 1

if chainPart >= 0b1111 {
return fmt.Errorf(`Reached max number of blockchains for did method: %s`, method)
}

if networkPart >= 0b1111 {
return fmt.Errorf(`Reached max number of networks for did method: %s`, method)
}

// join parts
DIDMethodNetwork[m][flg] = (chainPart << 4) | networkPart
return nil
}

// BuildDIDType builds bytes type from chain and network
func BuildDIDType(method DIDMethod, blockchain Blockchain,
network NetworkID) ([2]byte, error) {
Expand Down
15 changes: 14 additions & 1 deletion did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ func TestDID_Custom_Parse_DID(t *testing.T) {
require.NoError(t, err)
err = RegisterDIDMethodWithByte("test_method", 0b00000011)
require.NoError(t, err)
err = RegisterChainID("test_chain", "test_net", 101)
require.NoError(t, err)
err = RegisterDIDMethodNetwork("test_method", "test_chain", "test_net", 0b0001_0001)
require.NoError(t, err)
err = RegisterDIDMethodWithByte("method", 0b0000_0100)
Expand All @@ -406,11 +408,18 @@ func TestDID_Custom_Parse_DID(t *testing.T) {
require.NoError(t, err)
n, err = GetNetwork("network")
require.NoError(t, err)

err = RegisterChainID(b, n, 102)
require.NoError(t, err)
err = RegisterDIDMethodNetwork(m, b, n, 0b0001_0001)
require.NoError(t, err)

err = RegisterChainID(b, Test, 103)
require.NoError(t, err)

err = RegisterDIDMethodNetwork(DIDMethodIden3, b, Test, 0b01000000|0b00000011)
require.NoError(t, err)
err = RegisterChainID(ReadOnly, n, 104)
require.NoError(t, err)
err = RegisterDIDMethodNetwork(DIDMethodIden3, ReadOnly, n, 0b01000000|0b00000011)
require.NoError(t, err)

Expand All @@ -423,6 +432,10 @@ func TestDID_Custom_Parse_DID(t *testing.T) {
require.NoError(t, err)
err = RegisterNetwork("network2")
require.NoError(t, err)

err = RegisterChainID("chain2", "network2", 105)
require.NoError(t, err)

err = RegisterDIDMethodNetwork("method2", "chain2", "network2", 0b0001_0001)
require.NoError(t, err)

Expand Down

0 comments on commit 357f743

Please sign in to comment.