7 "github.com/bytom/crypto/ed25519/chainkd"
8 "github.com/bytom/crypto/sha3pool"
9 "github.com/bytom/errors"
10 "github.com/tendermint/go-wire/data/base58"
13 type KeyImportParams struct {
14 KeyAlias string `json:"alias"`
15 Password string `json:"password"`
16 XPrv string `json:"xprv"`
17 Index uint64 `json:"index"`
18 AccountAlias string `json:"account_alias"`
21 func (bcr *BlockchainReactor) walletExportKey(ctx context.Context, in struct {
22 Password string `json:"password"`
23 XPub chainkd.XPub `json:"xpub"`
25 key, err := bcr.wallet.ExportAccountPrivKey(bcr.hsm, in.XPub, in.Password)
27 return NewErrorResponse(err)
30 type privateKey struct {
31 PrivateKey string `json:"private_key"`
33 return NewSuccessResponse(privateKey{PrivateKey: *key})
36 func (bcr *BlockchainReactor) walletImportKey(ctx context.Context, in KeyImportParams) Response {
37 rawData, err := base58.Decode(in.XPrv)
39 return NewErrorResponse(err)
42 if len(rawData) != 68 {
43 return NewErrorResponse(errors.New("invalid private key hash length"))
47 sha3pool.Sum256(hashed[:], rawData[:64])
48 if res := bytes.Compare(hashed[:4], rawData[64:]); res != 0 {
49 return NewErrorResponse(errors.New("private hash error"))
53 copy(xprv[:], rawData[:64])
55 xpub, err := bcr.wallet.ImportAccountPrivKey(bcr.hsm, xprv, in.KeyAlias, in.Password, in.Index, in.AccountAlias)
57 return NewErrorResponse(err)
59 return NewSuccessResponse(xpub)