mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
Merge pull request #604 from nspcc-dev/rfc6979-update
Update rfc6979 package to the latest version
This commit is contained in:
commit
2b02c145c3
9 changed files with 39 additions and 55 deletions
4
go.mod
4
go.mod
|
@ -7,8 +7,8 @@ require (
|
|||
github.com/go-redis/redis v6.10.2+incompatible
|
||||
github.com/go-yaml/yaml v2.1.0+incompatible
|
||||
github.com/mr-tron/base58 v1.1.2
|
||||
github.com/nspcc-dev/dbft v0.0.0-20200113100648-b3578fc15d82
|
||||
github.com/nspcc-dev/rfc6979 v0.1.0
|
||||
github.com/nspcc-dev/dbft v0.0.0-20200116150450-80b3f6f0dff8
|
||||
github.com/nspcc-dev/rfc6979 v0.2.0
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/prometheus/client_golang v1.2.1
|
||||
github.com/stretchr/testify v1.4.0
|
||||
|
|
8
go.sum
8
go.sum
|
@ -94,12 +94,16 @@ github.com/nspcc-dev/dbft v0.0.0-20191205084618-dacb1a30c254 h1:A4OkQDQOSPsJF8qU
|
|||
github.com/nspcc-dev/dbft v0.0.0-20191205084618-dacb1a30c254/go.mod h1:w1Ln2aT+dBlPhLnuZhBV+DfPEdS2CHWWLp5JTScY3bw=
|
||||
github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae h1:T5V1QANlNMKun0EPB3eqg2PTXG4rmLhzDyEiV63kdB0=
|
||||
github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae/go.mod h1:3FjXOoHmA51EGfb5GS/HOv7VdmngNRTssSeQ729dvGY=
|
||||
github.com/nspcc-dev/dbft v0.0.0-20200113100648-b3578fc15d82 h1:jwFLk2NKgyUbmJGlZsn3qIjoRqUkKajXb45ZuWbvkT4=
|
||||
github.com/nspcc-dev/dbft v0.0.0-20200113100648-b3578fc15d82/go.mod h1:IyIyVYKfi41kAlGWqicz9G8Iyni71Resuhtd9Y5ujJM=
|
||||
github.com/nspcc-dev/dbft v0.0.0-20200116150450-80b3f6f0dff8 h1:WjfnKH75ncU5iySB6ooTsbg2P0q1iQoecTN4gHIuEbs=
|
||||
github.com/nspcc-dev/dbft v0.0.0-20200116150450-80b3f6f0dff8/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk=
|
||||
github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck=
|
||||
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
|
||||
github.com/nspcc-dev/neofs-crypto v0.2.3 h1:aca3X2aly92ENRbFK+kH6Hd+J9EQ4Eu6XMVoITSIKtc=
|
||||
github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
|
||||
github.com/nspcc-dev/rfc6979 v0.1.0 h1:Lwg7esRRoyK1Up/IN1vAef1EmvrBeMHeeEkek2fAJ6c=
|
||||
github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
|
||||
github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE=
|
||||
github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
|
||||
github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY=
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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...)
|
||||
|
|
|
@ -105,16 +105,13 @@ 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)
|
||||
)
|
||||
|
||||
r, s, err := rfc6979.SignECDSA(privateKey, digest[:], sha256.New)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, s := rfc6979.SignECDSA(privateKey, digest[:], sha256.New)
|
||||
|
||||
params := privateKey.Curve.Params()
|
||||
curveOrderByteSize := params.P.BitLen() / 8
|
||||
|
@ -123,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.
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue