Skip to content

Commit

Permalink
Merge 49815c5 into d00a945
Browse files Browse the repository at this point in the history
  • Loading branch information
robdefeo committed Oct 23, 2019
2 parents d00a945 + 49815c5 commit 3a5ce8e
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 26 deletions.
21 changes: 16 additions & 5 deletions cmd/mailchain/internal/http/handlers/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint: lll
package handlers

import (
"encoding/json"
"fmt"
"net/http"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/mailchain/mailchain/cmd/mailchain/internal/http/params"
"github.com/mailchain/mailchain/errs"
"github.com/mailchain/mailchain/internal/address"
"github.com/mailchain/mailchain/internal/mailbox"
"github.com/mailchain/mailchain/internal/pubkey"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -66,10 +66,16 @@ func GetPublicKey(finders map[string]mailbox.PubKeyFinder) func(w http.ResponseW
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithStack(err))
return
}
encodedKey, encodingType, err := pubkey.EncodeByProtocol(publicKey, req.Protocol)
if err != nil {
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithStack(err))
return
}

w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(GetPublicKeyResponseBody{
PublicKey: hexutil.Encode(publicKey),
PublicKey: encodedKey,
PublicKeyEncoding: encodingType,
})
}
}
Expand Down Expand Up @@ -143,10 +149,15 @@ type GetPublicKeyResponse struct {
//
// swagger:model GetPublicKeyResponseBody
type GetPublicKeyResponseBody struct {
// The public key
// The public key encoded as per `public_key_encoding`
//
// Required: true
// nolint: lll
// example: 0x79964e63752465973b6b3c610d8ac773fc7ce04f5d1ba599ba8768fb44cef525176f81d3c7603d5a2e466bc96da7b2443bef01b78059a98f45d5c440ca379463
PublicKey string `json:"public_key"`

// Encoding method used for encoding the `public_key`
//
// Required: true
// example: hex/0x-prefix
PublicKeyEncoding string `json:"public_key_encoding"`
}
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/http/handlers/pubkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestGetPublicKey(t *testing.T) {
"network": "mainnet",
"protocol": "ethereum",
},
"{\"public_key\":\"0x3ada323710def1e02f3586710ae3624ceefba1638e9d9894f724a5401997cd792933ddfd0687874e515a8ab479a38646e6db9f3d8b74d27c4e4eae5a116f9f1400\"}\n",
"{\"public_key\":\"0x3ada323710def1e02f3586710ae3624ceefba1638e9d9894f724a5401997cd792933ddfd0687874e515a8ab479a38646e6db9f3d8b74d27c4e4eae5a116f9f1400\",\"public_key_encoding\":\"hex/0x-prefix\"}\n",
http.StatusOK,
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/http/handlers/resolve_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GetResolveName(resolvers map[string]nameservice.ForwardLookup) func(w http.
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithStack(err))
return
}
encAddress, err := address.EncodeByProtocol(resolvedAddress, protocol)
encAddress, _, err := address.EncodeByProtocol(resolvedAddress, protocol)
if err != nil {
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithMessage(err, "failed to encode address"))
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/nameservice/handler/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Forward(resolver nameservice.ForwardLookup) func(w http.ResponseWriter, r *
errs.JSONWriter(w, http.StatusInternalServerError, err)
return
}
encAddress, err := address.EncodeByProtocol(resolvedAddress, protocol)
encAddress, _, err := address.EncodeByProtocol(resolvedAddress, protocol)
if err != nil {
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithMessage(err, "failed to encode address"))
return
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/mitchellh/go-homedir v1.1.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/mr-tron/base58 v1.1.0
Expand Down
9 changes: 5 additions & 4 deletions internal/address/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
package address

import (
"github.com/mailchain/mailchain/internal/protocols"
"github.com/mailchain/mailchain/internal/encoding"
"github.com/mailchain/mailchain/internal/protocols"
"github.com/pkg/errors"
)

