当前位置: 首页>>代码示例>>Golang>>正文


Golang btcjson.Cmd类代码示例

本文整理汇总了Golang中github.com/conformal/btcjson.Cmd的典型用法代码示例。如果您正苦于以下问题:Golang Cmd类的具体用法?Golang Cmd怎么用?Golang Cmd使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Cmd类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handleNotifyNewTXs

// handleNotifyNewTXs implements the notifynewtxs command extension for
// websocket connections.
func handleNotifyNewTXs(s *rpcServer, cmd btcjson.Cmd, wallet walletChan) error {
	id := cmd.Id()
	reply := &btcjson.Reply{Id: &id}

	notifyCmd, ok := cmd.(*btcws.NotifyNewTXsCmd)
	if !ok {
		return btcjson.ErrInternal
	}

	for _, addr := range notifyCmd.Addresses {
		addr, err := btcutil.DecodeAddr(addr)
		if err != nil {
			return fmt.Errorf("cannot decode address: %v", err)
		}

		// TODO(jrick) Notifing for non-P2PKH addresses is currently
		// unsuported.
		if _, ok := addr.(*btcutil.AddressPubKeyHash); !ok {
			return fmt.Errorf("address is not P2PKH: %v", addr.EncodeAddress())
		}

		s.ws.AddTxRequest(wallet, addr.EncodeAddress())
	}

	mreply, _ := json.Marshal(reply)
	wallet <- mreply
	return nil
}
开发者ID:Nevtep,项目名称:mastercoind,代码行数:30,代码来源:rpcwebsocket.go

示例2: sendCmd

// sendCmd sends the passed command to the associated server and returns a
// response channel on which the reply will be deliver at some point in the
// future.  It handles both websocket and HTTP POST mode depending on the
// configuration of the client.
func (c *Client) sendCmd(cmd btcjson.Cmd) chan *response {
	// Choose which marshal and send function to use depending on whether
	// the client running in HTTP POST mode or not.  When running in HTTP
	// POST mode, the command is issued via an HTTP client.  Otherwise,
	// the command is issued via the asynchronous websocket channels.
	responseChan := make(chan *response, 1)
	if c.config.HttpPostMode {
		c.marshalAndSendPost(cmd, responseChan)
		return responseChan
	}

	// Check whether the websocket connection has never been established,
	// in which case the handler goroutines are not running.
	select {
	case <-c.connEstablished:
	default:
		responseChan <- &response{err: ErrClientNotConnected}
		return responseChan
	}

	err := c.addRequest(cmd.Id().(uint64), &jsonRequest{
		cmd:          cmd,
		responseChan: responseChan,
	})
	if err != nil {
		responseChan <- &response{err: err}
		return responseChan
	}
	c.marshalAndSend(cmd, responseChan)
	return responseChan
}
开发者ID:awt,项目名称:btcrpcclient,代码行数:35,代码来源:infrastructure.go

示例3: standardCmdReply

// standardCmdReply checks that a parsed command is a standard
// Bitcoin JSON-RPC command and runs the proper handler to reply to the
// command.
func standardCmdReply(cmd btcjson.Cmd, s *rpcServer) (reply btcjson.Reply) {
	id := cmd.Id()
	reply.Id = &id

	handler, ok := rpcHandlers[cmd.Method()]
	if !ok {
		reply.Error = &btcjson.ErrMethodNotFound
		return reply
	}

	result, err := handler(s, cmd)
	if err != nil {
		jsonErr, ok := err.(btcjson.Error)
		if !ok {
			// In the case where we did not have a btcjson
			// error to begin with, make a new one to send,
			// but this really should not happen.
			jsonErr = btcjson.Error{
				Code:    btcjson.ErrInternal.Code,
				Message: err.Error(),
			}
		}
		reply.Error = &jsonErr
	} else {
		reply.Result = result
	}
	return reply
}
开发者ID:Nevtep,项目名称:mastercoind,代码行数:31,代码来源:rpcserver.go

示例4: respondToAnyCmd

// respondToAnyCmd checks that a parsed command is a standard or
// extension JSON-RPC command and runs the proper handler to reply to
// the command.  Any and all responses are sent to the wallet from
// this function.
func respondToAnyCmd(cmd btcjson.Cmd, s *rpcServer, wallet walletChan) {
	// Lookup the websocket extension for the command and if it doesn't
	// exist fallback to handling the command as a standard command.
	wsHandler, ok := wsHandlers[cmd.Method()]
	if !ok {
		reply := standardCmdReply(cmd, s)
		mreply, _ := json.Marshal(reply)
		wallet <- mreply
		return
	}

	// Call the appropriate handler which responds unless there was an
	// error in which case the error is marshalled and sent here.
	if err := wsHandler(s, cmd, wallet); err != nil {
		var reply btcjson.Reply
		jsonErr, ok := err.(btcjson.Error)
		if ok {
			reply.Error = &jsonErr
			mreply, _ := json.Marshal(reply)
			wallet <- mreply
			return
		}

		// In the case where we did not have a btcjson
		// error to begin with, make a new one to send,
		// but this really should not happen.
		jsonErr = btcjson.Error{
			Code:    btcjson.ErrInternal.Code,
			Message: err.Error(),
		}
		reply.Error = &jsonErr
		mreply, _ := json.Marshal(reply)
		wallet <- mreply
	}
}
开发者ID:Nevtep,项目名称:mastercoind,代码行数:39,代码来源:rpcwebsocket.go

