github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/tools/base58/base58.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"fmt"
     6  	"flag"
     7  	"strings"
     8  	"io/ioutil"
     9  	"encoding/hex"
    10  	"github.com/piotrnar/gocoin/lib/btc"
    11  )
    12  
    13  var (
    14  	decode = flag.Bool("d", false, "run base58 decode (instead of encode)")
    15  	binary = flag.Bool("b", false, "binary (insted of hex) for decode output")
    16  	help = flag.Bool("h", false, "print this help")
    17  )
    18  
    19  func main() {
    20  	flag.Parse()
    21  	if *help {
    22  		flag.PrintDefaults()
    23  		return
    24  	}
    25  
    26  	msg, _ := ioutil.ReadAll(os.Stdin)
    27  	if len(msg)==0 {
    28  		return
    29  	}
    30  
    31  	if *decode {
    32  		res := btc.Decodeb58(strings.Trim(string(msg), " \t\n\r"))
    33  		if *binary {
    34  			os.Stdout.Write(res)
    35  		} else {
    36  			fmt.Println(hex.EncodeToString(res))
    37  		}
    38  	} else {
    39  		fmt.Println(btc.Encodeb58(msg))
    40  	}
    41  }