Merge btcws repo into btcjson/btcws directory.

This commit is contained in:
Dave Collins 2015-02-19 12:53:23 -06:00
commit 66f128651d
5 changed files with 3655 additions and 0 deletions

88
btcjson/btcws/README.md Normal file
View file

@ -0,0 +1,88 @@
btcws
=====
[![Build Status](https://travis-ci.org/btcsuite/btcd.png?branch=master)]
(https://travis-ci.org/btcsuite/btcws)
Package btcws implements extensions to the standard bitcoind JSON-RPC
API for the btcd suite of programs (btcd, btcwallet, and btcgui).
Importing this package registers all implemented custom requests with
btcjson (using btcjson.RegisterCustomCmd).
## Sample Use
```Go
// Client Side
import (
"golang.org/x/net/websocket"
"github.com/btcsuite/btcd/btcjson/btcws"
)
// Create rescan command.
id := 0
addrs := map[string]struct{}{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH": struct{},
}
cmd, err := btcws.NewRescanCmd(id, 270000, addrs)
// Set up a handler for a reply with id 0.
AddReplyHandler(id, func(reply map[string]interface{}) {
// Deal with reply.
})
// JSON marshal and send rescan request to websocket connection.
websocket.JSON.Send(btcdWSConn, cmd)
// Server Side
import (
"golang.org/x/net/websocket"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcjson/btcws"
)
// Get marshaled request.
var b []byte
err := websocket.Message.Receive(clientWSConn, &b)
// Parse marshaled command.
cmd, err := btcjson.ParseMarshaledCmd(b)
// If this is a rescan command, handle and reply.
rcmd, ok := cmd.(*btcws.RescanCmd)
if ok {
// Do stuff
var reply []byte
err := websocket.Message.Send(clientWSConn, reply)
}
```
## Installation
```bash
$ go get github.com/btcsuite/btcd/btcjson/btcws
```
## GPG Verification Key
All official release tags are signed by Conformal so users can ensure the code
has not been tampered with and is coming from Conformal. To verify the
signature perform the following:
- Download the public key from the Conformal website at
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
- Import the public key into your GPG keyring:
```bash
gpg --import GIT-GPG-KEY-conformal.txt
```
- Verify the release tag with the following command where `TAG_NAME` is a
placeholder for the specific tag:
```bash
git tag -v TAG_NAME
```
## License
Package btcws is licensed under the liberal ISC License.

1664
btcjson/btcws/cmds.go Normal file

File diff suppressed because it is too large Load diff

364
btcjson/btcws/cmds_test.go Normal file
View file

@ -0,0 +1,364 @@
// Copyright (c) 2013 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// this has to be in the real package so we can mock up structs
package btcws
import (
"reflect"
"testing"
"github.com/btcsuite/btcd/btcjson"
"github.com/davecgh/go-spew/spew"
)
var testAccount = "account"
var cmdtests = []struct {
name string
f func() (btcjson.Cmd, error)
result btcjson.Cmd // after marshal and unmarshal
}{
{
name: "createencryptedwallet",
f: func() (btcjson.Cmd, error) {
return NewCreateEncryptedWalletCmd(
float64(1),
"banana"), nil
},
result: &CreateEncryptedWalletCmd{
id: float64(1),
Passphrase: "banana",
},
},
{
name: "createnewaccount",
f: func() (btcjson.Cmd, error) {
return NewCreateNewAccountCmd(
float64(1),
"account"), nil
},
result: &CreateNewAccountCmd{
id: float64(1),
Account: "account",
},
},
{
name: "getbestblock",
f: func() (btcjson.Cmd, error) {
return NewGetBestBlockCmd(float64(1)), nil
},
result: &GetBestBlockCmd{
id: float64(1),
},
},
{
name: "getcurrentnet",
f: func() (btcjson.Cmd, error) {
return NewGetCurrentNetCmd(float64(1)), nil
},
result: &GetCurrentNetCmd{
id: float64(1),
},
},
{
name: "getunconfirmedbalance no optargs",
f: func() (btcjson.Cmd, error) {
return NewGetUnconfirmedBalanceCmd(float64(1))
},
result: &GetUnconfirmedBalanceCmd{
id: float64(1),
Account: "",
},
},
{
name: "getunconfirmedbalance one optarg",
f: func() (btcjson.Cmd, error) {
return NewGetUnconfirmedBalanceCmd(float64(1),
testAccount)
},
result: &GetUnconfirmedBalanceCmd{
id: float64(1),
Account: testAccount,
},
},
{
name: "listaddresstransactions no optargs",
f: func() (btcjson.Cmd, error) {
addrs := []string{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
}
return NewListAddressTransactionsCmd(
float64(1),
addrs)
},
result: &ListAddressTransactionsCmd{
id: float64(1),
Account: "",
Addresses: []string{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
},
},
},
{
name: "listaddresstransactions one optarg",
f: func() (btcjson.Cmd, error) {
addrs := []string{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
}
return NewListAddressTransactionsCmd(
float64(1),
addrs,
testAccount)
},
result: &ListAddressTransactionsCmd{
id: float64(1),
Account: testAccount,
Addresses: []string{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
},
},
},
{
name: "listalltransactions no optargs",
f: func() (btcjson.Cmd, error) {
return NewListAllTransactionsCmd(float64(1))
},
result: &ListAllTransactionsCmd{
id: float64(1),
Account: nil,
},
},
{
name: "listalltransactions one optarg",
f: func() (btcjson.Cmd, error) {
return NewListAllTransactionsCmd(
float64(1),
testAccount)
},
result: &ListAllTransactionsCmd{
id: float64(1),
Account: &testAccount,
},
},
{
name: "notifyreceived",
f: func() (btcjson.Cmd, error) {
addrs := []string{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
}
return NewNotifyReceivedCmd(
float64(1),
addrs), nil
},
result: &NotifyReceivedCmd{
id: float64(1),
Addresses: []string{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
},
},
},
{
name: "notifynewtransactions",
f: func() (btcjson.Cmd, error) {
return NewNotifyNewTransactionsCmd(
float64(1),
true)
},
result: &NotifyNewTransactionsCmd{
id: float64(1),
Verbose: true,
},
},
{
name: "notifyspent",
f: func() (btcjson.Cmd, error) {
ops := []OutPoint{
{
Hash: "000102030405060708091011121314" +
"1516171819202122232425262728" +
"293031",
Index: 1,
},
}
return NewNotifySpentCmd(float64(1), ops), nil
},
result: &NotifySpentCmd{
id: float64(1),
OutPoints: []OutPoint{
{
Hash: "000102030405060708091011121314" +
"1516171819202122232425262728" +
"293031",
Index: 1,
},
},
},
},
{
name: "renameaccount",
f: func() (btcjson.Cmd, error) {
return NewRenameAccountCmd(
float64(1),
"old",
"new"), nil
},
result: &RenameAccountCmd{
id: float64(1),
OldAccount: "old",
NewAccount: "new",
},
},
{
name: "rescan no optargs",
f: func() (btcjson.Cmd, error) {
addrs := []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"}
ops := []OutPoint{
{
Hash: "000102030405060708091011121314" +
"1516171819202122232425262728" +
"293031",
Index: 1,
},
}
return NewRescanCmd(
float64(1),
"0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
addrs,
ops)
},
result: &RescanCmd{
id: float64(1),
BeginBlock: "0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
Addresses: []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"},
OutPoints: []OutPoint{
{
Hash: "000102030405060708091011121314" +
"1516171819202122232425262728" +
"293031",
Index: 1,
},
},
EndBlock: "",
},
},
{
name: "rescan one optarg",
f: func() (btcjson.Cmd, error) {
addrs := []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"}
ops := []OutPoint{
{
Hash: "000102030405060708091011121314" +
"1516171819202122232425262728" +
"293031",
Index: 1,
},
}
return NewRescanCmd(
float64(1),
"0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
addrs,
ops,
"0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03")
},
result: &RescanCmd{
id: float64(1),
BeginBlock: "0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
Addresses: []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"},
OutPoints: []OutPoint{
{
Hash: "000102030405060708091011121314" +
"1516171819202122232425262728" +
"293031",
Index: 1,
},
},
EndBlock: "0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03",
},
},
{
name: "walletislocked no optargs",
f: func() (btcjson.Cmd, error) {
return NewWalletIsLockedCmd(float64(1))
},
result: &WalletIsLockedCmd{
id: float64(1),
Account: "",
},
},
{
name: "walletislocked one optarg",
f: func() (btcjson.Cmd, error) {
return NewWalletIsLockedCmd(
float64(1),
testAccount)
},
result: &WalletIsLockedCmd{
id: float64(1),
Account: testAccount,
},
},
}
func TestCmds(t *testing.T) {
for _, test := range cmdtests {
c, err := test.f()
if err != nil {
t.Errorf("%s: failed to run func: %v",
test.name, err)
continue
}
mc, err := c.MarshalJSON()
if err != nil {
t.Errorf("%s: failed to marshal cmd: %v",
test.name, err)
continue
}
c2, err := btcjson.ParseMarshaledCmd(mc)
if err != nil {
t.Errorf("%s: failed to ummarshal cmd: %v",
test.name, err)
continue
}
if !reflect.DeepEqual(test.result, c2) {
t.Errorf("%s: unmarshal not as expected. "+
"got %v wanted %v", test.name, spew.Sdump(c2),
spew.Sdump(test.result))
}
if !reflect.DeepEqual(c, c2) {
t.Errorf("%s: unmarshal not as we started with. "+
"got %v wanted %v", test.name, spew.Sdump(c2),
spew.Sdump(c))
}
// id from Id func must match result.
if c.Id() != test.result.Id() {
t.Errorf("%s: Id returned incorrect id. "+
"got %v wanted %v", test.name, c.Id(),
test.result.Id())
}
// method from Method func must match result.
if c.Method() != test.result.Method() {
t.Errorf("%s: Method returned incorrect method. "+
"got %v wanted %v", test.name, c.Method(),
test.result.Method())
}
// Read marshaled command back into c. Should still
// match result.
if err := c.UnmarshalJSON(mc); err != nil {
t.Errorf("%s: error while unmarshalling: %v", test.name,
err)
}
if !reflect.DeepEqual(test.result, c) {
t.Errorf("%s: unmarshal not as expected. "+
"got %v wanted %v", test.name, spew.Sdump(c),
spew.Sdump(test.result))
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,290 @@
// Copyright (c) 2013 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcws_test
import (
"reflect"
"testing"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcjson/btcws"
"github.com/davecgh/go-spew/spew"
)
var ntfntests = []struct {
name string
f func() btcjson.Cmd
result btcjson.Cmd // after marshal and unmarshal
}{
{
name: "accountbalance",
f: func() btcjson.Cmd {
return btcws.NewAccountBalanceNtfn("abcde", 1.2345, true)
},
result: &btcws.AccountBalanceNtfn{
Account: "abcde",
Balance: 1.2345,
Confirmed: true,
},
},
{
name: "blockconnected",
f: func() btcjson.Cmd {
return btcws.NewBlockConnectedNtfn(
"000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
153469)
},
result: &btcws.BlockConnectedNtfn{
Hash: "000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
Height: 153469,
},
},
{
name: "blockdisconnected",
f: func() btcjson.Cmd {
return btcws.NewBlockDisconnectedNtfn(
"000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
153469)
},
result: &btcws.BlockDisconnectedNtfn{
Hash: "000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
Height: 153469,
},
},
{
name: "btcdconnected",
f: func() btcjson.Cmd {
return btcws.NewBtcdConnectedNtfn(true)
},
result: &btcws.BtcdConnectedNtfn{
Connected: true,
},
},
{
name: "recvtx no block",
f: func() btcjson.Cmd {
return btcws.NewRecvTxNtfn("lalala the hex tx", nil)
},
result: &btcws.RecvTxNtfn{
HexTx: "lalala the hex tx",
Block: nil,
},
},
{
name: "recvtx with block",
f: func() btcjson.Cmd {
block := &btcws.BlockDetails{
Height: 153469,
Hash: "000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
Index: 1,
Time: 1386944019,
}
return btcws.NewRecvTxNtfn("lalala the hex tx", block)
},
result: &btcws.RecvTxNtfn{
HexTx: "lalala the hex tx",
Block: &btcws.BlockDetails{
Height: 153469,
Hash: "000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
Index: 1,
Time: 1386944019,
},
},
},
{
name: "redeemingtx",
f: func() btcjson.Cmd {
return btcws.NewRedeemingTxNtfn("lalala the hex tx", nil)
},
result: &btcws.RedeemingTxNtfn{
HexTx: "lalala the hex tx",
Block: nil,
},
},
{
name: "redeemingtx with block",
f: func() btcjson.Cmd {
block := &btcws.BlockDetails{
Height: 153469,
Hash: "000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
Index: 1,
Time: 1386944019,
}
return btcws.NewRedeemingTxNtfn("lalala the hex tx", block)
},
result: &btcws.RedeemingTxNtfn{
HexTx: "lalala the hex tx",
Block: &btcws.BlockDetails{
Height: 153469,
Hash: "000000004811dda1c320ad5d0ea184a20a53acd92292c5f1cb926c3ee82abf70",
Index: 1,
Time: 1386944019,
},
},
},
{
name: "rescanfinished",
f: func() btcjson.Cmd {
return btcws.NewRescanFinishedNtfn(
"00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
12345, 1240784732)
},
result: &btcws.RescanFinishedNtfn{
Hash: "00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
Height: 12345,
Time: 1240784732,
},
},
{
name: "rescanprogress",
f: func() btcjson.Cmd {
return btcws.NewRescanProgressNtfn(
"00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
12345, 1240784732)
},
result: &btcws.RescanProgressNtfn{
Hash: "00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
Height: 12345,
Time: 1240784732,
},
},
{
name: "newtx",
f: func() btcjson.Cmd {
details := &btcjson.ListTransactionsResult{
Account: "original",
Address: "mnSsMBY8j4AhQzbR6XqawND7NPTECVdtLd",
Category: "receive",
Amount: 100,
Fee: 0.0,
Confirmations: 6707,
Generated: false,
BlockHash: "000000000b20bf5fe8e25b19f9ec340744cda321a17ade12af9838530a75098b",
BlockIndex: 2,
BlockTime: 1397079345,
TxID: "cb082a63b29f446551829d03fa8bac02d3825a18994d5feec564f14101fc5fad",
WalletConflicts: []string{},
Time: 123123123,
TimeReceived: 1397079169,
Comment: "comment",
OtherAccount: "",
}
return btcws.NewTxNtfn("abcde", details)
},
result: &btcws.TxNtfn{
Account: "abcde",
Details: &btcjson.ListTransactionsResult{
Account: "original",
Address: "mnSsMBY8j4AhQzbR6XqawND7NPTECVdtLd",
Category: "receive",
Amount: 100,
Fee: 0.0,
Confirmations: 6707,
Generated: false,
BlockHash: "000000000b20bf5fe8e25b19f9ec340744cda321a17ade12af9838530a75098b",
BlockIndex: 2,
BlockTime: 1397079345,
TxID: "cb082a63b29f446551829d03fa8bac02d3825a18994d5feec564f14101fc5fad",
WalletConflicts: []string{},
Time: 123123123,
TimeReceived: 1397079169,
Comment: "comment",
OtherAccount: "",
},
},
},
{
name: "walletlockstate",
f: func() btcjson.Cmd {
return btcws.NewWalletLockStateNtfn("abcde", true)
},
result: &btcws.WalletLockStateNtfn{
Account: "abcde",
Locked: true,
},
},
{
name: "txaccepted",
f: func() btcjson.Cmd {
return btcws.NewTxAcceptedNtfn(
"062f2b5f7d28c787e0f3aee382132241cd590efb7b83bd2c7f506de5aa4ef275",
34567765)
},
result: &btcws.TxAcceptedNtfn{
TxID: "062f2b5f7d28c787e0f3aee382132241cd590efb7b83bd2c7f506de5aa4ef275",
Amount: 34567765,
},
},
{
name: "txacceptedverbose",
f: func() btcjson.Cmd {
return btcws.NewTxAcceptedVerboseNtfn(&btcjson.TxRawResult{
Hex: "01000000010cdf900074a3622499a2f28f44a94476f27a8900a2bdd60e042754b6cab09741000000008a473044022012e11012fad1eb21ba1c82deb8da98778b08e714b72f281293064528343fae0502204294d7520f469f9673087a55395de0ce0e9074dce236db9fe7f30013b5fd00b90141047b6ff7832b4a763666e5481a0bd9eedb656d9f882d215c16fe9563d7b191cd67b2a41601a853a9f9d92773ae6f912ef451a089148e510623759cf55c408efdefffffffff02f4063f00000000001976a914b269e0ceec5d5b5e192cf580ae42341e0f79b0b588aca8c84b02000000001976a91439233c0d43a1411e547c60bad8985bae3530b6af88ac00000000",
Txid: "0cfeb968fb5d0f6b9a2a1de37c0607a1964dd3e335f203377cec90e03b20869e",
Version: 0x1,
LockTime: 0x0,
})
},
result: &btcws.TxAcceptedVerboseNtfn{
RawTx: &btcjson.TxRawResult{
Hex: "01000000010cdf900074a3622499a2f28f44a94476f27a8900a2bdd60e042754b6cab09741000000008a473044022012e11012fad1eb21ba1c82deb8da98778b08e714b72f281293064528343fae0502204294d7520f469f9673087a55395de0ce0e9074dce236db9fe7f30013b5fd00b90141047b6ff7832b4a763666e5481a0bd9eedb656d9f882d215c16fe9563d7b191cd67b2a41601a853a9f9d92773ae6f912ef451a089148e510623759cf55c408efdefffffffff02f4063f00000000001976a914b269e0ceec5d5b5e192cf580ae42341e0f79b0b588aca8c84b02000000001976a91439233c0d43a1411e547c60bad8985bae3530b6af88ac00000000",
Txid: "0cfeb968fb5d0f6b9a2a1de37c0607a1964dd3e335f203377cec90e03b20869e",
Version: 0x1,
LockTime: 0x0,
},
},
},
}
func TestNtfns(t *testing.T) {
for _, test := range ntfntests {
// create notification.
n := test.f()
// verify that id is nil.
if n.Id() != nil {
t.Errorf("%s: notification should not have non-nil id %v",
test.name, n.Id())
continue
}
mn, err := n.MarshalJSON()
if err != nil {
t.Errorf("%s: failed to marshal notification: %v",
test.name, err)
continue
}
n2, err := btcjson.ParseMarshaledCmd(mn)
if err != nil {
t.Errorf("%s: failed to ummarshal cmd: %v",
test.name, err)
continue
}
if !reflect.DeepEqual(test.result, n2) {
t.Errorf("%s: unmarshal not as expected. "+
"got %v wanted %v", test.name, spew.Sdump(n2),
spew.Sdump(test.result))
}
if !reflect.DeepEqual(n, n2) {
t.Errorf("%s: unmarshal not as we started with. "+
"got %v wanted %v", test.name, spew.Sdump(n2),
spew.Sdump(n))
}
// Read marshaled notification back into n. Should still
// match result.
if err := n.UnmarshalJSON(mn); err != nil {
t.Errorf("%s: unmarshal failed: %v", test.name, err)
continue
}
if !reflect.DeepEqual(test.result, n) {
t.Errorf("%s: unmarshal not as expected. "+
"got %v wanted %v", test.name, spew.Sdump(n),
spew.Sdump(test.result))
}
}
}