Skip to content

Commit

Permalink
Merge pull request #705 from glouvigny/dev/add-contact-improvements
Browse files Browse the repository at this point in the history
feat(core): better contact request management
  • Loading branch information
moul committed Dec 1, 2018
2 parents 7ffcf12 + 1e12966 commit bad5112
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Expand Up @@ -2,7 +2,7 @@ import { ScrollView, TextInput, Platform, Clipboard } from 'react-native'
import { btoa } from 'b64-lite'
import React, { PureComponent } from 'react'

import { Button, Flex, TextInputMultilineFix } from './index'
import { Button, Flex, TextInputMultilineFix, Text } from './index'
import { RelayContext } from '../../relay'
import { colors } from '../../constants'
import {
Expand Down Expand Up @@ -67,6 +67,7 @@ export default class PublicKeyWithActions extends PureComponent {
: [props.initialKey, props.initialName]
const missingInitialData = props.initialKey === undefined
this.state = {
err: null,
contact: {
id: initialKey || '',
displayName: initialName || '',
Expand Down Expand Up @@ -102,7 +103,6 @@ export default class PublicKeyWithActions extends PureComponent {
this.props.navigation.goBack(null)
} catch (err) {
this.setState({ err })
console.error(err)
}
}

Expand All @@ -119,6 +119,13 @@ export default class PublicKeyWithActions extends PureComponent {
contact: { id, displayName },
} = this.state

let errors = []
try {
errors = this.state.err.res.errors
} catch (e) {
// noop
}

return (
<ScrollView>
<Flex.Rows style={[padding]} align='center'>
Expand Down Expand Up @@ -178,6 +185,7 @@ export default class PublicKeyWithActions extends PureComponent {
onPress={this.onSubmit}
/>
) : null}
{errors.map((err, i) => <Text multiline key={i}>{err.message}</Text>)}
</Flex.Rows>
</ScrollView>
)
Expand Down
17 changes: 17 additions & 0 deletions core/entity/contact.go
@@ -1,9 +1,26 @@
package entity

import (
"crypto/x509"
"encoding/base64"

"github.com/pkg/errors"
)

func (c Contact) Validate() error {
if c.ID == "" {
return ErrInvalidEntity
}

pubKeyBytes, err := base64.StdEncoding.DecodeString(c.ID)
if err != nil {
return err
}

if _, err := x509.ParsePKIXPublicKey(pubKeyBytes); err != nil {
return errors.Wrap(ErrInvalidEntity, "invalid public key")
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions core/node/errors.go
Expand Up @@ -3,6 +3,7 @@ package node
import "errors"

var (
ErrContactIsMyself = errors.New("contact is myself")
ErrEntityAlreadyExists = errors.New("entity already exists")
ErrInvalidEventSender = errors.New("invalid event sender")
ErrInvalidInput = errors.New("invalid input")
Expand Down
22 changes: 20 additions & 2 deletions core/node/nodeapi.go
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/jinzhu/gorm"

"berty.tech/core/api/node"
"berty.tech/core/api/p2p"
"berty.tech/core/entity"
Expand Down Expand Up @@ -162,19 +164,35 @@ func (n *Node) ContactRequest(ctx context.Context, req *node.ContactRequestInput

// input validation
if err := req.Contact.Validate(); err != nil {
return nil, errors.Wrap(err, ErrInvalidInput.Error())
return nil, ErrInvalidInput
}

// check for duplicate
sql := n.sql(ctx)
contact, err := bsql.FindContact(sql, req.Contact)
if err != nil {

if errors.Cause(err) == gorm.ErrRecordNotFound {
// save contact in database
contact = req.Contact
contact.Status = entity.Contact_IsRequested
if err = sql.Set("gorm:association_autoupdate", true).Save(contact).Error; err != nil {
return nil, errors.Wrap(err, "failed to save contact")
}
} else if err != nil {
return nil, err

} else if contact.Status == entity.Contact_RequestedMe {
logger().Info("this contact has already asked us, accepting the request")
return n.ContactAcceptRequest(ctx, contact)

} else if contact.Status == entity.Contact_IsRequested {
logger().Info("contact has already been requested, sending event again")

} else if contact.Status == entity.Contact_Myself {
return nil, ErrContactIsMyself

} else {
return nil, ErrEntityAlreadyExists
}

// send request to peer
Expand Down

0 comments on commit bad5112

Please sign in to comment.