7 "github.com/tendermint/go-wire/data/base58"
9 "github.com/bytom/crypto/ed25519/chainkd"
10 "github.com/bytom/crypto/sha3pool"
11 "github.com/bytom/errors"
14 //KeyImportParams private key import param
15 type KeyImportParams struct {
16 KeyAlias string `json:"alias"`
17 Password string `json:"password"`
18 XPrv string `json:"xprv"`
19 Index uint64 `json:"index"`
20 AccountAlias string `json:"account_alias"`
23 func (a *API) walletExportKey(ctx context.Context, in struct {
24 Password string `json:"password"`
25 XPub chainkd.XPub `json:"xpub"`
27 key, err := a.wallet.ExportAccountPrivKey(in.XPub, in.Password)
29 return NewErrorResponse(err)
32 type privateKey struct {
33 PrivateKey string `json:"private_key"`
35 return NewSuccessResponse(privateKey{PrivateKey: *key})
38 func (a *API) walletImportKey(ctx context.Context, in KeyImportParams) Response {
39 rawData, err := base58.Decode(in.XPrv)
41 return NewErrorResponse(err)
44 if len(rawData) != 68 {
45 return NewErrorResponse(errors.New("invalid private key hash length"))
49 sha3pool.Sum256(hashed[:], rawData[:64])
50 if res := bytes.Compare(hashed[:4], rawData[64:]); res != 0 {
51 return NewErrorResponse(errors.New("private hash error"))
55 copy(xprv[:], rawData[:64])
57 xpub, err := a.wallet.ImportAccountPrivKey(xprv, in.KeyAlias, in.Password, in.Index, in.AccountAlias)
59 return NewErrorResponse(err)
61 return NewSuccessResponse(xpub)
64 func (a *API) keyImportProgress(ctx context.Context) Response {
65 data, err := a.wallet.GetRescanStatus()
67 return NewErrorResponse(err)
69 return NewSuccessResponse(data)