示例5: marshalAndSendPost

// marshalAndSendPost marshals the passed command to JSON-RPC and sends it to
// the server by issuing an HTTP POST request and returns a response channel
// on which the reply will be delivered.  Typically a new connection is opened
// and closed for each command when using this method, however, the underlying
// HTTP client might coalesce multiple commands depending on several factors
// including the remote server configuration.
func (c *Client) marshalAndSendPost(cmd btcjson.Cmd, responseChan chan *futureResult) {
	marshalledJSON, err := json.Marshal(cmd)
	if err != nil {
		responseChan <- &futureResult{reply: nil, err: err}
		return
	}

	// Generate a request to the configured RPC server.
	protocol := "http"
	if !c.config.DisableTLS {
		protocol = "https"
	}
	url := protocol + "://" + c.config.Host
	req, err := http.NewRequest("POST", url, bytes.NewReader(marshalledJSON))
	if err != nil {
		responseChan <- &futureResult{reply: nil, err: err}
		return
	}
	req.Close = true
	req.Header.Set("Content-Type", "application/json")

	// Configure basic access authorization.
	req.SetBasicAuth(c.config.User, c.config.Pass)

	log.Tracef("Sending command [%s] with id %d", cmd.Method(), cmd.Id())
	c.sendPostRequest(req, cmd, responseChan)
}
开发者ID:GeertJohan,项目名称:btcrpcclient,代码行数:33,代码来源:infrastructure.go

示例6: handleBtcdConnectedNtfn

// handleBtcdConnectedNtfn handles btcwallet btcdconnected notifications,
// updating the GUI with info about whether btcd is connected to btcwallet
// or not.
func handleBtcdConnectedNtfn(n btcjson.Cmd) {
	bcn, ok := n.(*btcws.BtcdConnectedNtfn)
	if !ok {
		log.Printf("[ERR] %v handler: unexpected type", n.Method())
		return
	}

	updateChans.btcdConnected <- bcn.Connected
}
开发者ID:hsk81,项目名称:btcgui,代码行数:12,代码来源:updates.go

示例7: handleBlockDisconnectedNtfn

// handleBlockDisconnectedNtfn handles btcd/btcwallet blockdisconnected
// notifications resulting from blocks disconnected from the main chain.
//
// TODO(jrick): handle this by rolling back tx history and balances.
func handleBlockDisconnectedNtfn(n btcjson.Cmd) {
	bdn, ok := n.(*btcws.BlockDisconnectedNtfn)
	if !ok {
		log.Printf("[ERR] %v handler: unexpected type", n.Method())
		return
	}

	// TODO
	_ = bdn
}
开发者ID:hsk81,项目名称:btcgui,代码行数:14,代码来源:updates.go

示例8: marshalAndSend

// marshalAndSend marshals the passed command to JSON-RPC and sends it to the
// server.  It returns a response channel on which the reply will be delivered.
func (c *Client) marshalAndSend(cmd btcjson.Cmd, responseChan chan *futureResult) {
	marshalledJSON, err := json.Marshal(cmd)
	if err != nil {
		responseChan <- &futureResult{reply: nil, err: err}
		return
	}

	log.Tracef("Sending command [%s] with id %d", cmd.Method(), cmd.Id())
	c.sendMessage(marshalledJSON)
}
开发者ID:GeertJohan,项目名称:btcrpcclient,代码行数:12,代码来源:infrastructure.go

示例9: DeferToBtcd

// DeferToBtcd sends a marshaled JSON-RPC request to btcd and returns
// the reply.
func DeferToBtcd(cmd btcjson.Cmd) (interface{}, *btcjson.Error) {
	// Update cmd with a new ID so replies can be handled without frontend
	// IDs clashing with requests originating in btcwallet.  The original
	// request ID is always used in the frontend's response.
	cmd.SetId(<-NewJSONID)

	request := NewRPCRequest(cmd, nil)
	response := <-CurrentRPCConn().SendRequest(request)
	return response.Result, response.Err
}
开发者ID:hsk81,项目名称:btcwallet,代码行数:12,代码来源:cmdmgr.go

示例10: NtfnRescanProgress

