frostfs-api-go/pkg/owner/wallet.go
Leonard Lyubich e887368be6 [#340] owner: Prevent potential NPE in NEO3WalletFromPublicKey
Copy `Curve` field of `ecdsa.PublicKey` arg to `keys.PublicKey` instance in
`NEO3WalletFromPublicKey` function.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-09-20 14:41:29 +03:00

57 lines
1 KiB
Go

package owner
import (
"crypto/ecdsa"
"fmt"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
crypto "github.com/nspcc-dev/neofs-crypto"
)
// NEO3Wallet represents NEO3 wallet address.
type NEO3Wallet [NEO3WalletSize]byte
// NEO3WalletSize contains size of neo3 wallet.
const NEO3WalletSize = 25
// NEO3WalletFromPublicKey converts public key to NEO3 wallet address.
func NEO3WalletFromPublicKey(key *ecdsa.PublicKey) (*NEO3Wallet, error) {
if key == nil {
return nil, crypto.ErrEmptyPublicKey
}
neoPublicKey := keys.PublicKey{
Curve: key.Curve,
X: key.X,
Y: key.Y,
}
d, err := base58.Decode(neoPublicKey.Address())
if err != nil {
return nil, fmt.Errorf("can't decode neo3 address from key: %w", err)
}
w := new(NEO3Wallet)
copy(w.Bytes(), d)
return w, nil
}
func (w *NEO3Wallet) String() string {
if w != nil {
return base58.Encode(w[:])
}
return ""
}
// Bytes returns slice of NEO3 wallet address bytes.
func (w *NEO3Wallet) Bytes() []byte {
if w != nil {
return w[:]
}
return nil
}