summaryrefslogtreecommitdiff
path: root/data/transactions/logicsig.go
blob: e2869a89b6a842e7dd3025f9d4a4a27dbea6096e (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
// Copyright (C) 2019-2023 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 (
	"bytes"

	"github.com/algorand/go-algorand/crypto"
)

// EvalMaxArgs is the maximum number of arguments to an LSig
const EvalMaxArgs = 255

// LogicSig contains logic for validating a transaction.
// LogicSig is signed by an account, allowing delegation of operations.
// OR
// LogicSig defines a contract account.
type LogicSig struct {
	_struct struct{} `codec:",omitempty,omitemptyarray"`

	// Logic signed by Sig or Msig, OR hashed to be the Address of an account.
	Logic []byte `codec:"l,allocbound=config.MaxLogicSigMaxSize"`

	Sig  crypto.Signature   `codec:"sig"`
	Msig crypto.MultisigSig `codec:"msig"`

	// Args are not signed, but checked by Logic
	Args [][]byte `codec:"arg,allocbound=EvalMaxArgs,allocbound=config.MaxLogicSigMaxSize"`
}

// Blank returns true if there is no content in this LogicSig
func (lsig *LogicSig) Blank() bool {
	return len(lsig.Logic) == 0
}

// Len returns the length of Logic plus the length of the Args
// This is limited by config.ConsensusParams.LogicSigMaxSize
func (lsig *LogicSig) Len() int {
	lsiglen := len(lsig.Logic)
	for _, arg := range lsig.Args {
		lsiglen += len(arg)
	}
	return lsiglen
}

// Equal returns true if both LogicSig are equivalent.
//
// Out of paranoia, Equal distinguishes zero-length byte slices
// from byte slice-typed nil values as they may have subtly
// different behaviors within the evaluation of a LogicSig,
// due to differences in msgpack encoding behavior.
func (lsig *LogicSig) Equal(b *LogicSig) bool {
	sigs := lsig.Sig == b.Sig && lsig.Msig.Equal(b.Msig)
	if !sigs {
		return false
	}
	if !safeSliceCheck(lsig.Logic, b.Logic) {
		return false
	}

	if len(lsig.Args) != len(b.Args) {
		return false
	}
	for i := range lsig.Args {
		if !safeSliceCheck(lsig.Args[i], b.Args[i]) {
			return false
		}
	}
	return true
}

func safeSliceCheck(a, b []byte) bool {
	if a != nil && b != nil {
		return bytes.Equal(a, b)
	}
	return a == nil && b == nil
}