forked from FourthState/plasma-mvp-sidechain
-
Notifications
You must be signed in to change notification settings - Fork 2
/
confirmSig.go
86 lines (74 loc) · 1.86 KB
/
confirmSig.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/FourthState/plasma-mvp-sidechain/client"
"github.com/FourthState/plasma-mvp-sidechain/client/context"
"github.com/ethereum/go-ethereum/accounts"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
)
const (
flagAddr = "addr"
)
func init() {
rootCmd.AddCommand(signCmd)
signCmd.Flags().String(flagAddr, "", "Address to sign with")
viper.BindPFlags(signCmd.Flags())
}
var signCmd = &cobra.Command{
Use: "sign <position>",
Short: "Sign positions to create confirmation signatures, format: 0.0.0.0::0.0.0.0",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// get position
posStr := args[0]
pos, err := client.ParsePositions(posStr)
if err != nil {
return err
}
ctx := context.NewClientContextFromViper()
dir := viper.GetString(FlagHomeDir)
ks := client.GetKeyStore(dir)
// get address to sign with
addrStr := viper.GetString(flagAddr)
addr, err := client.StrToAddress(addrStr)
if err != nil {
return err
}
acc := accounts.Account{
Address: addr,
}
// get account to sign with
acct, err := ks.Find(acc)
if err != nil {
return err
}
// get passphrase
passphrase, err := ctx.GetPassphraseFromStdin(addr)
if err != nil {
return err
}
// sign positions
signBytes1 := pos[0].GetSignBytes()
hash1 := ethcrypto.Keccak256(signBytes1)
sig1, err := ks.SignHashWithPassphrase(acct, passphrase, hash1)
var sig2 []byte
if len(pos) > 1 {
signBytes2 := pos[1].GetSignBytes()
hash2 := ethcrypto.Keccak256(signBytes2)
sig2, err = ks.SignHashWithPassphrase(acct, passphrase, hash2)
if err != nil {
return err
}
}
fmt.Println()
fmt.Println("Sig 1:")
fmt.Printf("%X", sig1)
fmt.Println()
fmt.Println("Sig 2:")
fmt.Printf("%X", sig2)
fmt.Println()
return nil
},
}