This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
115 lines (103 loc) · 2.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: [email protected]
// Address examples
//
// tz1KzpjBnunNJVABHBnzfG4iuLmphitExW2p
// tz3gN8NTLNLJg5KRsUU47NHNVHbdhcFXjjaB
// KT1GyeRktoGPEKsWpchWguyy8FAf3aNHkw2T
package main
import (
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"blockwatch.cc/tzgo/tezos"
)
var (
flags = flag.NewFlagSet("addr", flag.ContinueOnError)
verbose bool
)
func init() {
flags.Usage = func() {}
flags.BoolVar(&verbose, "v", false, "be verbose")
}
func main() {
if err := flags.Parse(os.Args[1:]); err != nil {
if err == flag.ErrHelp {
fmt.Println("Address Test")
flags.PrintDefaults()
os.Exit(0)
}
log.Fatalln("Error:", err)
}
if err := run(); err != nil {
log.Fatalln("Error:", err)
}
}
func run() error {
if flags.NArg() < 1 {
return fmt.Errorf("Address required")
}
if flags.NArg() == 2 {
return blinded()
}
var (
key tezos.Key
addr tezos.Address
err error
)
// try decoding as key
if key, err = tezos.ParseKey(flags.Arg(0)); err != nil {
// try decoding an address
if addr, err = tezos.ParseAddress(flags.Arg(0)); err != nil {
// try decoding as hex
buf, err := hex.DecodeString(flags.Arg(0))
if err != nil {
return err
}
err = addr.Decode(buf)
if err != nil {
if err = key.UnmarshalBinary(buf); err != nil {
return fmt.Errorf("Not a valid key or address")
}
}
}
} else {
addr = key.Address()
}
if key.IsValid() {
fmt.Printf("Key %s\n", key.String())
fmt.Printf("ReKey %s\n", tezos.NewKey(addr.KeyType(), key.Data))
fmt.Printf("KeyData %x\n", key.Data)
addr = key.Address()
}
fmt.Printf("Address %s\n", addr.String())
fmt.Printf("Short %s\n", tezos.Short(addr))
fmt.Printf("PkType %s\n", addr.Type())
fmt.Printf("PkHash %x\n", addr.Hash())
return nil
}
func blinded() error {
// Example
// "pkh": "tz1T1rRqmAk4XtGadNJuNpq8dUdWqLv2Gtq4",
// "secret": "06da1e038224114366831e47aee7f128f4675311",
// try decoding an address
addr, err := tezos.ParseAddress(flags.Arg(0))
if err != nil {
return err
}
// try blinding with secret
secret, err := hex.DecodeString(flags.Arg(1))
if err != nil {
return err
}
blind, err := tezos.BlindAddress(addr, secret)
if err != nil {
return err
}
fmt.Printf("Address %s\n", addr.String())
fmt.Printf("Hash %x\n", addr.Hash())
fmt.Printf("Blinded %s\n", blind.String())
return nil
}