Skip to content

Instantly share code, notes, and snippets.

@tyler-smith
Created December 15, 2018 17:50
Show Gist options
  • Save tyler-smith/85c6c0e910657b6a4ed7215b8fe41c56 to your computer and use it in GitHub Desktop.
Save tyler-smith/85c6c0e910657b6a4ed7215b8fe41c56 to your computer and use it in GitHub Desktop.
Convert to Cashaddr
// Put addresses in addrs.txt file in the directory you run this program from
// It will convert them to cashaddr and write them to stdout
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type cashaddrResp struct {
Cashaddr string `json:"cashaddr"`
}
func main() {
addrs, err := readAddrs("./addrs.txt")
if err != nil {
log.Fatal("Read addrs failed for: ./addrs.txt")
}
for _, addr := range addrs {
r, err := http.Get("https://cashaddr.bitcoincash.org/convert?address=" + addr)
if err != nil {
log.Fatal("Get failed for:", addr)
}
resp, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal("Read response failed for:", addr)
}
cr := &cashaddrResp{}
err = json.Unmarshal(resp, cr)
if err != nil {
log.Fatal("Unmarshal response failed for:", addr)
}
fmt.Println(cr.Cashaddr)
}
}
func readAddrs(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment