Implement Bitcore socket.io method sendTransaction
parent
b2b9cc3f73
commit
395e69143d
|
@ -149,7 +149,13 @@ var onMessageHandlers = map[string]func(*SocketIoServer, json.RawMessage) (inter
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
// sendTransaction
|
"\"sendTransaction\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) {
|
||||||
|
tx, err := unmarshalSendTransaction(params)
|
||||||
|
if err == nil {
|
||||||
|
rv, err = s.sendTransaction(tx)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SocketIoServer) onMessage(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} {
|
func (s *SocketIoServer) onMessage(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} {
|
||||||
|
@ -602,6 +608,32 @@ func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetai
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unmarshalSendTransaction(params []byte) (tx string, err error) {
|
||||||
|
p, err := unmarshalArray(params, 1)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tx, ok := p[0].(string)
|
||||||
|
if ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = errors.New("incorrect parameter")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type resultSendTransaction struct {
|
||||||
|
Result string `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SocketIoServer) sendTransaction(tx string) (res resultSendTransaction, err error) {
|
||||||
|
txid, err := s.chain.SendRawTransaction(tx)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
res.Result = txid
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SocketIoServer) onSubscribe(c *gosocketio.Channel, req json.RawMessage) interface{} {
|
func (s *SocketIoServer) onSubscribe(c *gosocketio.Channel, req json.RawMessage) interface{} {
|
||||||
glog.Info(c.Id(), " onSubscribe ", req)
|
glog.Info(c.Id(), " onSubscribe ", req)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -163,6 +163,22 @@
|
||||||
return socket.send({ method, params }, f);
|
return socket.send({ method, params }, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendTransaction() {
|
||||||
|
var tx = document.getElementById('sendTransactionHex').value;
|
||||||
|
sendTransactionF(tx, function (result) {
|
||||||
|
console.log('sendTransaction sent successfully');
|
||||||
|
console.log(result);
|
||||||
|
document.getElementById('sendTransactionResult').innerText = JSON.stringify(result).replace(/,/g, ", ");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendTransactionF(hex, f) {
|
||||||
|
const method = 'sendTransaction';
|
||||||
|
const params = [
|
||||||
|
hex,
|
||||||
|
];
|
||||||
|
return socket.send({ method, params }, f);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
@ -265,6 +281,20 @@
|
||||||
<div class="col" id="getDetailedTransactionResult">
|
<div class="col" id="getDetailedTransactionResult">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<input class="btn btn-secondary" type="button" value="sendTransaction" onclick="sendTransaction()">
|
||||||
|
</div>
|
||||||
|
<div class="col-8">
|
||||||
|
<input type="text" class="form-control" id="sendTransactionHex" value="010000000001019d64f0c72a0d206001decbffaa722eb1044534c74eee7a5df8318e42a4323ec10000000017160014550da1f5d25a9dae2eafd6902b4194c4c6500af6ffffffff02809698000000000017a914cd668d781ece600efa4b2404dc91fd26b8b8aed8870553d7360000000017a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a8702473044022076aba4ad559616905fa51d4ddd357fc1fdb428d40cb388e042cdd1da4a1b7357022011916f90c712ead9a66d5f058252efd280439ad8956a967e95d437d246710bc9012102a80a5964c5612bb769ef73147b2cf3c149bc0fd4ecb02f8097629c94ab013ffd00000000">
|
||||||
|
</div>
|
||||||
|
<div class="col">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col" id="sendTransactionResult">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue