@@ -17,6 +17,7 @@ package common
1717import (
1818 "errors"
1919 "fmt"
20+ "slices"
2021
2122 "github.com/blinklabs-io/gouroboros/cbor"
2223)
@@ -28,8 +29,11 @@ const (
2829 ScriptRefTypePlutusV3 = 3
2930)
3031
32+ type ScriptHash = Blake2b224
33+
3134type Script interface {
3235 isScript ()
36+ Hash () ScriptHash
3337}
3438
3539type ScriptRef struct {
@@ -82,15 +86,43 @@ type PlutusV1Script []byte
8286
8387func (PlutusV1Script ) isScript () {}
8488
89+ func (s PlutusV1Script ) Hash () ScriptHash {
90+ return Blake2b224Hash (
91+ slices .Concat (
92+ []byte {ScriptRefTypePlutusV1 },
93+ []byte (s ),
94+ ),
95+ )
96+ }
97+
8598type PlutusV2Script []byte
8699
87100func (PlutusV2Script ) isScript () {}
88101
102+ func (s PlutusV2Script ) Hash () ScriptHash {
103+ return Blake2b224Hash (
104+ slices .Concat (
105+ []byte {ScriptRefTypePlutusV2 },
106+ []byte (s ),
107+ ),
108+ )
109+ }
110+
89111type PlutusV3Script []byte
90112
91113func (PlutusV3Script ) isScript () {}
92114
115+ func (s PlutusV3Script ) Hash () ScriptHash {
116+ return Blake2b224Hash (
117+ slices .Concat (
118+ []byte {ScriptRefTypePlutusV3 },
119+ []byte (s ),
120+ ),
121+ )
122+ }
123+
93124type NativeScript struct {
125+ cbor.DecodeStoreCbor
94126 item any
95127}
96128
@@ -101,6 +133,7 @@ func (n *NativeScript) Item() any {
101133}
102134
103135func (n * NativeScript ) UnmarshalCBOR (data []byte ) error {
136+ n .SetCbor (data )
104137 id , err := cbor .DecodeIdFromList (data )
105138 if err != nil {
106139 return err
@@ -128,6 +161,15 @@ func (n *NativeScript) UnmarshalCBOR(data []byte) error {
128161 return nil
129162}
130163
164+ func (s NativeScript ) Hash () ScriptHash {
165+ return Blake2b224Hash (
166+ slices .Concat (
167+ []byte {ScriptRefTypeNativeScript },
168+ []byte (s .Cbor ()),
169+ ),
170+ )
171+ }
172+
131173type NativeScriptPubkey struct {
132174 cbor.StructAsArray
133175 Type uint
0 commit comments