forked from stellar/go
/
main.go
119 lines (100 loc) · 2.07 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
// stellar-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"
"io/ioutil"
"log"
"os"
"strings"
"github.com/howeyc/gopass"
"github.com/stellar/go/build"
"github.com/stellar/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 {
file, err := os.Open(*infile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
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:")
fmt.Printf(" source: %s\n", txe.Tx.SourceAccount.Address())
fmt.Printf(" ops: %d\n", len(txe.Tx.Operations))
fmt.Printf(" sigs: %d\n", len(txe.Signatures))
fmt.Println("")
// TODO: add operation details
// read seed
seed, err := readLine("Enter seed: ", true)
if err != nil {
log.Fatal(err)
}
// sign the transaction
b := &build.TransactionEnvelopeBuilder{E: &txe}
b.Init()
err = b.MutateTX(build.PublicNetwork)
if err != nil {
log.Fatal(err)
}
err = b.Mutate(build.Sign{seed})
if err != nil {
log.Fatal(err)
}
newEnv, err := xdr.MarshalBase64(b.E)
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.Fprintf(os.Stdout, prompt)
var line string
var err error
if private {
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
}