From dbeb810b0252a2b6bcbb4d2667e0a9b7a5593576 Mon Sep 17 00:00:00 2001 From: Dimasik Kolezhniuk Date: Tue, 19 Dec 2023 15:21:42 +0100 Subject: [PATCH] Add errors to register the same values to DID Method, ChainID, Network Flag (#459) --- chain.go | 11 +++++++++-- did.go | 26 ++++++++++++++++++------- did_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/chain.go b/chain.go index 4a4e61b..e3c1121 100644 --- a/chain.go +++ b/chain.go @@ -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 diff --git a/did.go b/did.go index adf3008..ffb4e55 100644 --- a/did.go +++ b/did.go @@ -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 @@ -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 diff --git a/did_test.go b/did_test.go index 51e26b5..260cb6c 100644 --- a/did_test.go +++ b/did_test.go @@ -434,7 +434,7 @@ 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", @@ -442,9 +442,9 @@ func TestCustomDIDRegistration(t *testing.T) { 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", @@ -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, + }, }, } @@ -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", @@ -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", @@ -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", }, }