2018-03-02 15:24:09 +00:00
|
|
|
package wallet
|
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
import (
|
2020-01-09 15:30:25 +00:00
|
|
|
"errors"
|
2020-02-20 10:28:35 +00:00
|
|
|
"fmt"
|
2020-01-09 15:30:25 +00:00
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2023-12-08 12:38:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-12-08 12:38:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2019-08-27 13:29:42 +00:00
|
|
|
)
|
2018-03-02 15:24:09 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Account represents a NEO account. It holds the private and the public key
|
2018-03-02 15:24:09 +00:00
|
|
|
// along with some metadata.
|
|
|
|
type Account struct {
|
|
|
|
// NEO private key.
|
2019-08-27 13:29:42 +00:00
|
|
|
privateKey *keys.PrivateKey
|
2018-03-02 15:24:09 +00:00
|
|
|
|
2022-09-01 16:04:47 +00:00
|
|
|
// Script hash corresponding to the Address.
|
|
|
|
scriptHash util.Uint160
|
|
|
|
|
2019-02-13 18:01:10 +00:00
|
|
|
// NEO public address.
|
2018-03-02 15:24:09 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
|
|
|
|
// Encrypted WIF of the account also known as the key.
|
|
|
|
EncryptedWIF string `json:"key"`
|
|
|
|
|
|
|
|
// Label is a label the user had made for this account.
|
|
|
|
Label string `json:"label"`
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Contract is a Contract object which describes the details of the contract.
|
2018-03-02 15:24:09 +00:00
|
|
|
// This field can be null (for watch-only address).
|
|
|
|
Contract *Contract `json:"contract"`
|
|
|
|
|
|
|
|
// Indicates whether the account is locked by the user.
|
|
|
|
// the client shouldn't spend the funds in a locked account.
|
|
|
|
Locked bool `json:"lock"`
|
|
|
|
|
|
|
|
// Indicates whether the account is the default change account.
|
2021-05-13 15:19:48 +00:00
|
|
|
Default bool `json:"isDefault"`
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 18:01:10 +00:00
|
|
|
// Contract represents a subset of the smartcontract to embed in the
|
2018-03-02 15:24:09 +00:00
|
|
|
// Account so it's NEP-6 compliant.
|
|
|
|
type Contract struct {
|
2020-01-15 13:36:57 +00:00
|
|
|
// Script of the contract deployed on the blockchain.
|
|
|
|
Script []byte `json:"script"`
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
// A list of parameters used deploying this contract.
|
2020-03-04 10:58:33 +00:00
|
|
|
Parameters []ContractParam `json:"parameters"`
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
// Indicates whether the contract has been deployed to the blockchain.
|
|
|
|
Deployed bool `json:"deployed"`
|
2023-12-08 12:38:46 +00:00
|
|
|
|
|
|
|
// InvocationBuilder returns invocation script for deployed contracts.
|
|
|
|
// In case contract is not deployed or has 0 arguments, this field is ignored.
|
|
|
|
// It might be executed on a partially formed tx, and is primarily needed to properly
|
|
|
|
// calculate network fee for complex contract signers.
|
|
|
|
InvocationBuilder func(tx *transaction.Transaction) ([]byte, error) `json:"-"`
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 10:58:33 +00:00
|
|
|
// ContractParam is a descriptor of a contract parameter
|
|
|
|
// containing type and optional name.
|
|
|
|
type ContractParam struct {
|
2020-02-20 10:28:35 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type smartcontract.ParamType `json:"type"`
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:08:25 +00:00
|
|
|
// ScriptHash returns the hash of contract's script.
|
|
|
|
func (c Contract) ScriptHash() util.Uint160 {
|
|
|
|
return hash.Hash160(c.Script)
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
// NewAccount creates a new Account with a random generated PrivateKey.
|
|
|
|
func NewAccount() (*Account, error) {
|
2019-08-27 13:29:42 +00:00
|
|
|
priv, err := keys.NewPrivateKey()
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-04 09:40:22 +00:00
|
|
|
return NewAccountFromPrivateKey(priv), nil
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 12:38:46 +00:00
|
|
|
// NewContractAccount creates a contract account belonging to some deployed contract.
|
|
|
|
// SignTx can be called on this account with no error and will create invocation script,
|
|
|
|
// which puts provided arguments on stack for use in `verify`.
|
|
|
|
func NewContractAccount(hash util.Uint160, args ...any) *Account {
|
|
|
|
return &Account{
|
|
|
|
Address: address.Uint160ToString(hash),
|
|
|
|
Contract: &Contract{
|
|
|
|
Parameters: make([]ContractParam, len(args)),
|
|
|
|
Deployed: true,
|
|
|
|
InvocationBuilder: func(tx *transaction.Transaction) ([]byte, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for i := range args {
|
|
|
|
emit.Any(w.BinWriter, args[i])
|
|
|
|
}
|
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:01:07 +00:00
|
|
|
// SignTx signs transaction t and updates it's Witnesses.
|
2021-03-25 16:18:01 +00:00
|
|
|
func (a *Account) SignTx(net netmode.Magic, t *transaction.Transaction) error {
|
2022-08-24 14:09:57 +00:00
|
|
|
var (
|
|
|
|
haveAcc bool
|
|
|
|
pos int
|
|
|
|
)
|
2022-08-24 14:40:14 +00:00
|
|
|
if a.Locked {
|
|
|
|
return errors.New("account is locked")
|
|
|
|
}
|
2022-08-24 12:36:52 +00:00
|
|
|
if a.Contract == nil {
|
|
|
|
return errors.New("account has no contract")
|
|
|
|
}
|
2022-08-24 14:09:57 +00:00
|
|
|
for i := range t.Signers {
|
2022-09-01 16:04:47 +00:00
|
|
|
if t.Signers[i].Account.Equals(a.ScriptHash()) {
|
2022-08-24 14:09:57 +00:00
|
|
|
haveAcc = true
|
|
|
|
pos = i
|
|
|
|
break
|
2022-08-06 14:45:46 +00:00
|
|
|
}
|
2022-08-24 14:09:57 +00:00
|
|
|
}
|
|
|
|
if !haveAcc {
|
|
|
|
return errors.New("transaction is not signed by this account")
|
|
|
|
}
|
|
|
|
if len(t.Scripts) < pos {
|
|
|
|
return errors.New("transaction is not yet signed by the previous signer")
|
|
|
|
}
|
|
|
|
if len(t.Scripts) == pos {
|
|
|
|
t.Scripts = append(t.Scripts, transaction.Witness{
|
|
|
|
VerificationScript: a.Contract.Script, // Can be nil for deployed contract.
|
|
|
|
})
|
|
|
|
}
|
2023-12-08 12:38:46 +00:00
|
|
|
if a.Contract.Deployed && a.Contract.InvocationBuilder != nil {
|
|
|
|
invoc, err := a.Contract.InvocationBuilder(t)
|
|
|
|
t.Scripts[pos].InvocationScript = invoc
|
|
|
|
return err
|
|
|
|
}
|
2022-08-24 14:09:57 +00:00
|
|
|
if len(a.Contract.Parameters) == 0 {
|
2020-09-02 09:20:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-20 08:21:43 +00:00
|
|
|
if a.privateKey == nil {
|
2022-08-24 14:40:14 +00:00
|
|
|
return errors.New("account key is not available (need to decrypt?)")
|
2021-04-20 08:21:43 +00:00
|
|
|
}
|
2021-03-25 16:18:01 +00:00
|
|
|
sign := a.privateKey.SignHashable(uint32(net), t)
|
2020-02-28 16:01:07 +00:00
|
|
|
|
2022-10-05 07:45:52 +00:00
|
|
|
invoc := append([]byte{byte(opcode.PUSHDATA1), keys.SignatureLen}, sign...)
|
2022-08-24 14:09:57 +00:00
|
|
|
if len(a.Contract.Parameters) == 1 {
|
|
|
|
t.Scripts[pos].InvocationScript = invoc
|
|
|
|
} else {
|
|
|
|
t.Scripts[pos].InvocationScript = append(t.Scripts[pos].InvocationScript, invoc...)
|
2020-08-17 08:49:43 +00:00
|
|
|
}
|
2020-02-28 16:01:07 +00:00
|
|
|
|
|
|
|
return nil
|
2022-08-24 15:50:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:42:42 +00:00
|
|
|
// SignHashable signs the given Hashable item and returns the signature. If this
|
|
|
|
// account can't sign (CanSign() returns false) nil is returned.
|
|
|
|
func (a *Account) SignHashable(net netmode.Magic, item hash.Hashable) []byte {
|
|
|
|
if !a.CanSign() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a.privateKey.SignHashable(uint32(net), item)
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:50:31 +00:00
|
|
|
// CanSign returns true when account is not locked and has a decrypted private
|
|
|
|
// key inside, so it's ready to create real signatures.
|
|
|
|
func (a *Account) CanSign() bool {
|
|
|
|
return !a.Locked && a.privateKey != nil
|
2020-02-28 16:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:47:52 +00:00
|
|
|
// GetVerificationScript returns account's verification script.
|
|
|
|
func (a *Account) GetVerificationScript() []byte {
|
2020-02-28 16:01:07 +00:00
|
|
|
if a.Contract != nil {
|
|
|
|
return a.Contract.Script
|
|
|
|
}
|
2022-09-01 14:52:44 +00:00
|
|
|
return a.privateKey.PublicKey().GetVerificationScript()
|
2020-02-28 16:01:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 15:30:25 +00:00
|
|
|
// Decrypt decrypts the EncryptedWIF with the given passphrase returning error
|
2022-09-01 18:03:37 +00:00
|
|
|
// if anything goes wrong. After the decryption Account can be used to sign
|
|
|
|
// things unless it's locked. Don't decrypt the key unless you want to sign
|
|
|
|
// something and don't forget to call Close after use for maximum safety.
|
2021-06-04 11:27:22 +00:00
|
|
|
func (a *Account) Decrypt(passphrase string, scrypt keys.ScryptParams) error {
|
2020-01-09 15:30:25 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if a.EncryptedWIF == "" {
|
|
|
|
return errors.New("no encrypted wif in the account")
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
2021-06-04 11:27:22 +00:00
|
|
|
a.privateKey, err = keys.NEP2Decrypt(a.EncryptedWIF, passphrase, scrypt)
|
2020-02-20 10:06:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt encrypts the wallet's PrivateKey with the given passphrase
|
|
|
|
// under the NEP-2 standard.
|
2021-06-04 11:27:22 +00:00
|
|
|
func (a *Account) Encrypt(passphrase string, scrypt keys.ScryptParams) error {
|
|
|
|
wif, err := keys.NEP2Encrypt(a.privateKey, passphrase, scrypt)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.EncryptedWIF = wif
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-01 18:03:37 +00:00
|
|
|
// PrivateKey returns private key corresponding to the account if it's unlocked.
|
|
|
|
// Please be very careful when using it, do not copy its contents and do not
|
|
|
|
// keep a pointer to it unless you absolutely need to. Most of the time you can
|
|
|
|
// use other methods (PublicKey, ScriptHash, SignHashable) depending on your
|
|
|
|
// needs and it'll be safer this way.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (a *Account) PrivateKey() *keys.PrivateKey {
|
|
|
|
return a.privateKey
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:52:44 +00:00
|
|
|
// PublicKey returns the public key associated with the private key corresponding to
|
|
|
|
// the account. It can return nil if account is locked (use CanSign to check).
|
|
|
|
func (a *Account) PublicKey() *keys.PublicKey {
|
|
|
|
if !a.CanSign() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a.privateKey.PublicKey()
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:04:47 +00:00
|
|
|
// ScriptHash returns the script hash (account) that the Account.Address is
|
|
|
|
// derived from. It never returns an error, so if this Account has an invalid
|
|
|
|
// Address you'll just get a zero script hash.
|
|
|
|
func (a *Account) ScriptHash() util.Uint160 {
|
|
|
|
if a.scriptHash.Equals(util.Uint160{}) {
|
|
|
|
a.scriptHash, _ = address.StringToUint160(a.Address)
|
|
|
|
}
|
|
|
|
return a.scriptHash
|
|
|
|
}
|
|
|
|
|
2022-09-01 12:28:35 +00:00
|
|
|
// Close cleans up the private key used by Account and disassociates it from
|
|
|
|
// Account. The Account can no longer sign anything after this call, but Decrypt
|
|
|
|
// can make it usable again.
|
|
|
|
func (a *Account) Close() {
|
|
|
|
if a.privateKey == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.privateKey.Destroy()
|
|
|
|
a.privateKey = nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
// NewAccountFromWIF creates a new Account from the given WIF.
|
|
|
|
func NewAccountFromWIF(wif string) (*Account, error) {
|
2019-08-27 13:29:42 +00:00
|
|
|
privKey, err := keys.NewPrivateKeyFromWIF(wif)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-04 09:40:22 +00:00
|
|
|
return NewAccountFromPrivateKey(privKey), nil
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 09:22:40 +00:00
|
|
|
// NewAccountFromEncryptedWIF creates a new Account from the given encrypted WIF.
|
2021-06-04 11:27:22 +00:00
|
|
|
func NewAccountFromEncryptedWIF(wif string, pass string, scrypt keys.ScryptParams) (*Account, error) {
|
|
|
|
priv, err := keys.NEP2Decrypt(wif, pass, scrypt)
|
2020-02-20 09:22:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:40:22 +00:00
|
|
|
a := NewAccountFromPrivateKey(priv)
|
2020-02-20 09:22:40 +00:00
|
|
|
a.EncryptedWIF = wif
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:47:36 +00:00
|
|
|
// ConvertMultisig sets a's contract to multisig contract with m sufficient signatures.
|
|
|
|
func (a *Account) ConvertMultisig(m int, pubs []*keys.PublicKey) error {
|
2022-09-01 13:39:48 +00:00
|
|
|
if a.Locked {
|
|
|
|
return errors.New("account is locked")
|
|
|
|
}
|
2022-09-01 13:44:50 +00:00
|
|
|
if a.privateKey == nil {
|
|
|
|
return errors.New("account key is not available (need to decrypt?)")
|
|
|
|
}
|
2020-02-20 12:47:36 +00:00
|
|
|
var found bool
|
2022-09-01 13:44:50 +00:00
|
|
|
accKey := a.privateKey.PublicKey()
|
2020-02-20 12:47:36 +00:00
|
|
|
for i := range pubs {
|
2022-09-01 13:44:50 +00:00
|
|
|
if accKey.Equal(pubs[i]) {
|
2020-02-20 12:47:36 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return errors.New("own public key was not found among multisig keys")
|
|
|
|
}
|
|
|
|
|
|
|
|
script, err := smartcontract.CreateMultiSigRedeemScript(m, pubs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:04:47 +00:00
|
|
|
a.scriptHash = hash.Hash160(script)
|
|
|
|
a.Address = address.Uint160ToString(a.scriptHash)
|
2020-02-20 12:47:36 +00:00
|
|
|
a.Contract = &Contract{
|
|
|
|
Script: script,
|
|
|
|
Parameters: getContractParams(m),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:40:22 +00:00
|
|
|
// NewAccountFromPrivateKey creates a wallet from the given PrivateKey.
|
|
|
|
func NewAccountFromPrivateKey(p *keys.PrivateKey) *Account {
|
2019-09-05 06:35:02 +00:00
|
|
|
pubKey := p.PublicKey()
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
a := &Account{
|
|
|
|
privateKey: p,
|
2022-09-01 16:04:47 +00:00
|
|
|
scriptHash: p.GetScriptHash(),
|
|
|
|
Address: p.Address(),
|
2020-02-20 10:28:35 +00:00
|
|
|
Contract: &Contract{
|
|
|
|
Script: pubKey.GetVerificationScript(),
|
|
|
|
Parameters: getContractParams(1),
|
|
|
|
},
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:35:02 +00:00
|
|
|
return a
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
2020-02-20 10:28:35 +00:00
|
|
|
|
2020-03-04 10:58:33 +00:00
|
|
|
func getContractParams(n int) []ContractParam {
|
|
|
|
params := make([]ContractParam, n)
|
2020-02-20 10:28:35 +00:00
|
|
|
for i := range params {
|
|
|
|
params[i].Name = fmt.Sprintf("parameter%d", i)
|
|
|
|
params[i].Type = smartcontract.SignatureType
|
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
|
|
|
}
|