// NtfnRescanProgress handles btcd rescanprogress notifications resulting
// from a partially completed rescan.
func NtfnRescanProgress(n btcjson.Cmd) error {
	cn, ok := n.(*btcws.RescanProgressNtfn)
	if !ok {
		return fmt.Errorf("%v handler: unexpected type", n.Method())
	}

	// Notify the rescan manager of the completed partial progress for
	// the current rescan.
	AcctMgr.rm.MarkProgress(cn.LastProcessed)

	return nil
}
开发者ID:GeertJohan,项目名称:btcwallet,代码行数:14,代码来源:ntfns.go

示例11: handleWalletLockStateNtfn

// handleWalletLockStateNtfn handles btcwallet walletlockstate notifications
// by updating the GUI with whether the wallet is locked or not, setting
// the sensitivity of various widgets for locking or unlocking the wallet.
func handleWalletLockStateNtfn(n btcjson.Cmd) {
	wlsn, ok := n.(*btcws.WalletLockStateNtfn)
	if !ok {
		log.Printf("[ERR] %v handler: unexpected type", n.Method())
		return
	}

	// TODO(jrick): do proper filtering and display all
	// account balances somewhere
	if wlsn.Account == "" {
		updateChans.lockState <- wlsn.Locked
	}
}
开发者ID:hsk81,项目名称:btcgui,代码行数:16,代码来源:updates.go

示例12: NtfnTxMined

// NtfnTxMined handles btcd notifications resulting from newly
// mined transactions that originated from this wallet.
func NtfnTxMined(n btcjson.Cmd, marshaled []byte) {
	tmn, ok := n.(*btcws.TxMinedNtfn)
	if !ok {
		log.Errorf("%v handler: unexpected type", n.Method())
		return
	}

	txid, err := btcwire.NewShaHashFromStr(tmn.TxID)
	if err != nil {
		log.Errorf("%v handler: invalid hash string", n.Method())
		return
	}
	blockhash, err := btcwire.NewShaHashFromStr(tmn.BlockHash)
	if err != nil {
		log.Errorf("%v handler: invalid block hash string", n.Method())
		return
	}

	err = accountstore.RecordMinedTx(txid, blockhash,
		tmn.BlockHeight, tmn.Index, tmn.BlockTime)
	if err != nil {
		log.Errorf("%v handler: %v", n.Method(), err)
		return
	}

	// Remove mined transaction from pool.
	UnminedTxs.Lock()
	delete(UnminedTxs.m, TXID(*txid))
	UnminedTxs.Unlock()
}
开发者ID:hsk81,项目名称:btcwallet,代码行数:32,代码来源:btcdrpc.go

示例13: handleNotifySpent

// handleNotifySpent implements the notifyspent command extension for
// websocket connections.
func handleNotifySpent(s *rpcServer, cmd btcjson.Cmd, wallet walletChan) error {
	id := cmd.Id()
	reply := &btcjson.Reply{Id: &id}

	notifyCmd, ok := cmd.(*btcws.NotifySpentCmd)
	if !ok {
		return btcjson.ErrInternal
	}

	s.ws.AddSpentRequest(wallet, notifyCmd.OutPoint)

	mreply, _ := json.Marshal(reply)
	wallet <- mreply
	return nil
}
开发者ID:Nevtep,项目名称:mastercoind,代码行数:17,代码来源:rpcwebsocket.go

示例14: handleGetCurrentNet

// handleGetCurrentNet implements the getcurrentnet command extension
// for websocket connections.
func handleGetCurrentNet(s *rpcServer, cmd btcjson.Cmd, wallet walletChan) error {
	id := cmd.Id()
	reply := &btcjson.Reply{Id: &id}

	var net btcwire.BitcoinNet
	if cfg.TestNet3 {
		net = btcwire.TestNet3
	} else {
		net = btcwire.MainNet
	}

	reply.Result = float64(net)
	mreply, _ := json.Marshal(reply)
	wallet <- mreply
	return nil
}
开发者ID:Nevtep,项目名称:mastercoind,代码行数:18,代码来源:rpcwebsocket.go

示例15: handleAccountBalanceNtfn

// handleAccountBalanceNtfn handles btcwallet accountbalance notifications by
// updating the GUI with either a new confirmed or unconfirmed balance.
func handleAccountBalanceNtfn(n btcjson.Cmd) {
	abn, ok := n.(*btcws.AccountBalanceNtfn)
	if !ok {
		log.Printf("[ERR] %v handler: unexpected type", n.Method())
		return
	}

	// TODO(jrick): do proper filtering and display all
	// account balances somewhere
	if abn.Account == "" {
		if abn.Confirmed {
			updateChans.balance <- abn.Balance
		} else {
			updateChans.unconfirmed <- abn.Balance
		}
	}

}
开发者ID:hsk81,项目名称:btcgui,代码行数:20,代码来源:updates.go


注:本文中的github.com/conformal/btcjson.Cmd类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。