github.com/cryptohub-digital/blockbook-fork@v0.0.0-20230713133354-673c927af7f1/api/typesv1.go (about)

     1  package api
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/cryptohub-digital/blockbook-fork/bchain"
     7  )
     8  
     9  // ScriptSigV1 is used for legacy api v1
    10  type ScriptSigV1 struct {
    11  	Hex string `json:"hex,omitempty"`
    12  	Asm string `json:"asm,omitempty"`
    13  }
    14  
    15  // VinV1 is used for legacy api v1
    16  type VinV1 struct {
    17  	Txid      string                   `json:"txid"`
    18  	Vout      uint32                   `json:"vout"`
    19  	Sequence  int64                    `json:"sequence,omitempty"`
    20  	N         int                      `json:"n"`
    21  	ScriptSig ScriptSigV1              `json:"scriptSig"`
    22  	AddrDesc  bchain.AddressDescriptor `json:"-"`
    23  	Addresses []string                 `json:"addresses"`
    24  	IsAddress bool                     `json:"-"`
    25  	Value     string                   `json:"value"`
    26  	ValueSat  big.Int                  `json:"-"`
    27  }
    28  
    29  // ScriptPubKeyV1 is used for legacy api v1
    30  type ScriptPubKeyV1 struct {
    31  	Hex       string                   `json:"hex,omitempty"`
    32  	Asm       string                   `json:"asm,omitempty"`
    33  	AddrDesc  bchain.AddressDescriptor `json:"-"`
    34  	Addresses []string                 `json:"addresses"`
    35  	IsAddress bool                     `json:"-"`
    36  	Type      string                   `json:"type,omitempty"`
    37  }
    38  
    39  // VoutV1 is used for legacy api v1
    40  type VoutV1 struct {
    41  	Value        string         `json:"value"`
    42  	ValueSat     big.Int        `json:"-"`
    43  	N            int            `json:"n"`
    44  	ScriptPubKey ScriptPubKeyV1 `json:"scriptPubKey"`
    45  	Spent        bool           `json:"spent"`
    46  	SpentTxID    string         `json:"spentTxId,omitempty"`
    47  	SpentIndex   int            `json:"spentIndex,omitempty"`
    48  	SpentHeight  int            `json:"spentHeight,omitempty"`
    49  }
    50  
    51  // TxV1 is used for legacy api v1
    52  type TxV1 struct {
    53  	Txid          string   `json:"txid"`
    54  	Version       int32    `json:"version,omitempty"`
    55  	Locktime      uint32   `json:"locktime,omitempty"`
    56  	Vin           []VinV1  `json:"vin"`
    57  	Vout          []VoutV1 `json:"vout"`
    58  	Blockhash     string   `json:"blockhash,omitempty"`
    59  	Blockheight   int      `json:"blockheight"`
    60  	Confirmations uint32   `json:"confirmations"`
    61  	Time          int64    `json:"time,omitempty"`
    62  	Blocktime     int64    `json:"blocktime"`
    63  	ValueOut      string   `json:"valueOut"`
    64  	ValueOutSat   big.Int  `json:"-"`
    65  	Size          int      `json:"size,omitempty"`
    66  	ValueIn       string   `json:"valueIn"`
    67  	ValueInSat    big.Int  `json:"-"`
    68  	Fees          string   `json:"fees"`
    69  	FeesSat       big.Int  `json:"-"`
    70  	Hex           string   `json:"hex"`
    71  }
    72  
    73  // AddressV1 is used for legacy api v1
    74  type AddressV1 struct {
    75  	Paging
    76  	AddrStr                 string   `json:"addrStr"`
    77  	Balance                 string   `json:"balance"`
    78  	TotalReceived           string   `json:"totalReceived"`
    79  	TotalSent               string   `json:"totalSent"`
    80  	UnconfirmedBalance      string   `json:"unconfirmedBalance"`
    81  	UnconfirmedTxApperances int      `json:"unconfirmedTxApperances"`
    82  	TxApperances            int      `json:"txApperances"`
    83  	Transactions            []*TxV1  `json:"txs,omitempty"`
    84  	Txids                   []string `json:"transactions,omitempty"`
    85  }
    86  
    87  // AddressUtxoV1 is used for legacy api v1
    88  type AddressUtxoV1 struct {
    89  	Txid          string  `json:"txid"`
    90  	Vout          uint32  `json:"vout"`
    91  	Amount        string  `json:"amount"`
    92  	AmountSat     big.Int `json:"satoshis"`
    93  	Height        int     `json:"height,omitempty"`
    94  	Confirmations int     `json:"confirmations"`
    95  }
    96  
    97  // BlockV1 contains information about block
    98  type BlockV1 struct {
    99  	Paging
   100  	BlockInfo
   101  	TxCount      int     `json:"txCount"`
   102  	Transactions []*TxV1 `json:"txs,omitempty"`
   103  }
   104  
   105  // TxToV1 converts Tx to TxV1
   106  func (w *Worker) TxToV1(tx *Tx) *TxV1 {
   107  	d := w.chainParser.AmountDecimals()
   108  	vinV1 := make([]VinV1, len(tx.Vin))
   109  	for i := range tx.Vin {
   110  		v := &tx.Vin[i]
   111  		vinV1[i] = VinV1{
   112  			AddrDesc:  v.AddrDesc,
   113  			Addresses: v.Addresses,
   114  			N:         v.N,
   115  			ScriptSig: ScriptSigV1{
   116  				Asm: v.Asm,
   117  				Hex: v.Hex,
   118  			},
   119  			IsAddress: v.IsAddress,
   120  			Sequence:  v.Sequence,
   121  			Txid:      v.Txid,
   122  			Value:     v.ValueSat.DecimalString(d),
   123  			ValueSat:  v.ValueSat.AsBigInt(),
   124  			Vout:      v.Vout,
   125  		}
   126  	}
   127  	voutV1 := make([]VoutV1, len(tx.Vout))
   128  	for i := range tx.Vout {
   129  		v := &tx.Vout[i]
   130  		voutV1[i] = VoutV1{
   131  			N: v.N,
   132  			ScriptPubKey: ScriptPubKeyV1{
   133  				AddrDesc:  v.AddrDesc,
   134  				Addresses: v.Addresses,
   135  				Asm:       v.Asm,
   136  				Hex:       v.Hex,
   137  				IsAddress: v.IsAddress,
   138  				Type:      v.Type,
   139  			},
   140  			Spent:       v.Spent,
   141  			SpentHeight: v.SpentHeight,
   142  			SpentIndex:  v.SpentIndex,
   143  			SpentTxID:   v.SpentTxID,
   144  			Value:       v.ValueSat.DecimalString(d),
   145  			ValueSat:    v.ValueSat.AsBigInt(),
   146  		}
   147  	}
   148  	return &TxV1{
   149  		Blockhash:     tx.Blockhash,
   150  		Blockheight:   tx.Blockheight,
   151  		Blocktime:     tx.Blocktime,
   152  		Confirmations: tx.Confirmations,
   153  		Fees:          tx.FeesSat.DecimalString(d),
   154  		FeesSat:       tx.FeesSat.AsBigInt(),
   155  		Hex:           tx.Hex,
   156  		Locktime:      tx.Locktime,
   157  		Size:          tx.Size,
   158  		Time:          tx.Blocktime,
   159  		Txid:          tx.Txid,
   160  		ValueIn:       tx.ValueInSat.DecimalString(d),
   161  		ValueInSat:    tx.ValueInSat.AsBigInt(),
   162  		ValueOut:      tx.ValueOutSat.DecimalString(d),
   163  		ValueOutSat:   tx.ValueOutSat.AsBigInt(),
   164  		Version:       tx.Version,
   165  		Vin:           vinV1,
   166  		Vout:          voutV1,
   167  	}
   168  }
   169  
   170  func (w *Worker) transactionsToV1(txs []*Tx) []*TxV1 {
   171  	v1 := make([]*TxV1, len(txs))
   172  	for i := range txs {
   173  		v1[i] = w.TxToV1(txs[i])
   174  	}
   175  	return v1
   176  }
   177  
   178  // AddressToV1 converts Address to AddressV1
   179  func (w *Worker) AddressToV1(a *Address) *AddressV1 {
   180  	d := w.chainParser.AmountDecimals()
   181  	return &AddressV1{
   182  		AddrStr:                 a.AddrStr,
   183  		Balance:                 a.BalanceSat.DecimalString(d),
   184  		Paging:                  a.Paging,
   185  		TotalReceived:           a.TotalReceivedSat.DecimalString(d),
   186  		TotalSent:               a.TotalSentSat.DecimalString(d),
   187  		Transactions:            w.transactionsToV1(a.Transactions),
   188  		TxApperances:            a.Txs,
   189  		Txids:                   a.Txids,
   190  		UnconfirmedBalance:      a.UnconfirmedBalanceSat.DecimalString(d),
   191  		UnconfirmedTxApperances: a.UnconfirmedTxs,
   192  	}
   193  }
   194  
   195  // AddressUtxoToV1 converts []AddressUtxo to []AddressUtxoV1
   196  func (w *Worker) AddressUtxoToV1(au Utxos) []AddressUtxoV1 {
   197  	d := w.chainParser.AmountDecimals()
   198  	v1 := make([]AddressUtxoV1, len(au))
   199  	for i := range au {
   200  		utxo := &au[i]
   201  		v1[i] = AddressUtxoV1{
   202  			AmountSat:     utxo.AmountSat.AsBigInt(),
   203  			Amount:        utxo.AmountSat.DecimalString(d),
   204  			Confirmations: utxo.Confirmations,
   205  			Height:        utxo.Height,
   206  			Txid:          utxo.Txid,
   207  			Vout:          uint32(utxo.Vout),
   208  		}
   209  	}
   210  	return v1
   211  }
   212  
   213  // BlockToV1 converts Address to Address1
   214  func (w *Worker) BlockToV1(b *Block) *BlockV1 {
   215  	return &BlockV1{
   216  		BlockInfo:    b.BlockInfo,
   217  		Paging:       b.Paging,
   218  		Transactions: w.transactionsToV1(b.Transactions),
   219  		TxCount:      b.TxCount,
   220  	}
   221  }