Skip to content

Commit

Permalink
Add Support for big Ints within plutusData
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvionied committed Jun 6, 2024
1 parent 1d2b593 commit fd4161e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ApolloBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"testing"

"github.com/Salvionied/apollo"
Expand Down Expand Up @@ -1233,3 +1234,20 @@ func TestMapWithCbor(t *testing.T) {
}

}

func TestPlutusBigInt(t *testing.T) {
x := "c249056bc75e2d63100000"
decoded, _ := hex.DecodeString(x)
var y PlutusData.PlutusData
err := cbor.Unmarshal(decoded, &y)
if err != nil {
t.Error(err)
}
if y.PlutusDataType != PlutusData.PlutusBigInt {
t.Error("Tx is not correct")
}
tmpBI := y.Value.(big.Int)
if tmpBI.String() != "100000000000000000000" {
t.Error("Tx is not correct")
}
}
15 changes: 15 additions & 0 deletions serialization/PlutusData/PlutusData.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -545,6 +546,7 @@ const (
PlutusMap
PlutusIntMap
PlutusInt
PlutusBigInt
PlutusBytes
PlutusShortArray
)
Expand Down Expand Up @@ -924,6 +926,8 @@ func (pd *PlutusData) MarshalCBOR() ([]uint8, error) {
} else {
return canonicalenc.Marshal(pd.Value)
}
} else if pd.PlutusDataType == PlutusBigInt {
return cbor.Marshal(pd.Value)
} else {
//enc, _ := cbor.EncOptions{Sort: cbor.SortCTAP2}.EncMode()
if pd.TagNr == 0 {
Expand Down Expand Up @@ -1128,9 +1132,15 @@ func (pd *PlutusData) UnmarshalCBOR(value []uint8) error {
if err != nil {
return err
}
//fmt.Println(hex.EncodeToString(value))
ok, valid := x.(cbor.Tag)
if valid {
switch ok.Content.(type) {
case big.Int:
pd.PlutusDataType = PlutusBigInt
tmpBigInt := x.(big.Int)
pd.Value = tmpBigInt
pd.TagNr = 0
case []interface{}:
pd.TagNr = ok.Number
pd.PlutusDataType = PlutusArray
Expand Down Expand Up @@ -1170,6 +1180,11 @@ func (pd *PlutusData) UnmarshalCBOR(value []uint8) error {
}
} else {
switch x.(type) {
case big.Int:
pd.PlutusDataType = PlutusBigInt
tmpBigInt := x.(big.Int)
pd.Value = tmpBigInt
pd.TagNr = 0
case []interface{}:
if value[0] == 0x9f {
y := PlutusIndefArray{}
Expand Down

0 comments on commit fd4161e

Please sign in to comment.