2019-02-07 19:22:49 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2019-02-07 19:22:49 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certDB struct {
|
|
|
|
db *dbx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *certDB) SavePublicKey(ctx context.Context, nodeID storj.NodeID, publicKey crypto.PublicKey) error {
|
|
|
|
tx, err := b.db.Open(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
// no rows err, so create/insert an entry
|
2019-02-07 20:39:20 +00:00
|
|
|
pubbytes, err := pkcrypto.PublicKeyToPKIX(publicKey)
|
2019-02-07 19:22:49 +00:00
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return Error.Wrap(errs.Combine(err, tx.Rollback()))
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return Error.Wrap(errs.Combine(err, tx.Rollback()))
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
|
|
|
_, err = tx.Create_CertRecord(ctx,
|
|
|
|
dbx.CertRecord_Publickey(pubbytes),
|
|
|
|
dbx.CertRecord_Id(nodeID.Bytes()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return Error.Wrap(errs.Combine(err, tx.Rollback()))
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// nodeID entry already exists, just return
|
|
|
|
return Error.Wrap(tx.Rollback())
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error.Wrap(tx.Commit())
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
func (b *certDB) GetPublicKey(ctx context.Context, nodeID storj.NodeID) (crypto.PublicKey, error) {
|
2019-02-07 19:22:49 +00:00
|
|
|
dbxInfo, err := b.db.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-07 20:39:20 +00:00
|
|
|
pubkey, err := pkcrypto.PublicKeyFromPKIX(dbxInfo.Publickey)
|
2019-02-07 19:22:49 +00:00
|
|
|
if err != nil {
|
2019-02-22 21:17:35 +00:00
|
|
|
return nil, Error.New("Failed to extract Public Key from Order: %+v", err)
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
2019-02-07 20:39:20 +00:00
|
|
|
return pubkey, nil
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|