rpc: allow to provide cosigners for NEP17-transfer-related commands
This commit is contained in:
parent
1d6ba389c9
commit
49c35dec20
4 changed files with 25 additions and 25 deletions
|
@ -477,7 +477,7 @@ func transferNEP17(ctx *cli.Context) error {
|
||||||
func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recipients []client.TransferTarget) error {
|
func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recipients []client.TransferTarget) error {
|
||||||
gas := flags.Fixed8FromContext(ctx, "gas")
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
||||||
|
|
||||||
tx, err := c.CreateNEP17MultiTransferTx(acc, int64(gas), recipients)
|
tx, err := c.CreateNEP17MultiTransferTx(acc, int64(gas), recipients, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -272,7 +272,7 @@ func claimGas(ctx *cli.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0, nil)
|
hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,19 +69,23 @@ func (c *Client) NEP17TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
|
||||||
// method of a given contract (token) to move specified amount of NEP17 assets
|
// method of a given contract (token) to move specified amount of NEP17 assets
|
||||||
// (in FixedN format using contract's number of decimals) to given account and
|
// (in FixedN format using contract's number of decimals) to given account and
|
||||||
// returns it. The returned transaction is not signed.
|
// returns it. The returned transaction is not signed.
|
||||||
func (c *Client) CreateNEP17TransferTx(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64, data interface{}) (*transaction.Transaction, error) {
|
func (c *Client) CreateNEP17TransferTx(acc *wallet.Account, to util.Uint160,
|
||||||
|
token util.Uint160, amount int64, gas int64, data interface{}, cosigners []SignerAccount) (*transaction.Transaction, error) {
|
||||||
return c.CreateNEP17MultiTransferTx(acc, gas, []TransferTarget{
|
return c.CreateNEP17MultiTransferTx(acc, gas, []TransferTarget{
|
||||||
{Token: token,
|
{Token: token,
|
||||||
Address: to,
|
Address: to,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
Data: data,
|
Data: data,
|
||||||
},
|
},
|
||||||
})
|
}, cosigners)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateNEP17MultiTransferTx creates an invocation transaction for performing NEP17 transfers
|
// CreateNEP17MultiTransferTx creates an invocation transaction for performing
|
||||||
// from a single sender to multiple recipients with the given data.
|
// NEP17 transfers from a single sender to multiple recipients with the given
|
||||||
func (c *Client) CreateNEP17MultiTransferTx(acc *wallet.Account, gas int64, recipients []TransferTarget) (*transaction.Transaction, error) {
|
// data and cosigners. Transaction's sender is included with the CalledByEntry
|
||||||
|
// scope by default.
|
||||||
|
func (c *Client) CreateNEP17MultiTransferTx(acc *wallet.Account, gas int64,
|
||||||
|
recipients []TransferTarget, cosigners []SignerAccount) (*transaction.Transaction, error) {
|
||||||
from, err := address.StringToUint160(acc.Address)
|
from, err := address.StringToUint160(acc.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("bad account address: %w", err)
|
return nil, fmt.Errorf("bad account address: %w", err)
|
||||||
|
@ -95,13 +99,13 @@ func (c *Client) CreateNEP17MultiTransferTx(acc *wallet.Account, gas int64, reci
|
||||||
if w.Err != nil {
|
if w.Err != nil {
|
||||||
return nil, fmt.Errorf("failed to create transfer script: %w", w.Err)
|
return nil, fmt.Errorf("failed to create transfer script: %w", w.Err)
|
||||||
}
|
}
|
||||||
return c.CreateTxFromScript(w.Bytes(), acc, -1, gas, []SignerAccount{{
|
return c.CreateTxFromScript(w.Bytes(), acc, -1, gas, append([]SignerAccount{{
|
||||||
Signer: transaction.Signer{
|
Signer: transaction.Signer{
|
||||||
Account: from,
|
Account: from,
|
||||||
Scopes: transaction.CalledByEntry,
|
Scopes: transaction.CalledByEntry,
|
||||||
},
|
},
|
||||||
Account: acc,
|
Account: acc,
|
||||||
}})
|
}}, cosigners...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTxFromScript creates transaction and properly sets cosigners and NetworkFee.
|
// CreateTxFromScript creates transaction and properly sets cosigners and NetworkFee.
|
||||||
|
@ -143,38 +147,34 @@ func (c *Client) CreateTxFromScript(script []byte, acc *wallet.Account, sysFee,
|
||||||
// TransferNEP17 creates an invocation transaction that invokes 'transfer' method
|
// TransferNEP17 creates an invocation transaction that invokes 'transfer' method
|
||||||
// on a given token to move specified amount of NEP17 assets (in FixedN format
|
// on a given token to move specified amount of NEP17 assets (in FixedN format
|
||||||
// using contract's number of decimals) to given account with data specified and
|
// using contract's number of decimals) to given account with data specified and
|
||||||
// sends it to the network returning just a hash of it.
|
// sends it to the network returning just a hash of it. Cosigners argument
|
||||||
func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64, data interface{}) (util.Uint256, error) {
|
// specifies a set of the transaction cosigners (may be nil or may include sender)
|
||||||
|
// with proper scope and accounts to cosign the transaction. If cosigning is
|
||||||
|
// impossible (e.g. due to locked cosigner's account) an error is returned.
|
||||||
|
func (c *Client) TransferNEP17(acc *wallet.Account, to util.Uint160, token util.Uint160,
|
||||||
|
amount int64, gas int64, data interface{}, cosigners []SignerAccount) (util.Uint256, error) {
|
||||||
if !c.initDone {
|
if !c.initDone {
|
||||||
return util.Uint256{}, errNetworkNotInitialized
|
return util.Uint256{}, errNetworkNotInitialized
|
||||||
}
|
}
|
||||||
|
|
||||||
tx, err := c.CreateNEP17TransferTx(acc, to, token, amount, gas, data)
|
tx, err := c.CreateNEP17TransferTx(acc, to, token, amount, gas, data, cosigners)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.Uint256{}, err
|
return util.Uint256{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := acc.SignTx(c.GetNetwork(), tx); err != nil {
|
return c.SignAndPushTx(tx, acc, cosigners)
|
||||||
return util.Uint256{}, fmt.Errorf("can't sign tx: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.SendRawTransaction(tx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients.
|
// MultiTransferNEP17 is similar to TransferNEP17, buf allows to have multiple recipients.
|
||||||
func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients []TransferTarget) (util.Uint256, error) {
|
func (c *Client) MultiTransferNEP17(acc *wallet.Account, gas int64, recipients []TransferTarget, cosigners []SignerAccount) (util.Uint256, error) {
|
||||||
if !c.initDone {
|
if !c.initDone {
|
||||||
return util.Uint256{}, errNetworkNotInitialized
|
return util.Uint256{}, errNetworkNotInitialized
|
||||||
}
|
}
|
||||||
|
|
||||||
tx, err := c.CreateNEP17MultiTransferTx(acc, gas, recipients)
|
tx, err := c.CreateNEP17MultiTransferTx(acc, gas, recipients, cosigners)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.Uint256{}, err
|
return util.Uint256{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := acc.SignTx(c.GetNetwork(), tx); err != nil {
|
return c.SignAndPushTx(tx, acc, cosigners)
|
||||||
return util.Uint256{}, fmt.Errorf("can't sign tx: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.SendRawTransaction(tx)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -707,7 +707,7 @@ func TestCreateNEP17TransferTx(t *testing.T) {
|
||||||
gasContractHash, err := c.GetNativeContractHash(nativenames.Gas)
|
gasContractHash, err := c.GetNativeContractHash(nativenames.Gas)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil)
|
tx, err := c.CreateNEP17TransferTx(acc, util.Uint160{}, gasContractHash, 1000, 0, nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, acc.SignTx(testchain.Network(), tx))
|
require.NoError(t, acc.SignTx(testchain.Network(), tx))
|
||||||
require.NoError(t, chain.VerifyTx(tx))
|
require.NoError(t, chain.VerifyTx(tx))
|
||||||
|
|
Loading…
Add table
Reference in a new issue