Implement Bitcore socket.io method getDetailedTransaction

This commit is contained in:
Martin Boehm 2018-02-19 23:54:21 +01:00
parent aac52a3fcb
commit aa2d6d7816
2 changed files with 106 additions and 7 deletions

View File

@ -142,7 +142,13 @@ var onMessageHandlers = map[string]func(*SocketIoServer, json.RawMessage) (inter
"\"getInfo\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) { "\"getInfo\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) {
return s.getInfo() return s.getInfo()
}, },
// getDetailedTransaction "\"getDetailedTransaction\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) {
txid, err := unmarshalGetDetailedTransaction(params)
if err == nil {
rv, err = s.getDetailedTransaction(txid)
}
return
},
// sendTransaction // sendTransaction
} }
@ -226,7 +232,7 @@ type addressHistoryIndexes struct {
} }
type txInputs struct { type txInputs struct {
PrevTxID string `json:"prevTxId"` PrevTxID string `json:"prevTxId,omitempty"`
OutputIndex int `json:"outputIndex"` OutputIndex int `json:"outputIndex"`
Script string `json:"script"` Script string `json:"script"`
ScriptAsm string `json:"scriptAsm"` ScriptAsm string `json:"scriptAsm"`
@ -247,13 +253,13 @@ type txOutputs struct {
type resTx struct { type resTx struct {
Hex string `json:"hex"` Hex string `json:"hex"`
BlockHash string `json:"blockHash"` BlockHash string `json:"blockHash,omitempty"`
Height int `json:"height"` Height int `json:"height"`
BlockTimestamp int64 `json:"blockTimestamp"` BlockTimestamp int64 `json:"blockTimestamp"`
Version int `json:"version"` Version int `json:"version"`
Hash string `json:"hash"` Hash string `json:"hash"`
Locktime int `json:"locktime"` Locktime int `json:"locktime"`
Size int `json:"size"` Size int `json:"size,omitempty"`
Inputs []txInputs `json:"inputs"` Inputs []txInputs `json:"inputs"`
InputSatoshis int64 `json:"inputSatoshis"` InputSatoshis int64 `json:"inputSatoshis"`
Outputs []txOutputs `json:"outputs"` Outputs []txOutputs `json:"outputs"`
@ -299,7 +305,7 @@ func stringInSlice(a string, list []string) bool {
func txToResTx(tx *bchain.Tx, height int, hi []txInputs, ho []txOutputs) resTx { func txToResTx(tx *bchain.Tx, height int, hi []txInputs, ho []txOutputs) resTx {
return resTx{ return resTx{
BlockHash: tx.BlockHash, // BlockHash: tx.BlockHash,
BlockTimestamp: tx.Blocktime, BlockTimestamp: tx.Blocktime,
// FeeSatoshis, // FeeSatoshis,
Hash: tx.Txid, Hash: tx.Txid,
@ -400,7 +406,13 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
ahi := addressHistoryItem{} ahi := addressHistoryItem{}
ahi.Addresses = ads ahi.Addresses = ads
ahi.Confirmations = int(tx.Confirmations) ahi.Confirmations = int(tx.Confirmations)
ahi.Tx = txToResTx(tx, int(bestheight)-int(tx.Confirmations), hi, ho) var height int
if tx.Confirmations == 0 {
height = -1
} else {
height = int(bestheight) - int(tx.Confirmations)
}
ahi.Tx = txToResTx(tx, height, hi, ho)
res.Result.Items = append(res.Result.Items, ahi) res.Result.Items = append(res.Result.Items, ahi)
} }
} }
@ -534,7 +546,63 @@ func (s *SocketIoServer) getInfo() (res resultGetInfo, err error) {
return return
} }
func (s *SocketIoServer) onSubscribe(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} { func unmarshalGetDetailedTransaction(params []byte) (hash string, err error) {
p, err := unmarshalArray(params, 1)
if err != nil {
return
}
hash, ok := p[0].(string)
if ok {
return
}
err = errors.New("incorrect parameter")
return
}
type resultGetDetailedTransaction struct {
Result resTx `json:"result"`
}
func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetailedTransaction, err error) {
bestheight, _, err := s.db.GetBestBlock()
if err != nil {
return
}
tx, err := s.chain.GetTransaction(txid)
if err != nil {
return res, err
}
hi := make([]txInputs, 0)
ho := make([]txOutputs, 0)
for _, vin := range tx.Vin {
ai := txInputs{
Script: vin.ScriptSig.Hex,
ScriptAsm: vin.ScriptSig.Asm,
Sequence: int64(vin.Sequence),
OutputIndex: int(vin.Vout),
}
hi = append(hi, ai)
}
for _, vout := range tx.Vout {
ao := txOutputs{
Satoshis: int64(vout.Value * 10E8),
Script: vout.ScriptPubKey.Hex,
ScriptAsm: vout.ScriptPubKey.Asm,
SpentIndex: int(vout.N),
}
ho = append(ho, ao)
}
var height int
if tx.Confirmations == 0 {
height = -1
} else {
height = int(bestheight) - int(tx.Confirmations)
}
res.Result = txToResTx(tx, height, hi, ho)
return
}
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
} }

View File

@ -146,6 +146,23 @@
return socket.send({ method, params }, f); return socket.send({ method, params }, f);
} }
function getDetailedTransaction() {
var hash = document.getElementById('getDetailedTransactionHash').value;
lookupDetailedTransaction(hash, function (result) {
console.log('getDetailedTransaction sent successfully');
console.log(result);
document.getElementById('getDetailedTransactionResult').innerText = JSON.stringify(result).replace(/,/g, ", ");
});
}
function lookupDetailedTransaction(hash, f) {
const method = 'getDetailedTransaction';
const params = [
hash,
];
return socket.send({ method, params }, f);
}
</script> </script>
</head> </head>
@ -234,6 +251,20 @@
<div class="col-10" id="getInfoResult"> <div class="col-10" id="getInfoResult">
</div> </div>
</div> </div>
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="getDetailedTransaction" onclick="getDetailedTransaction()">
</div>
<div class="col-8">
<input type="text" class="form-control" id="getDetailedTransactionHash" value="474e6795760ebe81cb4023dc227e5a0efe340e1771c89a0035276361ed733de7">
</div>
<div class="col">
</div>
</div>
<div class="row">
<div class="col" id="getDetailedTransactionResult">
</div>
</div>
</div> </div>
</body> </body>