Skip to content

Commit

Permalink
Merge c870860 into b941e98
Browse files Browse the repository at this point in the history
  • Loading branch information
robdefeo committed Oct 24, 2019
2 parents b941e98 + c870860 commit b2ac5d0
Show file tree
Hide file tree
Showing 43 changed files with 565 additions and 120 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ openapi:
mailchain/goswagger-tool swagger generate spec -b ./cmd/mailchain/internal/http/handlers -o ./docs/openapi/spec.json

echo "" >> ./docs/openapi/spec.json

echo "// nolint: gofmt" > ./docs/openapi/spec.json
echo "package handlers" > ./cmd/mailchain/internal/http/handlers/openapi.go
echo "" >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo "// nolint: lll" >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo "// nolint: funlen" >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo 'func spec() string {' >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo ' return `' >> ./cmd/mailchain/internal/http/handlers/openapi.go
cat ./docs/openapi/spec.json >> ./cmd/mailchain/internal/http/handlers/openapi.go
cat ./docs/openapi/spec.json | sed 's/`/``/g' >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo '`' >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo '}' >> ./cmd/mailchain/internal/http/handlers/openapi.go
addlicense -l apache -c Finobo ./cmd/mailchain/internal/http/handlers/openapi.go
Expand Down
8 changes: 4 additions & 4 deletions cmd/mailchain/commands/commandstest/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

func ExecuteCommandC(root *cobra.Command, args []string, flags map[string]string) (c *cobra.Command, output string, err error) {
buf := new(bytes.Buffer)
if err := root.ValidateArgs(args); err != nil {
return nil, "", err
}
buf := new(bytes.Buffer)

root.SetOutput(buf)
root.SetArgs(args)
for x := range flags {
Expand All @@ -24,15 +25,14 @@ func ExecuteCommandC(root *cobra.Command, args []string, flags map[string]string
}

func AssertCommandOutput(t *testing.T, cmd *cobra.Command, err error, out, wantOutput string) bool {
assert := assert.New(t)
if err == nil {
if !assert.Equal(wantOutput, out) {
if !assert.Equal(t, wantOutput, out) {
t.Errorf("cmd().Execute().out = %v, want %v", out, wantOutput)
return false
}
}
if err != nil {
if !assert.Equal(wantOutput+"\n"+cmd.UsageString()+"\n", out) {
if !assert.Equal(t, wantOutput+"\n"+cmd.UsageString()+"\n", out) {
t.Errorf("cmd().Execute().out = %v, want %v", out, wantOutput)
return false
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type config struct {
}

// nolint: gocyclo
func produceConfig(s *settings.Base) (*config, error) {
func produceConfig(s *settings.Base) (*config, error) { // nolint: funlen
mailboxStore, err := s.MailboxState.Produce()
if err != nil {
return nil, errors.WithMessage(err, "Could not config mailbox store")
Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/http/handlers/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
)

// nolint: gocyclo
// nolint: funlen
// GetMessages returns a handler get spec
func GetMessages(inbox stores.State, receivers map[string]mailbox.Receiver, ks keystore.Store,
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/mailchain/internal/http/handlers/send_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (

// SendMessage handler http
func SendMessage(sent stores.Sent, senders map[string]sender.Message, ks keystore.Store,
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) {
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) { // nolint: funlen
encrypter := aes256cbc.NewEncrypter()
// Post swagger:route POST /messages Send SendMessage
//
Expand Down Expand Up @@ -104,7 +104,6 @@ func SendMessage(sent stores.Sent, senders map[string]sender.Message, ks keystor
}

w.WriteHeader(http.StatusOK)
return
}
}

Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func SetupFlags(cmd *cobra.Command) error {

func CreateNegroni(config *settings.Server, router http.Handler) *negroni.Negroni {
n := negroni.New()

if !config.CORS.Disabled.Get() {
n.Use(cors.New(cors.Options{
AllowedOrigins: config.CORS.AllowedOrigins.Get(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/prompts/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Secret(suppliedSecret, prePromptNote, promptLabel string, allowEmpty, confi

func secretFromPrompt(promptLabel string, confirmPrompt bool) (string, error) {
prompt := promptui.Prompt{
Label: fmt.Sprintf("%s", promptLabel),
Label: promptLabel,
Mask: '*',
}
secret, err := prompt.Run()
Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/prompts/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func SelectItemSkipable(label string, items []string, skipable bool) (selected s
if skipable {
items = append([]string{"skip"}, items...)
}

prompt := promptui.Select{
Label: label,
Items: items,
Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/settings/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/output"
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/values"
"github.com/mailchain/mailchain/internal/protocols"

"github.com/mailchain/mailchain/internal/protocols/ethereum"
"github.com/mailchain/mailchain/internal/protocols/substrate"
)
Expand Down
8 changes: 4 additions & 4 deletions cmd/mailchain/internal/settings/defaults/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type NetworkDefaults struct {
Disabled bool
}

func EthereumNetworkAny() NetworkDefaults {
return NetworkDefaults{
func EthereumNetworkAny() *NetworkDefaults {
return &NetworkDefaults{
NameServiceAddress: NameServiceAddressKind,
NameServiceDomainName: NameServiceDomainNameKind,
PublicKeyFinder: mailchain.ClientEtherscanNoAuth,
Expand All @@ -24,8 +24,8 @@ func EthereumNetworkAny() NetworkDefaults {
}
}

func SubstrateNetworkAny() NetworkDefaults {
return NetworkDefaults{
func SubstrateNetworkAny() *NetworkDefaults {
return &NetworkDefaults{
// NameServiceAddress: NameServiceAddressKind,
// NameServiceDomainName: NameServiceDomainNameKind,
PublicKeyFinder: SubstratePublicKeyFinder,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/settings/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/mailchain/mailchain/sender"
)

func network(s values.Store, protocol, network string, nd defaults.NetworkDefaults) *Network {
func network(s values.Store, protocol, network string, nd *defaults.NetworkDefaults) *Network {
k := &Network{
kind: network,
protocol: protocol,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/settings/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_network(t *testing.T) {
s values.Store
protocol string
network string
nd defaults.NetworkDefaults
nd *defaults.NetworkDefaults
}
tests := []struct {
name string
Expand Down
1 change: 1 addition & 0 deletions cmd/relay/relayer/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (f RelayFunc) HandleRequest(w http.ResponseWriter, req *http.Request) {
errs.JSONWriter(w, http.StatusBadGateway, err)
return
}

defer resp.Body.Close()
copyHeader(resp.Header, w.Header())
w.WriteHeader(resp.StatusCode)
Expand Down
21 changes: 21 additions & 0 deletions crypto/cipher/nacl/decrypter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nacl

import (
"github.com/mailchain/mailchain/crypto"
"github.com/mailchain/mailchain/crypto/cipher"
)

// NewDecrypter create a new decrypter attaching the private key to it
func NewDecrypter(privateKey crypto.PrivateKey) Decrypter {
return Decrypter{privateKey: privateKey}
}

// Decrypter will decrypt data using AES256CBC method
type Decrypter struct {
privateKey crypto.PrivateKey
}

// Decrypt data using recipient private key with AES in CBC mode.
func (d Decrypter) Decrypt(data cipher.EncryptedContent) (cipher.PlainContent, error) {
return easyOpen(data, d.privateKey.Bytes())
}
83 changes: 83 additions & 0 deletions crypto/cipher/nacl/decrypter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package nacl

import (
"reflect"
"testing"

"github.com/mailchain/mailchain/crypto"
"github.com/mailchain/mailchain/crypto/cipher"
"github.com/mailchain/mailchain/crypto/ed25519/ed25519test"
"github.com/stretchr/testify/assert"
)

func TestNewDecrypter(t *testing.T) {
type args struct {
privateKey crypto.PrivateKey
}
tests := []struct {
name string
args args
want Decrypter
}{
{
"success",
args{
ed25519test.CharlottePrivateKey,
},
Decrypter{
privateKey: ed25519test.CharlottePrivateKey,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewDecrypter(tt.args.privateKey); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDecrypter() = %v, want %v", got, tt.want)
}
})
}
}

func TestDecrypter_Decrypt(t *testing.T) {
assert := assert.New(t)
type fields struct {
privateKey crypto.PrivateKey
}
type args struct {
data cipher.EncryptedContent
}
tests := []struct {
name string
fields fields
args args
want cipher.PlainContent
wantErr bool
}{
{
"success-charlotte",
fields{
ed25519test.CharlottePrivateKey,
},
args{
cipher.EncryptedContent{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x5b, 0x19, 0x83, 0xe5, 0x6e, 0x7f, 0xed, 0xfe, 0xbb, 0xd0, 0x70, 0x34, 0xce, 0x25, 0x49, 0x76, 0xa3, 0x50, 0x78, 0x91, 0x18, 0xe6, 0xe3},
},
cipher.PlainContent{0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Decrypter{
privateKey: tt.fields.privateKey,
}
got, err := d.Decrypt(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Decrypter.Decrypt() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !assert.Equal(tt.want, got) {
t.Errorf("Decrypter.Decrypt() = %v, want %v", got, tt.want)
}
})
}
}
38 changes: 38 additions & 0 deletions crypto/cipher/nacl/encrypter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nacl

import (
"crypto/rand"
"io"

"github.com/mailchain/mailchain/crypto"
"github.com/mailchain/mailchain/crypto/cipher"
"github.com/mailchain/mailchain/crypto/ed25519"
"github.com/pkg/errors"
)

// NewEncrypter create a new encrypter with crypto rand for reader
func NewEncrypter() Encrypter {
return Encrypter{rand: rand.Reader}
}

// Encrypter will encrypt data using AES256CBC method
type Encrypter struct {
rand io.Reader
}

func (e Encrypter) Encrypt(recipientPublicKey crypto.PublicKey, message cipher.PlainContent) (cipher.EncryptedContent, error) {
if err := validatePublicKeyType(recipientPublicKey); err != nil {
return nil, err
}

return easySeal(message, recipientPublicKey.Bytes(), e.rand)
}

func validatePublicKeyType(recipientPublicKey crypto.PublicKey) error {
switch recipientPublicKey.(type) {
case ed25519.PublicKey:
return nil
default:
return errors.Errorf("invalid public key type for nacl encryption")
}
}

0 comments on commit b2ac5d0

Please sign in to comment.