diff --git a/.gitignore b/.gitignore index 72fb9416ea..b886ac349f 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ _cgo_export.* _testmain.go *.exe +/.idea \ No newline at end of file diff --git a/btcjson/walletsvrcmds.go b/btcjson/walletsvrcmds.go index 6697555172..8d9eaeba87 100644 --- a/btcjson/walletsvrcmds.go +++ b/btcjson/walletsvrcmds.go @@ -526,6 +526,36 @@ type SendToAddressCmd struct { CommentTo *string } +// NewTransferTransactionCmd returns a new instance which can be used to issue a +// transfertransaction JSON-RPC command. +func NewTransferTransactionCmd(address string, txId string) *TransferTransactionCmd { + return &TransferTransactionCmd{ + Address: address, + TxId: txId, + } +} + +// TransferTransactionCmd defines the transfertransaction JSON-RPC command. +type TransferTransactionCmd struct { + Address string + TxId string +} + +// TODO : Write summary +func NewSendPostDatedTxCmd(address string, amount int64, lockTime uint32) *SendPostDatedTxCmd { + return &SendPostDatedTxCmd{ + Address: address, + Amount: amount, + LockTime: lockTime, + } +} + +type SendPostDatedTxCmd struct { + Address string + Amount int64 + LockTime uint32 +} + // NewSendToAddressCmd returns a new instance which can be used to issue a // sendtoaddress JSON-RPC command. // @@ -688,6 +718,7 @@ func init() { MustRegisterCmd("move", (*MoveCmd)(nil), flags) MustRegisterCmd("sendfrom", (*SendFromCmd)(nil), flags) MustRegisterCmd("sendmany", (*SendManyCmd)(nil), flags) + MustRegisterCmd("transfertransaction", (*TransferTransactionCmd)(nil), flags) MustRegisterCmd("sendtoaddress", (*SendToAddressCmd)(nil), flags) MustRegisterCmd("setaccount", (*SetAccountCmd)(nil), flags) MustRegisterCmd("settxfee", (*SetTxFeeCmd)(nil), flags) diff --git a/btcjson/walletsvrcmds_test.go b/btcjson/walletsvrcmds_test.go index efc08cc945..b4cdb9d3f4 100644 --- a/btcjson/walletsvrcmds_test.go +++ b/btcjson/walletsvrcmds_test.go @@ -1006,6 +1006,20 @@ func TestWalletSvrCmds(t *testing.T) { Comment: btcjson.String("comment"), }, }, + { + name: "transfertransaction", + newCmd: func() (interface{}, error) { + return btcjson.NewCmd("transfertransaction", "1Address", "0000-0000") + }, + staticCmd: func() interface{} { + return btcjson.NewTransferTransactionCmd("1Address", "0000-0000") + }, + marshalled: `{"jsonrpc":"1.0","method":"transfertransaction","params":["1Address","0000-0000"],"id":1}`, + unmarshalled: &btcjson.TransferTransactionCmd{ + Address: "1Address", + TxId: "0000-0000", + }, + }, { name: "sendtoaddress", newCmd: func() (interface{}, error) { diff --git a/rpcclient/wallet.go b/rpcclient/wallet.go index 27546f5de0..5d887701c6 100644 --- a/rpcclient/wallet.go +++ b/rpcclient/wallet.go @@ -442,6 +442,60 @@ func (c *Client) SetTxFee(fee btcutil.Amount) error { return c.SetTxFeeAsync(fee).Receive() } +// TransferTransaction +// +// FutureTransferTransactionResult is a future promise to deliver the result of a +// TransferTransactionAsync RPC invocation (or an applicable error). +type FutureTransferTransactionResult chan *response + +// Receive waits for the response promised by the future and returns the hash +// of the transaction transferring the transaction of the passed id to the given address. +func (r FutureTransferTransactionResult) Receive() (*chainhash.Hash, error) { + res, err := receiveFuture(r) + if err != nil { + return nil, err + } + + // Unmarshal result as a string. + var txHash string + err = json.Unmarshal(res, &txHash) + if err != nil { + return nil, err + } + + return chainhash.NewHashFromStr(txHash) +} + +// TransferTransactionAsync returns an instance of a type that can be used to get the +// result of the RPC at some future time by invoking the Receive function on the +// returned instance. +// +// See TransferTransaction for the blocking version and more details. +func (c *Client) TransferTransactionAsync(address btcutil.Address, txId string) FutureTransferTransactionResult { + addr := address.EncodeAddress() + cmd := btcjson.NewTransferTransactionCmd(addr, txId) + return c.sendCmd(cmd) +} + +// TransferTransaction transfers the transaction of the passed id to the given address. +// +// NOTE: This function requires to the wallet to be unlocked. See the +// WalletPassphrase function for more details. +func (c *Client) TransferTransaction(address btcutil.Address, txId string) (*chainhash.Hash, error) { + return c.TransferTransactionAsync(address, txId).Receive() +} + +// TODO : Write summary +func (c *Client) SendPostDatedTransactionAsync(address btcutil.Address, amount int64, lockTime uint32) FutureTransferTransactionResult { + addr := address.EncodeAddress() + cmd := btcjson.NewSendPostDatedTxCmd(addr, amount, lockTime) + return c.sendCmd(cmd) +} + +func (c *Client) SendPostDatedTransaction(address btcutil.Address, amount int64, lockTime uint32) (*chainhash.Hash, error) { + return c.SendPostDatedTransactionAsync(address, amount, lockTime).Receive() +} + // FutureSendToAddressResult is a future promise to deliver the result of a // SendToAddressAsync RPC invocation (or an applicable error). type FutureSendToAddressResult chan *response diff --git a/rpcserver.go b/rpcserver.go index 6ade888d38..e688402bcc 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -214,6 +214,7 @@ var rpcAskWallet = map[string]struct{}{ "sendfrom": {}, "sendmany": {}, "sendtoaddress": {}, + "transfertransaction": {}, "setaccount": {}, "settxfee": {}, "signmessage": {}, @@ -4281,7 +4282,7 @@ func newRPCServer(config *rpcserverConfig) (*rpcServer, error) { gbtWorkState: newGbtWorkState(config.TimeSource), helpCacher: newHelpCacher(), requestProcessShutdown: make(chan struct{}), - quit: make(chan int), + quit: make(chan int), } if cfg.RPCUser != "" && cfg.RPCPass != "" { login := cfg.RPCUser + ":" + cfg.RPCPass