Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add errors to register the same values to DID Method, ChainID, Network Flag #459

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ func ChainIDfromDID(did w3c.DID) (ChainID, error) {
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)
if ok && existingChainID == ChainID(chainID) {
return nil
}

for _, v := range chainIDs {
if v == ChainID(chainID) {
return fmt.Errorf(`can't register chain id %d for '%s' because it's already registered for another chain id`, chainID, k)
}
}

chainIDs[k] = ChainID(chainID)

return nil
Expand Down
26 changes: 19 additions & 7 deletions did.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,23 @@ var DIDMethodByte = map[DIDMethod]byte{

// RegisterDIDMethod registers new DID method with byte flag
func RegisterDIDMethod(m DIDMethod, b byte) error {
existingByte, ok := DIDMethodByte[m]
if ok && existingByte != b {
return fmt.Errorf("DID method '%s' already registered with byte %b", m, existingByte)
}

max := DIDMethodByte[DIDMethodOther]
if b >= max {
return fmt.Errorf("Can't register DID method byte: current %b, maximum byte allowed: %b", b, max-1)
}

existingByte, ok := DIDMethodByte[m]
if ok && existingByte == b {
return nil
}

for _, v := range DIDMethodByte {
if v == b {
return fmt.Errorf(`can't register method '%s' because DID method byte '%b' already registered for another method`, m, b)
}
}

didMethods[m] = m
DIDMethodByte[m] = b

Expand Down Expand Up @@ -277,9 +284,14 @@ func RegisterDIDMethodNetwork(params DIDMethodNetworkParams, opts ...Registratio
}
}
existedFlag, ok := DIDMethodNetwork[m][flg]
if ok && existedFlag != params.NetworkFlag {
return fmt.Errorf("DID method network '%s' with blockchain '%s' and network '%s' already registered with another flag '%b'",
m, b, n, existedFlag)
if ok && existedFlag == params.NetworkFlag {
return nil
}

for _, v := range DIDMethodNetwork[m] {
if v == params.NetworkFlag {
return fmt.Errorf(`DID network flag %b is already registered for the another network id for '%s' method`, v, m)
}
}

DIDMethodNetwork[m][flg] = params.NetworkFlag
Expand Down
55 changes: 48 additions & 7 deletions did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,17 +434,17 @@ func TestCustomDIDRegistration(t *testing.T) {
Network: NoNetwork,
NetworkFlag: 0b00000000,
},
opts: []RegistrationOptions{WithChainID(103)},
opts: []RegistrationOptions{WithChainID(104)},
},
{
Description: "register one more network to existing did method",
Data: DIDMethodNetworkParams{
Method: DIDMethodIden3,
Blockchain: ReadOnly,
Network: "network",
NetworkFlag: 0b01000000 | 0b00000011,
NetworkFlag: 0b11000000 | 0b00000011,
},
opts: []RegistrationOptions{WithChainID(104)},
opts: []RegistrationOptions{WithChainID(105)},
},
{
Description: "register known chain id to new did method",
Expand All @@ -454,7 +454,16 @@ func TestCustomDIDRegistration(t *testing.T) {
Network: Mumbai,
NetworkFlag: 0b0001_0001,
},
opts: []RegistrationOptions{WithDIDMethodByte(1)},
opts: []RegistrationOptions{WithDIDMethodByte(0b0000111)},
},
{
Description: "register known chain id to new did method",
Data: DIDMethodNetworkParams{
Method: "iden3",
Blockchain: ReadOnly,
Network: NoNetwork,
NetworkFlag: 0b0000_0000,
},
},
}

Expand Down Expand Up @@ -506,7 +515,7 @@ func TestCustomDIDRegistration_Negative(t *testing.T) {
NetworkFlag: 0b0001_0001,
},
opts: []RegistrationOptions{WithChainID(1)},
err: "chainID 'polygon:mumbai' already registered with value 80001",
err: "can't register chain id 1 for 'polygon:mumbai' because it's already registered for another chain id",
},
{
Description: "try to overwrite existing DID method byte",
Expand All @@ -517,7 +526,7 @@ func TestCustomDIDRegistration_Negative(t *testing.T) {
NetworkFlag: 0b00100000 | 0b00000001,
},
opts: []RegistrationOptions{WithChainID(1), WithDIDMethodByte(0b00000010)},
err: "DID method 'iden3' already registered with byte 1",
err: "can't register method 'iden3' because DID method byte '10' already registered for another method",
},
{
Description: "try to write max did method byte",
Expand All @@ -539,7 +548,39 @@ func TestCustomDIDRegistration_Negative(t *testing.T) {
NetworkFlag: 0b00100000 | 0b00000011,
},
opts: nil,
err: "DID method network 'iden3' with blockchain 'eth' and network 'main' already registered with another flag '100001'",
err: "DID network flag 100011 is already registered for the another network id for 'iden3' method",
},
{
Description: "register new did method with existing method byte",
Data: DIDMethodNetworkParams{
Method: "new_method",
Blockchain: "new_chain",
Network: "new_net",
NetworkFlag: 0b0001_0001,
},
opts: []RegistrationOptions{WithChainID(101), WithDIDMethodByte(0b00000001)},
err: "can't register method 'new_method' because DID method byte '1' already registered for another method",
},
{
Description: "register new did method with existing chain id",
Data: DIDMethodNetworkParams{
Method: "new_method",
Blockchain: Ethereum,
Network: Main,
NetworkFlag: 0b0001_0001,
},
opts: []RegistrationOptions{WithChainID(101), WithDIDMethodByte(0b10000000)},
err: "can't register chain id 101 for 'eth:main' because it's already registered for another chain id",
},
{
Description: "register new network and chain with existing networkFlag for existing existing did method",
Data: DIDMethodNetworkParams{
Method: DIDMethodIden3,
Blockchain: "supa_chain",
Network: "supa_net",
NetworkFlag: 0b00010000 | 0b00000001,
},
err: "DID network flag 10001 is already registered for the another network id for 'iden3' method",
},
}

Expand Down