mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-02-18 11:15:36 +00:00
*: 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:
parent
8d4dd2d2e1
commit
138e125646
6 changed files with 15 additions and 50 deletions
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||||
"github.com/CityOfZion/neo-go/pkg/io"
|
"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/util"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm"
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
||||||
|
@ -189,13 +188,8 @@ func (p *Payload) Sign(key *privateKey) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
verif, err := smartcontract.CreateSignatureRedeemScript(key.PublicKey())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Witness.InvocationScript = append([]byte{byte(opcode.PUSHBYTES64)}, sig...)
|
p.Witness.InvocationScript = append([]byte{byte(opcode.PUSHBYTES64)}, sig...)
|
||||||
p.Witness.VerificationScript = verif
|
p.Witness.VerificationScript = key.PublicKey().GetVerificationScript()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
"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/util"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm"
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
||||||
gherr "github.com/pkg/errors"
|
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
|
// checkKeyedWitness checks hash of signature check contract with a given public
|
||||||
// key against current list of script hashes for verifying in the interop context.
|
// key against current list of script hashes for verifying in the interop context.
|
||||||
func (ic *interopContext) checkKeyedWitness(key *keys.PublicKey) (bool, error) {
|
func (ic *interopContext) checkKeyedWitness(key *keys.PublicKey) (bool, error) {
|
||||||
script, err := smartcontract.CreateSignatureRedeemScript(key)
|
return ic.checkHashedWitness(hash.Hash160(key.GetVerificationScript()))
|
||||||
if err != nil {
|
|
||||||
return false, gherr.Wrap(err, "failed to create signature script for a key")
|
|
||||||
}
|
|
||||||
return ic.checkHashedWitness(hash.Hash160(script))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtimeCheckWitness checks witnesses.
|
// runtimeCheckWitness checks witnesses.
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto"
|
"github.com/CityOfZion/neo-go/pkg/crypto"
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||||
"github.com/CityOfZion/neo-go/pkg/io"
|
"github.com/CityOfZion/neo-go/pkg/io"
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -222,13 +223,19 @@ func (p *PublicKey) EncodeBinary(w *io.BinWriter) {
|
||||||
w.WriteBytes(p.Bytes())
|
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.
|
// Signature returns a NEO-specific hash of the key.
|
||||||
func (p *PublicKey) Signature() []byte {
|
func (p *PublicKey) Signature() []byte {
|
||||||
b := p.Bytes()
|
sig := hash.Hash160(p.GetVerificationScript())
|
||||||
b = append([]byte{0x21}, b...)
|
|
||||||
b = append(b, 0xAC)
|
|
||||||
|
|
||||||
sig := hash.Hash160(b)
|
|
||||||
|
|
||||||
return sig.Bytes()
|
return sig.Bytes()
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,19 +90,3 @@ func WIFDecode(wif string, version byte) (*WIF, error) {
|
||||||
w.Compressed = true
|
w.Compressed = true
|
||||||
return w, nil
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ func SignTx(tx *transaction.Transaction, wif *keys.WIF) error {
|
||||||
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
|
if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil {
|
||||||
return errs.Wrap(err, "failed to create invocation script")
|
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.Scripts = append(tx.Scripts, &witness)
|
||||||
tx.Hash()
|
tx.Hash()
|
||||||
|
|
||||||
|
|
|
@ -10,21 +10,6 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
"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.
|
// CreateMultiSigRedeemScript creates a script runnable by the VM.
|
||||||
func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) {
|
func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) {
|
||||||
if m <= 1 {
|
if m <= 1 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue