From 2a14273db92246ba29105f508cfa34a8a8a122eb Mon Sep 17 00:00:00 2001 From: Salvionied Date: Thu, 7 Dec 2023 22:56:41 +0100 Subject: [PATCH] Fix: Address Serialization In PlutusType --- plutusencoder/plutus.go | 102 +++++++++++++++++++++------- plutusencoder/plutusencoder_test.go | 22 +++--- 2 files changed, 90 insertions(+), 34 deletions(-) diff --git a/plutusencoder/plutus.go b/plutusencoder/plutus.go index 0c3505b..66e8c9b 100644 --- a/plutusencoder/plutus.go +++ b/plutusencoder/plutus.go @@ -21,8 +21,14 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 121, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusBytes, + }, + }, }, PlutusData.PlutusData{ TagNr: 121, @@ -34,8 +40,13 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 121, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusBytes}, + }, }, }, }, @@ -51,8 +62,14 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 122, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusBytes, + }, + }, }, PlutusData.PlutusData{ TagNr: 121, @@ -64,8 +81,13 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 121, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusBytes}, + }, }, }, }, @@ -80,8 +102,14 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 121, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusBytes, + }, + }, }, PlutusData.PlutusData{ TagNr: 121, @@ -93,8 +121,13 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 122, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusBytes}, + }, }, }, }, @@ -109,8 +142,14 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 122, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusBytes, + }, + }, }, PlutusData.PlutusData{ TagNr: 121, @@ -122,8 +161,13 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 122, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.StakingPart, + PlutusDataType: PlutusData.PlutusBytes}, + }, }, }, }, @@ -138,8 +182,14 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 121, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusBytes, + }, + }, }, PlutusData.PlutusData{ TagNr: 122, @@ -155,8 +205,14 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro Value: PlutusData.PlutusIndefArray{ PlutusData.PlutusData{ TagNr: 122, - PlutusDataType: PlutusData.PlutusBytes, - Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusArray, + Value: PlutusData.PlutusIndefArray{ + PlutusData.PlutusData{ + TagNr: 0, + Value: address.PaymentPart, + PlutusDataType: PlutusData.PlutusBytes, + }, + }, }, PlutusData.PlutusData{ TagNr: 122, @@ -418,14 +474,14 @@ func DecodePlutusAddress(data PlutusData.PlutusData, network byte) Address.Addre if data.PlutusDataType != PlutusData.PlutusArray && data.TagNr != 121 && len(data.Value.(PlutusData.PlutusIndefArray)) != 2 { return Address.Address{} } - pkh := data.Value.(PlutusData.PlutusIndefArray)[0].Value.([]byte) + pkh := data.Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].Value.([]byte) is_script := data.Value.(PlutusData.PlutusIndefArray)[0].TagNr == 122 skh := []byte{} skh_exists := data.Value.(PlutusData.PlutusIndefArray)[1].TagNr == 121 is_skh_script := false if skh_exists { - is_skh_script = data.Value.(PlutusData.PlutusIndefArray)[1].Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].TagNr == 122 - skh = data.Value.(PlutusData.PlutusIndefArray)[1].Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].Value.([]byte) + is_skh_script = data.Value.(PlutusData.PlutusIndefArray)[1].Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].TagNr == 122 + skh = data.Value.(PlutusData.PlutusIndefArray)[1].Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].Value.(PlutusData.PlutusIndefArray)[0].Value.([]byte) } var addrType byte if is_script { diff --git a/plutusencoder/plutusencoder_test.go b/plutusencoder/plutusencoder_test.go index b3e292c..8397771 100644 --- a/plutusencoder/plutusencoder_test.go +++ b/plutusencoder/plutusencoder_test.go @@ -219,13 +219,13 @@ func TestPDAddressesStruct(t *testing.T) { } fmt.Println(marshaled) encoded, err := cbor.Marshal(marshaled) - if hex.EncodeToString(encoded) != "d87b81d8799fd879581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61d8799fd8799fd879581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffff" { + if hex.EncodeToString(encoded) != "d87b81d8799fd8799f581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61ffd8799fd8799fd8799f581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffffff" { t.Error(hex.EncodeToString(encoded)) } } func TestUnmarshalPDAddressesStruct(t *testing.T) { - decoded, _ := hex.DecodeString("d87b81d8799fd879581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61d8799fd8799fd879581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffff") + decoded, _ := hex.DecodeString("d87b81d8799fd8799f581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61ffd8799fd8799fd8799f581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffffff") pd := PlutusData.PlutusData{} err := cbor.Unmarshal(decoded, &pd) if err != nil { @@ -250,7 +250,7 @@ func TestPDAddress(t *testing.T) { t.Error(err) } encoded, _ := cbor.Marshal(pd) - if hex.EncodeToString(encoded) != "d8799fd879581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61d8799fd8799fd879581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffff" { + if hex.EncodeToString(encoded) != "d8799fd8799f581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61ffd8799fd8799fd8799f581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffffff" { t.Error(hex.EncodeToString(encoded)) } addressSCRIPT_KEY := "addr1z99tz7hungv6furtdl3zn72sree86wtghlcr4jc637r2eadkp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzs0706a3" @@ -261,7 +261,7 @@ func TestPDAddress(t *testing.T) { t.Error(err) } encoded, _ = cbor.Marshal(pd) - if hex.EncodeToString(encoded) != "d8799fd87a581c4ab17afc9a19a4f06b6fe229f9501e727d3968bff03acb1a8f86acf5d8799fd8799fd879581cb60abac5d101517cd99ae6c6c9ab582bc8603a07b57941df212782c5ffffff" { + if hex.EncodeToString(encoded) != "d8799fd87a9f581c4ab17afc9a19a4f06b6fe229f9501e727d3968bff03acb1a8f86acf5ffd8799fd8799fd8799f581cb60abac5d101517cd99ae6c6c9ab582bc8603a07b57941df212782c5ffffffff" { t.Error(hex.EncodeToString(encoded)) } addressSCRIPT_NONE := "addr1w9hvftxrlw74wzk6vf0jfyp8wl8vt4arf8aq70rm4paselc46ptfq" @@ -271,14 +271,14 @@ func TestPDAddress(t *testing.T) { t.Error(err) } encoded, _ = cbor.Marshal(pd) - if hex.EncodeToString(encoded) != "d8799fd87a581c6ec4acc3fbbd570ada625f24902777cec5d7a349fa0f3c7ba87b0cffd87a9fffff" { + if hex.EncodeToString(encoded) != "d8799fd87a9f581c6ec4acc3fbbd570ada625f24902777cec5d7a349fa0f3c7ba87b0cffffd87a9fffff" { t.Error(hex.EncodeToString(encoded)) } } func TestDecodeAddressStruct(t *testing.T) { - decoded_addr, _ := hex.DecodeString("d8799fd87a581c6ec4acc3fbbd570ada625f24902777cec5d7a349fa0f3c7ba87b0cffd87a9fffff") + decoded_addr, _ := hex.DecodeString("d8799fd87a9f581c6ec4acc3fbbd570ada625f24902777cec5d7a349fa0f3c7ba87b0cffffd87a9fffff") var pd PlutusData.PlutusData err := cbor.Unmarshal(decoded_addr, &pd) if err != nil { @@ -286,26 +286,26 @@ func TestDecodeAddressStruct(t *testing.T) { } address := plutusencoder.DecodePlutusAddress(pd, 0b0001) if address.String() != "addr1w9hvftxrlw74wzk6vf0jfyp8wl8vt4arf8aq70rm4paselc46ptfq" { - t.Error(address) + t.Error(address, "expected", "addr1w9hvftxrlw74wzk6vf0jfyp8wl8vt4arf8aq70rm4paselc46ptfq") } - decoded_addr, _ = hex.DecodeString("d8799fd87a581c4ab17afc9a19a4f06b6fe229f9501e727d3968bff03acb1a8f86acf5d8799fd8799fd879581cb60abac5d101517cd99ae6c6c9ab582bc8603a07b57941df212782c5ffffff") + decoded_addr, _ = hex.DecodeString("d8799fd87a9f581c4ab17afc9a19a4f06b6fe229f9501e727d3968bff03acb1a8f86acf5ffd8799fd8799fd8799f581cb60abac5d101517cd99ae6c6c9ab582bc8603a07b57941df212782c5ffffffff") err = cbor.Unmarshal(decoded_addr, &pd) if err != nil { t.Error(err) } address = plutusencoder.DecodePlutusAddress(pd, 0b0001) if address.String() != "addr1z99tz7hungv6furtdl3zn72sree86wtghlcr4jc637r2eadkp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzs0706a3" { - t.Error(address) + t.Error(address, "expected", "addr1z99tz7hungv6furtdl3zn72sree86wtghlcr4jc637r2eadkp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzs0706a3") } - decoded_addr, _ = hex.DecodeString("d8799fd879581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61d8799fd8799fd879581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffff") + decoded_addr, _ = hex.DecodeString("d8799fd8799f581cbb2ff620c0dd8b0adc19e6ffadea1a150c85d1b22d05e2db10c55c61ffd8799fd8799fd8799f581c3b8c8a100c16cf62b9c2bacc40453aaa67ced633993f2b4eec5b88e4ffffffff") err = cbor.Unmarshal(decoded_addr, &pd) if err != nil { t.Error(err) } address = plutusencoder.DecodePlutusAddress(pd, 0b0001) if address.String() != "addr1qxajla3qcrwckzkur8n0lt02rg2sepw3kgkstckmzrz4ccfm3j9pqrqkea3tns46e3qy2w42vl8dvvue8u45amzm3rjqvv2nxh" { - t.Error(address) + t.Error(address, "expected", "addr1qxajla3qcrwckzkur8n0lt02rg2sepw3kgkstckmzrz4ccfm3j9pqrqkea3tns46e3qy2w42vl8dvvue8u45amzm3rjqvv2nxh") } }