Skip to content

Commit 3dcd8a2

Browse files
authored
feat: helper functions to convert hashes to PlutusData (#1111)
Fixes #1104 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 923217b commit 3dcd8a2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

ledger/common/common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/blinklabs-io/gouroboros/cbor"
23+
"github.com/blinklabs-io/plutigo/pkg/data"
2324
"github.com/btcsuite/btcd/btcutil/bech32"
2425
"golang.org/x/crypto/blake2b"
2526
)
@@ -46,6 +47,10 @@ func (b Blake2b256) Bytes() []byte {
4647
return b[:]
4748
}
4849

50+
func (b Blake2b256) ToPlutusData() data.PlutusData {
51+
return data.NewByteString(b[:])
52+
}
53+
4954
// Blake2b256Hash generates a Blake2b-256 hash from the provided data
5055
func Blake2b256Hash(data []byte) Blake2b256 {
5156
tmpHash, err := blake2b.New(Blake2b256Size, nil)
@@ -77,6 +82,10 @@ func (b Blake2b224) Bytes() []byte {
7782
return b[:]
7883
}
7984

85+
func (b Blake2b224) ToPlutusData() data.PlutusData {
86+
return data.NewByteString(b[:])
87+
}
88+
8089
func (b Blake2b224) MarshalJSON() ([]byte, error) {
8190
return json.Marshal(b.String())
8291
}
@@ -112,6 +121,10 @@ func (b Blake2b160) Bytes() []byte {
112121
return b[:]
113122
}
114123

124+
func (b Blake2b160) ToPlutusData() data.PlutusData {
125+
return data.NewByteString(b[:])
126+
}
127+
115128
// Blake2b160Hash generates a Blake2b-160 hash from the provided data
116129
func Blake2b160Hash(data []byte) Blake2b160 {
117130
tmpHash, err := blake2b.New(Blake2b160Size, nil)

ledger/common/common_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package common
1717
import (
1818
"encoding/hex"
1919
"encoding/json"
20+
"reflect"
2021
"testing"
2122

2223
"github.com/blinklabs-io/gouroboros/cbor"
2324
"github.com/blinklabs-io/gouroboros/internal/test"
25+
"github.com/blinklabs-io/plutigo/pkg/data"
2426
)
2527

2628
func TestAssetFingerprint(t *testing.T) {
@@ -244,6 +246,20 @@ func TestBlake2b224_String(t *testing.T) {
244246
}
245247
}
246248

249+
func TestBlake2b224_ToPlutusData(t *testing.T) {
250+
testData := []byte("blinklabs")
251+
hash := Blake2b224Hash(testData)
252+
expectedHash, err := hex.DecodeString("d33ef286551f50d455cfeb68b45b02622fb05ef21cfd1aabd0d7880c")
253+
if err != nil {
254+
t.Fatalf("unexpected error: %s", err)
255+
}
256+
expectedPd := data.NewByteString(expectedHash)
257+
tmpPd := hash.ToPlutusData()
258+
if !reflect.DeepEqual(tmpPd, expectedPd) {
259+
t.Fatalf("did not get expected PlutusData: got: %#v\n wanted: %#v", tmpPd, expectedPd)
260+
}
261+
}
262+
247263
func TestCertificateTypeMethods(t *testing.T) {
248264
tests := []struct {
249265
name string

0 commit comments

Comments
 (0)