|
4 | 4 | package parachain
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "fmt"
|
8 | 9 | "strings"
|
9 | 10 |
|
@@ -125,3 +126,114 @@ func removeLeadingZeroHashForSlice(s []string) []string {
|
125 | 126 | func removeLeadingZeroHash(s string) string {
|
126 | 127 | return strings.Replace(s, "0x", "", 1)
|
127 | 128 | }
|
| 129 | + |
| 130 | +type Destination struct { |
| 131 | + Variant types.U8 |
| 132 | + DestinationBytes types.Data |
| 133 | +} |
| 134 | + |
| 135 | +type ForeignAccountId32 struct { |
| 136 | + ParaID uint32 |
| 137 | + ID types.H256 |
| 138 | + Fee types.U128 |
| 139 | +} |
| 140 | + |
| 141 | +type ForeignAccountId20 struct { |
| 142 | + ParaID uint32 |
| 143 | + ID types.H160 |
| 144 | + Fee types.U128 |
| 145 | +} |
| 146 | + |
| 147 | +type RegisterToken struct { |
| 148 | + Token types.H160 |
| 149 | + Fee types.U128 |
| 150 | +} |
| 151 | + |
| 152 | +type SendToken struct { |
| 153 | + Token types.H160 |
| 154 | + Destination Destination |
| 155 | +} |
| 156 | + |
| 157 | +type SendNativeToken struct { |
| 158 | + TokenID types.H256 |
| 159 | + Destination Destination |
| 160 | +} |
| 161 | + |
| 162 | +type InboundMessage struct { |
| 163 | + Version types.U8 |
| 164 | + ChainID types.U64 |
| 165 | + Command types.U8 |
| 166 | + CommandBytes types.Data |
| 167 | +} |
| 168 | + |
| 169 | +func GetDestination(input []byte) (string, error) { |
| 170 | + var inboundMessage = &InboundMessage{} |
| 171 | + err := types.DecodeFromBytes(input, inboundMessage) |
| 172 | + if err != nil { |
| 173 | + return "", fmt.Errorf("failed to decode message: %v", err) |
| 174 | + } |
| 175 | + |
| 176 | + address := "" |
| 177 | + switch inboundMessage.Command { |
| 178 | + case 0: |
| 179 | + // Register token does not have a destination |
| 180 | + break |
| 181 | + case 1: |
| 182 | + // Send token has a destination |
| 183 | + var command = &SendToken{} |
| 184 | + err = types.DecodeFromBytes(inboundMessage.CommandBytes, command) |
| 185 | + if err != nil { |
| 186 | + return "", fmt.Errorf("failed to decode send token command: %v", err) |
| 187 | + } |
| 188 | + |
| 189 | + address, err = decodeDestination(command.Destination.Variant, command.Destination.DestinationBytes) |
| 190 | + if err != nil { |
| 191 | + return "", fmt.Errorf("decode destination: %v", err) |
| 192 | + } |
| 193 | + case 2: |
| 194 | + // Send native token has a destination |
| 195 | + var command = &SendNativeToken{} |
| 196 | + err = types.DecodeFromBytes(inboundMessage.CommandBytes, command) |
| 197 | + if err != nil { |
| 198 | + return "", fmt.Errorf("failed to decode send native token command: %v", err) |
| 199 | + } |
| 200 | + |
| 201 | + address, err = decodeDestination(command.Destination.Variant, command.Destination.DestinationBytes) |
| 202 | + if err != nil { |
| 203 | + return "", fmt.Errorf("decode destination: %v", err) |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return address, nil |
| 208 | +} |
| 209 | + |
| 210 | +func decodeDestination(variant types.U8, destinationBytes []byte) (string, error) { |
| 211 | + switch variant { |
| 212 | + case 0: |
| 213 | + // Account32 |
| 214 | + account32 := &types.H256{} |
| 215 | + err := types.DecodeFromBytes(destinationBytes, account32) |
| 216 | + if err != nil { |
| 217 | + return "", fmt.Errorf("failed to decode destination: %v", err) |
| 218 | + } |
| 219 | + return account32.Hex(), nil |
| 220 | + case 1: |
| 221 | + // Account32 on destination parachain |
| 222 | + var account = &ForeignAccountId32{} |
| 223 | + err := types.DecodeFromBytes(destinationBytes, account) |
| 224 | + if err != nil { |
| 225 | + return "", fmt.Errorf("failed to decode foreign account: %v", err) |
| 226 | + } |
| 227 | + return account.ID.Hex(), nil |
| 228 | + case 2: |
| 229 | + // Account20 |
| 230 | + var account = &ForeignAccountId20{} |
| 231 | + err := types.DecodeFromBytes(destinationBytes, account) |
| 232 | + if err != nil { |
| 233 | + return "", fmt.Errorf("failed to decode foreign account: %v", err) |
| 234 | + } |
| 235 | + return account.ID.Hex(), nil |
| 236 | + } |
| 237 | + |
| 238 | + return "", errors.New("destination variant could not be matched") |
| 239 | +} |
0 commit comments