2016-07-01 03:18:55 +00:00
|
|
|
package core
|
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"github.com/yawning/bulb/utils/pkcs1"
|
|
|
|
"log"
|
|
|
|
)
|
2016-07-01 03:18:55 +00:00
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
type Identity struct {
|
2016-08-30 02:46:41 +00:00
|
|
|
core Ricochet
|
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
address string
|
|
|
|
privateKey *rsa.PrivateKey
|
2016-07-01 03:18:55 +00:00
|
|
|
|
|
|
|
contactList *ContactList
|
|
|
|
}
|
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
func CreateIdentity(core Ricochet) (*Identity, error) {
|
2016-08-30 02:46:41 +00:00
|
|
|
me := &Identity{
|
|
|
|
core: core,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := me.loadIdentity()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Loading identity failed: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactList, err := LoadContactList(core)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Loading contact list failed: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
me.contactList = contactList
|
2016-08-30 01:49:30 +00:00
|
|
|
|
2016-08-30 02:46:41 +00:00
|
|
|
return me, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Identity) loadIdentity() error {
|
|
|
|
config := me.core.Config().OpenRead()
|
2016-08-30 01:49:30 +00:00
|
|
|
defer config.Close()
|
2016-08-30 02:46:41 +00:00
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
if config.Identity.ServiceKey != "" {
|
|
|
|
keyData, err := base64.StdEncoding.DecodeString(config.Identity.ServiceKey)
|
|
|
|
if err != nil {
|
2016-08-30 02:46:41 +00:00
|
|
|
return err
|
2016-08-30 01:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me.privateKey, _, err = pkcs1.DecodePrivateKeyDER(keyData)
|
|
|
|
if err != nil {
|
2016-08-30 02:46:41 +00:00
|
|
|
return err
|
2016-08-30 01:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me.address, err = pkcs1.OnionAddr(&me.privateKey.PublicKey)
|
|
|
|
if err != nil {
|
2016-08-30 02:46:41 +00:00
|
|
|
return err
|
|
|
|
} else if me.address == "" {
|
|
|
|
return errors.New("Invalid onion address")
|
2016-08-30 01:49:30 +00:00
|
|
|
}
|
|
|
|
me.address = "ricochet:" + me.address
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 02:46:41 +00:00
|
|
|
return nil
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Identity) Address() string {
|
|
|
|
return me.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Identity) ContactList() *ContactList {
|
|
|
|
return me.contactList
|
|
|
|
}
|