From 46b82b4fb5d5d6d9df382585d393a98c889101c2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 16 Jan 2020 18:30:16 +0300 Subject: [PATCH] keys: don't return error from PrivateKey.Sign As it can't ever happen. --- pkg/consensus/crypto.go | 5 +++ pkg/core/helper_test.go | 6 ++-- pkg/crypto/keys/private_key.go | 4 +-- pkg/crypto/keys/private_key_test.go | 3 +- pkg/crypto/keys/sign_verify_test.go | 5 ++- pkg/rpc/txBuilder.go | 6 +--- pkg/vm/vm_test.go | 48 ++++++++++------------------- 7 files changed, 30 insertions(+), 47 deletions(-) diff --git a/pkg/consensus/crypto.go b/pkg/consensus/crypto.go index 97f97fdc4..4088a64c9 100644 --- a/pkg/consensus/crypto.go +++ b/pkg/consensus/crypto.go @@ -24,6 +24,11 @@ func (p *privateKey) UnmarshalBinary(data []byte) (err error) { return } +// Sign implements dbft's crypto.PrivateKey interface. +func (p *privateKey) Sign(data []byte) ([]byte, error) { + return p.PrivateKey.Sign(data), nil +} + // publicKey is a wrapper around keys.PublicKey // which implements crypto.PublicKey interface. type publicKey struct { diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index f750af78f..888fa7219 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -82,9 +82,9 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block { panic(err) } b := b.GetHashableData() - sig, err := pKey.Sign(b) - if err != nil || len(sig) != 64 { - panic(err) + sig := pKey.Sign(b) + if len(sig) != 64 { + panic("wrong signature length") } invScript = append(invScript, byte(opcode.PUSHBYTES64)) invScript = append(invScript, sig...) diff --git a/pkg/crypto/keys/private_key.go b/pkg/crypto/keys/private_key.go index cca174d78..c3aef6663 100644 --- a/pkg/crypto/keys/private_key.go +++ b/pkg/crypto/keys/private_key.go @@ -105,7 +105,7 @@ func (p *PrivateKey) Signature() []byte { } // Sign signs arbitrary length data using the private key. -func (p *PrivateKey) Sign(data []byte) ([]byte, error) { +func (p *PrivateKey) Sign(data []byte) []byte { var ( privateKey = p.ecdsa() digest = sha256.Sum256(data) @@ -120,7 +120,7 @@ func (p *PrivateKey) Sign(data []byte) ([]byte, error) { copy(signature[curveOrderByteSize-len(rBytes):], rBytes) copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes) - return signature, nil + return signature } // ecsda converts the key to a usable ecsda.PrivateKey for signing data. diff --git a/pkg/crypto/keys/private_key_test.go b/pkg/crypto/keys/private_key_test.go index 570ba021d..e457cfda2 100644 --- a/pkg/crypto/keys/private_key_test.go +++ b/pkg/crypto/keys/private_key_test.go @@ -48,8 +48,7 @@ func TestSigning(t *testing.T) { //Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299 PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721") - data, err := PrivateKey.Sign([]byte("sample")) - assert.Nil(t, err) + data := PrivateKey.Sign([]byte("sample")) r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" diff --git a/pkg/crypto/keys/sign_verify_test.go b/pkg/crypto/keys/sign_verify_test.go index 8c9195642..7ba494947 100644 --- a/pkg/crypto/keys/sign_verify_test.go +++ b/pkg/crypto/keys/sign_verify_test.go @@ -13,8 +13,7 @@ func TestPubKeyVerify(t *testing.T) { privKey, err := NewPrivateKey() assert.Nil(t, err) - signedData, err := privKey.Sign(data) - assert.Nil(t, err) + signedData := privKey.Sign(data) pubKey := privKey.PublicKey() result := pubKey.Verify(signedData, hashedData.BytesBE()) expected := true @@ -28,7 +27,7 @@ func TestWrongPubKey(t *testing.T) { privKey, _ := NewPrivateKey() sample := []byte("sample") hashedData := hash.Sha256(sample) - signedData, _ := privKey.Sign(sample) + signedData := privKey.Sign(sample) secondPrivKey, _ := NewPrivateKey() wrongPubKey := secondPrivKey.PublicKey() diff --git a/pkg/rpc/txBuilder.go b/pkg/rpc/txBuilder.go index 6a31f29ec..38c8d2665 100644 --- a/pkg/rpc/txBuilder.go +++ b/pkg/rpc/txBuilder.go @@ -96,7 +96,6 @@ func SignTx(tx *transaction.Transaction, wif *keys.WIF) error { // GetInvocationScript returns NEO VM script containing transaction signature. func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) { var ( - err error buf = io.NewBufBinWriter() signature []byte ) @@ -105,10 +104,7 @@ func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, er return nil, errs.Wrap(buf.Err, "Failed to encode transaction to binary") } data := buf.Bytes() - signature, err = wif.PrivateKey.Sign(data[:(len(data) - 1)]) - if err != nil { - return nil, errs.Wrap(err, "Failed ti sign transaction with private key") - } + signature = wif.PrivateKey.Sign(data[:(len(data) - 1)]) return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil } diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 2fb8fc634..a8e02606d 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -2477,8 +2477,7 @@ func TestCHECKSIGNoSigLoaded(t *testing.T) { pk, err := keys.NewPrivateKey() assert.Nil(t, err) msg := "NEO - An Open Network For Smart Economy" - sig, err := pk.Sign([]byte(msg)) - assert.Nil(t, err) + sig := pk.Sign([]byte(msg)) pbytes := pk.PublicKey().Bytes() vm := load(prog) vm.estack.PushVal(sig) @@ -2491,8 +2490,7 @@ func TestCHECKSIGBadKey(t *testing.T) { pk, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig, err := pk.Sign(msg) - assert.Nil(t, err) + sig := pk.Sign(msg) pbytes := pk.PublicKey().Bytes()[:4] vm := load(prog) vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) @@ -2506,8 +2504,7 @@ func TestCHECKSIGWrongSig(t *testing.T) { pk, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig, err := pk.Sign(msg) - assert.Nil(t, err) + sig := pk.Sign(msg) pbytes := pk.PublicKey().Bytes() vm := load(prog) vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) @@ -2523,8 +2520,7 @@ func TestCHECKSIGGood(t *testing.T) { pk, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig, err := pk.Sign(msg) - assert.Nil(t, err) + sig := pk.Sign(msg) pbytes := pk.PublicKey().Bytes() vm := load(prog) vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) @@ -2540,8 +2536,7 @@ func TestVERIFYGood(t *testing.T) { pk, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig, err := pk.Sign(msg) - assert.Nil(t, err) + sig := pk.Sign(msg) pbytes := pk.PublicKey().Bytes() vm := load(prog) vm.estack.PushVal(msg) @@ -2557,8 +2552,7 @@ func TestVERIFYBad(t *testing.T) { pk, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig, err := pk.Sign(msg) - assert.Nil(t, err) + sig := pk.Sign(msg) pbytes := pk.PublicKey().Bytes() vm := load(prog) vm.estack.PushVal(util.ArrayReverse(msg)) @@ -2592,10 +2586,8 @@ func TestCHECKMULTISIGNotEnoughKeys(t *testing.T) { pk2, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig1, err := pk1.Sign(msg) - assert.Nil(t, err) - sig2, err := pk2.Sign(msg) - assert.Nil(t, err) + sig1 := pk1.Sign(msg) + sig2 := pk2.Sign(msg) pbytes1 := pk1.PublicKey().Bytes() vm := load(prog) vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) @@ -2611,10 +2603,8 @@ func TestCHECKMULTISIGNoHash(t *testing.T) { pk2, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig1, err := pk1.Sign(msg) - assert.Nil(t, err) - sig2, err := pk2.Sign(msg) - assert.Nil(t, err) + sig1 := pk1.Sign(msg) + sig2 := pk2.Sign(msg) pbytes1 := pk1.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes() vm := load(prog) @@ -2630,10 +2620,8 @@ func TestCHECKMULTISIGBadKey(t *testing.T) { pk2, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig1, err := pk1.Sign(msg) - assert.Nil(t, err) - sig2, err := pk2.Sign(msg) - assert.Nil(t, err) + sig1 := pk1.Sign(msg) + sig2 := pk2.Sign(msg) pbytes1 := pk1.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()[:4] vm := load(prog) @@ -2650,10 +2638,8 @@ func TestCHECKMULTISIGBadSig(t *testing.T) { pk2, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig1, err := pk1.Sign(msg) - assert.Nil(t, err) - sig2, err := pk2.Sign(msg) - assert.Nil(t, err) + sig1 := pk1.Sign(msg) + sig2 := pk2.Sign(msg) pbytes1 := pk1.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes() vm := load(prog) @@ -2672,10 +2658,8 @@ func TestCHECKMULTISIGGood(t *testing.T) { pk2, err := keys.NewPrivateKey() assert.Nil(t, err) msg := []byte("NEO - An Open Network For Smart Economy") - sig1, err := pk1.Sign(msg) - assert.Nil(t, err) - sig2, err := pk2.Sign(msg) - assert.Nil(t, err) + sig1 := pk1.Sign(msg) + sig2 := pk2.Sign(msg) pbytes1 := pk1.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes() vm := load(prog)