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)))
|
balances[randomUint256()] = util.Fixed8(int64(randomInt(1, 10000)))
|
||||||
k, err := keys.NewPrivateKey()
|
k, err := keys.NewPrivateKey()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
p, err := k.PublicKey()
|
votes[i] = k.PublicKey()
|
||||||
assert.Nil(t, err)
|
|
||||||
votes[i] = p
|
|
||||||
}
|
}
|
||||||
|
|
||||||
a := &AccountState{
|
a := &AccountState{
|
||||||
|
|
|
@ -44,10 +44,7 @@ func NEP2ScryptParams() ScryptParams {
|
||||||
// NEP2Encrypt encrypts a the PrivateKey using a given passphrase
|
// NEP2Encrypt encrypts a the PrivateKey using a given passphrase
|
||||||
// under the NEP-2 standard.
|
// under the NEP-2 standard.
|
||||||
func NEP2Encrypt(priv *PrivateKey, passphrase string) (s string, err error) {
|
func NEP2Encrypt(priv *PrivateKey, passphrase string) (s string, err error) {
|
||||||
address, err := priv.Address()
|
address := priv.Address()
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
|
|
||||||
addrHash := hash.Checksum([]byte(address))
|
addrHash := hash.Checksum([]byte(address))
|
||||||
// Normalize the passphrase according to the NFC standard.
|
// 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 s, errors.New("password mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
return privKey.WIF()
|
return privKey.WIF(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareAddressHash(priv *PrivateKey, inhash []byte) bool {
|
func compareAddressHash(priv *PrivateKey, inhash []byte) bool {
|
||||||
address, err := priv.Address()
|
address := priv.Address()
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
addrHash := hash.Checksum([]byte(address))
|
addrHash := hash.Checksum([]byte(address))
|
||||||
return bytes.Equal(addrHash, inhash)
|
return bytes.Equal(addrHash, inhash)
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,12 +31,10 @@ func TestNEP2Decrypt(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, testCase.PrivateKey, privKey.String())
|
assert.Equal(t, testCase.PrivateKey, privKey.String())
|
||||||
|
|
||||||
wif, err := privKey.WIF()
|
wif := privKey.WIF()
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, testCase.Wif, wif)
|
assert.Equal(t, testCase.Wif, wif)
|
||||||
|
|
||||||
address, err := privKey.Address()
|
address := privKey.Address()
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, testCase.Address, address)
|
assert.Equal(t, testCase.Address, address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
@ -58,18 +57,15 @@ func NewPrivateKeyFromRawBytes(b []byte) (*PrivateKey, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey derives the public key from the private key.
|
// PublicKey derives the public key from the private key.
|
||||||
func (p *PrivateKey) PublicKey() (*PublicKey, error) {
|
func (p *PrivateKey) PublicKey() *PublicKey {
|
||||||
var (
|
var (
|
||||||
c = elliptic.P256()
|
c = elliptic.P256()
|
||||||
q = new(big.Int).SetBytes(p.b)
|
q = new(big.Int).SetBytes(p.b)
|
||||||
)
|
)
|
||||||
|
|
||||||
x, y := c.ScalarBaseMult(q.Bytes())
|
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
|
// 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.
|
// WIF returns the (wallet import format) of the PrivateKey.
|
||||||
// Good documentation about this process can be found here:
|
// Good documentation about this process can be found here:
|
||||||
// https://en.bitcoin.it/wiki/Wallet_import_format
|
// https://en.bitcoin.it/wiki/Wallet_import_format
|
||||||
func (p *PrivateKey) WIF() (string, error) {
|
func (p *PrivateKey) WIF() string {
|
||||||
return WIFEncode(p.b, WIFVersion, true)
|
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
|
// Address derives the public NEO address that is coupled with the private key, and
|
||||||
// returns it as a string.
|
// returns it as a string.
|
||||||
func (p *PrivateKey) Address() (string, error) {
|
func (p *PrivateKey) Address() string {
|
||||||
pk, err := p.PublicKey()
|
pk := p.PublicKey()
|
||||||
if err != nil {
|
return pk.Address()
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return pk.Address(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signature creates the signature using the private key.
|
// Signature creates the signature using the private key.
|
||||||
func (p *PrivateKey) Signature() ([]byte, error) {
|
func (p *PrivateKey) Signature() []byte {
|
||||||
pk, err := p.PublicKey()
|
pk := p.PublicKey()
|
||||||
if err != nil {
|
return pk.Signature()
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return pk.Signature(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sign signs arbitrary length data using the private key.
|
// Sign signs arbitrary length data using the private key.
|
||||||
|
|
|
@ -14,14 +14,12 @@ func TestPrivateKey(t *testing.T) {
|
||||||
for _, testCase := range keytestcases.Arr {
|
for _, testCase := range keytestcases.Arr {
|
||||||
privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey)
|
privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
address, err := privKey.Address()
|
address := privKey.Address()
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, testCase.Address, address)
|
assert.Equal(t, testCase.Address, address)
|
||||||
|
|
||||||
wif, err := privKey.WIF()
|
wif := privKey.WIF()
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, testCase.Wif, wif)
|
assert.Equal(t, testCase.Wif, wif)
|
||||||
pubKey, _ := privKey.PublicKey()
|
pubKey := privKey.PublicKey()
|
||||||
assert.Equal(t, hex.EncodeToString(pubKey.Bytes()), testCase.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++ {
|
for i := 0; i < 4; i++ {
|
||||||
k, err := NewPrivateKey()
|
k, err := NewPrivateKey()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
p, err := k.PublicKey()
|
p := k.PublicKey()
|
||||||
assert.Nil(t, err)
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
assert.Nil(t, p.EncodeBinary(buf))
|
assert.Nil(t, p.EncodeBinary(buf))
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,7 @@ func TestPubKeyVerify(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
signedData, err := privKey.Sign(data)
|
signedData, err := privKey.Sign(data)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
pubKey, err := privKey.PublicKey()
|
pubKey := privKey.PublicKey()
|
||||||
assert.Nil(t, err)
|
|
||||||
result := pubKey.Verify(signedData, hashedData.Bytes())
|
result := pubKey.Verify(signedData, hashedData.Bytes())
|
||||||
expected := true
|
expected := true
|
||||||
assert.Equal(t, expected, result)
|
assert.Equal(t, expected, result)
|
||||||
|
@ -29,7 +28,7 @@ func TestWrongPubKey(t *testing.T) {
|
||||||
signedData, _ := privKey.Sign(sample)
|
signedData, _ := privKey.Sign(sample)
|
||||||
|
|
||||||
secondPrivKey, _ := NewPrivateKey()
|
secondPrivKey, _ := NewPrivateKey()
|
||||||
wrongPubKey, _ := secondPrivKey.PublicKey()
|
wrongPubKey := secondPrivKey.PublicKey()
|
||||||
|
|
||||||
actual := wrongPubKey.Verify(signedData, hashedData.Bytes())
|
actual := wrongPubKey.Verify(signedData, hashedData.Bytes())
|
||||||
expcted := false
|
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.
|
// GetVerificationScript returns NEO VM bytecode with checksig command for the public key.
|
||||||
func (wif WIF) GetVerificationScript() ([]byte, error) {
|
func (wif WIF) GetVerificationScript() []byte {
|
||||||
const (
|
const (
|
||||||
pushbytes33 = 0x21
|
pushbytes33 = 0x21
|
||||||
checksig = 0xac
|
checksig = 0xac
|
||||||
|
@ -101,11 +101,8 @@ func (wif WIF) GetVerificationScript() ([]byte, error) {
|
||||||
vScript []byte
|
vScript []byte
|
||||||
pubkey *PublicKey
|
pubkey *PublicKey
|
||||||
)
|
)
|
||||||
pubkey, err := wif.PrivateKey.PublicKey()
|
pubkey = wif.PrivateKey.PublicKey()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
vScript = append([]byte{pushbytes33}, pubkey.Bytes()...)
|
vScript = append([]byte{pushbytes33}, pubkey.Bytes()...)
|
||||||
vScript = append(vScript, checksig)
|
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
|
wif, assetID, address, amount, balancer = params.wif, params.assetID, params.address, params.value, params.balancer
|
||||||
)
|
)
|
||||||
|
|
||||||
if fromAddress, err = wif.PrivateKey.Address(); err != nil {
|
fromAddress = wif.PrivateKey.Address()
|
||||||
return nil, errs.Wrapf(err, "Failed to take address from WIF: %v", wif.S)
|
|
||||||
}
|
|
||||||
|
|
||||||
if fromAddressHash, err = crypto.Uint160DecodeAddress(fromAddress); err != nil {
|
if fromAddressHash, err = crypto.Uint160DecodeAddress(fromAddress); err != nil {
|
||||||
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress)
|
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 {
|
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
|
||||||
return nil, errs.Wrap(err, "Failed to create invocation script")
|
return nil, errs.Wrap(err, "Failed to create invocation script")
|
||||||
}
|
}
|
||||||
if witness.VerificationScript, err = wif.GetVerificationScript(); err != nil {
|
witness.VerificationScript = wif.GetVerificationScript()
|
||||||
return nil, errs.Wrap(err, "Failed to create verification script")
|
|
||||||
}
|
|
||||||
tx.Scripts = append(tx.Scripts, &witness)
|
tx.Scripts = append(tx.Scripts, &witness)
|
||||||
tx.Hash()
|
tx.Hash()
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ func NewAccount() (*Account, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newAccountFromPrivateKey(priv)
|
return newAccountFromPrivateKey(priv), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecryptAccount decrypt the encryptedWIF with the given passphrase and
|
// DecryptAccount decrypt the encryptedWIF with the given passphrase and
|
||||||
|
@ -87,23 +87,14 @@ func NewAccountFromWIF(wif string) (*Account, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newAccountFromPrivateKey(privKey)
|
return newAccountFromPrivateKey(privKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// newAccountFromPrivateKey created a wallet from the given PrivateKey.
|
// newAccountFromPrivateKey created a wallet from the given PrivateKey.
|
||||||
func newAccountFromPrivateKey(p *keys.PrivateKey) (*Account, error) {
|
func newAccountFromPrivateKey(p *keys.PrivateKey) *Account {
|
||||||
pubKey, err := p.PublicKey()
|
pubKey := p.PublicKey()
|
||||||
if err != nil {
|
pubAddr := p.Address()
|
||||||
return nil, err
|
wif := p.WIF()
|
||||||
}
|
|
||||||
pubAddr, err := p.Address()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
wif, err := p.WIF()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
a := &Account{
|
a := &Account{
|
||||||
publicKey: pubKey.Bytes(),
|
publicKey: pubKey.Bytes(),
|
||||||
|
@ -112,5 +103,5 @@ func newAccountFromPrivateKey(p *keys.PrivateKey) (*Account, error) {
|
||||||
wif: wif,
|
wif: wif,
|
||||||
}
|
}
|
||||||
|
|
||||||
return a, nil
|
return a
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue