From b6d66cf6a868dbe7d25ea4d8a94a33c5c621c98b Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Jun 2018 13:55:37 +0200 Subject: [PATCH 1/7] Add litecoin backend package --- contrib/backends/litecoin/Makefile | 13 +++++ .../debian/backend-litecoin-testnet.conffiles | 1 + .../debian/backend-litecoin-testnet.dirs | 1 + .../debian/backend-litecoin-testnet.install | 2 + .../debian/backend-litecoin-testnet.logrotate | 10 ++++ .../debian/backend-litecoin-testnet.postinst | 20 ++++++++ .../debian/backend-litecoin-testnet.service | 47 +++++++++++++++++++ .../debian/backend-litecoin.conffiles | 1 + .../litecoin/debian/backend-litecoin.dirs | 1 + .../litecoin/debian/backend-litecoin.install | 2 + .../debian/backend-litecoin.logrotate | 10 ++++ .../litecoin/debian/backend-litecoin.postinst | 20 ++++++++ .../litecoin/debian/backend-litecoin.service | 47 +++++++++++++++++++ contrib/backends/litecoin/debian/changelog | 5 ++ contrib/backends/litecoin/debian/compat | 1 + contrib/backends/litecoin/debian/control | 16 +++++++ contrib/backends/litecoin/debian/rules | 11 +++++ contrib/backends/litecoin/litecoin.conf | 18 +++++++ .../backends/litecoin/litecoin_testnet.conf | 15 ++++++ 19 files changed, 241 insertions(+) create mode 100644 contrib/backends/litecoin/Makefile create mode 100644 contrib/backends/litecoin/debian/backend-litecoin-testnet.conffiles create mode 100644 contrib/backends/litecoin/debian/backend-litecoin-testnet.dirs create mode 100644 contrib/backends/litecoin/debian/backend-litecoin-testnet.install create mode 100644 contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate create mode 100644 contrib/backends/litecoin/debian/backend-litecoin-testnet.postinst create mode 100644 contrib/backends/litecoin/debian/backend-litecoin-testnet.service create mode 100644 contrib/backends/litecoin/debian/backend-litecoin.conffiles create mode 100644 contrib/backends/litecoin/debian/backend-litecoin.dirs create mode 100644 contrib/backends/litecoin/debian/backend-litecoin.install create mode 100644 contrib/backends/litecoin/debian/backend-litecoin.logrotate create mode 100644 contrib/backends/litecoin/debian/backend-litecoin.postinst create mode 100644 contrib/backends/litecoin/debian/backend-litecoin.service create mode 100644 contrib/backends/litecoin/debian/changelog create mode 100644 contrib/backends/litecoin/debian/compat create mode 100644 contrib/backends/litecoin/debian/control create mode 100755 contrib/backends/litecoin/debian/rules create mode 100644 contrib/backends/litecoin/litecoin.conf create mode 100644 contrib/backends/litecoin/litecoin_testnet.conf diff --git a/contrib/backends/litecoin/Makefile b/contrib/backends/litecoin/Makefile new file mode 100644 index 00000000..3df13fbb --- /dev/null +++ b/contrib/backends/litecoin/Makefile @@ -0,0 +1,13 @@ +LITECOIN_VERSION := 0.16.0 + +all: + wget https://download.litecoin.org/litecoin-${LITECOIN_VERSION}/linux/litecoin-${LITECOIN_VERSION}-x86_64-linux-gnu.tar.gz + tar -xf litecoin-${LITECOIN_VERSION}-x86_64-linux-gnu.tar.gz + mv litecoin-${LITECOIN_VERSION} litecoin + rm litecoin/bin/litecoin-qt + rm litecoin/bin/test_litecoin + + +clean: + rm -rf litecoin + rm -f litecoin-${LITECOIN_VERSION}-x86_64-linux-gnu.tar.gz diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.conffiles b/contrib/backends/litecoin/debian/backend-litecoin-testnet.conffiles new file mode 100644 index 00000000..6e00ded9 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.conffiles @@ -0,0 +1 @@ +/opt/coins/nodes/litecoin_testnet/litecoin_testnet.conf diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.dirs b/contrib/backends/litecoin/debian/backend-litecoin-testnet.dirs new file mode 100644 index 00000000..60844601 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.dirs @@ -0,0 +1 @@ +/opt/coins/data/litecoin_testnet/backend diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.install b/contrib/backends/litecoin/debian/backend-litecoin-testnet.install new file mode 100644 index 00000000..4a78b7e9 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.install @@ -0,0 +1,2 @@ +litecoin/* /opt/coins/nodes/litecoin_testnet +litecoin_testnet.conf /opt/coins/nodes/litecoin_testnet diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate b/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate new file mode 100644 index 00000000..a380acfd --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate @@ -0,0 +1,10 @@ +/opt/coins/data/litecoin_testnet/backend/testnet3/debug.log +/opt/coins/data/litecoin_testnet/backend/testnet3/db.log +{ + rotate 7 + daily + compress + missingok + notifempty + copytruncate +} diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.postinst b/contrib/backends/litecoin/debian/backend-litecoin-testnet.postinst new file mode 100644 index 00000000..e323208b --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.postinst @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +case "$1" in + + configure) + if ! id -u litecoin &> /dev/null + then + useradd --system -M -U litecoin -s /bin/false + fi + + if [ "$(stat -c '%U' /opt/coins/data/litecoin_testnet/backend)" != "litecoin" ] + then + chown -R litecoin:litecoin /opt/coins/data/litecoin_testnet/backend + fi + ;; + +esac + +#DEBHELPER# diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.service b/contrib/backends/litecoin/debian/backend-litecoin-testnet.service new file mode 100644 index 00000000..fd2ddaef --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.service @@ -0,0 +1,47 @@ +# It is not recommended to modify this file in-place, because it will +# be overwritten during package upgrades. If you want to add further +# options or overwrite existing ones then use +# $ systemctl edit litecoin-testnet.service +# See "man systemd.service" for details. + +# Note that almost all daemon options could be specified in +# /opt/coins/nodes/litecoin_testnet/litecoin_testnet.conf + +[Unit] +Description=litecoin daemon (testnet) +After=network.target + +[Service] +ExecStart=/opt/coins/nodes/litecoin_testnet/bin/litecoind -datadir=/opt/coins/data/litecoin_testnet/backend -conf=/opt/coins/nodes/litecoin_testnet/litecoin_testnet.conf -pid=/run/litecoind/litecoin_testnet.pid +# Creates /run/litecoind owned by litecoin +RuntimeDirectory=litecoind +User=litecoin +Type=forking +PIDFile=/run/litecoind/litecoin_testnet.pid +Restart=on-failure + +# Resource limits +LimitNOFILE=500000 + +# Hardening measures +#################### + +# Provide a private /tmp and /var/tmp. +PrivateTmp=true + +# Mount /usr, /boot/ and /etc read-only for the process. +ProtectSystem=full + +# Disallow the process and all of its children to gain +# new privileges through execve(). +NoNewPrivileges=true + +# Use a new /dev namespace only populated with API pseudo devices +# such as /dev/null, /dev/zero and /dev/random. +PrivateDevices=true + +# Deny the creation of writable and executable memory mappings. +MemoryDenyWriteExecute=true + +[Install] +WantedBy=multi-user.target diff --git a/contrib/backends/litecoin/debian/backend-litecoin.conffiles b/contrib/backends/litecoin/debian/backend-litecoin.conffiles new file mode 100644 index 00000000..9808ed97 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin.conffiles @@ -0,0 +1 @@ +/opt/coins/nodes/litecoin/litecoin.conf diff --git a/contrib/backends/litecoin/debian/backend-litecoin.dirs b/contrib/backends/litecoin/debian/backend-litecoin.dirs new file mode 100644 index 00000000..eae2c1f8 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin.dirs @@ -0,0 +1 @@ +/opt/coins/data/litecoin/backend diff --git a/contrib/backends/litecoin/debian/backend-litecoin.install b/contrib/backends/litecoin/debian/backend-litecoin.install new file mode 100644 index 00000000..8a70cf18 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin.install @@ -0,0 +1,2 @@ +litecoin/* /opt/coins/nodes/litecoin +litecoin.conf /opt/coins/nodes/litecoin diff --git a/contrib/backends/litecoin/debian/backend-litecoin.logrotate b/contrib/backends/litecoin/debian/backend-litecoin.logrotate new file mode 100644 index 00000000..70ac2c16 --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin.logrotate @@ -0,0 +1,10 @@ +/opt/coins/data/litecoin/backend/debug.log +/opt/coins/data/litecoin/backend/db.log +{ + rotate 7 + daily + compress + missingok + notifempty + copytruncate +} diff --git a/contrib/backends/litecoin/debian/backend-litecoin.postinst b/contrib/backends/litecoin/debian/backend-litecoin.postinst new file mode 100644 index 00000000..3f612fae --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin.postinst @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +case "$1" in + + configure) + if ! id -u litecoin &> /dev/null + then + useradd --system -M -U litecoin -s /bin/false + fi + + if [ "$(stat -c '%U' /opt/coins/data/litecoin/backend)" != "litecoin" ] + then + chown -R litecoin:litecoin /opt/coins/data/litecoin/backend + fi + ;; + +esac + +#DEBHELPER# diff --git a/contrib/backends/litecoin/debian/backend-litecoin.service b/contrib/backends/litecoin/debian/backend-litecoin.service new file mode 100644 index 00000000..d825703b --- /dev/null +++ b/contrib/backends/litecoin/debian/backend-litecoin.service @@ -0,0 +1,47 @@ +# It is not recommended to modify this file in-place, because it will +# be overwritten during package upgrades. If you want to add further +# options or overwrite existing ones then use +# $ systemctl edit litecoin.service +# See "man systemd.service" for details. + +# Note that almost all daemon options could be specified in +# /opt/coins/nodes/litecoin/litecoin.conf + +[Unit] +Description=litecoin daemon (mainnet) +After=network.target + +[Service] +ExecStart=/opt/coins/nodes/litecoin/bin/litecoind -datadir=/opt/coins/data/litecoin/backend -conf=/opt/coins/nodes/litecoin/litecoin.conf -pid=/run/litecoind/litecoin.pid +# Creates /run/litecoind owned by litecoin +RuntimeDirectory=litecoind +User=litecoin +Type=forking +PIDFile=/run/litecoind/litecoin.pid +Restart=on-failure + +# Resource limits +LimitNOFILE=500000 + +# Hardening measures +#################### + +# Provide a private /tmp and /var/tmp. +PrivateTmp=true + +# Mount /usr, /boot/ and /etc read-only for the process. +ProtectSystem=full + +# Disallow the process and all of its children to gain +# new privileges through execve(). +NoNewPrivileges=true + +# Use a new /dev namespace only populated with API pseudo devices +# such as /dev/null, /dev/zero and /dev/random. +PrivateDevices=true + +# Deny the creation of writable and executable memory mappings. +MemoryDenyWriteExecute=true + +[Install] +WantedBy=multi-user.target diff --git a/contrib/backends/litecoin/debian/changelog b/contrib/backends/litecoin/debian/changelog new file mode 100644 index 00000000..c0fb2d95 --- /dev/null +++ b/contrib/backends/litecoin/debian/changelog @@ -0,0 +1,5 @@ +litecoin (0.16.0-satoshilabs1) unstable; urgency=medium + + * Initial build + + -- Martin Bohm Mon, 11 Jun 2018 11:12:13 +0200 diff --git a/contrib/backends/litecoin/debian/compat b/contrib/backends/litecoin/debian/compat new file mode 100644 index 00000000..ec635144 --- /dev/null +++ b/contrib/backends/litecoin/debian/compat @@ -0,0 +1 @@ +9 diff --git a/contrib/backends/litecoin/debian/control b/contrib/backends/litecoin/debian/control new file mode 100644 index 00000000..5868d2f0 --- /dev/null +++ b/contrib/backends/litecoin/debian/control @@ -0,0 +1,16 @@ +Source: litecoin +Section: satoshilabs +Priority: optional +Maintainer: martin.bohm@satoshilabs.com +Build-Depends: debhelper, wget, tar, gzip, make, dh-systemd, dh-exec +Standards-Version: 3.9.5 + +Package: backend-litecoin +Architecture: amd64 +Depends: ${shlibs:Depends}, ${misc:Depends}, logrotate +Description: Satoshilabs packaged litecoin server + +Package: backend-litecoin-testnet +Architecture: amd64 +Depends: ${shlibs:Depends}, ${misc:Depends}, logrotate +Description: Satoshilabs packaged litecoin server diff --git a/contrib/backends/litecoin/debian/rules b/contrib/backends/litecoin/debian/rules new file mode 100755 index 00000000..f69489df --- /dev/null +++ b/contrib/backends/litecoin/debian/rules @@ -0,0 +1,11 @@ +#!/usr/bin/make -f + +DH_VERBOSE = 1 + +%: + dh $@ --with=systemd + +override_dh_systemd_start: + dh_systemd_start --no-start + +override_dh_installinit: diff --git a/contrib/backends/litecoin/litecoin.conf b/contrib/backends/litecoin/litecoin.conf new file mode 100644 index 00000000..a2f3c4b6 --- /dev/null +++ b/contrib/backends/litecoin/litecoin.conf @@ -0,0 +1,18 @@ +daemon=1 +server=1 +nolisten=1 +rpcuser=rpc +rpcpassword=rpc +rpcport=8034 +txindex=1 +whitelist=127.0.0.1 +rpcallowip=127.0.0.1 + +zmqpubhashtx=tcp://127.0.0.1:38334 +zmqpubhashblock=tcp://127.0.0.1:38334 + +rpcworkqueue=1100 +maxmempool=2000 +dbcache=1000 + + diff --git a/contrib/backends/litecoin/litecoin_testnet.conf b/contrib/backends/litecoin/litecoin_testnet.conf new file mode 100644 index 00000000..4671d80b --- /dev/null +++ b/contrib/backends/litecoin/litecoin_testnet.conf @@ -0,0 +1,15 @@ +daemon=1 +server=1 +testnet=1 +nolisten=1 +rpcuser=rpc +rpcpassword=rpc +rpcport=18033 +txindex=1 + +zmqpubhashtx=tcp://127.0.0.1:48333 +zmqpubhashblock=tcp://127.0.0.1:48333 + +rpcworkqueue=1100 +maxmempool=2000 +dbcache=1000 From 2add702c2ff3054316597eae41d826611eaea104 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Jun 2018 14:02:35 +0200 Subject: [PATCH 2/7] Add litecoin to backend makefile --- contrib/backends/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/backends/Makefile b/contrib/backends/Makefile index 0531302a..9e5ede01 100644 --- a/contrib/backends/Makefile +++ b/contrib/backends/Makefile @@ -1,4 +1,4 @@ -TARGETS = bitcoin zcash bcash ethereum bgold dash +TARGETS = bitcoin zcash bcash ethereum bgold dash litecoin IMAGE = blockbook-backend-build-deb NO_CACHE = false From 0f50cea533e11fc10cfa5479fbe9186555ee3608 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Jun 2018 14:06:29 +0200 Subject: [PATCH 3/7] Set litecoin_testnet backend ports --- contrib/backends/litecoin/litecoin_testnet.conf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/backends/litecoin/litecoin_testnet.conf b/contrib/backends/litecoin/litecoin_testnet.conf index 4671d80b..15af2933 100644 --- a/contrib/backends/litecoin/litecoin_testnet.conf +++ b/contrib/backends/litecoin/litecoin_testnet.conf @@ -4,11 +4,11 @@ testnet=1 nolisten=1 rpcuser=rpc rpcpassword=rpc -rpcport=18033 +rpcport=18034 txindex=1 -zmqpubhashtx=tcp://127.0.0.1:48333 -zmqpubhashblock=tcp://127.0.0.1:48333 +zmqpubhashtx=tcp://127.0.0.1:48334 +zmqpubhashblock=tcp://127.0.0.1:48334 rpcworkqueue=1100 maxmempool=2000 From 4d874eae9532be0bf8a795a2717a6616d0ff34da Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Jun 2018 14:53:26 +0200 Subject: [PATCH 4/7] Fix litecoin backend package parameters --- .../litecoin/debian/backend-litecoin-testnet.logrotate | 4 ++-- contrib/backends/litecoin/litecoin.conf | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate b/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate index a380acfd..373521f1 100644 --- a/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate +++ b/contrib/backends/litecoin/debian/backend-litecoin-testnet.logrotate @@ -1,5 +1,5 @@ -/opt/coins/data/litecoin_testnet/backend/testnet3/debug.log -/opt/coins/data/litecoin_testnet/backend/testnet3/db.log +/opt/coins/data/litecoin_testnet/backend/testnet4/debug.log +/opt/coins/data/litecoin_testnet/backend/testnet4/db.log { rotate 7 daily diff --git a/contrib/backends/litecoin/litecoin.conf b/contrib/backends/litecoin/litecoin.conf index a2f3c4b6..6e226084 100644 --- a/contrib/backends/litecoin/litecoin.conf +++ b/contrib/backends/litecoin/litecoin.conf @@ -6,7 +6,6 @@ rpcpassword=rpc rpcport=8034 txindex=1 whitelist=127.0.0.1 -rpcallowip=127.0.0.1 zmqpubhashtx=tcp://127.0.0.1:38334 zmqpubhashblock=tcp://127.0.0.1:38334 From 914a8140a9f0a473b0a642ca2c22d04dd733ab0e Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Jun 2018 18:02:25 +0200 Subject: [PATCH 5/7] Add litecoin blockbook implementation --- bchain/coins/blockchain.go | 3 + bchain/coins/litecoin/litecoinparser.go | 66 +++++++++++++++++++ bchain/coins/litecoin/litecoinrpc.go | 56 ++++++++++++++++ .../blockbook-litecoin-testnet.conffiles | 1 + .../blockbook-litecoin-testnet.cron.daily | 2 + .../debian/blockbook-litecoin-testnet.dirs | 2 + .../debian/blockbook-litecoin-testnet.install | 6 ++ .../debian/blockbook-litecoin-testnet.links | 2 + .../blockbook-litecoin-testnet.postinst | 23 +++++++ .../debian/blockbook-litecoin-testnet.service | 43 ++++++++++++ build/deb/debian/blockbook-litecoin.conffiles | 1 + .../deb/debian/blockbook-litecoin.cron.daily | 2 + build/deb/debian/blockbook-litecoin.dirs | 2 + build/deb/debian/blockbook-litecoin.install | 6 ++ build/deb/debian/blockbook-litecoin.links | 2 + build/deb/debian/blockbook-litecoin.postinst | 23 +++++++ build/deb/debian/blockbook-litecoin.service | 43 ++++++++++++ build/deb/debian/control | 10 +++ configs/litecoin.json | 12 ++++ configs/litecoin_testnet.json | 12 ++++ 20 files changed, 317 insertions(+) create mode 100644 bchain/coins/litecoin/litecoinparser.go create mode 100644 bchain/coins/litecoin/litecoinrpc.go create mode 100644 build/deb/debian/blockbook-litecoin-testnet.conffiles create mode 100644 build/deb/debian/blockbook-litecoin-testnet.cron.daily create mode 100644 build/deb/debian/blockbook-litecoin-testnet.dirs create mode 100755 build/deb/debian/blockbook-litecoin-testnet.install create mode 100644 build/deb/debian/blockbook-litecoin-testnet.links create mode 100644 build/deb/debian/blockbook-litecoin-testnet.postinst create mode 100644 build/deb/debian/blockbook-litecoin-testnet.service create mode 100644 build/deb/debian/blockbook-litecoin.conffiles create mode 100644 build/deb/debian/blockbook-litecoin.cron.daily create mode 100644 build/deb/debian/blockbook-litecoin.dirs create mode 100755 build/deb/debian/blockbook-litecoin.install create mode 100644 build/deb/debian/blockbook-litecoin.links create mode 100644 build/deb/debian/blockbook-litecoin.postinst create mode 100644 build/deb/debian/blockbook-litecoin.service create mode 100644 configs/litecoin.json create mode 100644 configs/litecoin_testnet.json diff --git a/bchain/coins/blockchain.go b/bchain/coins/blockchain.go index 0bcae649..ea995562 100644 --- a/bchain/coins/blockchain.go +++ b/bchain/coins/blockchain.go @@ -7,6 +7,7 @@ import ( "blockbook/bchain/coins/btg" "blockbook/bchain/coins/dash" "blockbook/bchain/coins/eth" + "blockbook/bchain/coins/litecoin" "blockbook/bchain/coins/zec" "blockbook/common" "context" @@ -35,6 +36,8 @@ func init() { blockChainFactories["Bgold"] = btg.NewBGoldRPC blockChainFactories["Dash"] = dash.NewDashRPC blockChainFactories["Dash Testnet"] = dash.NewDashRPC + blockChainFactories["Litecoin"] = litecoin.NewLitecoinRPC + blockChainFactories["Litecoin Testnet"] = litecoin.NewLitecoinRPC } // GetCoinNameFromConfig gets coin name from config file diff --git a/bchain/coins/litecoin/litecoinparser.go b/bchain/coins/litecoin/litecoinparser.go new file mode 100644 index 00000000..cc5be3e3 --- /dev/null +++ b/bchain/coins/litecoin/litecoinparser.go @@ -0,0 +1,66 @@ +package litecoin + +import ( + "blockbook/bchain/coins/btc" + + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/wire" +) + +const ( + MainnetMagic wire.BitcoinNet = 0xdbb6c0fb + TestnetMagic wire.BitcoinNet = 0xf1c8d2fd + RegtestMagic wire.BitcoinNet = 0xdab5bffa +) + +var ( + MainNetParams chaincfg.Params + TestNetParams chaincfg.Params + RegtestParams chaincfg.Params +) + +func init() { + MainNetParams = chaincfg.MainNetParams + MainNetParams.Net = MainnetMagic + MainNetParams.PubKeyHashAddrID = 48 + MainNetParams.ScriptHashAddrID = 5 + MainNetParams.Bech32HRPSegwit = "ltc" + + TestNetParams = chaincfg.TestNet3Params + TestNetParams.Net = TestnetMagic + TestNetParams.PubKeyHashAddrID = 111 + TestNetParams.ScriptHashAddrID = 196 + TestNetParams.Bech32HRPSegwit = "tltc" + + err := chaincfg.Register(&MainNetParams) + if err == nil { + err = chaincfg.Register(&TestNetParams) + } + if err != nil { + panic(err) + } +} + +// LitecoinParser handle +type LitecoinParser struct { + *btc.BitcoinParser +} + +// NewLitecoinParser returns new LitecoinParser instance +func NewLitecoinParser(params *chaincfg.Params, c *btc.Configuration) *LitecoinParser { + return &LitecoinParser{BitcoinParser: btc.NewBitcoinParser(params, c)} +} + +// GetChainParams contains network parameters for the main Litecoin network, +// the regression test Litecoin network, the test Litecoin network and +// the simulation test Litecoin network, in this order +func GetChainParams(chain string) *chaincfg.Params { + switch chain { + case "test": + return &TestNetParams + case "regtest": + return &RegtestParams + default: + return &MainNetParams + } +} diff --git a/bchain/coins/litecoin/litecoinrpc.go b/bchain/coins/litecoin/litecoinrpc.go new file mode 100644 index 00000000..8e650f0d --- /dev/null +++ b/bchain/coins/litecoin/litecoinrpc.go @@ -0,0 +1,56 @@ +package litecoin + +import ( + "blockbook/bchain" + "blockbook/bchain/coins/btc" + "encoding/json" + + "github.com/golang/glog" +) + +// LitecoinRPC is an interface to JSON-RPC bitcoind service. +type LitecoinRPC struct { + *btc.BitcoinRPC +} + +// NewLitecoinRPC returns new LitecoinRPC instance. +func NewLitecoinRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { + b, err := btc.NewBitcoinRPC(config, pushHandler) + if err != nil { + return nil, err + } + + s := &LitecoinRPC{ + b.(*btc.BitcoinRPC), + } + s.RPCMarshaler = btc.JSONMarshalerV2{} + + return s, nil +} + +// Initialize initializes LitecoinRPC instance. +func (b *LitecoinRPC) Initialize() error { + chainName, err := b.GetChainInfoAndInitializeMempool(b) + if err != nil { + return err + } + + glog.Info("Chain name ", chainName) + params := GetChainParams(chainName) + + // always create parser + b.Parser = NewLitecoinParser(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 +} diff --git a/build/deb/debian/blockbook-litecoin-testnet.conffiles b/build/deb/debian/blockbook-litecoin-testnet.conffiles new file mode 100644 index 00000000..37c23600 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.conffiles @@ -0,0 +1 @@ +/opt/coins/blockbook/litecoin_testnet/config/blockchaincfg.json diff --git a/build/deb/debian/blockbook-litecoin-testnet.cron.daily b/build/deb/debian/blockbook-litecoin-testnet.cron.daily new file mode 100644 index 00000000..dc020b29 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.cron.daily @@ -0,0 +1,2 @@ +#!/bin/sh +/opt/coins/blockbook/litecoin_testnet/bin/logrotate.sh diff --git a/build/deb/debian/blockbook-litecoin-testnet.dirs b/build/deb/debian/blockbook-litecoin-testnet.dirs new file mode 100644 index 00000000..13a2895e --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.dirs @@ -0,0 +1,2 @@ +/opt/coins/data/litecoin_testnet/blockbook +/opt/coins/blockbook/litecoin_testnet/logs diff --git a/build/deb/debian/blockbook-litecoin-testnet.install b/build/deb/debian/blockbook-litecoin-testnet.install new file mode 100755 index 00000000..be526a7c --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.install @@ -0,0 +1,6 @@ +#!/usr/bin/dh-exec +blockbook /opt/coins/blockbook/litecoin_testnet/bin +cert /opt/coins/blockbook/litecoin_testnet +static /opt/coins/blockbook/litecoin_testnet +configs/litecoin_testnet.json => /opt/coins/blockbook/litecoin_testnet/config/blockchaincfg.json +logrotate.sh /opt/coins/blockbook/litecoin_testnet/bin diff --git a/build/deb/debian/blockbook-litecoin-testnet.links b/build/deb/debian/blockbook-litecoin-testnet.links new file mode 100644 index 00000000..fbc8ec69 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.links @@ -0,0 +1,2 @@ +/opt/coins/blockbook/litecoin_testnet/cert/testcert.crt /opt/coins/blockbook/litecoin_testnet/cert/blockbook.crt +/opt/coins/blockbook/litecoin_testnet/cert/testcert.key /opt/coins/blockbook/litecoin_testnet/cert/blockbook.key diff --git a/build/deb/debian/blockbook-litecoin-testnet.postinst b/build/deb/debian/blockbook-litecoin-testnet.postinst new file mode 100644 index 00000000..97600e76 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.postinst @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +case "$1" in + + configure) + if ! id -u blockbook-litecoin &> /dev/null + then + useradd --system -M -U blockbook-litecoin -s /bin/false + fi + + for dir in /opt/coins/data/litecoin_testnet/blockbook /opt/coins/blockbook/litecoin_testnet/logs + do + if [ "$(stat -c '%U' $dir)" != "blockbook-litecoin" ] + then + chown -R blockbook-litecoin:blockbook-litecoin $dir + fi + done + ;; + +esac + +#DEBHELPER# diff --git a/build/deb/debian/blockbook-litecoin-testnet.service b/build/deb/debian/blockbook-litecoin-testnet.service new file mode 100644 index 00000000..545d8203 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin-testnet.service @@ -0,0 +1,43 @@ +# It is not recommended to modify this file in-place, because it will +# be overwritten during package upgrades. If you want to add further +# options or overwrite existing ones then use +# $ systemctl edit blockbook-litecoin-testnet.service +# See "man systemd.service" for details. + +[Unit] +Description=Blockbook daemon (Litecoin testnet) +After=network.target +Wants=backend-litecoin-testnet.service + +[Service] +ExecStart=/opt/coins/blockbook/litecoin_testnet/bin/blockbook -blockchaincfg=/opt/coins/blockbook/litecoin_testnet/config/blockchaincfg.json -datadir=/opt/coins/data/litecoin_testnet/blockbook/db -sync -httpserver=:19033 -socketio=:19133 -certfile=/opt/coins/blockbook/litecoin_testnet/cert/blockbook -explorer=https://ltc-explorer.trezor.io/ -log_dir=/opt/coins/blockbook/litecoin_testnet/logs +User=blockbook-litecoin +Type=simple +Restart=on-failure +WorkingDirectory=/opt/coins/blockbook/litecoin_testnet + +# Resource limits +LimitNOFILE=500000 + +# Hardening measures +#################### + +# Provide a private /tmp and /var/tmp. +PrivateTmp=true + +# Mount /usr, /boot/ and /etc read-only for the process. +ProtectSystem=full + +# Disallow the process and all of its children to gain +# new privileges through execve(). +NoNewPrivileges=true + +# Use a new /dev namespace only populated with API pseudo devices +# such as /dev/null, /dev/zero and /dev/random. +PrivateDevices=true + +# Deny the creation of writable and executable memory mappings. +MemoryDenyWriteExecute=true + +[Install] +WantedBy=multi-user.target diff --git a/build/deb/debian/blockbook-litecoin.conffiles b/build/deb/debian/blockbook-litecoin.conffiles new file mode 100644 index 00000000..bd1d3b15 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.conffiles @@ -0,0 +1 @@ +/opt/coins/blockbook/litecoin/config/blockchaincfg.json diff --git a/build/deb/debian/blockbook-litecoin.cron.daily b/build/deb/debian/blockbook-litecoin.cron.daily new file mode 100644 index 00000000..17534506 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.cron.daily @@ -0,0 +1,2 @@ +#!/bin/sh +/opt/coins/blockbook/litecoin/bin/logrotate.sh diff --git a/build/deb/debian/blockbook-litecoin.dirs b/build/deb/debian/blockbook-litecoin.dirs new file mode 100644 index 00000000..64a24800 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.dirs @@ -0,0 +1,2 @@ +/opt/coins/data/litecoin/blockbook +/opt/coins/blockbook/litecoin/logs diff --git a/build/deb/debian/blockbook-litecoin.install b/build/deb/debian/blockbook-litecoin.install new file mode 100755 index 00000000..cbde7919 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.install @@ -0,0 +1,6 @@ +#!/usr/bin/dh-exec +blockbook /opt/coins/blockbook/litecoin/bin +cert /opt/coins/blockbook/litecoin +static /opt/coins/blockbook/litecoin +configs/litecoin.json => /opt/coins/blockbook/litecoin/config/blockchaincfg.json +logrotate.sh /opt/coins/blockbook/litecoin/bin diff --git a/build/deb/debian/blockbook-litecoin.links b/build/deb/debian/blockbook-litecoin.links new file mode 100644 index 00000000..412f2c9d --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.links @@ -0,0 +1,2 @@ +/opt/coins/blockbook/litecoin/cert/testcert.crt /opt/coins/blockbook/litecoin/cert/blockbook.crt +/opt/coins/blockbook/litecoin/cert/testcert.key /opt/coins/blockbook/litecoin/cert/blockbook.key diff --git a/build/deb/debian/blockbook-litecoin.postinst b/build/deb/debian/blockbook-litecoin.postinst new file mode 100644 index 00000000..de2f7ecc --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.postinst @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +case "$1" in + + configure) + if ! id -u blockbook-litecoin &> /dev/null + then + useradd --system -M -U blockbook-litecoin -s /bin/false + fi + + for dir in /opt/coins/data/litecoin/blockbook /opt/coins/blockbook/litecoin/logs + do + if [ "$(stat -c '%U' $dir)" != "blockbook-litecoin" ] + then + chown -R blockbook-litecoin:blockbook-litecoin $dir + fi + done + ;; + +esac + +#DEBHELPER# diff --git a/build/deb/debian/blockbook-litecoin.service b/build/deb/debian/blockbook-litecoin.service new file mode 100644 index 00000000..dde7c0a4 --- /dev/null +++ b/build/deb/debian/blockbook-litecoin.service @@ -0,0 +1,43 @@ +# It is not recommended to modify this file in-place, because it will +# be overwritten during package upgrades. If you want to add further +# options or overwrite existing ones then use +# $ systemctl edit blockbook-litecoin.service +# See "man systemd.service" for details. + +[Unit] +Description=Blockbook daemon (Litecoin mainnet) +After=network.target +Wants=backend-litecoin.service + +[Service] +ExecStart=/opt/coins/blockbook/litecoin/bin/blockbook -blockchaincfg=/opt/coins/blockbook/litecoin/config/blockchaincfg.json -datadir=/opt/coins/data/litecoin/blockbook/db -sync -httpserver=:9033 -socketio=:9133 -certfile=/opt/coins/blockbook/litecoin/cert/blockbook -explorer=https://ltc-explorer.trezor.io/ -log_dir=/opt/coins/blockbook/litecoin/logs +User=blockbook-litecoin +Type=simple +Restart=on-failure +WorkingDirectory=/opt/coins/blockbook/litecoin + +# Resource limits +LimitNOFILE=500000 + +# Hardening measures +#################### + +# Provide a private /tmp and /var/tmp. +PrivateTmp=true + +# Mount /usr, /boot/ and /etc read-only for the process. +ProtectSystem=full + +# Disallow the process and all of its children to gain +# new privileges through execve(). +NoNewPrivileges=true + +# Use a new /dev namespace only populated with API pseudo devices +# such as /dev/null, /dev/zero and /dev/random. +PrivateDevices=true + +# Deny the creation of writable and executable memory mappings. +MemoryDenyWriteExecute=true + +[Install] +WantedBy=multi-user.target diff --git a/build/deb/debian/control b/build/deb/debian/control index 1ec0e178..214557a7 100644 --- a/build/deb/debian/control +++ b/build/deb/debian/control @@ -54,3 +54,13 @@ Package: blockbook-dash-testnet Architecture: amd64 Depends: ${shlibs:Depends}, ${misc:Depends}, coreutils, passwd, findutils, psmisc, backend-dash-testnet Description: Satoshilabs blockbook server (Dash testnet) + +Package: blockbook-litecoin +Architecture: amd64 +Depends: ${shlibs:Depends}, ${misc:Depends}, coreutils, passwd, findutils, psmisc, backend-litecoin +Description: Satoshilabs blockbook server (Litecoin mainnet) + +Package: blockbook-litecoin-testnet +Architecture: amd64 +Depends: ${shlibs:Depends}, ${misc:Depends}, coreutils, passwd, findutils, psmisc, backend-litecoin-testnet +Description: Satoshilabs blockbook server (Litecoin testnet) diff --git a/configs/litecoin.json b/configs/litecoin.json new file mode 100644 index 00000000..2a351101 --- /dev/null +++ b/configs/litecoin.json @@ -0,0 +1,12 @@ +{ + "coin_name": "Litecoin", + "rpcURL": "http://localhost:8034", + "rpcUser": "rpc", + "rpcPass": "rpc", + "rpcTimeout": 25, + "parse": true, + "zeroMQBinding": "tcp://localhost:38334", + "mempoolWorkers": 8, + "mempoolSubWorkers": 2, + "blockAddressesToKeep": 300 +} diff --git a/configs/litecoin_testnet.json b/configs/litecoin_testnet.json new file mode 100644 index 00000000..67232ebb --- /dev/null +++ b/configs/litecoin_testnet.json @@ -0,0 +1,12 @@ +{ + "coin_name": "Litecoin Testnet", + "rpcURL": "http://localhost:18034", + "rpcUser": "rpc", + "rpcPass": "rpc", + "rpcTimeout": 25, + "parse": true, + "zeroMQBinding": "tcp://localhost:48334", + "mempoolWorkers": 8, + "mempoolSubWorkers": 2, + "blockAddressesToKeep": 300 +} From 6e88c8970ab8e3dc9de0eebdf7af5f8298d61954 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Jun 2018 22:21:50 +0200 Subject: [PATCH 6/7] Fix litecoin blockbook ports --- build/deb/debian/blockbook-litecoin-testnet.service | 2 +- build/deb/debian/blockbook-litecoin.service | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build/deb/debian/blockbook-litecoin-testnet.service b/build/deb/debian/blockbook-litecoin-testnet.service index 545d8203..b2764b2c 100644 --- a/build/deb/debian/blockbook-litecoin-testnet.service +++ b/build/deb/debian/blockbook-litecoin-testnet.service @@ -10,7 +10,7 @@ After=network.target Wants=backend-litecoin-testnet.service [Service] -ExecStart=/opt/coins/blockbook/litecoin_testnet/bin/blockbook -blockchaincfg=/opt/coins/blockbook/litecoin_testnet/config/blockchaincfg.json -datadir=/opt/coins/data/litecoin_testnet/blockbook/db -sync -httpserver=:19033 -socketio=:19133 -certfile=/opt/coins/blockbook/litecoin_testnet/cert/blockbook -explorer=https://ltc-explorer.trezor.io/ -log_dir=/opt/coins/blockbook/litecoin_testnet/logs +ExecStart=/opt/coins/blockbook/litecoin_testnet/bin/blockbook -blockchaincfg=/opt/coins/blockbook/litecoin_testnet/config/blockchaincfg.json -datadir=/opt/coins/data/litecoin_testnet/blockbook/db -sync -httpserver=:19034 -socketio=:19134 -certfile=/opt/coins/blockbook/litecoin_testnet/cert/blockbook -explorer=https://ltc-explorer.trezor.io/ -log_dir=/opt/coins/blockbook/litecoin_testnet/logs User=blockbook-litecoin Type=simple Restart=on-failure diff --git a/build/deb/debian/blockbook-litecoin.service b/build/deb/debian/blockbook-litecoin.service index dde7c0a4..9833d60d 100644 --- a/build/deb/debian/blockbook-litecoin.service +++ b/build/deb/debian/blockbook-litecoin.service @@ -10,7 +10,7 @@ After=network.target Wants=backend-litecoin.service [Service] -ExecStart=/opt/coins/blockbook/litecoin/bin/blockbook -blockchaincfg=/opt/coins/blockbook/litecoin/config/blockchaincfg.json -datadir=/opt/coins/data/litecoin/blockbook/db -sync -httpserver=:9033 -socketio=:9133 -certfile=/opt/coins/blockbook/litecoin/cert/blockbook -explorer=https://ltc-explorer.trezor.io/ -log_dir=/opt/coins/blockbook/litecoin/logs +ExecStart=/opt/coins/blockbook/litecoin/bin/blockbook -blockchaincfg=/opt/coins/blockbook/litecoin/config/blockchaincfg.json -datadir=/opt/coins/data/litecoin/blockbook/db -sync -httpserver=:9034 -socketio=:9134 -certfile=/opt/coins/blockbook/litecoin/cert/blockbook -explorer=https://ltc-explorer.trezor.io/ -log_dir=/opt/coins/blockbook/litecoin/logs User=blockbook-litecoin Type=simple Restart=on-failure From 7d8c493f215162a7d588b8f07c837d841cf9ac38 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 12 Jun 2018 12:35:52 +0200 Subject: [PATCH 7/7] Add litecoin parser tests --- bchain/coins/litecoin/litecoinparser.go | 10 +- bchain/coins/litecoin/litecoinparser_test.go | 277 +++++++++++++++++++ 2 files changed, 280 insertions(+), 7 deletions(-) create mode 100644 bchain/coins/litecoin/litecoinparser_test.go diff --git a/bchain/coins/litecoin/litecoinparser.go b/bchain/coins/litecoin/litecoinparser.go index cc5be3e3..3cb06f34 100644 --- a/bchain/coins/litecoin/litecoinparser.go +++ b/bchain/coins/litecoin/litecoinparser.go @@ -16,20 +16,19 @@ const ( var ( MainNetParams chaincfg.Params TestNetParams chaincfg.Params - RegtestParams chaincfg.Params ) func init() { MainNetParams = chaincfg.MainNetParams MainNetParams.Net = MainnetMagic MainNetParams.PubKeyHashAddrID = 48 - MainNetParams.ScriptHashAddrID = 5 + MainNetParams.ScriptHashAddrID = 50 MainNetParams.Bech32HRPSegwit = "ltc" TestNetParams = chaincfg.TestNet3Params TestNetParams.Net = TestnetMagic TestNetParams.PubKeyHashAddrID = 111 - TestNetParams.ScriptHashAddrID = 196 + TestNetParams.ScriptHashAddrID = 58 TestNetParams.Bech32HRPSegwit = "tltc" err := chaincfg.Register(&MainNetParams) @@ -52,14 +51,11 @@ func NewLitecoinParser(params *chaincfg.Params, c *btc.Configuration) *LitecoinP } // GetChainParams contains network parameters for the main Litecoin network, -// the regression test Litecoin network, the test Litecoin network and -// the simulation test Litecoin network, in this order +// and the test Litecoin network func GetChainParams(chain string) *chaincfg.Params { switch chain { case "test": return &TestNetParams - case "regtest": - return &RegtestParams default: return &MainNetParams } diff --git a/bchain/coins/litecoin/litecoinparser_test.go b/bchain/coins/litecoin/litecoinparser_test.go new file mode 100644 index 00000000..2d8cd90e --- /dev/null +++ b/bchain/coins/litecoin/litecoinparser_test.go @@ -0,0 +1,277 @@ +package litecoin + +import ( + "blockbook/bchain" + "blockbook/bchain/coins/btc" + "encoding/hex" + "reflect" + "testing" +) + +func TestAddressToOutputScript_Testnet(t *testing.T) { + type args struct { + address string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "P2PKH1", + args: args{address: "mgPdTgEq6YqUJ4yzQgR8jH5TCX5c5yRwCP"}, + want: "76a91409957dfdb3eb620a94b99857e13949551584c33688ac", + wantErr: false, + }, + { + name: "P2SH1", + args: args{address: "2MvGVySztevmycxrSmMRjJaVj2iJin7qpap"}, + want: "a9142126232e3f47ae0f1246ec5f05fc400d83c86a0d87", + wantErr: false, + }, + { + name: "P2SH2", + args: args{address: "2N9a2TNzWz1FEKGFxUdMEh62V83URdZ5QAZ"}, + want: "a914b31049e7ee51501fe19e3e0cdb803dc84cf99f9e87", + wantErr: false, + }, + } + parser := NewLitecoinParser(GetChainParams("test"), &btc.Configuration{}) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parser.AddressToOutputScript(tt.args.address) + if (err != nil) != tt.wantErr { + t.Errorf("AddressToOutputScript() error = %v, wantErr %v", err, tt.wantErr) + return + } + h := hex.EncodeToString(got) + if !reflect.DeepEqual(h, tt.want) { + t.Errorf("AddressToOutputScript() = %v, want %v", h, tt.want) + } + }) + } +} + +func TestAddressToOutputScript_Mainnet(t *testing.T) { + type args struct { + address string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "P2PKH1", + args: args{address: "LgJGe7aKy1wfXESKhiKeRWj6z4KjzCfXNW"}, + want: "76a914e72ba56ab6afccac045d696b979e3b5077e88d1988ac", + wantErr: false, + }, + { + name: "P2PKH2", + args: args{address: "LiTVReQ6N8rWc2pNg2XMwCWq7A9P15teWg"}, + want: "76a914feda50542e61108cf53b93dbffa0959f91ccb32588ac", + wantErr: false, + }, + { + name: "P2SH1", + args: args{address: "MLTQ8niHMnpJLNvK72zBeY91hQmUtoo8nX"}, + want: "a91489ba6cf45546f91f1bdf553e695d63fc6b8795bd87", + wantErr: false, + }, + { + name: "P2SH2", + args: args{address: "MAVWzxXm8KGkZTesqLtqywzrvbs96FEoKy"}, + want: "a9141c6fbaf46d64221e80cbae182c33ddf81b9294ac87", + wantErr: false, + }, + { + name: "witness_v0_keyhash", + args: args{address: "ltc1q5fgkuac9s2ry56jka5s6zqsyfcugcchrqgz2yl"}, + want: "0014a2516e770582864a6a56ed21a102044e388c62e3", + wantErr: false, + }, + { + name: "witness_v0_scripthashx", + args: args{address: "ltc1qu9dgdg330r6r84g5mw7wqshg04exv2uttmw2elfwx74h5tgntuzsk3x5nd"}, + want: "0020e15a86a23178f433d514dbbce042e87d72662b8b5edcacfd2e37ab7a2d135f05", + wantErr: false, + }, + } + parser := NewLitecoinParser(GetChainParams("main"), &btc.Configuration{}) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parser.AddressToOutputScript(tt.args.address) + if (err != nil) != tt.wantErr { + t.Errorf("AddressToOutputScript() error = %v, wantErr %v", err, tt.wantErr) + return + } + h := hex.EncodeToString(got) + if !reflect.DeepEqual(h, tt.want) { + t.Errorf("AddressToOutputScript() = %v, want %v", h, tt.want) + } + }) + } +} + +var ( + testTx1 bchain.Tx + + testTxPacked1 = "0001e4538ba8d7aa2002000000031e1977dc524bec5929e95d8d0946812944b7b5bda12f5b99fdf557773f2ee65e0100000000ffffffff8a398e44546dce0245452b90130e86832b21fd68f26662bc33aeb7c6c115d23c1900000000ffffffffb807ab93a7fcdff7af6d24581a4a18aa7c1db1ebecba2617a6805b009513940f0c00000000ffffffff020001a04a000000001976a9141ae882e788091732da6910595314447c9e38bd8d88ac27440f00000000001976a9146b474cbf0f6004329b630bdd4798f2c23d1751b688ac00000000" +) + +func init() { + var ( + addr1, addr2 bchain.Address + err error + ) + addr1, err = bchain.NewBaseAddress("LMgENNXzzuPxp7vfMjDrCU44bsmrEMgqvc") + if err == nil { + addr2, err = bchain.NewBaseAddress("LV1ByjbJNFTHyFQqwqwdJXKJznYDzXzg4B") + } + if err != nil { + panic(err) + } + + testTx1 = bchain.Tx{ + Hex: "02000000031e1977dc524bec5929e95d8d0946812944b7b5bda12f5b99fdf557773f2ee65e0100000000ffffffff8a398e44546dce0245452b90130e86832b21fd68f26662bc33aeb7c6c115d23c1900000000ffffffffb807ab93a7fcdff7af6d24581a4a18aa7c1db1ebecba2617a6805b009513940f0c00000000ffffffff020001a04a000000001976a9141ae882e788091732da6910595314447c9e38bd8d88ac27440f00000000001976a9146b474cbf0f6004329b630bdd4798f2c23d1751b688ac00000000", + Blocktime: 1519053456, + Txid: "1c50c1770374d7de2f81a87463a5225bb620d25fd467536223a5b715a47c9e32", + LockTime: 0, + Vin: []bchain.Vin{ + { + ScriptSig: bchain.ScriptSig{ + Hex: "", + }, + Txid: "5ee62e3f7757f5fd995b2fa1bdb5b744298146098d5de92959ec4b52dc77191e", + Vout: 1, + Sequence: 4294967295, + }, + { + ScriptSig: bchain.ScriptSig{ + Hex: "", + }, + Txid: "3cd215c1c6b7ae33bc6266f268fd212b83860e13902b454502ce6d54448e398a", + Vout: 25, + Sequence: 4294967295, + }, + { + ScriptSig: bchain.ScriptSig{ + Hex: "", + }, + Txid: "0f941395005b80a61726baecebb11d7caa184a1a58246daff7dffca793ab07b8", + Vout: 12, + Sequence: 4294967295, + }, + }, + Vout: []bchain.Vout{ + { + Value: 12.52000000, + N: 0, + ScriptPubKey: bchain.ScriptPubKey{ + Hex: "76a9141ae882e788091732da6910595314447c9e38bd8d88ac", + Addresses: []string{ + "LMgENNXzzuPxp7vfMjDrCU44bsmrEMgqvc", + }, + }, + Address: addr1, + }, + { + Value: 0.01000487, + N: 1, + ScriptPubKey: bchain.ScriptPubKey{ + Hex: "76a9146b474cbf0f6004329b630bdd4798f2c23d1751b688ac", + Addresses: []string{ + "LV1ByjbJNFTHyFQqwqwdJXKJznYDzXzg4B", + }, + }, + Address: addr2, + }, + }, + } +} + +func Test_PackTx(t *testing.T) { + type args struct { + tx bchain.Tx + height uint32 + blockTime int64 + parser *LitecoinParser + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "litecoin-1", + args: args{ + tx: testTx1, + height: 123987, + blockTime: 1519053456, + parser: NewLitecoinParser(GetChainParams("main"), &btc.Configuration{}), + }, + want: testTxPacked1, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime) + if (err != nil) != tt.wantErr { + t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr) + return + } + h := hex.EncodeToString(got) + if !reflect.DeepEqual(h, tt.want) { + t.Errorf("packTx() = %v, want %v", h, tt.want) + } + }) + } +} + +func Test_UnpackTx(t *testing.T) { + type args struct { + packedTx string + parser *LitecoinParser + } + tests := []struct { + name string + args args + want *bchain.Tx + want1 uint32 + wantErr bool + }{ + { + name: "litecoin-1", + args: args{ + packedTx: testTxPacked1, + parser: NewLitecoinParser(GetChainParams("main"), &btc.Configuration{}), + }, + want: &testTx1, + want1: 123987, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b, _ := hex.DecodeString(tt.args.packedTx) + got, got1, err := tt.args.parser.UnpackTx(b) + if (err != nil) != tt.wantErr { + t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("unpackTx() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1) + } + }) + } +}