keys: don't return error from PrivateKey.Sign

As it can't ever happen.
This commit is contained in:
Roman Khimov 2020-01-16 18:30:16 +03:00
parent e2fff3bb1d
commit 46b82b4fb5
7 changed files with 30 additions and 47 deletions

View file

@ -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 {

View file

@ -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...)

View file

@ -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.

View file

@ -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"

View file

@ -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()

View file

@ -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
}

View file

@ -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)