Skip to content

Commit

Permalink
Fix: Address Serialization In PlutusType
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvionied committed Dec 7, 2023
1 parent f7b6840 commit 2a14273
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 34 deletions.
102 changes: 79 additions & 23 deletions plutusencoder/plutus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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},
},
},
},
},
Expand All @@ -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,
Expand All @@ -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},
},
},
},
},
Expand All @@ -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,
Expand All @@ -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},
},
},
},
},
Expand All @@ -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,
Expand All @@ -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},
},
},
},
},
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions plutusencoder/plutusencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -271,41 +271,41 @@ 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 {
t.Error(err)
}
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")
}

}

0 comments on commit 2a14273

Please sign in to comment.