|
| 1 | +/* |
| 2 | + * Flow Playground |
| 3 | + * |
| 4 | + * Copyright 2019 Dapper Labs, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package adapter |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "regexp" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/dapperlabs/flow-playground-api/model" |
| 28 | +) |
| 29 | + |
| 30 | +// Backward compatibility address adapters. |
| 31 | +// |
| 32 | +// Because new blockchain execution is done using the emulator, it takes up first X accounts as service accounts, so if we |
| 33 | +// want to keep the same address space for the user we need to translate addresses coming from the user to the backend and vice-versa. |
| 34 | +// todo temp workaround to prevent API breaking changes, remove this in the v2. |
| 35 | +// We can avoid doing translations of address in the next version of playground we can start the address space at 0x0a. |
| 36 | + |
| 37 | +const numberOfServiceAccounts = 4 |
| 38 | +const addressLength = 8 |
| 39 | + |
| 40 | +// contentAddressFromAPI converts addresses found in content from the user input. |
| 41 | +func contentAddressFromAPI(input string) string { |
| 42 | + return contentAdapter(input, true) |
| 43 | +} |
| 44 | + |
| 45 | +// contentAddressToAPI converts addresses found in content to the user output. |
| 46 | +func contentAddressToAPI(input string) string { |
| 47 | + return contentAdapter(input, false) |
| 48 | +} |
| 49 | + |
| 50 | +func contentAdapter(input string, fromInput bool) string { |
| 51 | + r := regexp.MustCompile(`0x0*(\d+)`) |
| 52 | + |
| 53 | + // we must use this logic since if we parse the address to Address type |
| 54 | + // it outputs it in standard format which might be different to the input format |
| 55 | + for _, addressMatch := range r.FindAllStringSubmatch(input, -1) { |
| 56 | + original := addressMatch[0] |
| 57 | + addr, _ := strconv.Atoi(addressMatch[1]) |
| 58 | + |
| 59 | + if fromInput { |
| 60 | + addr = addr + numberOfServiceAccounts |
| 61 | + } else if addr > numberOfServiceAccounts { // don't convert if service address, shouldn't happen |
| 62 | + addr = addr - numberOfServiceAccounts |
| 63 | + } |
| 64 | + |
| 65 | + replaced := strings.ReplaceAll(original, addressMatch[1], fmt.Sprintf("%d", addr)) |
| 66 | + input = strings.ReplaceAll(input, original, replaced) |
| 67 | + } |
| 68 | + |
| 69 | + return input |
| 70 | +} |
| 71 | + |
| 72 | +// addressFromAPI converts the address from the user input and shifts it for number of service accounts. |
| 73 | +func addressFromAPI(address model.Address) model.Address { |
| 74 | + var b model.Address // create a copy |
| 75 | + copy(b[:], address[:]) |
| 76 | + b[len(b)-1] = b[len(b)-1] + numberOfServiceAccounts |
| 77 | + return b |
| 78 | +} |
| 79 | + |
| 80 | +func addressesFromAPI(addresses []model.Address) []model.Address { |
| 81 | + for i, address := range addresses { |
| 82 | + addresses[i] = addressFromAPI(address) |
| 83 | + } |
| 84 | + return addresses |
| 85 | +} |
| 86 | + |
| 87 | +// addressToAPI converts the address to the user output by subtracting the number of service accounts. |
| 88 | +func addressToAPI(address model.Address) model.Address { |
| 89 | + var b model.Address |
| 90 | + copy(b[addressLength-len(address):], address[:]) |
| 91 | + b[len(b)-1] = b[len(b)-1] - numberOfServiceAccounts |
| 92 | + return b |
| 93 | +} |
| 94 | + |
| 95 | +func addressesToAPI(addresses []model.Address) []model.Address { |
| 96 | + for i, address := range addresses { |
| 97 | + addresses[i] = addressToAPI(address) |
| 98 | + } |
| 99 | + return addresses |
| 100 | +} |
0 commit comments