From 138e1256463b1ec1a11bada585391e3466f4a58e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 3 Dec 2019 17:25:25 +0300 Subject: [PATCH] *: remove duplicate functions producing verification script Drop wif.GetVerificationScript(), drop smartcontract.CreateSignatureRedeemScript(), add GetVerificationScript() directly to the PublicKey and use it everywhere. --- pkg/consensus/payload.go | 8 +------- pkg/core/interop_system.go | 7 +------ pkg/crypto/keys/publickey.go | 17 ++++++++++++----- pkg/crypto/keys/wif.go | 16 ---------------- pkg/rpc/txBuilder.go | 2 +- pkg/smartcontract/contract.go | 15 --------------- 6 files changed, 15 insertions(+), 50 deletions(-) diff --git a/pkg/consensus/payload.go b/pkg/consensus/payload.go index e80f4bb79..fce529568 100644 --- a/pkg/consensus/payload.go +++ b/pkg/consensus/payload.go @@ -7,7 +7,6 @@ import ( "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/vm" "github.com/CityOfZion/neo-go/pkg/vm/opcode" @@ -189,13 +188,8 @@ func (p *Payload) Sign(key *privateKey) error { return err } - verif, err := smartcontract.CreateSignatureRedeemScript(key.PublicKey()) - if err != nil { - return err - } - p.Witness.InvocationScript = append([]byte{byte(opcode.PUSHBYTES64)}, sig...) - p.Witness.VerificationScript = verif + p.Witness.VerificationScript = key.PublicKey().GetVerificationScript() return nil } diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index 70bb85bbb..1d8a4a981 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/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" "github.com/CityOfZion/neo-go/pkg/vm" gherr "github.com/pkg/errors" @@ -309,11 +308,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) { - script, err := smartcontract.CreateSignatureRedeemScript(key) - if err != nil { - return false, gherr.Wrap(err, "failed to create signature script for a key") - } - return ic.checkHashedWitness(hash.Hash160(script)) + return ic.checkHashedWitness(hash.Hash160(key.GetVerificationScript())) } // runtimeCheckWitness checks witnesses. diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index dad925ffd..debe09d1e 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" "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/io" + "github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/pkg/errors" ) @@ -222,13 +223,19 @@ func (p *PublicKey) EncodeBinary(w *io.BinWriter) { w.WriteBytes(p.Bytes()) } +// GetVerificationScript returns NEO VM bytecode with CHECKSIG command for the +// public key. +func (p *PublicKey) GetVerificationScript() []byte { + b := p.Bytes() + b = append([]byte{byte(opcode.PUSHBYTES33)}, b...) + b = append(b, byte(opcode.CHECKSIG)) + + return b +} + // Signature returns a NEO-specific hash of the key. func (p *PublicKey) Signature() []byte { - b := p.Bytes() - b = append([]byte{0x21}, b...) - b = append(b, 0xAC) - - sig := hash.Hash160(b) + sig := hash.Hash160(p.GetVerificationScript()) return sig.Bytes() } diff --git a/pkg/crypto/keys/wif.go b/pkg/crypto/keys/wif.go index 8972328cf..3e30fc47a 100644 --- a/pkg/crypto/keys/wif.go +++ b/pkg/crypto/keys/wif.go @@ -90,19 +90,3 @@ func WIFDecode(wif string, version byte) (*WIF, error) { w.Compressed = true return w, nil } - -// GetVerificationScript returns NEO VM bytecode with checksig command for the public key. -func (wif WIF) GetVerificationScript() []byte { - const ( - pushbytes33 = 0x21 - checksig = 0xac - ) - var ( - vScript []byte - pubkey *PublicKey - ) - pubkey = wif.PrivateKey.PublicKey() - vScript = append([]byte{pushbytes33}, pubkey.Bytes()...) - vScript = append(vScript, checksig) - return vScript -} diff --git a/pkg/rpc/txBuilder.go b/pkg/rpc/txBuilder.go index bd79511ef..308c2647e 100644 --- a/pkg/rpc/txBuilder.go +++ b/pkg/rpc/txBuilder.go @@ -86,7 +86,7 @@ func SignTx(tx *transaction.Transaction, wif *keys.WIF) error { if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil { return errs.Wrap(err, "failed to create invocation script") } - witness.VerificationScript = wif.GetVerificationScript() + witness.VerificationScript = wif.PrivateKey.PublicKey().GetVerificationScript() tx.Scripts = append(tx.Scripts, &witness) tx.Hash() diff --git a/pkg/smartcontract/contract.go b/pkg/smartcontract/contract.go index 123971d32..4766659ea 100644 --- a/pkg/smartcontract/contract.go +++ b/pkg/smartcontract/contract.go @@ -10,21 +10,6 @@ import ( "github.com/CityOfZion/neo-go/pkg/vm/opcode" ) -// CreateSignatureRedeemScript creates a check signature script runnable by VM. -func CreateSignatureRedeemScript(key *keys.PublicKey) ([]byte, error) { - buf := new(bytes.Buffer) - err := vm.EmitBytes(buf, key.Bytes()) - if err != nil { - return nil, err - } - err = vm.EmitOpcode(buf, opcode.CHECKSIG) - if err != nil { - return nil, err - } - - return buf.Bytes(), nil -} - // CreateMultiSigRedeemScript creates a script runnable by the VM. func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) { if m <= 1 {