-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2433dac
commit 492e5b3
Showing
6 changed files
with
262 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package wallet | ||
|
||
import ( | ||
"github.com/pactus-project/pactus/crypto" | ||
"github.com/pactus-project/pactus/crypto/bls" | ||
"github.com/pactus-project/pactus/genesis" | ||
"github.com/pactus-project/pactus/types/tx" | ||
"github.com/pactus-project/pactus/wallet/vault" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type Manager struct { | ||
wallets map[string]*Wallet | ||
chainType genesis.ChainType | ||
} | ||
|
||
func NewWalletManager(chainType genesis.ChainType) *Manager { | ||
return &Manager{ | ||
wallets: make(map[string]*Wallet), | ||
chainType: chainType, | ||
} | ||
} | ||
|
||
func (w *Manager) GetValidatorAddress( | ||
publicKey string, | ||
) (string, error) { | ||
pubKey, err := bls.PublicKeyFromString(publicKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return pubKey.ValidatorAddress().String(), nil | ||
} | ||
|
||
func (w *Manager) CreateWallet( | ||
mnemonic, language, | ||
password, walletPath string, | ||
) error { | ||
wlt, err := Create(walletPath, mnemonic, language, w.chainType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := wlt.UpdatePassword("", password); err != nil { | ||
return err | ||
} | ||
|
||
if err := wlt.Save(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *Manager) LoadWallet( | ||
walletName, walletPath string, | ||
) error { | ||
if _, ok := w.wallets[walletName]; ok { | ||
// TODO: define special codes for errors | ||
return status.Errorf(codes.AlreadyExists, "wallet already loaded") | ||
} | ||
|
||
wlt, err := Open(walletPath, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w.wallets[walletName] = wlt | ||
|
||
return nil | ||
} | ||
|
||
func (w *Manager) UnloadWallet( | ||
walletName string, | ||
) error { | ||
if _, ok := w.wallets[walletName]; !ok { | ||
return status.Errorf(codes.NotFound, "wallet is not loaded") | ||
} | ||
|
||
delete(w.wallets, walletName) | ||
|
||
return nil | ||
} | ||
|
||
func (w *Manager) TotalBalance( | ||
walletName string, | ||
) (int64, error) { | ||
wlt, ok := w.wallets[walletName] | ||
if !ok { | ||
return 0, status.Errorf(codes.NotFound, "wallet is not loaded") | ||
} | ||
|
||
return wlt.TotalBalance().ToNanoPAC(), nil | ||
} | ||
|
||
func (w *Manager) SignRawTransaction( | ||
walletName, password string, rawTx []byte, | ||
) ([]byte, []byte, error) { | ||
wlt, ok := w.wallets[walletName] | ||
if !ok { | ||
return nil, nil, status.Errorf(codes.NotFound, "wallet is not loaded") | ||
} | ||
|
||
trx, err := tx.FromBytes(rawTx) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if err := wlt.SignTransaction(password, trx); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
data, err := trx.Bytes() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return trx.ID().Bytes(), data, nil | ||
} | ||
|
||
func (w *Manager) GetNewAddress( | ||
walletName, label string, | ||
addressType crypto.AddressType, | ||
) (*vault.AddressInfo, error) { | ||
wlt, ok := w.wallets[walletName] | ||
if !ok { | ||
return nil, status.Errorf(codes.NotFound, "wallet is not loaded") | ||
} | ||
|
||
var addressInfo *vault.AddressInfo | ||
switch addressType { | ||
case crypto.AddressTypeBLSAccount: | ||
info, err := wlt.NewBLSAccountAddress(label) | ||
if err != nil { | ||
return nil, err | ||
} | ||
addressInfo = info | ||
|
||
case crypto.AddressTypeValidator: | ||
info, err := wlt.NewValidatorAddress(label) | ||
if err != nil { | ||
return nil, err | ||
} | ||
addressInfo = info | ||
|
||
case crypto.AddressTypeTreasury: | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid address type") | ||
|
||
default: | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid address type") | ||
} | ||
|
||
if err := wlt.Save(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return addressInfo, nil | ||
} | ||
|
||
func (w *Manager) AddressHistory( | ||
walletName, address string, | ||
) ([]HistoryInfo, error) { | ||
wlt, ok := w.wallets[walletName] | ||
if !ok { | ||
return nil, status.Errorf(codes.NotFound, "wallet is not loaded") | ||
} | ||
|
||
return wlt.GetHistory(address), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.