Skip to content

Commit 33224d5

Browse files
committed
expanded helper commands
1 parent 2ac742c commit 33224d5

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

cmd/helpers/helpers.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package helpers
22

33
import (
44
"github.com/hyle-team/tss-svc/cmd/helpers/generate"
5+
"github.com/hyle-team/tss-svc/cmd/helpers/parse"
56
"github.com/spf13/cobra"
67
)
78

@@ -16,5 +17,5 @@ var Cmd = &cobra.Command{
1617

1718
func registerHelpersCommands(cmd *cobra.Command) {
1819
cmd.AddCommand(generate.Cmd)
19-
cmd.AddCommand(parsePubKeyCmd)
20+
cmd.AddCommand(parse.Cmd)
2021
}

cmd/helpers/parse_address.go cmd/helpers/parse/address.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package helpers
1+
package parse
22

33
import (
44
"crypto/elliptic"
@@ -12,9 +12,9 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
var parsePubKeyCmd = &cobra.Command{
16-
Use: "parse-address [x-cord] [y-cord]",
17-
Short: "Parse address from the given point",
15+
var parseAddressCmd = &cobra.Command{
16+
Use: "address [x-cord] [y-cord]",
17+
Short: "Parse eth address from the given point",
1818
Args: cobra.ExactArgs(2),
1919
RunE: func(cmd *cobra.Command, args []string) error {
2020
xCord, ok := new(big.Int).SetString(args[0], 10)
@@ -28,11 +28,8 @@ var parsePubKeyCmd = &cobra.Command{
2828
}
2929

3030
marshalled := elliptic.Marshal(tss.S256(), xCord, yCord)
31-
32-
// Marshalled point contains constant 0x04 first byte, we have to remove it
33-
pubBytes := marshalled[1:]
34-
35-
hash := crypto.Keccak256(pubBytes)
31+
// Marshalled point contains constant 0x04 first byte, we do not have to include it
32+
hash := crypto.Keccak256(marshalled[1:])
3633

3734
// The Ethereum address is the last 20 bytes of the hash.
3835
address := common.BytesToAddress(hash[12:]) // hash[12:32]

cmd/helpers/parse/parse.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package parse
2+
3+
import "github.com/spf13/cobra"
4+
5+
func init() {
6+
registerParseCommands(Cmd)
7+
}
8+
9+
var Cmd = &cobra.Command{
10+
Use: "parse",
11+
Short: "Command for parsing data",
12+
}
13+
14+
func registerParseCommands(cmd *cobra.Command) {
15+
cmd.AddCommand(parseAddressCmd, parsePubkeyCmd)
16+
}

cmd/helpers/parse/pubkey.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package parse
2+
3+
import (
4+
"crypto/elliptic"
5+
"fmt"
6+
"math/big"
7+
8+
"github.com/bnb-chain/tss-lib/v2/tss"
9+
"github.com/ethereum/go-ethereum/common/hexutil"
10+
"github.com/ethereum/go-ethereum/crypto"
11+
"github.com/pkg/errors"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var parsePubkeyCmd = &cobra.Command{
16+
Use: "pubkey [x-cord] [y-cord]",
17+
Short: "Parse pubkey from the given point",
18+
Args: cobra.ExactArgs(2),
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
xCord, ok := new(big.Int).SetString(args[0], 10)
21+
if !ok {
22+
return errors.New("failed to parse x-cord")
23+
}
24+
25+
yCord, ok := new(big.Int).SetString(args[1], 10)
26+
if !ok {
27+
return errors.New("failed to parse y-cord")
28+
}
29+
30+
marshalled := elliptic.Marshal(tss.S256(), xCord, yCord)
31+
// Marshalled point contains constant 0x04 first byte, we have to remove it
32+
fmt.Println("Pubkey:", hexutil.Encode(marshalled[1:]))
33+
34+
key, err := crypto.UnmarshalPubkey(marshalled)
35+
if err != nil {
36+
return errors.Wrap(err, "failed to unmarshal pubkey")
37+
}
38+
39+
compressed := crypto.CompressPubkey(key)
40+
fmt.Println("Compressed:", hexutil.Encode(compressed))
41+
42+
return nil
43+
},
44+
}

0 commit comments

Comments
 (0)