Skip to content

Commit

Permalink
core: cleanup initIdentity
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Dec 21, 2014
1 parent e7e6a26 commit 0d760a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
63 changes: 37 additions & 26 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ func NewIpfsNode(ctx context.Context, cfg *config.Config, online bool) (n *IpfsN
return nil, debugerror.Wrap(err)
}

// setup peerstore + local peer identity
n.Peerstore = peer.NewPeerstore()
n.Identity, n.PrivateKey, err = initIdentity(&n.Config.Identity, n.Peerstore, online)
// setup local peer identity
n.Identity, n.PrivateKey, err = initIdentity(&n.Config.Identity, online)
if err != nil {
return nil, debugerror.Wrap(err)
}

// setup Peerstore
n.Peerstore = peer.NewPeerstore()
if n.PrivateKey != nil {
n.Peerstore.AddPrivKey(n.Identity, n.PrivateKey)
}

blockstore, err := bstore.WriteCached(bstore.NewBlockstore(n.Datastore), kSizeBlockstoreWriteCache)
n.Exchange = offline.Exchange(blockstore)

Expand Down Expand Up @@ -178,41 +183,47 @@ func (n *IpfsNode) OnlineMode() bool {
return n.onlineMode
}

func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (p peer.ID, sk ic.PrivKey, err error) {
func initIdentity(cfg *config.Identity, online bool) (peer.ID, ic.PrivKey, error) {

if cfg.PeerID == "" {
err = debugerror.New("Identity was not set in config (was ipfs init run?)")
return
return "", nil, debugerror.New("Identity was not set in config (was ipfs init run?)")
}

if len(cfg.PeerID) == 0 {
err = debugerror.New("No peer ID in config! (was ipfs init run?)")
return
return "", nil, debugerror.New("No peer ID in config! (was ipfs init run?)")
}

id := peer.ID(b58.Decode(cfg.PeerID))

// when not online, don't need to parse private keys (yet)
if online {
var sk2 ic.PrivKey
sk2, err = cfg.DecodePrivateKey("passphrase todo!")
if err != nil {
return
}
if !online {
return id, nil, nil
}

var id2 peer.ID
id2, err = peer.IDFromPrivateKey(sk2)
if err != nil {
return
}
if id2 != id {
err = fmt.Errorf("private key in config does not match id: %s != %s", id, id2)
return
}
sk, err := loadPrivateKey(cfg, id)
if err != nil {
return "", nil, err
}

return id, sk, nil
}

sk = sk2
func loadPrivateKey(cfg *config.Identity, id peer.ID) (ic.PrivKey, error) {
sk, err := cfg.DecodePrivateKey("passphrase todo!")
if err != nil {
return nil, err
}
p = id
return

id2, err := peer.IDFromPrivateKey(sk)
if err != nil {
return nil, err
}

if id2 != id {
return nil, fmt.Errorf("private key in config does not match id: %s != %s", id, id2)
}

return sk, nil
}

func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
Expand Down
4 changes: 4 additions & 0 deletions peer/peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func (kb *keybook) PrivKey(p ID) ic.PrivKey {

func (kb *keybook) AddPrivKey(p ID, sk ic.PrivKey) error {

if sk == nil {
return errors.New("sk is nil (PrivKey)")
}

// check it's correct first
if !p.MatchesPrivateKey(sk) {
return errors.New("ID does not match PrivateKey")
Expand Down

0 comments on commit 0d760a9

Please sign in to comment.