Skip to content
Permalink
Browse files

NPCcoin support added

  • Loading branch information...
Password-saver committed Oct 29, 2019
1 parent 2c19d0c commit e10a2f3adffb069a405d3c27578212f0556080a3
@@ -26,6 +26,7 @@ import (
"blockbook/bchain/coins/namecoin"
"blockbook/bchain/coins/nuls"
"blockbook/bchain/coins/pivx"
"blockbook/bchain/coins/npccoin"
"blockbook/bchain/coins/polis"
"blockbook/bchain/coins/qtum"
"blockbook/bchain/coins/ravencoin"
@@ -87,6 +88,7 @@ func init() {
BlockChainFactories["Groestlcoin Testnet"] = grs.NewGroestlcoinRPC
BlockChainFactories["PIVX"] = pivx.NewPivXRPC
BlockChainFactories["PIVX Testnet"] = pivx.NewPivXRPC
BlockChainFactories["NPCcoin"] = npccoin.NewNPCcoinRPC
BlockChainFactories["Polis"] = polis.NewPolisRPC
BlockChainFactories["Zcoin"] = xzc.NewZcoinRPC
BlockChainFactories["Fujicoin"] = fujicoin.NewFujicoinRPC
@@ -0,0 +1,269 @@
package npccoin

import (
"blockbook/bchain"
"blockbook/bchain/coins/btc"
"blockbook/bchain/coins/utils"
"bytes"
"io"

"encoding/hex"
"encoding/json"

"math/big"

"github.com/martinboehm/btcd/blockchain"

"github.com/juju/errors"
"github.com/martinboehm/btcd/wire"
"github.com/martinboehm/btcutil/chaincfg"
)

// magic numbers
const (
MainnetMagic wire.BitcoinNet = 0x3416d9f3
TestnetMagic wire.BitcoinNet = 0x3416d9f4

// Zerocoin op codes
OP_ZEROCOINMINT = 0xc1
OP_ZEROCOINSPEND = 0xc2
)

// chain parameters
var (
MainNetParams chaincfg.Params
TestNetParams chaincfg.Params
)

func init() {
// PIVX mainnet Address encoding magics
MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic
MainNetParams.PubKeyHashAddrID = []byte{53} // starting with 'N'
MainNetParams.ScriptHashAddrID = []byte{54}
MainNetParams.PrivateKeyID = []byte{181}

// PIVX testnet Address encoding magics
TestNetParams = chaincfg.TestNet3Params
TestNetParams.Net = TestnetMagic
TestNetParams.PubKeyHashAddrID = []byte{65} // starting with 'x' or 'y'
TestNetParams.ScriptHashAddrID = []byte{66}
TestNetParams.PrivateKeyID = []byte{193}
}

// NPCcoinParser handle
type NPCcoinParser struct {
*btc.BitcoinParser
baseparser *bchain.BaseParser
BitcoinOutputScriptToAddressesFunc btc.OutputScriptToAddressesFunc
}

// NewNPCcoinParser returns new NPCcoinParser instance
func NewNPCcoinParser(params *chaincfg.Params, c *btc.Configuration) *NPCcoinParser {
p := &NPCcoinParser{
BitcoinParser: btc.NewBitcoinParser(params, c),
baseparser: &bchain.BaseParser{},
}
p.BitcoinOutputScriptToAddressesFunc = p.OutputScriptToAddressesFunc
p.OutputScriptToAddressesFunc = p.outputScriptToAddresses
return p
}

// GetChainParams contains network parameters for the main NPCcoin network
func GetChainParams(chain string) *chaincfg.Params {
if !chaincfg.IsRegistered(&MainNetParams) {
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err != nil {
panic(err)
}
}
switch chain {
case "test":
return &TestNetParams
default:
return &MainNetParams
}
}

// ParseBlock parses raw block to our Block struct
func (p *NPCcoinParser) ParseBlock(b []byte) (*bchain.Block, error) {
r := bytes.NewReader(b)
w := wire.MsgBlock{}
h := wire.BlockHeader{}
err := h.Deserialize(r)
if err != nil {
return nil, errors.Annotatef(err, "Deserialize")
}

if h.Version > 3 {
// Skip past AccumulatorCheckpoint which was added in npccoin block version 4
r.Seek(32, io.SeekCurrent)
}

err = utils.DecodeTransactions(r, 0, wire.WitnessEncoding, &w)
if err != nil {
return nil, errors.Annotatef(err, "DecodeTransactions")
}

txs := make([]bchain.Tx, len(w.Transactions))
for ti, t := range w.Transactions {
txs[ti] = p.TxFromMsgTx(t, false)
}

return &bchain.Block{
BlockHeader: bchain.BlockHeader{
Size: len(b),
Time: h.Timestamp.Unix(),
},
Txs: txs,
}, nil
}

// PackTx packs transaction to byte array using protobuf
func (p *NPCcoinParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
return p.baseparser.PackTx(tx, height, blockTime)
}

// UnpackTx unpacks transaction from protobuf byte array
func (p *NPCcoinParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
return p.baseparser.UnpackTx(buf)
}

// ParseTx parses byte array containing transaction and returns Tx struct
func (p *NPCcoinParser) ParseTx(b []byte) (*bchain.Tx, error) {
t := wire.MsgTx{}
r := bytes.NewReader(b)
if err := t.Deserialize(r); err != nil {
return nil, err
}
tx := p.TxFromMsgTx(&t, true)
tx.Hex = hex.EncodeToString(b)
return &tx, nil
}

// TxFromMsgTx parses tx and adds handling for OP_ZEROCOINSPEND inputs
func (p *NPCcoinParser) TxFromMsgTx(t *wire.MsgTx, parseAddresses bool) bchain.Tx {
vin := make([]bchain.Vin, len(t.TxIn))
for i, in := range t.TxIn {

// extra check to not confuse Tx with single OP_ZEROCOINSPEND input as a coinbase Tx
if !isZeroCoinSpendScript(in.SignatureScript) && blockchain.IsCoinBaseTx(t) {
vin[i] = bchain.Vin{
Coinbase: hex.EncodeToString(in.SignatureScript),
Sequence: in.Sequence,
}
break
}

s := bchain.ScriptSig{
Hex: hex.EncodeToString(in.SignatureScript),
// missing: Asm,
}

txid := in.PreviousOutPoint.Hash.String()

vin[i] = bchain.Vin{
Txid: txid,
Vout: in.PreviousOutPoint.Index,
Sequence: in.Sequence,
ScriptSig: s,
}
}
vout := make([]bchain.Vout, len(t.TxOut))
for i, out := range t.TxOut {
addrs := []string{}
if parseAddresses {
addrs, _, _ = p.OutputScriptToAddressesFunc(out.PkScript)
}
s := bchain.ScriptPubKey{
Hex: hex.EncodeToString(out.PkScript),
Addresses: addrs,
// missing: Asm,
// missing: Type,
}
var vs big.Int
vs.SetInt64(out.Value)
vout[i] = bchain.Vout{
ValueSat: vs,
N: uint32(i),
ScriptPubKey: s,
}
}
tx := bchain.Tx{
Txid: t.TxHash().String(),
Version: t.Version,
LockTime: t.LockTime,
Vin: vin,
Vout: vout,
// skip: BlockHash,
// skip: Confirmations,
// skip: Time,
// skip: Blocktime,
}
return tx
}

// ParseTxFromJson parses JSON message containing transaction and returns Tx struct
func (p *NPCcoinParser) ParseTxFromJson(msg json.RawMessage) (*bchain.Tx, error) {
var tx bchain.Tx
err := json.Unmarshal(msg, &tx)
if err != nil {
return nil, err
}

for i := range tx.Vout {
vout := &tx.Vout[i]
// convert vout.JsonValue to big.Int and clear it, it is only temporary value used for unmarshal
vout.ValueSat, err = p.AmountToBigInt(vout.JsonValue)
if err != nil {
return nil, err
}
vout.JsonValue = ""

if vout.ScriptPubKey.Addresses == nil {
vout.ScriptPubKey.Addresses = []string{}
}
}

return &tx, nil
}

// outputScriptToAddresses converts ScriptPubKey to bitcoin addresses
func (p *NPCcoinParser) outputScriptToAddresses(script []byte) ([]string, bool, error) {
if isZeroCoinSpendScript(script) {
return []string{"Zerocoin Spend"}, false, nil
}
if isZeroCoinMintScript(script) {
return []string{"Zerocoin Mint"}, false, nil
}

rv, s, _ := p.BitcoinOutputScriptToAddressesFunc(script)
return rv, s, nil
}

func (p *NPCcoinParser) GetAddrDescForUnknownInput(tx *bchain.Tx, input int) bchain.AddressDescriptor {
if len(tx.Vin) > input {
scriptHex := tx.Vin[input].ScriptSig.Hex

if scriptHex != "" {
script, _ := hex.DecodeString(scriptHex)
return script
}
}

s := make([]byte, 10)
return s
}

// Checks if script is OP_ZEROCOINMINT
func isZeroCoinMintScript(signatureScript []byte) bool {
return len(signatureScript) > 1 && signatureScript[0] == OP_ZEROCOINMINT
}

// Checks if script is OP_ZEROCOINSPEND
func isZeroCoinSpendScript(signatureScript []byte) bool {
return len(signatureScript) >= 100 && signatureScript[0] == OP_ZEROCOINSPEND
}
@@ -0,0 +1,59 @@
package npccoin

import (
"blockbook/bchain"
"blockbook/bchain/coins/btc"
"encoding/json"

"github.com/golang/glog"
)

// NPCcoinRPC is an interface to JSON-RPC bitcoind service.
type NPCcoinRPC struct {
*btc.BitcoinRPC
}

// NewNPCcoinRPC returns new NPCcoinRPC instance.
func NewNPCcoinRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
b, err := btc.NewBitcoinRPC(config, pushHandler)
if err != nil {
return nil, err
}

s := &NPCcoinRPC{
b.(*btc.BitcoinRPC),
}
s.RPCMarshaler = btc.JSONMarshalerV1{}
s.ChainConfig.SupportsEstimateFee = true
s.ChainConfig.SupportsEstimateSmartFee = false

return s, nil
}

// Initialize initializes NPCcoinRPC instance.
func (b *NPCcoinRPC) Initialize() error {
ci, err := b.GetChainInfo()
if err != nil {
return err
}
chainName := ci.Chain

glog.Info("Chain name ", chainName)
params := GetChainParams(chainName)

// always create parser
b.Parser = NewNPCcoinParser(params, b.ChainConfig)

// parameters for getInfo request
if params.Net == MainnetMagic {
b.Testnet = false
b.Network = "livenet"
} else {
b.Testnet = true
b.Network = "testnet"
}

glog.Info("rpc: block chain ", params.Name)

return nil
}

0 comments on commit e10a2f3

Please sign in to comment.
You can’t perform that action at this time.