From b19371127630ecbc00c2300abcecf3ed22ecceda Mon Sep 17 00:00:00 2001 From: Thomas Zarebczan Date: Fri, 22 Jan 2021 01:16:08 -0500 Subject: [PATCH] Add LBRY Support Been chatting with the Ren team on Telegram and we are very eager to add support for renLBC! We're a fork of Bitcoin Core, so integration should be fairly straightforward. Note: Rebased and made updates to LBRY params on 1/20/2021. Happy New Year Ren Team, looking forward to working together. --- chain/lbry/address.go | 14 +++ chain/lbry/address_test.go | 1 + chain/lbry/gas.go | 9 ++ chain/lbry/gas_test.go | 52 ++++++++++ chain/lbry/lbry.go | 177 ++++++++++++++++++++++++++++++++++ chain/lbry/lbry_suite_test.go | 13 +++ chain/lbry/lbry_test.go | 146 ++++++++++++++++++++++++++++ chain/lbry/utxo.go | 32 ++++++ chain/lbry/utxo_test.go | 1 + infra/.env | 10 ++ infra/docker-compose.yaml | 12 +++ infra/lbry/Dockerfile | 16 +++ infra/lbry/README.md | 22 +++++ infra/lbry/keygen.go | 24 +++++ infra/lbry/lbrycrd.conf | 10 ++ infra/lbry/run.sh | 22 +++++ multichain.go | 10 +- multichain_test.go | 21 ++++ 18 files changed, 590 insertions(+), 2 deletions(-) create mode 100644 chain/lbry/address.go create mode 100644 chain/lbry/address_test.go create mode 100644 chain/lbry/gas.go create mode 100644 chain/lbry/gas_test.go create mode 100644 chain/lbry/lbry.go create mode 100644 chain/lbry/lbry_suite_test.go create mode 100644 chain/lbry/lbry_test.go create mode 100644 chain/lbry/utxo.go create mode 100644 chain/lbry/utxo_test.go create mode 100644 infra/lbry/Dockerfile create mode 100644 infra/lbry/README.md create mode 100644 infra/lbry/keygen.go create mode 100644 infra/lbry/lbrycrd.conf create mode 100644 infra/lbry/run.sh diff --git a/chain/lbry/address.go b/chain/lbry/address.go new file mode 100644 index 00000000..403f6208 --- /dev/null +++ b/chain/lbry/address.go @@ -0,0 +1,14 @@ +package lbry + +import "github.com/renproject/multichain/chain/bitcoin" + +type ( + // AddressEncoder re-exports bitcoin.AddressEncoder. + AddressEncoder = bitcoin.AddressEncoder + + // AddressDecoder re-exports bitcoin.AddressDecoder. + AddressDecoder = bitcoin.AddressDecoder + + // AddressEncodeDecoder re-exports bitcoin.AddressEncodeDecoder. + AddressEncodeDecoder = bitcoin.AddressEncodeDecoder +) diff --git a/chain/lbry/address_test.go b/chain/lbry/address_test.go new file mode 100644 index 00000000..8423d735 --- /dev/null +++ b/chain/lbry/address_test.go @@ -0,0 +1 @@ +package lbry_test diff --git a/chain/lbry/gas.go b/chain/lbry/gas.go new file mode 100644 index 00000000..039b47b8 --- /dev/null +++ b/chain/lbry/gas.go @@ -0,0 +1,9 @@ +package lbry + +import "github.com/renproject/multichain/chain/bitcoin" + +// GasEstimator re-exports bitcoin.GasEstimator. +type GasEstimator = bitcoin.GasEstimator + +// NewGasEstimator re-exports bitcoin.NewGasEstimator. +var NewGasEstimator = bitcoin.NewGasEstimator diff --git a/chain/lbry/gas_test.go b/chain/lbry/gas_test.go new file mode 100644 index 00000000..afcdd2d2 --- /dev/null +++ b/chain/lbry/gas_test.go @@ -0,0 +1,52 @@ +package lbry_test + +import ( + "context" + + "github.com/renproject/multichain/chain/lbry" + "github.com/renproject/pack" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Gas", func() { + Context("when estimating LBRY network fee", func() { + It("should work", func() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + client := lbry.NewClient(lbry.DefaultClientOptions()) + + // estimate fee to include tx within 1 block. + fallback1 := uint64(123) + gasEstimator1 := lbry.NewGasEstimator(client, 1, pack.NewU256FromUint64(fallback1)) + gasPrice1, _, err := gasEstimator1.EstimateGas(ctx) + if err != nil { + Expect(gasPrice1).To(Equal(pack.NewU256FromUint64(fallback1))) + } + + // estimate fee to include tx within 10 blocks. + fallback2 := uint64(234) + gasEstimator2 := lbry.NewGasEstimator(client, 10, pack.NewU256FromUint64(fallback2)) + gasPrice2, _, err := gasEstimator2.EstimateGas(ctx) + if err != nil { + Expect(gasPrice2).To(Equal(pack.NewU256FromUint64(fallback2))) + } + + // estimate fee to include tx within 100 blocks. + fallback3 := uint64(345) + gasEstimator3 := lbry.NewGasEstimator(client, 100, pack.NewU256FromUint64(fallback3)) + gasPrice3, _, err := gasEstimator3.EstimateGas(ctx) + if err != nil { + Expect(gasPrice3).To(Equal(pack.NewU256FromUint64(fallback3))) + } + + // expect fees in this order at the very least. + if err == nil { + Expect(gasPrice1.GreaterThanEqual(gasPrice2)).To(BeTrue()) + Expect(gasPrice2.GreaterThanEqual(gasPrice3)).To(BeTrue()) + } + }) + }) +}) diff --git a/chain/lbry/lbry.go b/chain/lbry/lbry.go new file mode 100644 index 00000000..ddc50dda --- /dev/null +++ b/chain/lbry/lbry.go @@ -0,0 +1,177 @@ +package lbry + +import ( + "time" + + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/wire" +) + +func init() { + if err := chaincfg.Register(&MainNetParams); err != nil { + panic(err) + } + if err := chaincfg.Register(&TestNetParams); err != nil { + panic(err) + } + if err := chaincfg.Register(&RegressionNetParams); err != nil { + panic(err) + } +} + +// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for +// the main network, regression test network, and test network (version 3). +var genesisCoinbaseTx = wire.MsgTx{ + Version: 1, + TxIn: []*wire.TxIn{ + { + PreviousOutPoint: wire.OutPoint{ + Hash: chainhash.Hash{}, + Index: 0xffffffff, + }, + SignatureScript: []byte{ + 0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x17, + 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x74, + 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, + 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, + }, + Sequence: 0xffffffff, + }, + }, + TxOut: []*wire.TxOut{ + { + Value: 0x12a05f200, + PkScript: []byte{ + 0x76, 0xa9, 0x14, 0x34, 0x59, 0x91, 0xdb, 0xf5, + 0x7b, 0xfb, 0x01, 0x4b, 0x87, 0x00, 0x6a, 0xcd, + 0xfa, 0xfb, 0xfc, 0x5f, 0xe8, 0x29, 0x2f, 0x88, + 0xac, + }, + }, + }, + LockTime: 0, +} + +// https://github.com/lbryio/lbrycrd/blob/master/src/chainparams.cpp#L19 +var genesisMerkleRoot = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy. + 0xcc, 0x59, 0xe5, 0x9f, 0xf9, 0x7a, 0xc0, 0x92, + 0xb5, 0x5e, 0x42, 0x3a, 0xa5, 0x49, 0x51, 0x51, + 0xed, 0x6f, 0xb8, 0x05, 0x70, 0xa5, 0xbb, 0x78, + 0xcd, 0x5b, 0xd1, 0xc3, 0x82, 0x1c, 0x21, 0xb8, +}) + +var genesisBlock = wire.MsgBlock{ + Header: wire.BlockHeader{ + Version: 1, + PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + MerkleRoot: genesisMerkleRoot, // b8211c82c3d15bcd78bba57005b86fed515149a53a425eb592c07af99fe559cc + Timestamp: time.Unix(1446058291, 0), // Wednesday, October 28, 2015 6:51:31 PM GMT + Bits: 0x1f00ffff, + Nonce: 1287, + }, + Transactions: []*wire.MsgTx{&genesisCoinbaseTx}, +} + +var genesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy. + 0x63, 0xf4, 0x34, 0x6a, 0x4d, 0xb3, 0x4f, 0xdf, + 0xce, 0x29, 0xa7, 0x0f, 0x5e, 0x8d, 0x11, 0xf0, + 0x65, 0xf6, 0xb9, 0x16, 0x02, 0xb7, 0x03, 0x6c, + 0x7f, 0x22, 0xf3, 0xa0, 0x3b, 0x28, 0x89, 0x9c, +}) + +func newHashFromStr(hexStr string) *chainhash.Hash { + hash, err := chainhash.NewHashFromStr(hexStr) + if err != nil { + panic(err) + } + return hash +} + +// MainNetParams returns the chain configuration for mainnet. +var MainNetParams = chaincfg.Params{ + Name: "mainnet", + Net: 0xfae4aaf1, + DefaultPort: "9246", + + // Chain parameters + GenesisBlock: &genesisBlock, + GenesisHash: &genesisHash, + + // Address encoding magics + PubKeyHashAddrID: 85, + ScriptHashAddrID: 122, + PrivateKeyID: 28, + WitnessPubKeyHashAddrID: 0x06, // starts with p2 + WitnessScriptHashAddrID: 0x0A, // starts with 7Xh + BIP0034Height: 1, + BIP0065Height: 200000, + BIP0066Height: 200000, + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv + HDPublicKeyID: [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub + + // Human-readable part for Bech32 encoded segwit addresses, as defined in + // BIP 173. + Bech32HRPSegwit: "lbc", + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 0x8c, +} + +// TestNetParams returns the chain configuration for testnet. +var TestNetParams = chaincfg.Params{ + Name: "testnet", + Net: 0xfae4aae1, + DefaultPort: "19246", + + // Chain parameters + GenesisBlock: &genesisBlock, + GenesisHash: &genesisHash, + + // Address encoding magics + PubKeyHashAddrID: 111, + ScriptHashAddrID: 196, + PrivateKeyID: 239, + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with xprv + HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with xpub + + // Human-readable part for Bech32 encoded segwit addresses, as defined in + // BIP 173. + Bech32HRPSegwit: "tlbc", + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 0x8c, +} + +// RegressionNetParams returns the chain configuration for regression net. +var RegressionNetParams = chaincfg.Params{ + Name: "regtest", + + // LBRY has 0xfae4aad1 as RegTest (same as Bitcoin's RegTest). + // Setting it to an arbitrary value (leet_hex(LBRY)), so that we can + // register the regtest network. + Net: 0xfae4aad1, + + // Address encoding magics + PubKeyHashAddrID: 111, + ScriptHashAddrID: 196, + PrivateKeyID: 239, + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with xprv + HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with xpub + + // Human-readable part for Bech32 encoded segwit addresses, as defined in + // BIP 173. + Bech32HRPSegwit: "rlbc", + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 0x8c, +} diff --git a/chain/lbry/lbry_suite_test.go b/chain/lbry/lbry_suite_test.go new file mode 100644 index 00000000..82c8fddd --- /dev/null +++ b/chain/lbry/lbry_suite_test.go @@ -0,0 +1,13 @@ +package lbry_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestLBRY(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "LBRY Suite") +} diff --git a/chain/lbry/lbry_test.go b/chain/lbry/lbry_test.go new file mode 100644 index 00000000..0da67760 --- /dev/null +++ b/chain/lbry/lbry_test.go @@ -0,0 +1,146 @@ +package lbry_test + +import ( + "context" + "log" + "os" + "reflect" + "time" + + "github.com/btcsuite/btcutil" + "github.com/renproject/id" + "github.com/renproject/multichain/api/address" + "github.com/renproject/multichain/api/utxo" + "github.com/renproject/multichain/chain/lbry" + "github.com/renproject/pack" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("LBRY", func() { + Context("when submitting transactions", func() { + Context("when sending LBC to multiple addresses", func() { + It("should work", func() { + // Load private key, and assume that the associated address has + // funds to spend. You can do this by setting LBRY_PK to the + // value specified in the `./multichaindeploy/.env` file. + pkEnv := os.Getenv("LBRY_PK") + if pkEnv == "" { + panic("LBRY_PK is undefined") + } + wif, err := btcutil.DecodeWIF(pkEnv) + Expect(err).ToNot(HaveOccurred()) + + // PKH + pkhAddr, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(wif.PrivKey.PubKey().SerializeCompressed()), &lbry.RegressionNetParams) + Expect(err).ToNot(HaveOccurred()) + pkhAddrUncompressed, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(wif.PrivKey.PubKey().SerializeUncompressed()), &lbry.RegressionNetParams) + Expect(err).ToNot(HaveOccurred()) + log.Printf("PKH %v", pkhAddr.EncodeAddress()) + log.Printf("PKH (uncompressed) %v", pkhAddrUncompressed.EncodeAddress()) + + // WPKH + wpkAddr, err := btcutil.NewAddressWitnessPubKeyHash([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}, &lbry.RegressionNetParams) + Expect(err).ToNot(HaveOccurred()) + log.Printf("WPKH %v", wpkAddr.EncodeAddress()) + + // Setup the client and load the unspent transaction outputs. + client := lbry.NewClient(lbry.DefaultClientOptions()) + outputs, err := client.UnspentOutputs(context.Background(), 0, 999999999, address.Address(pkhAddr.EncodeAddress())) + Expect(err).ToNot(HaveOccurred()) + Expect(len(outputs)).To(BeNumerically(">", 0)) + output := outputs[0] + + // Check that we can load the output and that it is equal. + // Otherwise, something strange is happening with the RPC + // client. + output2, _, err := client.Output(context.Background(), output.Outpoint) + Expect(err).ToNot(HaveOccurred()) + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) + output2, _, err = client.UnspentOutput(context.Background(), output.Outpoint) + Expect(err).ToNot(HaveOccurred()) + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) + + // Build the transaction by consuming the outputs and spending + // them to a set of recipients. + inputs := []utxo.Input{ + {Output: utxo.Output{ + Outpoint: utxo.Outpoint{ + Hash: output.Outpoint.Hash[:], + Index: output.Outpoint.Index, + }, + PubKeyScript: output.PubKeyScript, + Value: output.Value, + }}, + } + recipients := []utxo.Recipient{ + { + To: address.Address(pkhAddr.EncodeAddress()), + Value: pack.NewU256FromU64(pack.NewU64((output.Value.Int().Uint64() - 1000) / 3)), + }, + { + To: address.Address(pkhAddrUncompressed.EncodeAddress()), + Value: pack.NewU256FromU64(pack.NewU64((output.Value.Int().Uint64() - 1000) / 3)), + }, + { + To: address.Address(wpkAddr.EncodeAddress()), + Value: pack.NewU256FromU64(pack.NewU64((output.Value.Int().Uint64() - 1000) / 3)), + }, + } + tx, err := lbry.NewTxBuilder(&lbry.RegressionNetParams).BuildTx(inputs, recipients) + Expect(err).ToNot(HaveOccurred()) + + // Get the digests that need signing from the transaction, and + // sign them. In production, this would be done using the RZL + // MPC algorithm, but for the purposes of this test, using an + // explicit privkey is ok. + sighashes, err := tx.Sighashes() + signatures := make([]pack.Bytes65, len(sighashes)) + Expect(err).ToNot(HaveOccurred()) + for i := range sighashes { + hash := id.Hash(sighashes[i]) + privKey := (*id.PrivKey)(wif.PrivKey) + signature, err := privKey.Sign(&hash) + Expect(err).ToNot(HaveOccurred()) + signatures[i] = pack.NewBytes65(signature) + } + Expect(tx.Sign(signatures, pack.NewBytes(wif.SerializePubKey()))).To(Succeed()) + + // Submit the transaction to the lbry node. Again, this + // should be running a la `./multichaindeploy`. + txHash, err := tx.Hash() + Expect(err).ToNot(HaveOccurred()) + err = client.SubmitTx(context.Background(), tx) + Expect(err).ToNot(HaveOccurred()) + log.Printf("TXID %v", txHash) + + for { + // Loop until the transaction has at least a few + // confirmations. This implies that the transaction is + // definitely valid, and the test has passed. We were + // successfully able to use the multichain to construct and + // submit a LBRY transaction! + confs, err := client.Confirmations(context.Background(), txHash) + Expect(err).ToNot(HaveOccurred()) + log.Printf(" %v/3 confirmations", confs) + if confs >= 1 { + break + } + time.Sleep(10 * time.Second) + } + ctxWithTimeout, cancelCtxWithTimeout := context.WithTimeout(context.Background(), time.Second) + defer cancelCtxWithTimeout() + _, _, err = client.UnspentOutput(ctxWithTimeout, output.Outpoint) + Expect(err).To(HaveOccurred()) + + // Check that we can load the output and that it is equal. + // Otherwise, something strange is happening with the RPC + // client. + output2, _, err = client.Output(context.Background(), output.Outpoint) + Expect(err).ToNot(HaveOccurred()) + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) + }) + }) + }) +}) diff --git a/chain/lbry/utxo.go b/chain/lbry/utxo.go new file mode 100644 index 00000000..9a8be83c --- /dev/null +++ b/chain/lbry/utxo.go @@ -0,0 +1,32 @@ +package lbry + +import "github.com/renproject/multichain/chain/bitcoin" + +type ( + // Tx re-exports bitcoin.Tx. + Tx = bitcoin.Tx + + // TxBuilder re-exports bitcoin.TxBuilder. + TxBuilder = bitcoin.TxBuilder + + // Client re-exports bitcoin.Client. + Client = bitcoin.Client + + // ClientOptions re-exports bitcoin.ClientOptions. + ClientOptions = bitcoin.ClientOptions +) + +var ( + // NewTxBuilder re-exports bitcoin.NewTxBuilder. + NewTxBuilder = bitcoin.NewTxBuilder + + // NewClient re-exports bitcoin.NewClient. + NewClient = bitcoin.NewClient +) + +// DefaultClientOptions returns ClientOptions with the default settings. These +// settings are valid for use with the default local deployment of the +// multichain. In production, the host, user, and password should be changed. +func DefaultClientOptions() ClientOptions { + return bitcoin.DefaultClientOptions().WithHost("http://0.0.0.0:29245") +} diff --git a/chain/lbry/utxo_test.go b/chain/lbry/utxo_test.go new file mode 100644 index 00000000..8423d735 --- /dev/null +++ b/chain/lbry/utxo_test.go @@ -0,0 +1 @@ +package lbry_test diff --git a/infra/.env b/infra/.env index 92ced4f5..56d0b061 100644 --- a/infra/.env +++ b/infra/.env @@ -58,6 +58,16 @@ export ETHEREUM_ADDRESS=0xa0df350d2637096571F7A701CBc1C5fdE30dF76A export FILECOIN_PK=7b2254797065223a22736563703235366b31222c22507269766174654b6579223a22756d6a634e436a487a5438455757485849754a4c4b58745035437153323435666238626c656c756e5448493d227d export FILECOIN_ADDRESS=f1ej2tountzqwnu6uswhqdzvw6yy5xvcig6rxl2qa +# +# LBRY +# + +# Address that will receive mining rewards. Generally, this is set to an address +# for which the private key is known by a test suite. This allows the test suite +# access to plenty of testing funds. +export LBRY_PK=cV9WB2d7VTCJkkJy5RrzkQvqtiFJvAXbL8XUte8YRBH9zG2u1LTu +export LBRY_ADDRESS=mufonb3XbgFWH6ayP84sz81x6rS9oCxvqR + # # Terra # diff --git a/infra/docker-compose.yaml b/infra/docker-compose.yaml index b30be3fb..9e53ed4e 100644 --- a/infra/docker-compose.yaml +++ b/infra/docker-compose.yaml @@ -125,6 +125,18 @@ services: entrypoint: - "./root/run.sh" + # + # LBRY + # + lbry: + build: + context: ./lbry + ports: + - "0.0.0.0:29245:29245" + entrypoint: + - "./root/run.sh" + - "${LBRY_ADDRESS}" + # # Zcash # diff --git a/infra/lbry/Dockerfile b/infra/lbry/Dockerfile new file mode 100644 index 00000000..e3790fc7 --- /dev/null +++ b/infra/lbry/Dockerfile @@ -0,0 +1,16 @@ +FROM ubuntu:xenial + +RUN apt-get update --fix-missing && apt-get install --yes software-properties-common wget unzip + +RUN wget -c https://github.com/lbryio/lbrycrd/releases/download/v0.17.4.6/lbrycrd-linux-1746.zip -O tmp.zip && unzip tmp.zip -d lbrycrd && rm tmp.zip +RUN mv ./lbrycrd /app +RUN chmod +x /app/lbrycrdd +RUN chmod +x /app/lbrycrd-cli + +COPY lbrycrd.conf /root/.lbrycrd/lbrycrd.conf +COPY run.sh /root/run.sh +RUN chmod +x /root/run.sh + +EXPOSE 29245 + +ENTRYPOINT ["./root/run.sh"] diff --git a/infra/lbry/README.md b/infra/lbry/README.md new file mode 100644 index 00000000..bebce882 --- /dev/null +++ b/infra/lbry/README.md @@ -0,0 +1,22 @@ +# README + +## Generate a keypair (privatekey + address) + +```bash +$ go run keygen.go +LBRY_PK=cV9WB2d7VTCJkkJy5RrzkQvqtiFJvAXbL8XUte8YRBH9zG2u1LTu +LBRY_ADDRESS=n3qZBVhiMHW6vxizTdpnBRpvHXHqer3Q2x +``` + +## Build your docker container + +```bash +docker build . +``` + +## Run the container + +```bash +# Regtest +docker run -p 29245:29245 lbrycrd:latest "n3qZBVhiMHW6vxizTdpnBRpvHXHqer3Q2x" +``` diff --git a/infra/lbry/keygen.go b/infra/lbry/keygen.go new file mode 100644 index 00000000..a9e04dcf --- /dev/null +++ b/infra/lbry/keygen.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + + "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcutil" + "github.com/renproject/id" + "github.com/renproject/multichain/chain/lbry" +) + +func main() { + privKey := id.NewPrivKey() + wif, err := btcutil.NewWIF((*btcec.PrivateKey)(privKey), &lbry.RegressionNetParams, true) + if err != nil { + panic(err) + } + addrPubKeyHash, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(wif.SerializePubKey()), &lbry.RegressionNetParams) + if err != nil { + panic(err) + } + fmt.Printf("LBRY_PK=%v\n", wif) + fmt.Printf("LBRY_ADDRESS=%v\n", addrPubKeyHash) +} diff --git a/infra/lbry/lbrycrd.conf b/infra/lbry/lbrycrd.conf new file mode 100644 index 00000000..b04d1351 --- /dev/null +++ b/infra/lbry/lbrycrd.conf @@ -0,0 +1,10 @@ +daemon=1 +regtest=1 +rpcuser=user +rpcpassword=password +rpcallowip=0.0.0.0/0 +server=1 +txindex=1 + +[regtest] +rpcbind=0.0.0.0 diff --git a/infra/lbry/run.sh b/infra/lbry/run.sh new file mode 100644 index 00000000..6aa8f7b2 --- /dev/null +++ b/infra/lbry/run.sh @@ -0,0 +1,22 @@ +#!/bin/bash +ADDRESS=$1 + +# Start +/app/lbrycrdd -conf=/root/.lbrycrd/lbrycrd.conf # -server -rpcbind=0.0.0.0 -rpcallowip=0.0.0.0/0 -rpcuser=user -rpcpassword=password +sleep 10 + +# Print setup +echo "LBRY_ADDRESS=$ADDRESS" + +# Import the address +/app/lbrycrd-cli importaddress $ADDRESS + +# Generate enough block to pass the maturation time +/app/lbrycrd-cli generatetoaddress 101 $ADDRESS + +# Simulate mining +while : +do + /app/lbrycrd-cli generatetoaddress 1 $ADDRESS + sleep 10 +done diff --git a/multichain.go b/multichain.go index 436a7fc0..ed433472 100644 --- a/multichain.go +++ b/multichain.go @@ -108,6 +108,7 @@ const ( ETH = Asset("ETH") // Ether FIL = Asset("FIL") // Filecoin FTM = Asset("FTM") // Fantom + LBC = Asset("LBC") // LBRY SOL = Asset("SOL") // Solana LUNA = Asset("LUNA") // Luna ZEC = Asset("ZEC") // Zcash @@ -142,6 +143,8 @@ func (asset Asset) OriginChain() Chain { return Filecoin case FTM: return Fantom + case LBC: + return LBRY case LUNA: return Terra case SOL: @@ -167,7 +170,7 @@ func (asset Asset) OriginChain() Chain { // ChainType returns the chain-type (Account or UTXO) for the given asset func (asset Asset) ChainType() ChainType { switch asset { - case BCH, BTC, DGB, DOGE, ZEC: + case BCH, BTC, DGB, DOGE, LBC, ZEC: return ChainTypeUTXOBased case BNB, ETH, FIL, LUNA: return ChainTypeAccountBased @@ -218,6 +221,7 @@ const ( Ethereum = Chain("Ethereum") Fantom = Chain("Fantom") Filecoin = Chain("Filecoin") + LBRY = Chain("LBRY") Solana = Chain("Solana") Terra = Chain("Terra") Zcash = Chain("Zcash") @@ -252,7 +256,7 @@ func (chain *Chain) Unmarshal(buf []byte, rem int) ([]byte, int, error) { // for the chain. func (chain Chain) ChainType() ChainType { switch chain { - case Bitcoin, BitcoinCash, DigiByte, Dogecoin, Zcash: + case Bitcoin, BitcoinCash, DigiByte, Dogecoin, LBRY, Zcash: return ChainTypeUTXOBased case BinanceSmartChain, Ethereum, Filecoin, Terra: return ChainTypeAccountBased @@ -300,6 +304,8 @@ func (chain Chain) NativeAsset() Asset { return ETH case Filecoin: return FIL + case LBRY: + return LBC case Terra: return LUNA case Zcash: diff --git a/multichain_test.go b/multichain_test.go index 27f5165e..f6a8e96a 100644 --- a/multichain_test.go +++ b/multichain_test.go @@ -30,6 +30,7 @@ import ( // "github.com/renproject/multichain/chain/digibyte" "github.com/renproject/multichain/chain/dogecoin" "github.com/renproject/multichain/chain/filecoin" + "github.com/renproject/multichain/chain/lbry" "github.com/renproject/multichain/chain/terra" "github.com/renproject/multichain/chain/zcash" "github.com/renproject/pack" @@ -653,6 +654,26 @@ var _ = Describe("Multichain", func() { dogecoin.NewTxBuilder(&dogecoin.RegressionNetParams), multichain.Dogecoin, }, + { + "LBRY_PK", + func(pkh []byte) (btcutil.Address, error) { + addr, err := btcutil.NewAddressPubKeyHash(pkh, &lbry.RegressionNetParams) + return addr, err + }, + func(script []byte) (btcutil.Address, error) { + addr, err := btcutil.NewAddressScriptHash(script, &lbry.RegressionNetParams) + return addr, err + }, + pack.NewString("http://0.0.0.0:29245"), + func(rpcURL pack.String, pkhAddr btcutil.Address) (multichain.UTXOClient, []multichain.UTXOutput, func(context.Context, pack.Bytes) (int64, error)) { + client := lbry.NewClient(lbry.DefaultClientOptions()) + outputs, err := client.UnspentOutputs(ctx, 0, 999999999, multichain.Address(pkhAddr.EncodeAddress())) + Expect(err).NotTo(HaveOccurred()) + return client, outputs, client.Confirmations + }, + lbry.NewTxBuilder(&lbry.RegressionNetParams), + multichain.LBRY, + }, { "ZCASH_PK", func(pkh []byte) (btcutil.Address, error) {