Improve compatibility of socket.io interface with original Bitcore

This commit is contained in:
Martin Boehm 2018-02-26 15:19:53 +01:00
parent 2375837d0a
commit ad68018b1a
2 changed files with 46 additions and 52 deletions

View File

@ -264,30 +264,29 @@ type addressHistoryIndexes struct {
} }
type txInputs struct { type txInputs struct {
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"` Sequence int64 `json:"sequence"`
Sequence int64 `json:"sequence"` Address *string `json:"address"`
Address string `json:"address"` Satoshis int64 `json:"satoshis"`
Satoshis int64 `json:"satoshis"`
} }
type txOutputs struct { type txOutputs struct {
Satoshis int64 `json:"satoshis"` Satoshis int64 `json:"satoshis"`
Script string `json:"script"` Script *string `json:"script"`
ScriptAsm string `json:"scriptAsm"` ScriptAsm *string `json:"scriptAsm"`
SpentTxID string `json:"spentTxId,omitempty"` SpentTxID *string `json:"spentTxId,omitempty"`
SpentIndex int `json:"spentIndex,omitempty"` SpentIndex int `json:"spentIndex,omitempty"`
SpentHeight int `json:"spentHeight,omitempty"` SpentHeight int `json:"spentHeight,omitempty"`
Address string `json:"address"` Address *string `json:"address"`
} }
type resTx struct { type resTx struct {
Hex string `json:"hex"` Hex string `json:"hex"`
BlockHash string `json:"blockHash,omitempty"` BlockHash string `json:"blockHash,omitempty"`
Height int `json:"height"` Height int `json:"height"`
BlockTimestamp int64 `json:"blockTimestamp"` BlockTimestamp int64 `json:"blockTimestamp,omitempty"`
Version int `json:"version"` Version int `json:"version"`
Hash string `json:"hash"` Hash string `json:"hash"`
Locktime int `json:"locktime,omitempty"` Locktime int `json:"locktime,omitempty"`
@ -362,51 +361,47 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
ho := make([]txOutputs, 0) ho := make([]txOutputs, 0)
for _, vin := range tx.Vin { for _, vin := range tx.Vin {
ai := txInputs{ ai := txInputs{
Script: vin.ScriptSig.Hex, Script: &vin.ScriptSig.Hex,
ScriptAsm: vin.ScriptSig.Asm, ScriptAsm: &vin.ScriptSig.Asm,
Sequence: int64(vin.Sequence), Sequence: int64(vin.Sequence),
OutputIndex: int(vin.Vout), OutputIndex: int(vin.Vout),
} }
stxid, _, err := s.db.GetSpentOutput(vin.Txid, vin.Vout) otx, err := s.chain.GetTransaction(vin.Txid)
if err != nil { if err != nil {
return res, err return res, err
} }
if stxid != "" { if len(otx.Vout) > int(vin.Vout) {
otx, err := s.chain.GetTransaction(stxid) vout := otx.Vout[vin.Vout]
if err != nil { if len(vout.ScriptPubKey.Addresses) == 1 {
return res, err a := vout.ScriptPubKey.Addresses[0]
} ai.Address = &a
SpentOutputLoop: if stringInSlice(a, addr) {
for _, vout := range otx.Vout { hi, ok := ads[a]
for _, a := range vout.ScriptPubKey.Addresses { if ok {
if stringInSlice(a, addr) { hi.InputIndexes = append(hi.InputIndexes, int(vout.N))
ai.Address = a } else {
hi, ok := ads[a] hi := addressHistoryIndexes{}
if ok { hi.InputIndexes = append(hi.InputIndexes, int(vout.N))
hi.InputIndexes = append(hi.InputIndexes, int(vout.N)) hi.OutputIndexes = make([]int, 0)
} else { ads[a] = hi
hi := addressHistoryIndexes{}
hi.InputIndexes = append(hi.InputIndexes, int(vout.N))
hi.OutputIndexes = make([]int, 0)
ads[a] = hi
}
break SpentOutputLoop
} }
} }
} }
ai.Satoshis = int64(vout.Value * 1E8)
} }
hi = append(hi, ai) hi = append(hi, ai)
} }
for _, vout := range tx.Vout { for _, vout := range tx.Vout {
ao := txOutputs{ ao := txOutputs{
Satoshis: int64(vout.Value * 1E8), Satoshis: int64(vout.Value * 1E8),
Script: vout.ScriptPubKey.Hex, Script: &vout.ScriptPubKey.Hex,
ScriptAsm: vout.ScriptPubKey.Asm, ScriptAsm: &vout.ScriptPubKey.Asm,
SpentIndex: int(vout.N), SpentIndex: int(vout.N),
} }
for _, a := range vout.ScriptPubKey.Addresses { if len(vout.ScriptPubKey.Addresses) == 1 {
a := vout.ScriptPubKey.Addresses[0]
ao.Address = &a
if stringInSlice(a, addr) { if stringInSlice(a, addr) {
ao.Address = a
hi, ok := ads[a] hi, ok := ads[a]
if ok { if ok {
hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N)) hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N))
@ -416,7 +411,6 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N)) hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N))
ads[a] = hi ads[a] = hi
} }
break
} }
} }
ho = append(ho, ao) ho = append(ho, ao)
@ -428,7 +422,7 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
if tx.Confirmations == 0 { if tx.Confirmations == 0 {
height = -1 height = -1
} else { } else {
height = int(bestheight) - int(tx.Confirmations) height = int(bestheight) - int(tx.Confirmations) + 1
} }
ahi.Tx = txToResTx(tx, height, hi, ho) ahi.Tx = txToResTx(tx, height, hi, ho)
res.Result.Items = append(res.Result.Items, ahi) res.Result.Items = append(res.Result.Items, ahi)
@ -594,8 +588,8 @@ func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetai
ho := make([]txOutputs, 0) ho := make([]txOutputs, 0)
for _, vin := range tx.Vin { for _, vin := range tx.Vin {
ai := txInputs{ ai := txInputs{
Script: vin.ScriptSig.Hex, Script: &vin.ScriptSig.Hex,
ScriptAsm: vin.ScriptSig.Asm, ScriptAsm: &vin.ScriptSig.Asm,
Sequence: int64(vin.Sequence), Sequence: int64(vin.Sequence),
OutputIndex: int(vin.Vout), OutputIndex: int(vin.Vout),
} }
@ -606,21 +600,21 @@ func (s *SocketIoServer) getDetailedTransaction(txid string) (res resultGetDetai
if len(otx.Vout) > int(vin.Vout) { if len(otx.Vout) > int(vin.Vout) {
vout := otx.Vout[vin.Vout] vout := otx.Vout[vin.Vout]
if len(vout.ScriptPubKey.Addresses) == 1 { if len(vout.ScriptPubKey.Addresses) == 1 {
ai.Address = vout.ScriptPubKey.Addresses[0] ai.Address = &vout.ScriptPubKey.Addresses[0]
} }
ai.Satoshis += int64(vout.Value * 1E8) ai.Satoshis = int64(vout.Value * 1E8)
} }
hi = append(hi, ai) hi = append(hi, ai)
} }
for _, vout := range tx.Vout { for _, vout := range tx.Vout {
ao := txOutputs{ ao := txOutputs{
Satoshis: int64(vout.Value * 1E8), Satoshis: int64(vout.Value * 1E8),
Script: vout.ScriptPubKey.Hex, Script: &vout.ScriptPubKey.Hex,
ScriptAsm: vout.ScriptPubKey.Asm, ScriptAsm: &vout.ScriptPubKey.Asm,
SpentIndex: int(vout.N), SpentIndex: int(vout.N),
} }
if len(vout.ScriptPubKey.Addresses) == 1 { if len(vout.ScriptPubKey.Addresses) == 1 {
ao.Address = vout.ScriptPubKey.Addresses[0] ao.Address = &vout.ScriptPubKey.Addresses[0]
} }
ho = append(ho, ao) ho = append(ho, ao)
} }

View File

@ -54,7 +54,7 @@
function getAddressHistory() { function getAddressHistory() {
var addresses = document.getElementById('getAddressHistoryAddresses').value.split(","); var addresses = document.getElementById('getAddressHistoryAddresses').value.split(",");
var mempool = document.getElementById("getAddressHistoryMempool").checked; var mempool = document.getElementById("getAddressHistoryMempool").checked;
lookupAddressHistories(addresses, 0, 1, mempool, 2000000, 0, function (result) { lookupAddressHistories(addresses, 0, 5, mempool, 2000000, 0, function (result) {
console.log('getAddressHistory sent successfully'); console.log('getAddressHistory sent successfully');
console.log(result); console.log(result);
document.getElementById('getAddressHistoryResult').innerText = JSON.stringify(result).replace(/,/g, ", "); document.getElementById('getAddressHistoryResult').innerText = JSON.stringify(result).replace(/,/g, ", ");