github.com/llamaondrugs/blockbook@v0.3.2/api/typesv1.go (about)

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