core: implement (*Transaction).GetSignedPart()

Marshalling it and taking all but last byte violates incapsulation
and is just wrong in case transaction already contains any witnesses.
This commit is contained in:
Evgenii Stratonikov 2020-02-28 10:05:13 +03:00
parent 95d9f36c98
commit 72d72296c3
2 changed files with 11 additions and 10 deletions

View file

@ -197,6 +197,16 @@ func (t Transaction) GroupOutputByAssetID() map[util.Uint256][]*Output {
return m
}
// GetSignedPart returns a part of the transaction which must be signed.
func (t *Transaction) GetSignedPart() []byte {
buf := io.NewBufBinWriter()
t.encodeHashableFields(buf.BinWriter)
if buf.Err != nil {
return nil
}
return buf.Bytes()
}
// Bytes converts the transaction to []byte
func (t *Transaction) Bytes() []byte {
buf := io.NewBufBinWriter()

View file

@ -94,16 +94,7 @@ 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 (
buf = io.NewBufBinWriter()
signature []byte
)
tx.EncodeBinary(buf.BinWriter)
if buf.Err != nil {
return nil, errs.Wrap(buf.Err, "Failed to encode transaction to binary")
}
data := buf.Bytes()
signature = wif.PrivateKey.Sign(data[:(len(data) - 1)])
signature := wif.PrivateKey.Sign(tx.GetSignedPart())
return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil
}