/
bit2evm_command.go
86 lines (71 loc) · 2 KB
/
bit2evm_command.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 cli
import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/fatih/color"
wallet "github.com/nikola43/stardust/wallet"
)
type Bit2EvmCommand struct {
args *Args
}
const (
bit2evmCommand = "bit2evm"
bit2evmDescription = "generate ETH public and private key from BTC private key"
)
func newBit2EvmCommand() Command {
return Command{
Name: bit2evmCommand,
Description: bit2evmDescription,
Exec: &Bit2EvmCommand{},
}
}
func (c *Bit2EvmCommand) ExecCommand(ctx context.Context, args []string) error {
c.args = &Args{args}
if len(args) == 0 {
return ErrorFromString(fmt.Sprintf("file not found"))
}
wif, err := wallet.ImportWIF(args[0])
if err != nil {
log.Fatal(err)
}
w := wallet.GenerateBTCWalletFromWIF(wif)
if err != nil {
log.Fatal(err)
}
fmt.Println(color.CyanString("BTC Public Key: "), color.YellowString(w.PublicKey))
fmt.Println(color.CyanString("BTC Private Key: "), color.YellowString(w.PrivateKey))
fmt.Println()
ethDerivedPrivateKey := HashValue(w.PrivateKey)
ethDerivedPublicKey, err := GenerateAddressFromPlainPrivateKey(ethDerivedPrivateKey)
if err != nil {
log.Fatal(err)
}
fmt.Println(color.CyanString("ETH Derived Public Key: "), color.YellowString(ethDerivedPublicKey.Hex()))
fmt.Println(color.CyanString("ETH Derived Private Key: "), color.YellowString(ethDerivedPrivateKey))
fmt.Println()
return nil
}
func GenerateAddressFromPlainPrivateKey(pk string) (common.Address, error) {
var address common.Address
privateKey, err := crypto.HexToECDSA(pk)
if err != nil {
return address, err
}
publicKeyECDSA, ok := privateKey.Public().(*ecdsa.PublicKey)
if !ok {
return address, errors.New("error casting public key to ECDSA")
}
return crypto.PubkeyToAddress(*publicKeyECDSA), nil
}
func HashValue(value string) string {
hash := sha256.New()
hash.Write([]byte(value))
return hex.EncodeToString(hash.Sum(nil))
}