Repository navigation
Expand file tree
/
Copy pathclass.go
More file actions
107 lines (93 loc) · 2.77 KB
/
Copy pathclass.go
File metadata and controls
107 lines (93 loc) · 2.77 KB
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
package core
import (
"encoding/json"
"fmt"
"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
)
var (
_ Class = (*Cairo0Class)(nil)
_ Class = (*Cairo1Class)(nil)
)
// Class unambiguously defines a [Contract]'s semantics.
type Class interface {
Version() uint64
}
// Cairo0Class unambiguously defines a [Contract]'s semantics.
type Cairo0Class struct {
Abi json.RawMessage
// External functions defined in the class.
Externals []EntryPoint
// Functions that receive L1 messages. See
// https://www.cairo-lang.org/docs/hello_starknet/l1l2.html#receiving-a-message-from-l1
L1Handlers []EntryPoint
// Constructors for the class. Currently, only one is allowed.
Constructors []EntryPoint
// Base64 encoding of compressed Program
Program string
}
// EntryPoint uniquely identifies a Cairo function to execute.
type EntryPoint struct {
// starknet_keccak hash of the function signature.
Selector *felt.Felt
// The offset of the instruction in the class's bytecode.
Offset *felt.Felt
}
func (c *Cairo0Class) Version() uint64 {
return 0
}
// Cairo1Class unambiguously defines a [Contract]'s semantics.
type Cairo1Class struct {
Abi string
AbiHash *felt.Felt
EntryPoints struct {
Constructor []SierraEntryPoint
External []SierraEntryPoint
L1Handler []SierraEntryPoint
}
Program []*felt.Felt
ProgramHash *felt.Felt
SemanticVersion string
Compiled json.RawMessage
}
type SierraEntryPoint struct {
Index uint64
Selector *felt.Felt
}
func (c *Cairo1Class) Version() uint64 {
return 1
}
func (c *Cairo1Class) Hash() *felt.Felt {
return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("CONTRACT_CLASS_V"+c.SemanticVersion)),
crypto.PoseidonArray(flattenSierraEntryPoints(c.EntryPoints.External)...),
crypto.PoseidonArray(flattenSierraEntryPoints(c.EntryPoints.L1Handler)...),
crypto.PoseidonArray(flattenSierraEntryPoints(c.EntryPoints.Constructor)...),
c.AbiHash,
c.ProgramHash,
)
}
func flattenSierraEntryPoints(entryPoints []SierraEntryPoint) []*felt.Felt {
result := make([]*felt.Felt, len(entryPoints)*2)
for i, entryPoint := range entryPoints {
// It is important that Selector is first because the order
// influences the class hash.
result[2*i] = entryPoint.Selector
result[2*i+1] = new(felt.Felt).SetUint64(entryPoint.Index)
}
return result
}
func VerifyClassHashes(classes map[felt.Felt]Class) error {
for hash, class := range classes {
cairo1Class, ok := class.(*Cairo1Class)
// cairo0 classes are deprecated and hard to verify their hash, just ignore them
if !ok {
return nil
}
cHash := cairo1Class.Hash()
if !cHash.Equal(&hash) {
return fmt.Errorf("cannot verify class hash: calculated hash %v, received hash %v", cHash.String(), hash.String())
}
}
return nil
}