Skip to content

Commit d7d6eff

Browse files
authored
refactor: helper for generating script hash (#1138)
Fixes #1135 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 1cfaa73 commit d7d6eff

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

ledger/common/script.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package common
1717
import (
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+
3134
type Script interface {
3235
isScript()
36+
Hash() ScriptHash
3337
}
3438

3539
type ScriptRef struct {
@@ -82,15 +86,43 @@ type PlutusV1Script []byte
8286

8387
func (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+
8598
type PlutusV2Script []byte
8699

87100
func (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+
89111
type PlutusV3Script []byte
90112

91113
func (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+
93124
type NativeScript struct {
125+
cbor.DecodeStoreCbor
94126
item any
95127
}
96128

@@ -101,6 +133,7 @@ func (n *NativeScript) Item() any {
101133
}
102134

103135
func (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+
131173
type NativeScriptPubkey struct {
132174
cbor.StructAsArray
133175
Type uint

ledger/common/script_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ func TestScriptRefDecode(t *testing.T) {
4040
)
4141
}
4242
}
43+
44+
func TestPlutusV3ScriptHash(t *testing.T) {
45+
testScriptBytes, _ := hex.DecodeString("587f01010032323232323225333002323232323253330073370e900118041baa0011323232533300a3370e900018059baa00513232533300f301100214a22c6eb8c03c004c030dd50028b18069807001180600098049baa00116300a300b0023009001300900230070013004375400229309b2b2b9a5573aaae7955cfaba157441")
46+
testScript := common.PlutusV3Script(testScriptBytes)
47+
expectedScriptHash := "2909c3d0441e76cd6ae1fc09664bb209868902e191c2b8c30b82d331"
48+
tmpHash := testScript.Hash()
49+
if tmpHash.String() != expectedScriptHash {
50+
t.Errorf("did not get expected script hash, got %s, wanted %s", tmpHash.String(), expectedScriptHash)
51+
}
52+
}

0 commit comments

Comments
 (0)