func EncodeByProtocol(in []byte, protocol string) (string, error) {
func EncodeByProtocol(in []byte, protocol string) (encoded, encodingType string, err error) {
switch protocol {
case protocols.Ethereum:
return encoding.EncodeZeroX(in), nil
encoded, encodingType = encoding.EncodeZeroX(in)
default:
return "", errors.Errorf("%q unsupported protocol", protocol)
err = errors.Errorf("%q unsupported protocol", protocol)
}
return encoded, encodingType, err
}
21 changes: 14 additions & 7 deletions internal/address/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package address
import (
"testing"

"github.com/mailchain/mailchain/internal/encoding"
"github.com/mailchain/mailchain/internal/testutil"
)

Expand All @@ -26,10 +27,11 @@ func TestEncodeByProtocol(t *testing.T) {
protocol string
}
tests := []struct {
name string
args args
want string
wantErr bool
name string
args args
wantEncoded string
wantEncodingType string
wantErr bool
}{
{
"ethereum",
Expand All @@ -38,6 +40,7 @@ func TestEncodeByProtocol(t *testing.T) {
"ethereum",
},
"0x5602ea95540bee46d03ba335eed6f49d117eab95c8ab8b71bae2cdd1e564a761",
encoding.TypeHex0XPrefix,
false,
},
{
Expand All @@ -47,18 +50,22 @@ func TestEncodeByProtocol(t *testing.T) {
"invalid",
},
"",
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := EncodeByProtocol(tt.args.in, tt.args.protocol)
gotEncoded, gotEncodingType, err := EncodeByProtocol(tt.args.in, tt.args.protocol)
if (err != nil) != tt.wantErr {
t.Errorf("EncodeByProtocol() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("EncodeByProtocol() = %v, want %v", got, tt.want)
if gotEncoded != tt.wantEncoded {
t.Errorf("EncodeByProtocol() gotEncoded = %v, want %v", gotEncoded, tt.wantEncoded)
}
if gotEncodingType != tt.wantEncodingType {
t.Errorf("EncodeByProtocol() gotEncodingType = %v, want %v", gotEncodingType, tt.wantEncodingType)
}
})
}
Expand Down
21 changes: 21 additions & 0 deletions internal/encoding/base58.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2019 Finobo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package encoding

import "github.com/mr-tron/base58"

func DecodeBase58(in string) ([]byte, error) {
return base58.Decode(in)
}
53 changes: 53 additions & 0 deletions internal/encoding/base58_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019 Finobo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package encoding

import (
"reflect"
"testing"
)

func TestDecodeBase58(t *testing.T) {
type args struct {
in string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
"success",
args{
"5CLmNK8f16nagFeF2h3iNeeChaxPiAsJu7piNYJgdPpmaRzPD",
},
[]byte{0x9, 0x86, 0xc6, 0x71, 0x43, 0xad, 0x96, 0x6f, 0xa5, 0x79, 0xc9, 0x1b, 0x30, 0xc6, 0x7f, 0x95, 0xe7, 0x4b, 0xcc, 0xe3, 0xc5, 0xec, 0xb9, 0x5c, 0x96, 0xbf, 0xb5, 0x82, 0x87, 0x65, 0x64, 0xe4, 0x9c, 0x8, 0x3f, 0x1c},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := DecodeBase58(tt.args.in)
if (err != nil) != tt.wantErr {
t.Errorf("DecodeBase58() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DecodeBase58() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions internal/encoding/zerox.go → internal/encoding/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/pkg/errors"
)

func EncodeZeroX(in []byte) string {
func EncodeZeroX(in []byte) (encoded, encoding string) {
out := make([]byte, len(in)*2+2)
copy(out, "0x")
hex.Encode(out[2:], in)
return string(out)
return string(out), TypeHex0XPrefix
}

func DecodeZeroX(in string) ([]byte, error) {
Expand Down
16 changes: 11 additions & 5 deletions internal/encoding/zerox_test.go → internal/encoding/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ func Test_EncodeZeroX(t *testing.T) {
in []byte
}
tests := []struct {
name string
args args
want string
name string
args args
wantEncoded string
wantEncoding string
}{
{
"success",
args{
testutil.MustHexDecodeString("5602ea95540bee46d03ba335eed6f49d117eab95c8ab8b71bae2cdd1e564a761"),
},
"0x5602ea95540bee46d03ba335eed6f49d117eab95c8ab8b71bae2cdd1e564a761",
TypeHex0XPrefix,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EncodeZeroX(tt.args.in); !assert.Equal(tt.want, got) {
t.Errorf("encodeZeroX() = %v, want %v", got, tt.want)
gotEncoded, gotEncoding := EncodeZeroX(tt.args.in)
if !assert.Equal(tt.wantEncoded, gotEncoded) {
t.Errorf("EncodeZeroX() gotEncoded = %v, want %v", gotEncoded, tt.wantEncoded)
}
if !assert.Equal(tt.wantEncoding, gotEncoding) {
t.Errorf("EncodeZeroX() gotEncoding = %v, want %v", gotEncoding, tt.wantEncoding)
}
})
}
Expand Down
33 changes: 33 additions & 0 deletions internal/pubkey/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019 Finobo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pubkey

import (
"github.com/mailchain/mailchain/internal/encoding"
"github.com/mailchain/mailchain/internal/protocols"
"github.com/pkg/errors"
)

func EncodeByProtocol(in []byte, protocol string) (encoded, encodingType string, err error) {
switch protocol {
case protocols.Ethereum:
encoded, encodingType = encoding.EncodeZeroX(in)
case protocols.Substrate:
encoded, encodingType = encoding.EncodeZeroX(in)
default:
err = errors.Errorf("%q unsupported protocol", protocol)
}
return encoded, encodingType, err
}

0 comments on commit 3a5ce8e

Please sign in to comment.