From ff54a3c5a014337a4ee89aad2515ad8c18a6e86c Mon Sep 17 00:00:00 2001 From: Salvionied Date: Fri, 23 Aug 2024 19:40:41 +0200 Subject: [PATCH] fix address --- plutusencoder/customtypes.go | 101 ++++++++++++++++++++++++ plutusencoder/plutus.go | 115 +++------------------------- plutusencoder/plutusencoder_test.go | 6 +- 3 files changed, 116 insertions(+), 106 deletions(-) diff --git a/plutusencoder/customtypes.go b/plutusencoder/customtypes.go index 9324759..52e6876 100644 --- a/plutusencoder/customtypes.go +++ b/plutusencoder/customtypes.go @@ -268,3 +268,104 @@ func GetAddressPlutusData(address Address.Address) (*PlutusData.PlutusData, erro return nil, fmt.Errorf("error: Pointer Addresses are not supported") } } + +func DecodePlutusAddress(data PlutusData.PlutusData, network byte) (Address.Address, error) { + if data.PlutusDataType != PlutusData.PlutusArray && data.TagNr != 121 && len(data.Value.(PlutusData.PlutusIndefArray)) != 2 { + fmt.Println("HEREE") + return Address.Address{}, fmt.Errorf("error: Invalid Address Data") + } + isIndef := true + switch data.Value.(type) { + case PlutusData.PlutusDefArray: + isIndef = false + case PlutusData.PlutusIndefArray: + isIndef = true + default: + return Address.Address{}, fmt.Errorf("error: Invalid Address Data") + } + if isIndef { + 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].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 { + if skh_exists { + if is_skh_script { + addrType = Address.SCRIPT_SCRIPT + } else { + addrType = Address.SCRIPT_KEY + } + } else { + addrType = Address.SCRIPT_NONE + } + } else { + if skh_exists { + if is_skh_script { + addrType = Address.KEY_SCRIPT + } else { + addrType = Address.KEY_KEY + } + } else { + addrType = Address.KEY_NONE + } + } + hrp := Address.ComputeHrp(addrType, network) + header := addrType<<4 | network + addr := Address.Address{ + PaymentPart: pkh, + StakingPart: skh, + AddressType: addrType, + Network: network, + HeaderByte: header, + Hrp: hrp} + return addr, nil + } else { + pkh := data.Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.([]byte) + is_script := data.Value.(PlutusData.PlutusDefArray)[0].TagNr == 122 + skh := []byte{} + skh_exists := data.Value.(PlutusData.PlutusDefArray)[1].TagNr == 121 + is_skh_script := false + if skh_exists { + is_skh_script = data.Value.(PlutusData.PlutusDefArray)[1].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].TagNr == 122 + skh = data.Value.(PlutusData.PlutusDefArray)[1].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.([]byte) + } + var addrType byte + if is_script { + if skh_exists { + if is_skh_script { + addrType = Address.SCRIPT_SCRIPT + } else { + addrType = Address.SCRIPT_KEY + } + } else { + addrType = Address.SCRIPT_NONE + } + } else { + if skh_exists { + if is_skh_script { + addrType = Address.KEY_SCRIPT + } else { + addrType = Address.KEY_KEY + } + } else { + addrType = Address.KEY_NONE + } + } + hrp := Address.ComputeHrp(addrType, network) + header := addrType<<4 | network + addr := Address.Address{ + PaymentPart: pkh, + StakingPart: skh, + AddressType: addrType, + Network: network, + HeaderByte: header, + Hrp: hrp} + return addr, nil + } +} diff --git a/plutusencoder/plutus.go b/plutusencoder/plutus.go index da3bdda..fb0cc6d 100644 --- a/plutusencoder/plutus.go +++ b/plutusencoder/plutus.go @@ -472,106 +472,6 @@ func UnmarshalPlutus(data *PlutusData.PlutusData, v interface{}, network byte) ( return ret } -func DecodePlutusAddress(data PlutusData.PlutusData, network byte) Address.Address { - if data.PlutusDataType != PlutusData.PlutusArray && data.TagNr != 121 && len(data.Value.(PlutusData.PlutusIndefArray)) != 2 { - return Address.Address{} - } - isIndef := true - switch data.Value.(type) { - case PlutusData.PlutusDefArray: - isIndef = false - case PlutusData.PlutusIndefArray: - isIndef = true - default: - return Address.Address{} - } - if isIndef { - 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].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 { - if skh_exists { - if is_skh_script { - addrType = Address.SCRIPT_SCRIPT - } else { - addrType = Address.SCRIPT_KEY - } - } else { - addrType = Address.SCRIPT_NONE - } - } else { - if skh_exists { - if is_skh_script { - addrType = Address.KEY_SCRIPT - } else { - addrType = Address.KEY_KEY - } - } else { - addrType = Address.KEY_NONE - } - } - hrp := Address.ComputeHrp(addrType, network) - header := addrType<<4 | network - addr := Address.Address{ - PaymentPart: pkh, - StakingPart: skh, - AddressType: addrType, - Network: network, - HeaderByte: header, - Hrp: hrp} - return addr - } else { - pkh := data.Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.([]byte) - is_script := data.Value.(PlutusData.PlutusDefArray)[0].TagNr == 122 - skh := []byte{} - skh_exists := data.Value.(PlutusData.PlutusDefArray)[1].TagNr == 121 - is_skh_script := false - if skh_exists { - is_skh_script = data.Value.(PlutusData.PlutusDefArray)[1].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].TagNr == 122 - skh = data.Value.(PlutusData.PlutusDefArray)[1].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.(PlutusData.PlutusDefArray)[0].Value.([]byte) - } - var addrType byte - if is_script { - if skh_exists { - if is_skh_script { - addrType = Address.SCRIPT_SCRIPT - } else { - addrType = Address.SCRIPT_KEY - } - } else { - addrType = Address.SCRIPT_NONE - } - } else { - if skh_exists { - if is_skh_script { - addrType = Address.KEY_SCRIPT - } else { - addrType = Address.KEY_KEY - } - } else { - addrType = Address.KEY_NONE - } - } - hrp := Address.ComputeHrp(addrType, network) - header := addrType<<4 | network - addr := Address.Address{ - PaymentPart: pkh, - StakingPart: skh, - AddressType: addrType, - Network: network, - HeaderByte: header, - Hrp: hrp} - return addr - } -} - func unmarshalPlutus(data *PlutusData.PlutusData, v interface{}, Plutusconstr uint64, PlutusType PlutusData.PlutusType, network byte) error { types := reflect.TypeOf(v) if types.Kind() != reflect.Ptr { @@ -607,7 +507,10 @@ func unmarshalPlutus(data *PlutusData.PlutusData, v interface{}, Plutusconstr ui } for idx, pAEl := range plutusValues { if tps.Field(idx+1).Type.String() == "Address.Address" { - addr := DecodePlutusAddress(pAEl, network) + addr, err := DecodePlutusAddress(pAEl, network) + if err != nil { + return fmt.Errorf("error: %v", err) + } reflect.ValueOf(v).Elem().Field(idx + 1).Set(reflect.ValueOf(addr)) continue } @@ -735,7 +638,10 @@ func unmarshalPlutus(data *PlutusData.PlutusData, v interface{}, Plutusconstr ui continue } if tps.Field(idx+1).Type.String() == "Address.Address" { - addr := DecodePlutusAddress(pAEl, network) + addr, err := DecodePlutusAddress(pAEl, network) + if err != nil { + return fmt.Errorf("error: %v", err) + } reflect.ValueOf(v).Elem().Field(idx + 1).Set(reflect.ValueOf(addr)) continue } @@ -885,7 +791,10 @@ func unmarshalPlutus(data *PlutusData.PlutusData, v interface{}, Plutusconstr ui reflect.ValueOf(v).Elem().FieldByName(idx).Set(reflect.ValueOf(asset)) continue case "Address.Address": - addr := DecodePlutusAddress(pAEl, network) + addr, err := DecodePlutusAddress(pAEl, network) + if err != nil { + return fmt.Errorf("error: %v", err) + } reflect.ValueOf(v).Elem().FieldByName(idx).Set(reflect.ValueOf(addr)) continue case "bool": diff --git a/plutusencoder/plutusencoder_test.go b/plutusencoder/plutusencoder_test.go index 01c5ac2..fb9470f 100644 --- a/plutusencoder/plutusencoder_test.go +++ b/plutusencoder/plutusencoder_test.go @@ -298,7 +298,7 @@ func TestDecodeAddressStruct(t *testing.T) { if err != nil { t.Error(err) } - address := plutusencoder.DecodePlutusAddress(pd, 0b0001) + address, _ := plutusencoder.DecodePlutusAddress(pd, 0b0001) if address.String() != "addr1w9hvftxrlw74wzk6vf0jfyp8wl8vt4arf8aq70rm4paselc46ptfq" { t.Error(address, "expected", "addr1w9hvftxrlw74wzk6vf0jfyp8wl8vt4arf8aq70rm4paselc46ptfq") } @@ -307,7 +307,7 @@ func TestDecodeAddressStruct(t *testing.T) { if err != nil { t.Error(err) } - address = plutusencoder.DecodePlutusAddress(pd, 0b0001) + address, _ = plutusencoder.DecodePlutusAddress(pd, 0b0001) if address.String() != "addr1z99tz7hungv6furtdl3zn72sree86wtghlcr4jc637r2eadkp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzs0706a3" { t.Error(address, "expected", "addr1z99tz7hungv6furtdl3zn72sree86wtghlcr4jc637r2eadkp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzs0706a3") } @@ -317,7 +317,7 @@ func TestDecodeAddressStruct(t *testing.T) { if err != nil { t.Error(err) } - address = plutusencoder.DecodePlutusAddress(pd, 0b0001) + address, _ = plutusencoder.DecodePlutusAddress(pd, 0b0001) if address.String() != "addr1qxajla3qcrwckzkur8n0lt02rg2sepw3kgkstckmzrz4ccfm3j9pqrqkea3tns46e3qy2w42vl8dvvue8u45amzm3rjqvv2nxh" { t.Error(address, "expected", "addr1qxajla3qcrwckzkur8n0lt02rg2sepw3kgkstckmzrz4ccfm3j9pqrqkea3tns46e3qy2w42vl8dvvue8u45amzm3rjqvv2nxh") }