Add BaseParser as base functionality of parsers to avoid code duplicity
This commit is contained in:
parent
9727a3dc8c
commit
d0089d2bcb
59
bchain/baseparser.go
Normal file
59
bchain/baseparser.go
Normal file
@ -0,0 +1,59 @@
|
||||
package bchain
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// BaseParser implements data parsing/handling functionality base for all other parsers
|
||||
type BaseParser struct{}
|
||||
|
||||
// AddressToOutputScript converts address to ScriptPubKey - currently not implemented
|
||||
func (p *BaseParser) AddressToOutputScript(address string) ([]byte, error) {
|
||||
return nil, errors.New("AddressToOutputScript: not implemented")
|
||||
}
|
||||
|
||||
// OutputScriptToAddresses converts ScriptPubKey to addresses - currently not implemented
|
||||
func (p *BaseParser) OutputScriptToAddresses(script []byte) ([]string, error) {
|
||||
return nil, errors.New("OutputScriptToAddresses: not implemented")
|
||||
}
|
||||
|
||||
// ParseBlock parses raw block to our Block struct - currently not implemented
|
||||
func (p *BaseParser) ParseBlock(b []byte) (*Block, error) {
|
||||
return nil, errors.New("ParseBlock: not implemented")
|
||||
}
|
||||
|
||||
// ParseTx parses byte array containing transaction and returns Tx struct - currently not implemented
|
||||
func (p *BaseParser) ParseTx(b []byte) (*Tx, error) {
|
||||
return nil, errors.New("ParseTx: not implemented")
|
||||
}
|
||||
|
||||
// PackedTxidLen returns length in bytes of packed txid
|
||||
func (p *BaseParser) PackedTxidLen() int {
|
||||
return 32
|
||||
}
|
||||
|
||||
// PackTxid packs txid to byte array
|
||||
func (p *BaseParser) PackTxid(txid string) ([]byte, error) {
|
||||
return hex.DecodeString(txid)
|
||||
}
|
||||
|
||||
// UnpackTxid unpacks byte array to txid
|
||||
func (p *BaseParser) UnpackTxid(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// PackBlockHash packs block hash to byte array
|
||||
func (p *BaseParser) PackBlockHash(hash string) ([]byte, error) {
|
||||
return hex.DecodeString(hash)
|
||||
}
|
||||
|
||||
// UnpackBlockHash unpacks byte array to block hash
|
||||
func (p *BaseParser) UnpackBlockHash(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// IsUTXOChain returns true if the block chain is UTXO type, otherwise false
|
||||
func (p *BaseParser) IsUTXOChain() bool {
|
||||
return true
|
||||
}
|
||||
@ -16,6 +16,7 @@ import (
|
||||
|
||||
// BitcoinParser handle
|
||||
type BitcoinParser struct {
|
||||
*bchain.BaseParser
|
||||
Params *chaincfg.Params
|
||||
}
|
||||
|
||||
@ -170,33 +171,3 @@ func (p *BitcoinParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
|
||||
tx.Blocktime = bt
|
||||
return tx, height, nil
|
||||
}
|
||||
|
||||
// PackedTxidLen returns length in bytes of packed txid
|
||||
func (p *BitcoinParser) PackedTxidLen() int {
|
||||
return 32
|
||||
}
|
||||
|
||||
// PackTxid packs txid to byte array
|
||||
func (p *BitcoinParser) PackTxid(txid string) ([]byte, error) {
|
||||
return hex.DecodeString(txid)
|
||||
}
|
||||
|
||||
// UnpackTxid unpacks byte array to txid
|
||||
func (p *BitcoinParser) UnpackTxid(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// PackBlockHash packs block hash to byte array
|
||||
func (p *BitcoinParser) PackBlockHash(hash string) ([]byte, error) {
|
||||
return hex.DecodeString(hash)
|
||||
}
|
||||
|
||||
// UnpackBlockHash unpacks byte array to block hash
|
||||
func (p *BitcoinParser) UnpackBlockHash(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// IsUTXOChain returns true if the block chain is UTXO type, otherwise false
|
||||
func (p *BitcoinParser) IsUTXOChain() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@ -15,6 +15,11 @@ import (
|
||||
ethcommon "github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
// EthereumParser handle
|
||||
type EthereumParser struct {
|
||||
*bchain.BaseParser
|
||||
}
|
||||
|
||||
type rpcTransaction struct {
|
||||
AccountNonce string `json:"nonce" gencodec:"required"`
|
||||
Price string `json:"gasPrice" gencodec:"required"`
|
||||
@ -98,10 +103,6 @@ func ethTxToTx(tx *rpcTransaction, blocktime int64, confirmations uint32) (*bcha
|
||||
}, nil
|
||||
}
|
||||
|
||||
// EthereumParser handle
|
||||
type EthereumParser struct {
|
||||
}
|
||||
|
||||
// GetAddrIDFromVout returns internal address representation of given transaction output
|
||||
func (p *EthereumParser) GetAddrIDFromVout(output *bchain.Vout) ([]byte, error) {
|
||||
if len(output.ScriptPubKey.Addresses) != 1 {
|
||||
@ -129,26 +130,6 @@ func (p *EthereumParser) GetAddrIDFromAddress(address string) ([]byte, error) {
|
||||
return hex.DecodeString(address)
|
||||
}
|
||||
|
||||
// AddressToOutputScript converts address to ScriptPubKey - currently not implemented
|
||||
func (p *EthereumParser) AddressToOutputScript(address string) ([]byte, error) {
|
||||
return nil, errors.New("AddressToOutputScript: not implemented")
|
||||
}
|
||||
|
||||
// OutputScriptToAddresses converts ScriptPubKey to addresses - currently not implemented
|
||||
func (p *EthereumParser) OutputScriptToAddresses(script []byte) ([]string, error) {
|
||||
return nil, errors.New("OutputScriptToAddresses: not implemented")
|
||||
}
|
||||
|
||||
// ParseTx parses byte array containing transaction and returns Tx struct - currently not implemented
|
||||
func (p *EthereumParser) ParseTx(b []byte) (*bchain.Tx, error) {
|
||||
return nil, errors.New("ParseTx: not implemented")
|
||||
}
|
||||
|
||||
// ParseBlock parses raw block to our Block struct - currently not implemented
|
||||
func (p *EthereumParser) ParseBlock(b []byte) (*bchain.Block, error) {
|
||||
return nil, errors.New("ParseBlock: not implemented")
|
||||
}
|
||||
|
||||
func hexDecode(s string) ([]byte, error) {
|
||||
b, err := hexutil.Decode(s)
|
||||
if err != nil && err != hexutil.ErrEmptyString {
|
||||
|
||||
@ -5,12 +5,12 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ZCashParser handle
|
||||
type ZCashParser struct{}
|
||||
type ZCashParser struct {
|
||||
*bchain.BaseParser
|
||||
}
|
||||
|
||||
// GetAddrIDFromVout returns internal address representation of given transaction output
|
||||
func (p *ZCashParser) GetAddrIDFromVout(output *bchain.Vout) ([]byte, error) {
|
||||
@ -64,53 +64,3 @@ func decodeTx(buf []byte) (*bchain.Tx, error) {
|
||||
}
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
// AddressToOutputScript converts address to ScriptPubKey - currently not implemented
|
||||
func (p *ZCashParser) AddressToOutputScript(address string) ([]byte, error) {
|
||||
return nil, errors.New("AddressToOutputScript: not implemented")
|
||||
}
|
||||
|
||||
// OutputScriptToAddresses converts ScriptPubKey to addresses - currently not implemented
|
||||
func (p *ZCashParser) OutputScriptToAddresses(script []byte) ([]string, error) {
|
||||
return nil, errors.New("OutputScriptToAddresses: not implemented")
|
||||
}
|
||||
|
||||
// ParseBlock parses raw block to our Block struct - currently not implemented
|
||||
func (p *ZCashParser) ParseBlock(b []byte) (*bchain.Block, error) {
|
||||
return nil, errors.New("ParseBlock: not implemented")
|
||||
}
|
||||
|
||||
// ParseTx parses byte array containing transaction and returns Tx struct - currently not implemented
|
||||
func (p *ZCashParser) ParseTx(b []byte) (*bchain.Tx, error) {
|
||||
return nil, errors.New("ParseTx: not implemented")
|
||||
}
|
||||
|
||||
// PackedTxidLen returns length in bytes of packed txid
|
||||
func (p *ZCashParser) PackedTxidLen() int {
|
||||
return 32
|
||||
}
|
||||
|
||||
// PackTxid packs txid to byte array
|
||||
func (p *ZCashParser) PackTxid(txid string) ([]byte, error) {
|
||||
return hex.DecodeString(txid)
|
||||
}
|
||||
|
||||
// UnpackTxid unpacks byte array to txid
|
||||
func (p *ZCashParser) UnpackTxid(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// PackBlockHash packs block hash to byte array
|
||||
func (p *ZCashParser) PackBlockHash(hash string) ([]byte, error) {
|
||||
return hex.DecodeString(hash)
|
||||
}
|
||||
|
||||
// UnpackBlockHash unpacks byte array to block hash
|
||||
func (p *ZCashParser) UnpackBlockHash(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// IsUTXOChain returns true if the block chain is UTXO type, otherwise false
|
||||
func (p *ZCashParser) IsUTXOChain() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user