Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: password on root key #126

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions bursa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
20 changes: 18 additions & 2 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down Expand Up @@ -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"
}
}
},
Expand Down
14 changes: 12 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"}`))
Expand Down
Loading