Skip to content

Commit

Permalink
feat: add network ids to /protocol endpoint
Browse files Browse the repository at this point in the history
* feat: add network ids to /protocol endpoint.
  • Loading branch information
pascaloseko authored and robdefeo committed Nov 26, 2019
1 parent e5a2400 commit 0f77673
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
21 changes: 14 additions & 7 deletions cmd/mailchain/internal/http/handlers/protocols.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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"`
}
18 changes: 16 additions & 2 deletions cmd/mailchain/internal/http/handlers/protocols_test.go
Expand Up @@ -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,
},
{
Expand All @@ -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,
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/mailchain/internal/settings/root.go
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/protocols/substrate/address.go
Expand Up @@ -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)
}
Expand Down
54 changes: 54 additions & 0 deletions internal/protocols/substrate/address_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions internal/protocols/substrate/substrate.go
Expand Up @@ -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.
Expand Down

0 comments on commit 0f77673

Please sign in to comment.