ricochet-go/core/identity.go

75 lines
1.3 KiB
Go
Raw Normal View History

2016-07-01 03:18:55 +00:00
package core
import (
"crypto/rsa"
"encoding/base64"
"errors"
"github.com/yawning/bulb/utils/pkcs1"
"log"
)
2016-07-01 03:18:55 +00:00
type Identity struct {
2016-08-30 02:46:41 +00:00
core Ricochet
address string
privateKey *rsa.PrivateKey
2016-07-01 03:18:55 +00:00
contactList *ContactList
}
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 02:46:41 +00:00
return me, nil
}
func (me *Identity) loadIdentity() error {
config := me.core.Config().OpenRead()
defer config.Close()
2016-08-30 02:46:41 +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
}
me.privateKey, _, err = pkcs1.DecodePrivateKeyDER(keyData)
if err != nil {
2016-08-30 02:46:41 +00:00
return err
}
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")
}
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
}