forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (125 loc) · 2.67 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// aiblocks-sign is a small interactive utility to help you contribute a
// signature to a transaction envelope.
//
// It prompts you for a key
package main
import (
"bufio"
"flag"
"fmt"
"github.com/aiblocks/go/keypair"
"github.com/aiblocks/go/network"
"io/ioutil"
"log"
"os"
"strings"
"github.com/howeyc/gopass"
"github.com/aiblocks/go/txnbuild"
"github.com/aiblocks/go/xdr"
)
var in *bufio.Reader
var infile = flag.String("infile", "", "transaction envelope")
func main() {
flag.Parse()
in = bufio.NewReader(os.Stdin)
var (
env string
err error
)
if *infile == "" {
// read envelope
env, err = readLine("Enter envelope (base64): ", false)
if err != nil {
log.Fatal(err)
}
} else {
var file *os.File
file, err = os.Open(*infile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var raw []byte
raw, err = ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
env = string(raw)
}
// parse the envelope
var txe xdr.TransactionEnvelope
err = xdr.SafeUnmarshalBase64(env, &txe)
if err != nil {
log.Fatal(err)
}
fmt.Println("")
fmt.Println("Transaction Summary:")
sourceAccount := txe.SourceAccount().ToAccountId()
fmt.Printf(" type: %s\n", txe.Type.String())
fmt.Printf(" source: %s\n", sourceAccount.Address())
fmt.Printf(" ops: %d\n", len(txe.Operations()))
fmt.Printf(" sigs: %d\n", len(txe.Signatures()))
if txe.IsFeeBump() {
fmt.Printf(" fee bump sigs: %d\n", len(txe.FeeBumpSignatures()))
}
fmt.Println("")
// TODO: add operation details
// read seed
seed, err := readLine("Enter seed: ", true)
if err != nil {
log.Fatal(err)
}
// sign the transaction
kp, err := keypair.ParseFull(seed)
if err != nil {
log.Fatal(err)
}
parsed, err := txnbuild.TransactionFromXDR(env)
if err != nil {
log.Fatal(err)
}
var newEnv string
if tx, ok := parsed.Transaction(); ok {
tx, err = tx.Sign(network.PublicNetworkPassphrase, kp)
if err != nil {
log.Fatal(err)
}
newEnv, err = tx.Base64()
if err != nil {
log.Fatal(err)
}
} else {
tx, _ := parsed.FeeBump()
tx, err = tx.Sign(network.PublicNetworkPassphrase, kp)
if err != nil {
log.Fatal(err)
}
newEnv, err = tx.Base64()
if err != nil {
log.Fatal(err)
}
}
fmt.Print("\n==== Result ====\n\n")
fmt.Print("```\n")
fmt.Println(newEnv)
fmt.Print("```\n")
}
func readLine(prompt string, private bool) (string, error) {
fmt.Println(prompt)
var line string
var err error
if private {
var str []byte
str, err = gopass.GetPasswdMasked()
if err != nil {
return "", err
}
line = string(str)
} else {
line, err = in.ReadString('\n')
if err != nil {
return "", err
}
}
return strings.Trim(line, "\n"), nil
}