Skip to content

Commit

Permalink
Add convertAddress tool. (#3304)
Browse files Browse the repository at this point in the history
## Summary

New tool: convertAddress
I share this tool with someone every few months, putting it in the repo along with some documentation should make it easier to share and encourage people to share it amongst themselves if it's useful.

Merge `debug` into `tools` to make it easier to organize these miscellaneous tools.
  • Loading branch information
winder authored Dec 20, 2021
1 parent 9cdcb39 commit 22a5ebb
Show file tree
Hide file tree
Showing 30 changed files with 105 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,14 @@ The following packages allow developers to interface with the Algorand system:
- `cmd/catchupsrv` ([README](cmd/catchupsrv/README.md)) is a tool to
assist with processing historic blocks on a new node.
- `libgoal` exports a Go interface useful for developers of Algorand clients.
- `debug` holds secondary commands which assist developers during debugging.
- `tools` ([README](tools/README.md)) various tools and utilities without a better place to go.
- `tools/debug` holds secondary commands which assist developers during debugging.
- `tools/misc` ([README](tools/misc/README.md)) small tools that are sometimes handy in a pinch.

The following packages contain tools to help Algorand developers deploy networks
of their own:

- `nodecontrol`
- `tools`
- `docker`
- `commandandcontrol` ([README](test/commandandcontrol/README.md)) is a tool to
automate a network of algod instances.
Expand Down
20 changes: 20 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Misfit Tools

Various tools and utilities that don't have a better place to go.

### debug

Tools for debugging algod. These were really useful before launch when we spent a lot of time analyzing node behavior, but aren't needed as much recently.

### misc

Small tools that are useful in niche situations.

### network

This is a go package used by some of the CLI commands.

### TEAL

Some of the earliest tools we built for working with TEAL programs. A lot of these are workarounds for things that no longer need to be worked around, but these are still nice examples of what TEAL can do.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions tools/misc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Misc Tools

Collection of various tools that are useful enough to save, but niche enough that they don't belong in goal.


### convertAddress

It's sometimes useful to convert addresses between the public "Stripped Base32 /w checksum" format, and the Base64 encoded 32 byte format. This tool converts the two formats.

There is only very minor error checking, results from this tool to not ensure valid inputs.

```sh
# Address to Base64 encoded bytes.
~$ go run convertAddress.go -addr E33YVTQNYH2BHI33OCYL7JQQSEMXD4EN74CZQ37FC6EZFHIBCNWOWXIZ5M
JveKzg3B9BOje3Cwv6YQkRlx8I3/BZhv5ReJkp0BE2w=

# Base64 encoded bytes to Address.
~$ go run convertAddress.go -addr JveKzg3B9BOje3Cwv6YQkRlx8I3/BZhv5ReJkp0BE2w=
E33YVTQNYH2BHI33OCYL7JQQSEMXD4EN74CZQ37FC6EZFHIBCNWOWXIZ5M

```

If you want to run it more than once, it may be useful to compile a binary rather than building with `go run` on each invokation:
```sh
# Build binary.
~$ go build convertAddress.go

# Use binary.
~$ ./convertAddress -addr JveKzg3B9BOje3Cwv6YQkRlx8I3/BZhv5ReJkp0BE2w=
E33YVTQNYH2BHI33OCYL7JQQSEMXD4EN74CZQ37FC6EZFHIBCNWOWXIZ5M
```
51 changes: 51 additions & 0 deletions tools/misc/convertAddress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

package main

import (
"encoding/base64"
"flag"
"fmt"

"github.com/algorand/go-algorand/data/basics"
)

func main() {
var addrInput string
flag.StringVar(&addrInput, "addr", "", "base64/algorand address to convert to the other")
flag.Parse()

if addrInput == "" {
fmt.Println("provide input with '-addr' flag.")
return
}

addrBytes, err := base64.StdEncoding.DecodeString(addrInput)
if err != nil {
// Failed to base64 decode, check for Algorand address format.
a, err := basics.UnmarshalChecksumAddress(addrInput)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(base64.StdEncoding.EncodeToString(a[:]))
return
}
var addr basics.Address
copy(addr[:], addrBytes)
fmt.Println(addr.String())
}

0 comments on commit 22a5ebb

Please sign in to comment.