OSDN Git Service

Modify createKeyResp structure
[bytom/bytom.git] / api / hsm.go
index 4a9b6d6..ebba716 100644 (file)
@@ -8,27 +8,40 @@ import (
        "github.com/bytom/blockchain/pseudohsm"
        "github.com/bytom/blockchain/txbuilder"
        "github.com/bytom/crypto/ed25519/chainkd"
-       "github.com/bytom/net/http/httperror"
 )
 
-func init() {
-       //Error code 050 represents alias of key duplicated
-       errorFormatter.Errors[pseudohsm.ErrDuplicateKeyAlias] = httperror.Info{400, "BTM050", "Alias already exists"}
-       //Error code 801 represents query request format error
-       errorFormatter.Errors[pseudohsm.ErrInvalidAfter] = httperror.Info{400, "BTM801", "Invalid `after` in query"}
-       //Error code 802 represents query reponses too many
-       errorFormatter.Errors[pseudohsm.ErrTooManyAliasesToList] = httperror.Info{400, "BTM802", "Too many aliases to list"}
+type createKeyResp struct {
+       Alias    string       `json:"alias"`
+       XPub     chainkd.XPub `json:"xpub"`
+       File     string       `json:"file"`
+       Mnemonic string       `json:"mnemonic"`
 }
 
 func (a *API) pseudohsmCreateKey(ctx context.Context, in struct {
        Alias    string `json:"alias"`
        Password string `json:"password"`
+       Mnemonic string `json:"nnemonic"`
+       Language string `json:"language"`
 }) Response {
-       xpub, err := a.wallet.Hsm.XCreate(in.Alias, in.Password)
+       if in.Language == "" {
+               in.Language = "en"
+       }
+       if len(in.Mnemonic) > 0 {
+               xpub, err := a.wallet.Hsm.ImportKeyFromMnemonic(in.Alias, in.Password, in.Mnemonic, in.Language)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File})
+       }
+       xpub, mnemonic, err := a.wallet.Hsm.XCreate(in.Alias, in.Password, in.Language)
        if err != nil {
                return NewErrorResponse(err)
        }
-       return NewSuccessResponse(xpub)
+       return NewSuccessResponse(&createKeyResp{Alias: xpub.Alias, XPub: xpub.XPub, File: xpub.File, Mnemonic: *mnemonic})
+}
+
+type importKeyResp struct {
+       Xpub *pseudohsm.XPub `json:"xpub"`
 }
 
 func (a *API) pseudohsmListKeys(ctx context.Context) Response {
@@ -66,7 +79,7 @@ func (a *API) pseudohsmSignTemplate(ctx context.Context, xpub chainkd.XPub, path
        return a.wallet.Hsm.XSign(xpub, path, data[:], password)
 }
 
-// ResetPasswordResp is response for reset password password
+// ResetPasswordResp is response for reset key password
 type ResetPasswordResp struct {
        Changed bool `json:"changed"`
 }
@@ -83,3 +96,20 @@ func (a *API) pseudohsmResetPassword(ctx context.Context, ins struct {
        resp.Changed = true
        return NewSuccessResponse(resp)
 }
+
+// CheckPasswordResp is response for check key password
+type CheckPasswordResp struct {
+       CheckResult bool `json:"check_result"`
+}
+
+func (a *API) pseudohsmCheckPassword(ctx context.Context, ins struct {
+       XPub     chainkd.XPub `json:"xpub"`
+       Password string       `json:"password"`
+}) Response {
+       resp := &CheckPasswordResp{CheckResult: false}
+       if _, err := a.wallet.Hsm.LoadChainKDKey(ins.XPub, ins.Password); err != nil {
+               return NewSuccessResponse(resp)
+       }
+       resp.CheckResult = true
+       return NewSuccessResponse(resp)
+}