Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LBRY Support #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions chain/lbry/address.go
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions chain/lbry/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lbry_test
9 changes: 9 additions & 0 deletions chain/lbry/gas.go
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions chain/lbry/gas_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
})
})
177 changes: 177 additions & 0 deletions chain/lbry/lbry.go
Original file line number Diff line number Diff line change
@@ -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,
}
13 changes: 13 additions & 0 deletions chain/lbry/lbry_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading