forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onflow#5939 from onflow/bastian/allow-export-of-gl…
…obal-registers Allow export of global registers (owner is empty string)
- Loading branch information
Showing
8 changed files
with
191 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
func ParseOwners(hexAddresses []string) (map[string]struct{}, error) { | ||
if len(hexAddresses) == 0 { | ||
return nil, fmt.Errorf("at least one address must be provided") | ||
} | ||
|
||
addresses := make(map[string]struct{}, len(hexAddresses)) | ||
for _, hexAddr := range hexAddresses { | ||
hexAddr = strings.TrimSpace(hexAddr) | ||
|
||
if len(hexAddr) > 0 { | ||
addr, err := ParseAddress(hexAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
addresses[string(addr[:])] = struct{}{} | ||
} else { | ||
// global registers has empty address | ||
addresses[""] = struct{}{} | ||
} | ||
} | ||
|
||
return addresses, nil | ||
} | ||
|
||
func ParseAddress(hexAddr string) (flow.Address, error) { | ||
b, err := hex.DecodeString(hexAddr) | ||
if err != nil { | ||
return flow.Address{}, fmt.Errorf( | ||
"address is not hex encoded %s: %w", | ||
strings.TrimSpace(hexAddr), | ||
err, | ||
) | ||
} | ||
|
||
return flow.BytesToAddress(b), nil | ||
} | ||
|
||
func OwnersToString(owners map[string]struct{}) string { | ||
var sb strings.Builder | ||
index := 0 | ||
for owner := range owners { | ||
if index > 0 { | ||
sb.WriteRune(',') | ||
} | ||
_, _ = fmt.Fprintf(&sb, "%x", owner) | ||
index++ | ||
} | ||
return sb.String() | ||
} |
Oops, something went wrong.