diff --git a/ledger/common/common.go b/ledger/common/common.go index 43137a05..6755f9bc 100644 --- a/ledger/common/common.go +++ b/ledger/common/common.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/plutigo/pkg/data" "github.com/btcsuite/btcd/btcutil/bech32" "golang.org/x/crypto/blake2b" ) @@ -46,6 +47,10 @@ func (b Blake2b256) Bytes() []byte { return b[:] } +func (b Blake2b256) ToPlutusData() data.PlutusData { + return data.NewByteString(b[:]) +} + // Blake2b256Hash generates a Blake2b-256 hash from the provided data func Blake2b256Hash(data []byte) Blake2b256 { tmpHash, err := blake2b.New(Blake2b256Size, nil) @@ -77,6 +82,10 @@ func (b Blake2b224) Bytes() []byte { return b[:] } +func (b Blake2b224) ToPlutusData() data.PlutusData { + return data.NewByteString(b[:]) +} + func (b Blake2b224) MarshalJSON() ([]byte, error) { return json.Marshal(b.String()) } @@ -112,6 +121,10 @@ func (b Blake2b160) Bytes() []byte { return b[:] } +func (b Blake2b160) ToPlutusData() data.PlutusData { + return data.NewByteString(b[:]) +} + // Blake2b160Hash generates a Blake2b-160 hash from the provided data func Blake2b160Hash(data []byte) Blake2b160 { tmpHash, err := blake2b.New(Blake2b160Size, nil) diff --git a/ledger/common/common_test.go b/ledger/common/common_test.go index 1bc45e61..946d7a4a 100644 --- a/ledger/common/common_test.go +++ b/ledger/common/common_test.go @@ -17,10 +17,12 @@ package common import ( "encoding/hex" "encoding/json" + "reflect" "testing" "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/internal/test" + "github.com/blinklabs-io/plutigo/pkg/data" ) func TestAssetFingerprint(t *testing.T) { @@ -244,6 +246,20 @@ func TestBlake2b224_String(t *testing.T) { } } +func TestBlake2b224_ToPlutusData(t *testing.T) { + testData := []byte("blinklabs") + hash := Blake2b224Hash(testData) + expectedHash, err := hex.DecodeString("d33ef286551f50d455cfeb68b45b02622fb05ef21cfd1aabd0d7880c") + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + expectedPd := data.NewByteString(expectedHash) + tmpPd := hash.ToPlutusData() + if !reflect.DeepEqual(tmpPd, expectedPd) { + t.Fatalf("did not get expected PlutusData: got: %#v\n wanted: %#v", tmpPd, expectedPd) + } +} + func TestCertificateTypeMethods(t *testing.T) { tests := []struct { name string