*: remove duplicate functions producing verification script

Drop wif.GetVerificationScript(), drop
smartcontract.CreateSignatureRedeemScript(), add GetVerificationScript()
directly to the PublicKey and use it everywhere.
This commit is contained in:
Roman Khimov 2019-12-03 17:25:25 +03:00
parent 8d4dd2d2e1
commit 138e125646
6 changed files with 15 additions and 50 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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