From a903147b60b3c1068b44e9485346463759d178e9 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 19 Feb 2020 12:10:36 +0300 Subject: [PATCH] keys: rename Signature to GetScriptHash, make it return Uint160 Signature itself wasn't used at all and its name is very misleading, Uint160 script hash is way more useful. --- pkg/consensus/consensus.go | 10 ++++------ pkg/core/interop_system.go | 3 +-- pkg/crypto/keys/private_key.go | 8 +++++--- pkg/crypto/keys/publickey.go | 13 +++++-------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 463d99ef8..fdb57dfc6 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -11,7 +11,6 @@ import ( coreb "github.com/CityOfZion/neo-go/pkg/core/block" "github.com/CityOfZion/neo-go/pkg/core/mempool" "github.com/CityOfZion/neo-go/pkg/core/transaction" - "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/util" @@ -221,16 +220,15 @@ func (s *service) validatePayload(p *Payload) bool { } pub := validators[p.validatorIndex] - vs := pub.(*publicKey).GetVerificationScript() - h := hash.Hash160(vs) + h := pub.(*publicKey).GetScriptHash() return p.Verify(h) } func (s *service) getKeyPair(pubs []crypto.PublicKey) (int, crypto.PrivateKey, crypto.PublicKey) { for i := range pubs { - script := pubs[i].(*publicKey).GetVerificationScript() - acc := s.wallet.GetAccount(hash.Hash160(script)) + sh := pubs[i].(*publicKey).GetScriptHash() + acc := s.wallet.GetAccount(sh) if acc == nil { continue } @@ -446,7 +444,7 @@ func (s *service) getVerifiedTx(count int) []block.Transaction { sh := s.wallet.GetChangeAddress() if sh.Equals(util.Uint160{}) { pk := s.dbft.Pub.(*publicKey) - sh = hash.Hash160(pk.GetVerificationScript()) + sh = pk.GetScriptHash() } txOuts = []transaction.Output{transaction.Output{ AssetID: core.UtilityTokenID(), diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index 811f036ae..7f00f51fe 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -8,7 +8,6 @@ import ( "github.com/CityOfZion/neo-go/pkg/core/block" "github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/transaction" - "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/smartcontract/trigger" "github.com/CityOfZion/neo-go/pkg/util" @@ -311,7 +310,7 @@ func (ic *interopContext) checkHashedWitness(hash util.Uint160) (bool, error) { // checkKeyedWitness checks hash of signature check contract with a given public // key against current list of script hashes for verifying in the interop context. func (ic *interopContext) checkKeyedWitness(key *keys.PublicKey) (bool, error) { - return ic.checkHashedWitness(hash.Hash160(key.GetVerificationScript())) + return ic.checkHashedWitness(key.GetScriptHash()) } // runtimeCheckWitness checks witnesses. diff --git a/pkg/crypto/keys/private_key.go b/pkg/crypto/keys/private_key.go index c3aef6663..bf99021d4 100644 --- a/pkg/crypto/keys/private_key.go +++ b/pkg/crypto/keys/private_key.go @@ -10,6 +10,7 @@ import ( "fmt" "math/big" + "github.com/CityOfZion/neo-go/pkg/util" "github.com/nspcc-dev/rfc6979" ) @@ -98,10 +99,11 @@ func (p *PrivateKey) Address() string { return pk.Address() } -// Signature creates the signature using the private key. -func (p *PrivateKey) Signature() []byte { +// GetScriptHash returns verification script hash for public key associated with +// the private key. +func (p *PrivateKey) GetScriptHash() util.Uint160 { pk := p.PublicKey() - return pk.Signature() + return pk.GetScriptHash() } // Sign signs arbitrary length data using the private key. diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index b22e2f9f3..60951851e 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -12,6 +12,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/encoding/address" "github.com/CityOfZion/neo-go/pkg/io" + "github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/pkg/errors" ) @@ -233,18 +234,14 @@ func (p *PublicKey) GetVerificationScript() []byte { return b } -// Signature returns a NEO-specific hash of the key. -func (p *PublicKey) Signature() []byte { - sig := hash.Hash160(p.GetVerificationScript()) - - return sig.BytesBE() +// GetScriptHash returns a Hash160 of verification script for the key. +func (p *PublicKey) GetScriptHash() util.Uint160 { + return hash.Hash160(p.GetVerificationScript()) } // Address returns a base58-encoded NEO-specific address based on the key hash. func (p *PublicKey) Address() string { - sig := hash.Hash160(p.GetVerificationScript()) - - return address.Uint160ToString(sig) + return address.Uint160ToString(p.GetScriptHash()) } // Verify returns true if the signature is valid and corresponds