|
| 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