Skip to content

Commit

Permalink
#146 Cli: Add possibility to restore acc from priv keys
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov authored and hleb-albau committed Jan 3, 2019
1 parent 2d77cea commit ed9df7f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
125 changes: 125 additions & 0 deletions cli/commands/importprivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package commands

import (
"bytes"
"encoding/hex"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/btcd/btcec"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/cli"
)

const (
flagName = "name"
)

//info = kb.writeLocalKey(secp256k1.PrivKeySecp256k1(derivedPriv), name, passwd)

// LinkTxCmd will create a link tx and sign it with the given key.
func ImportPrivateKeyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "import_private <name>",
Short: "Import private key",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

rootDir := viper.GetString(cli.HomeFlag)
kb, _ := keys.GetKeyBaseFromDirWithWritePerm(rootDir)
//var privateKey [32]byte
//
//kbType := reflect.TypeOf(kb)
//fmt.Println(kbType)
//
//kbValue := reflect.ValueOf(kb).Convert(kbType)
//fmt.Println(kbValue.NumMethod())
//
//method := reflect.ValueOf(kb).Convert(kbType).MethodByName("writeLocalKey")
//in := make([]reflect.Value, 3)
//in[0] = reflect.ValueOf(secp256k1.PrivKeySecp256k1(privateKey))
//in[1] = reflect.ValueOf("artur54321")
//in[2] = reflect.ValueOf("12345668")
//
//resp := method.Call(in)
//fmt.Println(resp)

//cbd19msfav283ykk2yeedv2hjfwjrp3xfdg0we7vdj
//cbdpub1addwnpepqthpt3zevh30gn3cfd5eelf3zuw2kxaqj948tjhu3ztrdervejwucsalyeq
//[38 45 173 80 146 139 15 246 55 75 107 187 25 121 131 220 152 254 99 73 204 227 249 40 107 134 252 237 74 55 4 169]
//[235 90 233 135 33 2 129 33 115 9 231 5 65 199 75 91 150 174 228 90 235 212 46 136 215 242 138 140 160 175 8 247 179 72 246 3 162 217]
//armor, _ := kb.Export("artur2")
//infoBytes, _ := mintkey.UnarmorInfoBytes(armor)
//fmt.Println(infoBytes)

name := args[0]

bufStdin := client.BufferStdin()
encryptPassword, err := client.GetCheckPassword(
"Enter a passphrase to encrypt your key to disk:",
"Repeat the passphrase:", bufStdin)

privateKey, err := client.GetString(
"Enter your private key (hex encoded):", bufStdin)
if err != nil {
return err
}
//pubKey := [33]byte{2, 129, 33, 115, 9, 231, 5, 65, 199, 75, 91, 150, 174, 228, 90, 235, 212, 46, 136, 215, 242, 138, 140, 160, 175, 8, 247, 179, 72, 246, 3, 162, 217}
//privKey := [32]byte{38, 45, 173, 80, 146, 139, 15, 246, 55, 75, 107, 187, 25, 121, 131, 220, 152, 254, 99, 73, 204, 227, 249, 40, 107, 134, 252, 237, 74, 55, 4, 169}

b, _ := hex.DecodeString(privateKey)

privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), b)
ethPubkey, _ := btcec.ParsePubKey(pubKey.SerializeUncompressed(), btcec.S256())

var cbdPubKey [33]byte
copy(cbdPubKey[:], ethPubkey.SerializeCompressed()[:])

var cbdPribKey [32]byte
copy(cbdPribKey[:], privKey.Serialize()[:])

pkArmor := mintkey.EncryptArmorPrivKey(secp256k1.PrivKeySecp256k1(cbdPribKey), encryptPassword)

buf := bytes.NewBuffer(nil)

buf.Write([]byte{13, 173, 21, 61, 10})
amino.EncodeString(buf, name)
buf.Write([]byte{18, 38, 235, 90, 233, 135, 33})
buf.Write(cbdPubKey[:])
buf.Write([]byte{26})
amino.EncodeString(buf, pkArmor)

bz := buf.Bytes()
bufRes := bytes.NewBuffer(nil)
amino.EncodeUvarint(bufRes, uint64(len(bz)))
bufRes.Write(bz)

armorStr := mintkey.ArmorInfoBytes(bufRes.Bytes())
// import armored bytesm
kb.Import(name, armorStr)

//pk, _ := kb.ExportPrivateKeyObject("artur", "1234qwer")
//fmt.Println(pk)
//pk, _ := kb.ExportPubKey("artur")
//pkUn, _ := mintkey.UnarmorPubKeyBytes(pk)
//fmt.Println(pkUn)
//
//pkArmor := mintkey.EncryptArmorPrivKey(pk, "1234qwer")
//fmt.Println(pkArmor)

//eager salon spare notice expire throw chaos hub dune poverty stuff trim ancient fame armor domain bring topple thing boost impulse hire best scorpion
//2 129 ... 162 217
//info, _ := kb.CreateKey("artur", "eager salon spare notice expire throw chaos hub dune poverty stuff trim ancient fame armor domain bring topple thing boost impulse hire best scorpion", "1234qwer")
//fmt.Println(info)

return nil
},
}

cmd.Flags().String(flagName, "", "Name of key owner")

return cmd
}
1 change: 1 addition & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
keys.Commands(),
client.LineBreak,
version.VersionCmd,
cyberdcmd.ImportPrivateKeyCmd(),
)

cyberdcli.AddCommand(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/spf13/viper v1.0.0
github.com/stretchr/testify v1.2.1
github.com/syndtr/goleveldb v0.0.0-20181128100959-b001fa50d6b2 // indirect
github.com/tendermint/btcd v0.0.0-20180816174608-e5840949ff4f // indirect
github.com/tendermint/btcd v0.0.0-20180816174608-e5840949ff4f
github.com/tendermint/go-amino v0.14.1
github.com/tendermint/iavl v0.12.0 // indirect
github.com/tendermint/tendermint v0.27.4
Expand Down

0 comments on commit ed9df7f

Please sign in to comment.