diff --git a/pkg/core/account_state_test.go b/pkg/core/account_state_test.go index 1452211ed..82009be0d 100644 --- a/pkg/core/account_state_test.go +++ b/pkg/core/account_state_test.go @@ -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{ diff --git a/pkg/crypto/keys/nep2.go b/pkg/crypto/keys/nep2.go index fc40fb899..c67888792 100644 --- a/pkg/crypto/keys/nep2.go +++ b/pkg/crypto/keys/nep2.go @@ -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) } diff --git a/pkg/crypto/keys/nep2_test.go b/pkg/crypto/keys/nep2_test.go index e3a7a1ebc..995677a7a 100644 --- a/pkg/crypto/keys/nep2_test.go +++ b/pkg/crypto/keys/nep2_test.go @@ -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) } } diff --git a/pkg/crypto/keys/private_key.go b/pkg/crypto/keys/private_key.go index 75c659cd0..5ae6acad1 100644 --- a/pkg/crypto/keys/private_key.go +++ b/pkg/crypto/keys/private_key.go @@ -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. diff --git a/pkg/crypto/keys/private_key_test.go b/pkg/crypto/keys/private_key_test.go index dc45ef547..ea021b6ea 100644 --- a/pkg/crypto/keys/private_key_test.go +++ b/pkg/crypto/keys/private_key_test.go @@ -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) } } diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index edb2a1113..34c0b334d 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -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)) diff --git a/pkg/crypto/keys/sign_verify_test.go b/pkg/crypto/keys/sign_verify_test.go index 3eb5c1b8e..3c359d6ff 100755 --- a/pkg/crypto/keys/sign_verify_test.go +++ b/pkg/crypto/keys/sign_verify_test.go @@ -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 diff --git a/pkg/crypto/keys/wif.go b/pkg/crypto/keys/wif.go index 4fcd71860..df856412f 100644 --- a/pkg/crypto/keys/wif.go +++ b/pkg/crypto/keys/wif.go @@ -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 } diff --git a/pkg/rpc/txBuilder.go b/pkg/rpc/txBuilder.go index 401af25fd..3109c1424 100644 --- a/pkg/rpc/txBuilder.go +++ b/pkg/rpc/txBuilder.go @@ -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() diff --git a/pkg/wallet/account.go b/pkg/wallet/account.go index 401665b65..6f4677d37 100644 --- a/pkg/wallet/account.go +++ b/pkg/wallet/account.go @@ -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 }