2021-03-30 22:46:33 +00:00
|
|
|
package neofs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Credentials contains methods that needed to work with NeoFS.
|
|
|
|
Credentials interface {
|
|
|
|
WIF() string
|
|
|
|
Owner() *owner.ID
|
|
|
|
PublicKey() *ecdsa.PublicKey
|
|
|
|
PrivateKey() *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:08:39 +00:00
|
|
|
credentials struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
ownerID *owner.ID
|
|
|
|
wif string
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates an instance of Credentials through string representation of secret.
|
|
|
|
// It allows passing WIF, path, hex-encoded and others.
|
|
|
|
func NewCredentials(secret string) (Credentials, error) {
|
|
|
|
key, err := crypto.LoadPrivateKey(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return setFromPrivateKey(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrivateKey returns ecdsa.PrivateKey.
|
2021-03-31 19:08:39 +00:00
|
|
|
func (c *credentials) PrivateKey() *ecdsa.PrivateKey {
|
2021-03-30 22:46:33 +00:00
|
|
|
return c.key
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKey returns ecdsa.PublicKey.
|
2021-03-31 19:08:39 +00:00
|
|
|
func (c *credentials) PublicKey() *ecdsa.PublicKey {
|
2021-03-30 22:46:33 +00:00
|
|
|
return &c.key.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Owner returns owner.ID.
|
2021-03-31 19:08:39 +00:00
|
|
|
func (c *credentials) Owner() *owner.ID {
|
|
|
|
return c.ownerID
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WIF returns string representation of WIF.
|
2021-03-31 19:08:39 +00:00
|
|
|
func (c *credentials) WIF() string {
|
2021-03-30 22:46:33 +00:00
|
|
|
return c.wif
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:08:39 +00:00
|
|
|
func setFromPrivateKey(key *ecdsa.PrivateKey) (*credentials, error) {
|
2021-03-30 22:46:33 +00:00
|
|
|
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ownerID := owner.NewIDFromNeo3Wallet(wallet)
|
|
|
|
wif, err := crypto.WIFEncode(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-31 19:08:39 +00:00
|
|
|
return &credentials{key: key, ownerID: ownerID, wif: wif}, nil
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|