From d0adbcfcdac9cbc810db46e5fa498f497e444b7c Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Wed, 15 Jan 2025 16:58:40 -0500 Subject: [PATCH] feat: password on root key (#126) Signed-off-by: Chris Gianelloni --- bursa.go | 14 +++++++++----- docs/docs.go | 20 ++++++++++++++++++-- docs/swagger.json | 17 ++++++++++++++++- docs/swagger.yaml | 14 ++++++++++++-- internal/api/api.go | 25 ++++++++++++++++++++++--- 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/bursa.go b/bursa.go index a6d5ec8..4638349 100644 --- a/bursa.go +++ b/bursa.go @@ -47,11 +47,11 @@ type Wallet struct { } func NewWallet( - mnemonic, network string, + mnemonic, network, password string, accountId uint, paymentId, stakeId, addressId uint32, ) (*Wallet, error) { - rootKey, err := GetRootKeyFromMnemonic(mnemonic) + rootKey, err := GetRootKeyFromMnemonic(mnemonic, password) if err != nil { return nil, fmt.Errorf("failed to get root key from mnemonic: %s", err) } @@ -75,7 +75,7 @@ func NewWallet( func NewDefaultWallet(mnemonic string) (*Wallet, error) { cfg := config.GetConfig() - w, err := NewWallet(mnemonic, cfg.Network, 0, 0, 0, 0) + w, err := NewWallet(mnemonic, "", cfg.Network, 0, 0, 0, 0) if err != nil { return nil, fmt.Errorf("failed to create default wallet: %s", err) } @@ -94,12 +94,16 @@ func NewMnemonic() (string, error) { return mnemonic, nil } -func GetRootKeyFromMnemonic(mnemonic string) (bip32.XPrv, error) { +func GetRootKeyFromMnemonic(mnemonic, password string) (bip32.XPrv, error) { entropy, err := bip39.EntropyFromMnemonic(mnemonic) if err != nil { return nil, err } - rootKey := GetRootKey(entropy, []byte{}) // TODO: support password + pwBytes := []byte{} + if password != "" { + pwBytes = []byte(password) + } + rootKey := GetRootKey(entropy, pwBytes) return rootKey, nil } diff --git a/docs/docs.go b/docs/docs.go index 0b029e7..9809bbd 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,4 +1,5 @@ -// Package docs Code generated by swaggo/swag. DO NOT EDIT +// Code generated by swaggo/swag. DO NOT EDIT. + package docs import "github.com/swaggo/swag" @@ -42,7 +43,7 @@ const docTemplate = `{ }, "/api/wallet/restore": { "post": { - "description": "Restores a wallet using the provided mnemonic seed phrase and returns wallet details.", + "description": "Restores a wallet using the provided mnemonic seed phrase and optional password and returns wallet details.", "consumes": [ "application/json" ], @@ -91,8 +92,23 @@ const docTemplate = `{ "mnemonic" ], "properties": { + "account_id": { + "type": "integer" + }, + "address_id": { + "type": "integer" + }, "mnemonic": { "type": "string" + }, + "password": { + "type": "string" + }, + "payment_id": { + "type": "integer" + }, + "stake_id": { + "type": "integer" } } }, diff --git a/docs/swagger.json b/docs/swagger.json index 1e67e17..89e5d5e 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -38,7 +38,7 @@ }, "/api/wallet/restore": { "post": { - "description": "Restores a wallet using the provided mnemonic seed phrase and returns wallet details.", + "description": "Restores a wallet using the provided mnemonic seed phrase and optional password and returns wallet details.", "consumes": [ "application/json" ], @@ -87,8 +87,23 @@ "mnemonic" ], "properties": { + "account_id": { + "type": "integer" + }, + "address_id": { + "type": "integer" + }, "mnemonic": { "type": "string" + }, + "password": { + "type": "string" + }, + "payment_id": { + "type": "integer" + }, + "stake_id": { + "type": "integer" } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2f53aa5..1be98d7 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2,8 +2,18 @@ basePath: / definitions: api.WalletRestoreRequest: properties: + account_id: + type: integer + address_id: + type: integer mnemonic: type: string + password: + type: string + payment_id: + type: integer + stake_id: + type: integer required: - mnemonic type: object @@ -64,8 +74,8 @@ paths: post: consumes: - application/json - description: Restores a wallet using the provided mnemonic seed phrase and returns - wallet details. + description: Restores a wallet using the provided mnemonic seed phrase and optional + password and returns wallet details. parameters: - description: Wallet Restore Request in: body diff --git a/internal/api/api.go b/internal/api/api.go index 4788268..b4f2890 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -33,9 +33,19 @@ import ( _ "github.com/blinklabs-io/bursa/docs" // docs is generated by Swag CLI ) +// WalletCreateRequest defines the request payload for wallet creation +type WalletCreateRequest struct { + Password string `json:"password"` +} + // WalletRestoreRequest defines the request payload for wallet restoration type WalletRestoreRequest struct { - Mnemonic string `json:"mnemonic" binding:"required"` + Mnemonic string `json:"mnemonic" binding:"required"` + Password string `json:"password"` + AccountId uint `json:"account_id"` + PaymentId uint32 `json:"payment_id"` + StakeId uint32 `json:"stake_id"` + AddressId uint32 `json:"address_id"` } // @title bursa @@ -238,7 +248,7 @@ func handleWalletCreate(w http.ResponseWriter, r *http.Request) { // handleWalletRestore handles the wallet restoration request. // // @Summary Restore a wallet using a mnemonic seed phrase -// @Description Restores a wallet using the provided mnemonic seed phrase and returns wallet details. +// @Description Restores a wallet using the provided mnemonic seed phrase and optional password and returns wallet details. // @Accept json // @Produce json // @Param request body WalletRestoreRequest true "Wallet Restore Request" @@ -261,7 +271,16 @@ func handleWalletRestore(w http.ResponseWriter, r *http.Request) { return } - wallet, err := bursa.NewDefaultWallet(req.Mnemonic) + cfg := config.GetConfig() + wallet, err := bursa.NewWallet( + req.Mnemonic, + req.Password, + cfg.Network, + req.AccountId, + req.PaymentId, + req.StakeId, + req.AddressId, + ) if err != nil { w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(`{"error":"Internal server error"}`))