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 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 // publicKey is a wrapper around keys.PublicKey
// which implements crypto.PublicKey interface. // which implements crypto.PublicKey interface.
type publicKey struct { type publicKey struct {

View file

@ -82,9 +82,9 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
panic(err) panic(err)
} }
b := b.GetHashableData() b := b.GetHashableData()
sig, err := pKey.Sign(b) sig := pKey.Sign(b)
if err != nil || len(sig) != 64 { if len(sig) != 64 {
panic(err) panic("wrong signature length")
} }
invScript = append(invScript, byte(opcode.PUSHBYTES64)) invScript = append(invScript, byte(opcode.PUSHBYTES64))
invScript = append(invScript, sig...) invScript = append(invScript, sig...)

View file

@ -105,7 +105,7 @@ func (p *PrivateKey) Signature() []byte {
} }
// Sign signs arbitrary length data using the private key. // 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 ( var (
privateKey = p.ecdsa() privateKey = p.ecdsa()
digest = sha256.Sum256(data) 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-len(rBytes):], rBytes)
copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes) copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes)
return signature, nil return signature
} }
// ecsda converts the key to a usable ecsda.PrivateKey for signing data. // 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 //Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299
PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721") PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721")
data, err := PrivateKey.Sign([]byte("sample")) data := PrivateKey.Sign([]byte("sample"))
assert.Nil(t, err)
r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716"
s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8"

View file

@ -13,8 +13,7 @@ func TestPubKeyVerify(t *testing.T) {
privKey, err := NewPrivateKey() privKey, err := NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
signedData, err := privKey.Sign(data) signedData := privKey.Sign(data)
assert.Nil(t, err)
pubKey := privKey.PublicKey() pubKey := privKey.PublicKey()
result := pubKey.Verify(signedData, hashedData.BytesBE()) result := pubKey.Verify(signedData, hashedData.BytesBE())
expected := true expected := true
@ -28,7 +27,7 @@ func TestWrongPubKey(t *testing.T) {
privKey, _ := NewPrivateKey() privKey, _ := NewPrivateKey()
sample := []byte("sample") sample := []byte("sample")
hashedData := hash.Sha256(sample) hashedData := hash.Sha256(sample)
signedData, _ := privKey.Sign(sample) signedData := privKey.Sign(sample)
secondPrivKey, _ := NewPrivateKey() secondPrivKey, _ := NewPrivateKey()
wrongPubKey := secondPrivKey.PublicKey() 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. // GetInvocationScript returns NEO VM script containing transaction signature.
func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) { func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) {
var ( var (
err error
buf = io.NewBufBinWriter() buf = io.NewBufBinWriter()
signature []byte 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") return nil, errs.Wrap(buf.Err, "Failed to encode transaction to binary")
} }
data := buf.Bytes() data := buf.Bytes()
signature, err = wif.PrivateKey.Sign(data[:(len(data) - 1)]) signature = wif.PrivateKey.Sign(data[:(len(data) - 1)])
if err != nil {
return nil, errs.Wrap(err, "Failed ti sign transaction with private key")
}
return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil
} }

View file

@ -2477,8 +2477,7 @@ func TestCHECKSIGNoSigLoaded(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := "NEO - An Open Network For Smart Economy" msg := "NEO - An Open Network For Smart Economy"
sig, err := pk.Sign([]byte(msg)) sig := pk.Sign([]byte(msg))
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.estack.PushVal(sig) vm.estack.PushVal(sig)
@ -2491,8 +2490,7 @@ func TestCHECKSIGBadKey(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes()[:4] pbytes := pk.PublicKey().Bytes()[:4]
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2506,8 +2504,7 @@ func TestCHECKSIGWrongSig(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2523,8 +2520,7 @@ func TestCHECKSIGGood(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2540,8 +2536,7 @@ func TestVERIFYGood(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.estack.PushVal(msg) vm.estack.PushVal(msg)
@ -2557,8 +2552,7 @@ func TestVERIFYBad(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.estack.PushVal(util.ArrayReverse(msg)) vm.estack.PushVal(util.ArrayReverse(msg))
@ -2592,10 +2586,8 @@ func TestCHECKMULTISIGNotEnoughKeys(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2611,10 +2603,8 @@ func TestCHECKMULTISIGNoHash(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
@ -2630,10 +2620,8 @@ func TestCHECKMULTISIGBadKey(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes()[:4] pbytes2 := pk2.PublicKey().Bytes()[:4]
vm := load(prog) vm := load(prog)
@ -2650,10 +2638,8 @@ func TestCHECKMULTISIGBadSig(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
@ -2672,10 +2658,8 @@ func TestCHECKMULTISIGGood(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()
vm := load(prog) vm := load(prog)