summaryrefslogtreecommitdiff
path: root/data/transactions/signedtxn.go
blob: 8b54300d9a188fa36dbe093ea11eb757848fcd70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package transactions

import (
	"errors"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/protocol"
)

// SignedTxn wraps a transaction and a signature.
// It exposes a Verify() method that verifies the signature and checks that the
// underlying transaction is well-formed.
// TODO: update this documentation now that there's multisig
type SignedTxn struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"`

	Sig      crypto.Signature   `codec:"sig"`
	Msig     crypto.MultisigSig `codec:"msig"`
	Lsig     LogicSig           `codec:"lsig"`
	Txn      Transaction        `codec:"txn"`
	AuthAddr basics.Address     `codec:"sgnr"`
}

// SignedTxnInBlock is how a signed transaction is encoded in a block.
type SignedTxnInBlock struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"`

	SignedTxnWithAD

	HasGenesisID   bool `codec:"hgi"`
	HasGenesisHash bool `codec:"hgh"`
}

// SignedTxnWithAD is a (decoded) SignedTxn with associated ApplyData
type SignedTxnWithAD struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"`

	SignedTxn
	ApplyData
}

// ID returns the Txid (i.e., hash) of the underlying transaction.
func (s SignedTxn) ID() Txid {
	return s.Txn.ID()
}

// ID on SignedTxnInBlock should never be called, because the ID depends
// on the block from which this transaction will be decoded.  By having
// a different return value from SignedTxn.ID(), we will catch errors at
// compile-time.
func (s SignedTxnInBlock) ID() {
}

// GetEncodedLength returns the length in bytes of the encoded transaction
func (s SignedTxn) GetEncodedLength() int {
	enc := s.MarshalMsg(protocol.GetEncodingBuf())
	defer protocol.PutEncodingBuf(enc)
	return len(enc)
}

// GetEncodedLength returns the length in bytes of the encoded transaction
func (s SignedTxnInBlock) GetEncodedLength() int {
	enc := s.MarshalMsg(protocol.GetEncodingBuf())
	defer protocol.PutEncodingBuf(enc)
	return len(enc)
}

// Authorizer returns the address against which the signature/msig/lsig should be checked,
// or so the SignedTxn claims.
// This is just s.AuthAddr or, if s.AuthAddr is zero, s.Txn.Sender.
// It's provided as a convenience method.
func (s SignedTxn) Authorizer() basics.Address {
	if (s.AuthAddr == basics.Address{}) {
		return s.Txn.Sender
	}
	return s.AuthAddr
}

// AssembleSignedTxn assembles a multisig-signed transaction from a transaction an optional sig, and an optional multisig.
// No signature checking is done -- for example, this might only be a partial multisig
// TODO: is this method used anywhere, or is it safe to remove?
func AssembleSignedTxn(txn Transaction, sig crypto.Signature, msig crypto.MultisigSig) (SignedTxn, error) {
	if sig != (crypto.Signature{}) && !msig.Blank() {
		return SignedTxn{}, errors.New("signed txn can only have one of sig or msig")
	}
	s := SignedTxn{
		Txn:  txn,
		Sig:  sig,
		Msig: msig,
	}
	return s, nil
}

// ToBeHashed implements the crypto.Hashable interface.
func (s *SignedTxnInBlock) ToBeHashed() (protocol.HashID, []byte) {
	return protocol.SignedTxnInBlock, protocol.Encode(s)
}

// Hash implements an optimized version of crypto.HashObj(s).
func (s *SignedTxnInBlock) Hash() crypto.Digest {
	enc := s.MarshalMsg(append(protocol.GetEncodingBuf(), []byte(protocol.SignedTxnInBlock)...))
	defer protocol.PutEncodingBuf(enc)
	return crypto.Hash(enc)
}

// WrapSignedTxnsWithAD takes an array SignedTxn and returns the same as SignedTxnWithAD
// Each txn's ApplyData is the default empty state.
func WrapSignedTxnsWithAD(txgroup []SignedTxn) []SignedTxnWithAD {
	txgroupad := make([]SignedTxnWithAD, len(txgroup))
	for i, tx := range txgroup {
		txgroupad[i].SignedTxn = tx
	}
	return txgroupad
}