Skip to content

Commit

Permalink
fix address
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvionied committed Aug 23, 2024
1 parent 7ab227e commit ff54a3c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 106 deletions.
101 changes: 101 additions & 0 deletions plutusencoder/customtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
115 changes: 12 additions & 103 deletions plutusencoder/plutus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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":
Expand Down
6 changes: 3 additions & 3 deletions plutusencoder/plutusencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down

0 comments on commit ff54a3c

Please sign in to comment.