Skip to content

multi: Add decred.#1279

Open
JoeGruffins wants to merge 2 commits intoMetacoSA:masterfrom
JoeGruffins:adddecred
Open

multi: Add decred.#1279
JoeGruffins wants to merge 2 commits intoMetacoSA:masterfrom
JoeGruffins:adddecred

Conversation

@JoeGruffins
Copy link
Copy Markdown

This is not complete but wanted to get feedback. Trying to add decred here with the intent of adding it to BTCpay Server. I appreciate any feedback and will do whatever it takes to get decred added. Thanks for your time.

@JoeGruffins
Copy link
Copy Markdown
Author

I believe this is complete now. Still waiting for feedback. Thanks.

@NicolasDorier
Copy link
Copy Markdown
Collaborator

hey @JoeGruffins sorry I didn't see the PR. I don't provide feedback on altcoins. Test it yourself and make sure it works.
You can ping DM me on twitter or chat.btcpayserver.org if I am too unresponsive.

@JoeGruffins
Copy link
Copy Markdown
Author

@NicolasDorier Thanks for replying! Gives us hope. We are changing a few things that aren't decred specific to allow decred to work. Please make sure we are not breaking anything and using methods you approve of! For example, whenever we hash a script or pubkey we use blake256 rather than sha256. We are trying to change as little as possible ofc.

@JoeGruffins JoeGruffins marked this pull request as draft October 23, 2025 06:50
@JoeGruffins
Copy link
Copy Markdown
Author

JoeGruffins commented Oct 23, 2025

Rebasing and squashing and adding more. Please have a brief look though...

@exitus1
Copy link
Copy Markdown

exitus1 commented Jan 9, 2026

Any updates on this? Having Decred on BTCpayserver would help our community use it as a medium of exchange.

@pubpete
Copy link
Copy Markdown

pubpete commented Jan 9, 2026

Any update on this? Would be a great addition for Decred as Cake Wallet is the go to mobile wallet for the community.

@cagochi
Copy link
Copy Markdown

cagochi commented Jan 9, 2026

Noticing some interest here, bumping for visibility.

@3pillars
Copy link
Copy Markdown

3pillars commented Jan 9, 2026

Let's getting done guys.

@PrimeDominus
Copy link
Copy Markdown

This needs approval from a maintainer to get merged. @NicolasDorier are you the only maintainer? @JoeGruffins are you able to assign someone to approve it?

@knocte
Copy link
Copy Markdown
Contributor

knocte commented Jan 10, 2026

Even if maintainer saw this PR and wanted to merge it, he may not be able to, given that it is marked as Draft. Ask the PR submitter to remove draft status first.

@PrimeDominus
Copy link
Copy Markdown

Even if maintainer saw this PR and wanted to merge it, he may not be able to, given that it is marked as Draft. Ask the PR submitter to remove draft status first.

Thanks for responding. @JoeGruffins I'm guessing it's up to you.

@JoeGruffins
Copy link
Copy Markdown
Author

We are working on it. We were told somewhere that tests should not be changed. The pr currently does that. Removing those and some more changes still incoming.

@JoeGruffins JoeGruffins force-pushed the adddecred branch 4 times, most recently from 8eeaede to ae081ea Compare March 31, 2026 05:08
@JoeGruffins JoeGruffins marked this pull request as ready for review March 31, 2026 05:11
@NicolasDorier
Copy link
Copy Markdown
Collaborator

NicolasDorier commented Apr 1, 2026

Altcoins shouldn't have to modify any file in NBitcoin's project, you should just put all your stuff in NBitcoin.Altcoins properly isolated.

Especially, you custom hash160 stuff poluting the core code everywhere, there is no way this get merged.
I think groestlcoin had some special hash160 stuff, but I don't remember, please look at them and don't modify files in NBitcoin project.

@JoeGruffins
Copy link
Copy Markdown
Author

JoeGruffins commented Apr 2, 2026

@NicolasDorier thank you for feedback! on it!

You don't want the tests right? They are in a separate commit so I can lop them off and just keep it local.

Add Decred (DCR) as an altcoin to NBitcoin with support for:

