forked from TrueCloudLab/neoneo-go
keys: simplify error handling for PublicKey() and associated
PublicKey() for PrivateKey now just can't fail and it makes no sense to return an error from it. There is a lot of associated functionality for which this also is true, so adjust it accordingly and simplify a lot of code.
This commit is contained in:
parent
60bc2e8053
commit
2c3e92923f
10 changed files with 40 additions and 74 deletions
|
@ -19,9 +19,7 @@ func TestDecodeEncodeAccountState(t *testing.T) {
|
|||
balances[randomUint256()] = util.Fixed8(int64(randomInt(1, 10000)))
|
||||
k, err := keys.NewPrivateKey()
|
||||
assert.Nil(t, err)
|
||||
p, err := k.PublicKey()
|
||||
assert.Nil(t, err)
|
||||
votes[i] = p
|
||||
votes[i] = k.PublicKey()
|
||||
}
|
||||
|
||||
a := &AccountState{
|
||||
|
|
|
@ -44,10 +44,7 @@ func NEP2ScryptParams() ScryptParams {
|
|||
// NEP2Encrypt encrypts a the PrivateKey using a given passphrase
|
||||
// under the NEP-2 standard.
|
||||
func NEP2Encrypt(priv *PrivateKey, passphrase string) (s string, err error) {
|
||||
address, err := priv.Address()
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
address := priv.Address()
|
||||
|
||||
addrHash := hash.Checksum([]byte(address))
|
||||
// Normalize the passphrase according to the NFC standard.
|
||||
|
@ -119,14 +116,11 @@ func NEP2Decrypt(key, passphrase string) (s string, err error) {
|
|||
return s, errors.New("password mismatch")
|
||||
}
|
||||
|
||||
return privKey.WIF()
|
||||
return privKey.WIF(), nil
|
||||
}
|
||||
|
||||
func compareAddressHash(priv *PrivateKey, inhash []byte) bool {
|
||||
address, err := priv.Address()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
address := priv.Address()
|
||||
addrHash := hash.Checksum([]byte(address))
|
||||
return bytes.Equal(addrHash, inhash)
|
||||
}
|
||||
|
|
|
@ -31,12 +31,10 @@ func TestNEP2Decrypt(t *testing.T) {
|
|||
|
||||
assert.Equal(t, testCase.PrivateKey, privKey.String())
|
||||
|
||||
wif, err := privKey.WIF()
|
||||
assert.Nil(t, err)
|
||||
wif := privKey.WIF()
|
||||
assert.Equal(t, testCase.Wif, wif)
|
||||
|
||||
address, err := privKey.Address()
|
||||
assert.Nil(t, err)
|
||||
address := privKey.Address()
|
||||
assert.Equal(t, testCase.Address, address)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
|
@ -58,18 +57,15 @@ func NewPrivateKeyFromRawBytes(b []byte) (*PrivateKey, error) {
|
|||
}
|
||||
|
||||
// PublicKey derives the public key from the private key.
|
||||
func (p *PrivateKey) PublicKey() (*PublicKey, error) {
|
||||
func (p *PrivateKey) PublicKey() *PublicKey {
|
||||
var (
|
||||
c = elliptic.P256()
|
||||
q = new(big.Int).SetBytes(p.b)
|
||||
)
|
||||
|
||||
x, y := c.ScalarBaseMult(q.Bytes())
|
||||
if !c.IsOnCurve(x, y) {
|
||||
return nil, errors.New("failed to derive public key using elliptic curve")
|
||||
}
|
||||
|
||||
return &PublicKey{X: x, Y: y}, nil
|
||||
return &PublicKey{X: x, Y: y}
|
||||
}
|
||||
|
||||
// NewPrivateKeyFromWIF returns a NEO PrivateKey from the given
|
||||
|
@ -85,27 +81,27 @@ func NewPrivateKeyFromWIF(wif string) (*PrivateKey, error) {
|
|||
// WIF returns the (wallet import format) of the PrivateKey.
|
||||
// Good documentation about this process can be found here:
|
||||
// https://en.bitcoin.it/wiki/Wallet_import_format
|
||||
func (p *PrivateKey) WIF() (string, error) {
|
||||
return WIFEncode(p.b, WIFVersion, true)
|
||||
func (p *PrivateKey) WIF() string {
|
||||
w, err := WIFEncode(p.b, WIFVersion, true)
|
||||
// The only way WIFEncode() can fail is if we're to give it a key of
|
||||
// wrong size, but we have a proper key here, aren't we?
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// Address derives the public NEO address that is coupled with the private key, and
|
||||
// returns it as a string.
|
||||
func (p *PrivateKey) Address() (string, error) {
|
||||
pk, err := p.PublicKey()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pk.Address(), nil
|
||||
func (p *PrivateKey) Address() string {
|
||||
pk := p.PublicKey()
|
||||
return pk.Address()
|
||||
}
|
||||
|
||||
// Signature creates the signature using the private key.
|
||||
func (p *PrivateKey) Signature() ([]byte, error) {
|
||||
pk, err := p.PublicKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pk.Signature(), nil
|
||||
func (p *PrivateKey) Signature() []byte {
|
||||
pk := p.PublicKey()
|
||||
return pk.Signature()
|
||||
}
|
||||
|
||||
// Sign signs arbitrary length data using the private key.
|
||||
|
|
|
@ -14,14 +14,12 @@ func TestPrivateKey(t *testing.T) {
|
|||
for _, testCase := range keytestcases.Arr {
|
||||
privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey)
|
||||
assert.Nil(t, err)
|
||||
address, err := privKey.Address()
|
||||
assert.Nil(t, err)
|
||||
address := privKey.Address()
|
||||
assert.Equal(t, testCase.Address, address)
|
||||
|
||||
wif, err := privKey.WIF()
|
||||
assert.Nil(t, err)
|
||||
wif := privKey.WIF()
|
||||
assert.Equal(t, testCase.Wif, wif)
|
||||
pubKey, _ := privKey.PublicKey()
|
||||
pubKey := privKey.PublicKey()
|
||||
assert.Equal(t, hex.EncodeToString(pubKey.Bytes()), testCase.PublicKey)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,7 @@ func TestEncodeDecodePublicKey(t *testing.T) {
|
|||
for i := 0; i < 4; i++ {
|
||||
k, err := NewPrivateKey()
|
||||
assert.Nil(t, err)
|
||||
p, err := k.PublicKey()
|
||||
assert.Nil(t, err)
|
||||
p := k.PublicKey()
|
||||
buf := new(bytes.Buffer)
|
||||
assert.Nil(t, p.EncodeBinary(buf))
|
||||
|
||||
|
|
|
@ -15,8 +15,7 @@ func TestPubKeyVerify(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
signedData, err := privKey.Sign(data)
|
||||
assert.Nil(t, err)
|
||||
pubKey, err := privKey.PublicKey()
|
||||
assert.Nil(t, err)
|
||||
pubKey := privKey.PublicKey()
|
||||
result := pubKey.Verify(signedData, hashedData.Bytes())
|
||||
expected := true
|
||||
assert.Equal(t, expected, result)
|
||||
|
@ -29,7 +28,7 @@ func TestWrongPubKey(t *testing.T) {
|
|||
signedData, _ := privKey.Sign(sample)
|
||||
|
||||
secondPrivKey, _ := NewPrivateKey()
|
||||
wrongPubKey, _ := secondPrivKey.PublicKey()
|
||||
wrongPubKey := secondPrivKey.PublicKey()
|
||||
|
||||
actual := wrongPubKey.Verify(signedData, hashedData.Bytes())
|
||||
expcted := false
|
||||
|
|
|
@ -92,7 +92,7 @@ func WIFDecode(wif string, version byte) (*WIF, error) {
|
|||
}
|
||||
|
||||
// GetVerificationScript returns NEO VM bytecode with checksig command for the public key.
|
||||
func (wif WIF) GetVerificationScript() ([]byte, error) {
|
||||
func (wif WIF) GetVerificationScript() []byte {
|
||||
const (
|
||||
pushbytes33 = 0x21
|
||||
checksig = 0xac
|
||||
|
@ -101,11 +101,8 @@ func (wif WIF) GetVerificationScript() ([]byte, error) {
|
|||
vScript []byte
|
||||
pubkey *PublicKey
|
||||
)
|
||||
pubkey, err := wif.PrivateKey.PublicKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pubkey = wif.PrivateKey.PublicKey()
|
||||
vScript = append([]byte{pushbytes33}, pubkey.Bytes()...)
|
||||
vScript = append(vScript, checksig)
|
||||
return vScript, nil
|
||||
return vScript
|
||||
}
|
||||
|
|
|
@ -25,9 +25,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
|
|||
wif, assetID, address, amount, balancer = params.wif, params.assetID, params.address, params.value, params.balancer
|
||||
)
|
||||
|
||||
if fromAddress, err = wif.PrivateKey.Address(); err != nil {
|
||||
return nil, errs.Wrapf(err, "Failed to take address from WIF: %v", wif.S)
|
||||
}
|
||||
fromAddress = wif.PrivateKey.Address()
|
||||
|
||||
if fromAddressHash, err = crypto.Uint160DecodeAddress(fromAddress); err != nil {
|
||||
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress)
|
||||
|
@ -59,9 +57,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
|
|||
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
|
||||
return nil, errs.Wrap(err, "Failed to create invocation script")
|
||||
}
|
||||
if witness.VerificationScript, err = wif.GetVerificationScript(); err != nil {
|
||||
return nil, errs.Wrap(err, "Failed to create verification script")
|
||||
}
|
||||
witness.VerificationScript = wif.GetVerificationScript()
|
||||
tx.Scripts = append(tx.Scripts, &witness)
|
||||
tx.Hash()
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ func NewAccount() (*Account, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newAccountFromPrivateKey(priv)
|
||||
return newAccountFromPrivateKey(priv), nil
|
||||
}
|
||||
|
||||
// DecryptAccount decrypt the encryptedWIF with the given passphrase and
|
||||
|
@ -87,23 +87,14 @@ func NewAccountFromWIF(wif string) (*Account, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newAccountFromPrivateKey(privKey)
|
||||
return newAccountFromPrivateKey(privKey), nil
|
||||
}
|
||||
|
||||
// newAccountFromPrivateKey created a wallet from the given PrivateKey.
|
||||
func newAccountFromPrivateKey(p *keys.PrivateKey) (*Account, error) {
|
||||
pubKey, err := p.PublicKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pubAddr, err := p.Address()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wif, err := p.WIF()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func newAccountFromPrivateKey(p *keys.PrivateKey) *Account {
|
||||
pubKey := p.PublicKey()
|
||||
pubAddr := p.Address()
|
||||
wif := p.WIF()
|
||||
|
||||
a := &Account{
|
||||
publicKey: pubKey.Bytes(),
|
||||
|
@ -112,5 +103,5 @@ func newAccountFromPrivateKey(p *keys.PrivateKey) (*Account, error) {
|
|||
wif: wif,
|
||||
}
|
||||
|
||||
return a, nil
|
||||
return a
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue