Skip to content

Commit 8981d3f

Browse files
feat(ledger): Add RawScriptBytes to Script interface (#1210)
It's rather awkward to get the script bytes right now, as you have to try casting it to different script types; If you just need the bytes (ex: to store those in a database, etc.) then the RawScriptBytes method gives you access directly to those bytes (without the version wrapper). Might also consider adding a RawBytes to give *exactly* what's on chain, and/or a Version, to indicate which script version it is, but I held off on that for now as this is all that I need to unblock myself. Signed-off-by: Pi Lanningham <[email protected]>
1 parent 36845d7 commit 8981d3f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

ledger/common/script.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ScriptHash = Blake2b224
3737
type Script interface {
3838
isScript()
3939
Hash() ScriptHash
40+
RawScriptBytes() []byte
4041
}
4142

4243
type ScriptRef struct {
@@ -114,6 +115,10 @@ func (s PlutusV1Script) Hash() ScriptHash {
114115
)
115116
}
116117

118+
func (s PlutusV1Script) RawScriptBytes() []byte {
119+
return []byte(s)
120+
}
121+
117122
type PlutusV2Script []byte
118123

119124
func (PlutusV2Script) isScript() {}
@@ -127,6 +132,10 @@ func (s PlutusV2Script) Hash() ScriptHash {
127132
)
128133
}
129134

135+
func (s PlutusV2Script) RawScriptBytes() []byte {
136+
return []byte(s)
137+
}
138+
130139
type PlutusV3Script []byte
131140

132141
func (PlutusV3Script) isScript() {}
@@ -140,6 +149,10 @@ func (s PlutusV3Script) Hash() ScriptHash {
140149
)
141150
}
142151

152+
func (s PlutusV3Script) RawScriptBytes() []byte {
153+
return []byte(s)
154+
}
155+
143156
func (s PlutusV3Script) Evaluate(
144157
scriptContext data.PlutusData,
145158
budget ExUnits,
@@ -238,6 +251,10 @@ func (s NativeScript) Hash() ScriptHash {
238251
)
239252
}
240253

254+
func (s NativeScript) RawScriptBytes() []byte {
255+
return s.Cbor()
256+
}
257+
241258
type NativeScriptPubkey struct {
242259
cbor.StructAsArray
243260
Type uint

ledger/common/script_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package common_test
1616

1717
import (
18+
"bytes"
1819
"encoding/hex"
1920
"reflect"
2021
"testing"
@@ -40,6 +41,13 @@ func TestScriptRefDecodeEncode(t *testing.T) {
4041
&expectedScript,
4142
)
4243
}
44+
if !bytes.Equal(testScriptRef.Script.RawScriptBytes(), scriptCbor) {
45+
t.Fatalf(
46+
"did not get expected raw script bytes\n got: %x\n wanted: %x",
47+
testScriptRef.Script.RawScriptBytes(),
48+
scriptCbor,
49+
)
50+
}
4351
scriptRefCbor, err := cbor.Encode(testScriptRef)
4452
if err != nil {
4553
t.Fatalf("unexpected error: %s", err)

0 commit comments

Comments
 (0)