- Decred transaction serialization (prefix/witness split, expiry, tree)
- Block header with stake fields (StakeRoot, VoteBits, Voters, etc.)
- Blake256/Blake3 hashing for PoW, checksums, and merkle trees
- DCP0005 block header commitments (combined regular+stake merkle root)
- DCP0011 ASERT difficulty algorithm with Blake3 PoW
- DecredGetInitStateBehavior for P2P handshake
- Base58 with Blake256 checksums and WIF private key sig scheme byte
- RIPEMD160(Blake256) for Hash160 (address derivation)
- Custom RPC handling via ConsensusFactory virtual methods
- Proper GetSignatureHash with SigHashNone, SigHashSingle,
  SigHashAnyOneCanPay support per dcrd spec
- Decred-specific RPC response parsing (getbalance, gettxoutsetinfo,
  getpeerinfo, getrawtransaction, getblock)
- Network definitions for mainnet, testnet, and simnet/regtest

All Decred-specific code is fully isolated in NBitcoin.Altcoins/Decred.cs.
Core NBitcoin changes are limited to altcoin-agnostic extension points:
- NetworkStringParser.CreateP2PKH(PubKey, Network) for custom Hash160
- ConsensusFactory RPC virtual methods for altcoin RPC customization
- Block.GetMerkleRoot() made virtual
- Consensus.GetWorkRequired() hook for custom difficulty algorithms
Add 20 unit tests covering:
- Network loading and lookup for mainnet/testnet/regtest
- P2PKH and P2SH address parsing (mainnet and testnet)
- Address generation and roundtrip on all networks
- WIF private key parsing and roundtrip (mainnet and testnet)
- Blake256 hash verification (Decred vs Bitcoin address differ)
- Genesis block parsing and roundtrip
- Block header serialization (180-byte format)
- Transaction serialization/deserialization roundtrip
- Prefix-only tx hashing verification
- ConsensusFactory type checks and RPC properties
- Network property validation (ports, factory type)
@JoeGruffins
Copy link
Copy Markdown
Author

Refactored to isolate all Decred-specific code in NBitcoin.Altcoins/Decred.cs. There are zero if (IsDecred) checks or Decred-specific classes in core now.

I looked at Groestlcoin's pattern. It works with zero core changes because Groestlcoin uses standard RIPEMD160(SHA256) for Hash160/address derivation - it only customizes the Base58 checksum hash via the existing Base58CheckEncoder.CalculateHash() virtual.

Decred's situation is different: it uses RIPEMD160(Blake256) for address derivation itself, not just the checksum. PubKey.Hash computes the hash with no network context, so by the time NetworkStringParser.CreateP2PKH(KeyId, Network) is called, the KeyId is already wrong. There was no existing extension point to hook into.

To avoid threading a Hash160 delegate through 9 files (the previous approach), I added one new virtual method to NetworkStringParser:

public virtual BitcoinPubKeyAddress CreateP2PKH(PubKey pubKey, Network network)
{
    return CreateP2PKH(pubKey.Hash, network);
}

And one line change in PubKey.GetAddress() to call it. Decred overrides this to compute the correct hash. This follows the same pattern as the existing CreateP2PKH(KeyId, Network) and GetBase58CheckEncoder() virtuals.

The other core additions are altcoin-agnostic extension points with Bitcoin-default behavior:

  • ConsensusFactory RPC virtual methods (hooks for custom RPC arg formats, response parsing, batch support, balance parsing)
  • Block.GetMerkleRoot() made virtual
  • Consensus.GetWorkRequired() hook for custom difficulty algorithms
  • UseCustomTLSCert on RPCClient
  • FundRawTransactionResponse.ChangePos made nullable
  • Null-safe GetPeersInfo field parsing (general robustness)
  • BlockchainInfo.Chain fallback when chain name isn't registered

Everything else (WIF sig scheme byte, protocol payloads/behaviors, Blake256 checksums, getnewaddress param handling, Decred balance response format) is handled entirely in Altcoins using existing extension points and ConsensusFactory overrides.

https://github.com/MetacoSA/NBitcoin/compare/ae081ead5a93904721b26d84e85b30e34dea9415..2f429b6a9ba5f197a633643f37bdf624148c9508

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants