diff --git a/cmd/mailchain/internal/http/handlers/protocols.go b/cmd/mailchain/internal/http/handlers/protocols.go index afba1fd5f..60dccc97e 100644 --- a/cmd/mailchain/internal/http/handlers/protocols.go +++ b/cmd/mailchain/internal/http/handlers/protocols.go @@ -33,15 +33,18 @@ func GetProtocols(base *settings.Root) func(w http.ResponseWriter, r *http.Reque continue } - networks := []string{} - + networks := []Network{} for _, network := range protocol.Networks { - if !network.Disabled() { - networks = append(networks, network.Kind()) + if !network.Disabled() && protocol.Kind == "ethereum" { + networks = append(networks, Network{Name: network.Kind(), ID: ""}) + } else if !network.Disabled() && protocol.Kind == "substrate" { + if network.Kind() == "edgeware-testnet" { + networks = append(networks, Network{Name: network.Kind(), ID: "42"}) + } } } - sort.Strings(networks) + sort.Slice(networks, func(i, j int) bool { return networks[i].Name < networks[j].Name }) resP := GetProtocolsProtocol{ Name: protocol.Kind, Networks: networks, @@ -72,10 +75,14 @@ type GetProtocolsResponse struct { Protocols []GetProtocolsProtocol `json:"protocols"` } -// GetProtocolsProtocol body +type Network struct { + Name string `json:"name"` + ID string `json:"id"` +} + type GetProtocolsProtocol struct { // in: body Name string `json:"name"` // in: body - Networks []string `json:"networks"` + Networks []Network `json:"networks"` } diff --git a/cmd/mailchain/internal/http/handlers/protocols_test.go b/cmd/mailchain/internal/http/handlers/protocols_test.go index 2cbe23fe6..d3cb227cd 100644 --- a/cmd/mailchain/internal/http/handlers/protocols_test.go +++ b/cmd/mailchain/internal/http/handlers/protocols_test.go @@ -67,7 +67,7 @@ func TestGetProtocols(t *testing.T) { return settings.FromStore(m) }(), }, - "{\"protocols\":[{\"name\":\"ethereum\",\"networks\":[\"kovan\",\"mainnet\",\"rinkeby\",\"ropsten\"]}]}\n", + "{\"protocols\":[{\"name\":\"ethereum\",\"networks\":[{\"name\":\"kovan\",\"id\":\"\"},{\"name\":\"mainnet\",\"id\":\"\"},{\"name\":\"rinkeby\",\"id\":\"\"},{\"name\":\"ropsten\",\"id\":\"\"}]}]}\n", http.StatusOK, }, { @@ -81,7 +81,21 @@ func TestGetProtocols(t *testing.T) { return settings.FromStore(m) }(), }, - "{\"protocols\":[{\"name\":\"ethereum\",\"networks\":[\"goerli\",\"kovan\",\"mainnet\",\"rinkeby\",\"ropsten\"]}]}\n", + "{\"protocols\":[{\"name\":\"ethereum\",\"networks\":[{\"name\":\"goerli\",\"id\":\"\"},{\"name\":\"kovan\",\"id\":\"\"},{\"name\":\"mainnet\",\"id\":\"\"},{\"name\":\"rinkeby\",\"id\":\"\"},{\"name\":\"ropsten\",\"id\":\"\"}]}]}\n", + http.StatusOK, + }, + { + "default-substrate", + args{ + func() *settings.Root { + m := valuestest.NewMockStore(mockCtrl) + m.EXPECT().IsSet("protocols.ethereum.disabled").Return(true) + m.EXPECT().GetBool("protocols.ethereum.disabled").Return(true) + m.EXPECT().IsSet(gomock.Any()).Return(false).AnyTimes() + return settings.FromStore(m) + }(), + }, + "{\"protocols\":[{\"name\":\"substrate\",\"networks\":[{\"name\":\"edgeware-testnet\",\"id\":\"42\"}]}]}\n", http.StatusOK, }, } diff --git a/cmd/mailchain/internal/settings/root.go b/cmd/mailchain/internal/settings/root.go index 38ef520e8..2904fdec1 100644 --- a/cmd/mailchain/internal/settings/root.go +++ b/cmd/mailchain/internal/settings/root.go @@ -30,6 +30,8 @@ func FromStore(s values.Store) *Root { }), protocols.Substrate: protocol(s, protocols.Substrate, map[string]NetworkClient{ substrate.EdgewareTestnet: network(s, protocols.Substrate, substrate.EdgewareTestnet, defaults.SubstrateNetworkAny()), + // substrate.PolkadotTestnet: network(s, protocols.Substrate, substrate.PolkadotTestnet, defaults.SubstrateNetworkAny()), + // substrate.KusamaTestnet: network(s, protocols.Substrate, substrate.KusamaTestnet, defaults.SubstrateNetworkAny()), }), }, // other diff --git a/internal/protocols/substrate/address.go b/internal/protocols/substrate/address.go index 64e3a2902..182ee5677 100644 --- a/internal/protocols/substrate/address.go +++ b/internal/protocols/substrate/address.go @@ -58,6 +58,12 @@ func prefixWithNetwork(network string, publicKey crypto.PublicKey) ([]byte, erro case EdgewareTestnet: // 42 = 0x2a return append([]byte{0x2a}, publicKey.Bytes()...), nil + case PolkadotTestnet: + // 0 = 0x00 + return append([]byte{0x00}, publicKey.Bytes()...), nil + case KusamaTestnet: + // 2 = 0x02 + return append([]byte{0x02}, publicKey.Bytes()...), nil default: return nil, errors.Errorf("unknown address prefix for %q", network) } diff --git a/internal/protocols/substrate/address_test.go b/internal/protocols/substrate/address_test.go index 13163958d..d96c20fbf 100644 --- a/internal/protocols/substrate/address_test.go +++ b/internal/protocols/substrate/address_test.go @@ -54,6 +54,42 @@ func Test_prefixWithNetwork(t *testing.T) { nil, true, }, + { + "polkadot-testnet", + args{ + "polkadot-testnet", + ed25519test.SofiaPublicKey, + }, + []byte{0x00, 0x72, 0x3c, 0xaa, 0x23, 0xa5, 0xb5, 0x11, 0xaf, 0x5a, 0xd7, 0xb7, 0xef, 0x60, 0x76, 0xe4, 0x14, 0xab, 0x7e, 0x75, 0xa9, 0xdc, 0x91, 0xe, 0xa6, 0xe, 0x41, 0x7a, 0x2b, 0x77, 0xa, 0x56, 0x71}, + false, + }, + { + "invalid", + args{ + "invalid", + ed25519test.SofiaPublicKey, + }, + nil, + true, + }, + { + "kusama-testnet", + args{ + "kusama-testnet", + ed25519test.SofiaPublicKey, + }, + []byte{0x02, 0x72, 0x3c, 0xaa, 0x23, 0xa5, 0xb5, 0x11, 0xaf, 0x5a, 0xd7, 0xb7, 0xef, 0x60, 0x76, 0xe4, 0x14, 0xab, 0x7e, 0x75, 0xa9, 0xdc, 0x91, 0xe, 0xa6, 0xe, 0x41, 0x7a, 0x2b, 0x77, 0xa, 0x56, 0x71}, + false, + }, + { + "invalid", + args{ + "invalid", + ed25519test.SofiaPublicKey, + }, + nil, + true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -126,6 +162,24 @@ func TestSS58AddressFormat(t *testing.T) { []byte{0x2a, 0x2e, 0x32, 0x2f, 0x87, 0x40, 0xc6, 0x1, 0x72, 0x11, 0x1a, 0xc8, 0xea, 0xdc, 0xdd, 0xa2, 0x51, 0x2f, 0x90, 0xd0, 0x6d, 0xe, 0x50, 0x3e, 0xf1, 0x89, 0x97, 0x9a, 0x15, 0x9b, 0xec, 0xe1, 0xe8, 0x6d, 0x48}, false, }, + { + "success-sofia-polkadot-testnet", + args{ + "polkadot-testnet", + ed25519test.SofiaPublicKey, + }, + []byte{0x0, 0x72, 0x3c, 0xaa, 0x23, 0xa5, 0xb5, 0x11, 0xaf, 0x5a, 0xd7, 0xb7, 0xef, 0x60, 0x76, 0xe4, 0x14, 0xab, 0x7e, 0x75, 0xa9, 0xdc, 0x91, 0xe, 0xa6, 0xe, 0x41, 0x7a, 0x2b, 0x77, 0xa, 0x56, 0x71, 0x90, 0x66}, + false, + }, + { + "success-charlotte-polkadot-testnet", + args{ + "polkadot-testnet", + ed25519test.CharlottePublicKey, + }, + []byte{0x0, 0x2e, 0x32, 0x2f, 0x87, 0x40, 0xc6, 0x1, 0x72, 0x11, 0x1a, 0xc8, 0xea, 0xdc, 0xdd, 0xa2, 0x51, 0x2f, 0x90, 0xd0, 0x6d, 0xe, 0x50, 0x3e, 0xf1, 0x89, 0x97, 0x9a, 0x15, 0x9b, 0xec, 0xe1, 0xe8, 0x93, 0x8d}, + false, + }, { "err-network", args{ diff --git a/internal/protocols/substrate/substrate.go b/internal/protocols/substrate/substrate.go index 413f883c7..eb7ee911b 100644 --- a/internal/protocols/substrate/substrate.go +++ b/internal/protocols/substrate/substrate.go @@ -17,6 +17,12 @@ package substrate const ( // EdgewareTestnet network name. EdgewareTestnet = "edgeware-testnet" + + // PolkadotTestnet network name + PolkadotTestnet = "polkadot-testnet" + + // KusamaTestnet network name + KusamaTestnet = "kusama-testnet" ) // Networks supported by substrate package.