2018-03-02 15:24:09 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-03-13 13:11:49 +00:00
|
|
|
"errors"
|
2018-03-02 15:24:09 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2019-08-27 13:29:42 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The current version of neo-go wallet implementations.
|
2020-06-16 10:47:29 +00:00
|
|
|
walletVersion = "3.0"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
2019-02-13 18:01:10 +00:00
|
|
|
// Wallet represents a NEO (NEP-2, NEP-6) compliant wallet.
|
2018-03-02 15:24:09 +00:00
|
|
|
type Wallet struct {
|
|
|
|
// Version of the wallet, used for later upgrades.
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
// A list of accounts which describes the details of each account
|
2018-03-02 15:24:09 +00:00
|
|
|
// in the wallet.
|
|
|
|
Accounts []*Account `json:"accounts"`
|
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
Scrypt keys.ScryptParams `json:"scrypt"`
|
2018-03-02 15:24:09 +00:00
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
// Extra metadata can be used for storing arbitrary data.
|
2018-03-02 15:24:09 +00:00
|
|
|
// This field can be empty.
|
2020-03-06 12:58:24 +00:00
|
|
|
Extra Extra `json:"extra"`
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
// Path where the wallet file is located..
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:58:24 +00:00
|
|
|
// Extra stores imported token contracts.
|
|
|
|
type Extra struct {
|
|
|
|
// Tokens is a list of imported token contracts.
|
|
|
|
Tokens []*Token
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
// NewWallet creates a new NEO wallet at the given location.
|
|
|
|
func NewWallet(location string) (*Wallet, error) {
|
|
|
|
file, err := os.Create(location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-27 07:45:42 +00:00
|
|
|
defer file.Close()
|
2018-03-02 15:24:09 +00:00
|
|
|
return newWallet(file), nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// NewWalletFromFile creates a Wallet from the given wallet file path.
|
2018-03-02 15:24:09 +00:00
|
|
|
func NewWalletFromFile(path string) (*Wallet, error) {
|
2021-09-21 05:09:49 +00:00
|
|
|
file, err := os.Open(path)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-21 05:09:49 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
wall := &Wallet{
|
|
|
|
path: file.Name(),
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(file).Decode(wall); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wall, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWallet(rw io.ReadWriter) *Wallet {
|
|
|
|
var path string
|
|
|
|
if f, ok := rw.(*os.File); ok {
|
|
|
|
path = f.Name()
|
|
|
|
}
|
|
|
|
return &Wallet{
|
|
|
|
Version: walletVersion,
|
|
|
|
Accounts: []*Account{},
|
2019-08-27 13:29:42 +00:00
|
|
|
Scrypt: keys.NEP2ScryptParams(),
|
2018-03-02 15:24:09 +00:00
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// CreateAccount generates a new account for the end user and encrypts
|
2018-03-02 15:24:09 +00:00
|
|
|
// the private key with the given passphrase.
|
|
|
|
func (w *Wallet) CreateAccount(name, passphrase string) error {
|
|
|
|
acc, err := NewAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
acc.Label = name
|
2021-06-04 11:27:22 +00:00
|
|
|
if err := acc.Encrypt(passphrase, w.Scrypt); err != nil {
|
2018-03-02 15:24:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.AddAccount(acc)
|
|
|
|
return w.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAccount adds an existing Account to the wallet.
|
|
|
|
func (w *Wallet) AddAccount(acc *Account) {
|
|
|
|
w.Accounts = append(w.Accounts, acc)
|
|
|
|
}
|
|
|
|
|
2020-03-13 13:11:49 +00:00
|
|
|
// RemoveAccount removes an Account with the specified addr
|
|
|
|
// from the wallet.
|
|
|
|
func (w *Wallet) RemoveAccount(addr string) error {
|
|
|
|
for i, acc := range w.Accounts {
|
|
|
|
if acc.Address == addr {
|
|
|
|
copy(w.Accounts[i:], w.Accounts[i+1:])
|
|
|
|
w.Accounts = w.Accounts[:len(w.Accounts)-1]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("account wasn't found")
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:58:24 +00:00
|
|
|
// AddToken adds new token to a wallet.
|
|
|
|
func (w *Wallet) AddToken(tok *Token) {
|
|
|
|
w.Extra.Tokens = append(w.Extra.Tokens, tok)
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:23:45 +00:00
|
|
|
// RemoveToken removes token with the specified hash from the wallet.
|
|
|
|
func (w *Wallet) RemoveToken(h util.Uint160) error {
|
|
|
|
for i, tok := range w.Extra.Tokens {
|
|
|
|
if tok.Hash.Equals(h) {
|
|
|
|
copy(w.Extra.Tokens[i:], w.Extra.Tokens[i+1:])
|
|
|
|
w.Extra.Tokens = w.Extra.Tokens[:len(w.Extra.Tokens)-1]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("token wasn't found")
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
// Path returns the location of the wallet on the filesystem.
|
|
|
|
func (w *Wallet) Path() string {
|
|
|
|
return w.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save saves the wallet data. It's the internal io.ReadWriter
|
|
|
|
// that is responsible for saving the data. This can
|
|
|
|
// be a buffer, file, etc..
|
|
|
|
func (w *Wallet) Save() error {
|
2021-07-29 13:00:20 +00:00
|
|
|
data, err := json.Marshal(w)
|
|
|
|
if err != nil {
|
2020-04-22 13:55:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-29 13:00:20 +00:00
|
|
|
|
|
|
|
return w.writeRaw(data)
|
2020-04-22 13:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// savePretty saves wallet in a beautiful JSON.
|
|
|
|
func (w *Wallet) savePretty() error {
|
2021-07-29 13:00:20 +00:00
|
|
|
data, err := json.MarshalIndent(w, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.writeRaw(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wallet) writeRaw(data []byte) error {
|
2022-02-22 16:27:32 +00:00
|
|
|
return os.WriteFile(w.path, data, 0644)
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSON outputs a pretty JSON representation of the wallet.
|
|
|
|
func (w *Wallet) JSON() ([]byte, error) {
|
|
|
|
return json.MarshalIndent(w, " ", " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the internal rw if its an io.ReadCloser.
|
|
|
|
func (w *Wallet) Close() {
|
|
|
|
}
|
2020-01-15 15:10:40 +00:00
|
|
|
|
|
|
|
// GetAccount returns account corresponding to the provided scripthash.
|
|
|
|
func (w *Wallet) GetAccount(h util.Uint160) *Account {
|
2020-11-18 20:10:48 +00:00
|
|
|
addr := address.Uint160ToString(h)
|
2020-01-15 15:10:40 +00:00
|
|
|
for _, acc := range w.Accounts {
|
2020-11-18 20:10:48 +00:00
|
|
|
if acc.Address == addr {
|
2020-01-15 15:10:40 +00:00
|
|
|
return acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-14 16:17:07 +00:00
|
|
|
|
|
|
|
// GetChangeAddress returns the default address to send transaction's change to.
|
|
|
|
func (w *Wallet) GetChangeAddress() util.Uint160 {
|
|
|
|
var res util.Uint160
|
|
|
|
var acc *Account
|
|
|
|
|
|
|
|
for i := range w.Accounts {
|
|
|
|
if acc == nil || w.Accounts[i].Default {
|
|
|
|
if w.Accounts[i].Contract != nil && vm.IsSignatureContract(w.Accounts[i].Contract.Script) {
|
|
|
|
acc = w.Accounts[i]
|
|
|
|
if w.Accounts[i].Default {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if acc != nil {
|
|
|
|
res = acc.Contract.ScriptHash()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|