@@ -17,6 +17,7 @@ package common
17
17
import (
18
18
"errors"
19
19
"fmt"
20
+ "slices"
20
21
21
22
"github.com/blinklabs-io/gouroboros/cbor"
22
23
)
@@ -28,8 +29,11 @@ const (
28
29
ScriptRefTypePlutusV3 = 3
29
30
)
30
31
32
+ type ScriptHash = Blake2b224
33
+
31
34
type Script interface {
32
35
isScript ()
36
+ Hash () ScriptHash
33
37
}
34
38
35
39
type ScriptRef struct {
@@ -82,15 +86,43 @@ type PlutusV1Script []byte
82
86
83
87
func (PlutusV1Script ) isScript () {}
84
88
89
+ func (s PlutusV1Script ) Hash () ScriptHash {
90
+ return Blake2b224Hash (
91
+ slices .Concat (
92
+ []byte {ScriptRefTypePlutusV1 },
93
+ []byte (s ),
94
+ ),
95
+ )
96
+ }
97
+
85
98
type PlutusV2Script []byte
86
99
87
100
func (PlutusV2Script ) isScript () {}
88
101
102
+ func (s PlutusV2Script ) Hash () ScriptHash {
103
+ return Blake2b224Hash (
104
+ slices .Concat (
105
+ []byte {ScriptRefTypePlutusV2 },
106
+ []byte (s ),
107
+ ),
108
+ )
109
+ }
110
+
89
111
type PlutusV3Script []byte
90
112
91
113
func (PlutusV3Script ) isScript () {}
92
114
115
+ func (s PlutusV3Script ) Hash () ScriptHash {
116
+ return Blake2b224Hash (
117
+ slices .Concat (
118
+ []byte {ScriptRefTypePlutusV3 },
119
+ []byte (s ),
120
+ ),
121
+ )
122
+ }
123
+
93
124
type NativeScript struct {
125
+ cbor.DecodeStoreCbor
94
126
item any
95
127
}
96
128
@@ -101,6 +133,7 @@ func (n *NativeScript) Item() any {
101
133
}
102
134
103
135
func (n * NativeScript ) UnmarshalCBOR (data []byte ) error {
136
+ n .SetCbor (data )
104
137
id , err := cbor .DecodeIdFromList (data )
105
138
if err != nil {
106
139
return err
@@ -128,6 +161,15 @@ func (n *NativeScript) UnmarshalCBOR(data []byte) error {
128
161
return nil
129
162
}
130
163
164
+ func (s NativeScript ) Hash () ScriptHash {
165
+ return Blake2b224Hash (
166
+ slices .Concat (
167
+ []byte {ScriptRefTypeNativeScript },
168
+ []byte (s .Cbor ()),
169
+ ),
170
+ )
171
+ }
172
+
131
173
type NativeScriptPubkey struct {
132
174
cbor.StructAsArray
133
175
Type uint
0 commit comments