github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/vipstarcoin/vipstarcoinparser.go (about)

     1  package vipstarcoin
     2  
     3  import (
     4  	"blockbook/bchain"
     5  	"blockbook/bchain/coins/btc"
     6  	"blockbook/bchain/coins/utils"
     7  	"bytes"
     8  	"encoding/json"
     9  	"io"
    10  
    11  	"github.com/martinboehm/btcd/wire"
    12  	"github.com/martinboehm/btcutil/chaincfg"
    13  )
    14  
    15  // magic numbers
    16  const (
    17  	MainnetMagic wire.BitcoinNet = 0x012ce7b5
    18  	TestnetMagic wire.BitcoinNet = 0x1a2b3c4d
    19  )
    20  
    21  // chain parameters
    22  var (
    23  	MainNetParams chaincfg.Params
    24  	TestNetParams chaincfg.Params
    25  )
    26  
    27  func init() {
    28  	MainNetParams = chaincfg.MainNetParams
    29  	MainNetParams.Net = MainnetMagic
    30  	MainNetParams.PubKeyHashAddrID = []byte{70}
    31  	MainNetParams.ScriptHashAddrID = []byte{50}
    32  	MainNetParams.Bech32HRPSegwit = "vips"
    33  
    34  	TestNetParams = chaincfg.TestNet3Params
    35  	TestNetParams.Net = TestnetMagic
    36  	TestNetParams.PubKeyHashAddrID = []byte{132}
    37  	TestNetParams.ScriptHashAddrID = []byte{110}
    38  	TestNetParams.Bech32HRPSegwit = "tvips"
    39  }
    40  
    41  // VIPSTARCOINParser handle
    42  type VIPSTARCOINParser struct {
    43  	*btc.BitcoinParser
    44  }
    45  
    46  // NewVIPSTARCOINParser returns new VIPSTARCOINParser instance
    47  func NewVIPSTARCOINParser(params *chaincfg.Params, c *btc.Configuration) *VIPSTARCOINParser {
    48  	return &VIPSTARCOINParser{BitcoinParser: btc.NewBitcoinParser(params, c)}
    49  }
    50  
    51  // GetChainParams contains network parameters for the main VIPSTARCOIN network,
    52  // and the test VIPSTARCOIN network
    53  func GetChainParams(chain string) *chaincfg.Params {
    54  	if !chaincfg.IsRegistered(&MainNetParams) {
    55  		err := chaincfg.Register(&MainNetParams)
    56  		if err == nil {
    57  			err = chaincfg.Register(&TestNetParams)
    58  		}
    59  		if err != nil {
    60  			panic(err)
    61  		}
    62  	}
    63  	switch chain {
    64  	case "test":
    65  		return &TestNetParams
    66  	default:
    67  		return &MainNetParams
    68  	}
    69  }
    70  
    71  func parseBlockHeader(r io.Reader) (*wire.BlockHeader, error) {
    72  	h := &wire.BlockHeader{}
    73  	err := h.Deserialize(r)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	// hash_state_root 32
    79  	// hash_utxo_root 32
    80  	// hash_prevout_stake 32
    81  	// hash_prevout_n 4
    82  	buf := make([]byte, 100)
    83  	_, err = io.ReadFull(r, buf)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	sigLength, err := wire.ReadVarInt(r, 0)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	sigBuf := make([]byte, sigLength)
    93  	_, err = io.ReadFull(r, sigBuf)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return h, err
    99  }
   100  
   101  // ParseBlock parses block from byte array
   102  func (p *VIPSTARCOINParser) ParseBlock(b []byte) (*bchain.Block, error) {
   103  	r := bytes.NewReader(b)
   104  	w := wire.MsgBlock{}
   105  
   106  	h, err := parseBlockHeader(r)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	err = utils.DecodeTransactions(r, 0, wire.WitnessEncoding, &w)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	txs := make([]bchain.Tx, len(w.Transactions))
   117  	for ti, t := range w.Transactions {
   118  		txs[ti] = p.TxFromMsgTx(t, false)
   119  	}
   120  
   121  	return &bchain.Block{
   122  		BlockHeader: bchain.BlockHeader{
   123  			Size: len(b),
   124  			Time: h.Timestamp.Unix(),
   125  		},
   126  		Txs: txs,
   127  	}, nil
   128  }
   129  
   130  // ParseTxFromJson parses JSON message containing transaction and returns Tx struct
   131  func (p *VIPSTARCOINParser) ParseTxFromJson(msg json.RawMessage) (*bchain.Tx, error) {
   132  	var tx bchain.Tx
   133  	err := json.Unmarshal(msg, &tx)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	for i := range tx.Vout {
   139  		vout := &tx.Vout[i]
   140  		// convert vout.JsonValue to big.Int and clear it, it is only temporary value used for unmarshal
   141  		vout.ValueSat, err = p.AmountToBigInt(vout.JsonValue)
   142  		if err != nil {
   143  			return nil, err
   144  		}
   145  		vout.JsonValue = ""
   146  
   147  		if vout.ScriptPubKey.Addresses == nil {
   148  			vout.ScriptPubKey.Addresses = []string{}
   149  		}
   150  	}
   151  
   152  	return &tx, nil
   153  }