Skip to content

Adding Support for a New Blockchain

Alejandro Isaza edited this page Mar 6, 2019 · 15 revisions

If you haven't, start by reading the guide to contributing.

C++ Implementation

Start by implementing the required functionality in C++. The code should be put in the src/X folder where X is the name of the Blockchain.

Don't just dump an existing codebase in the repo: the code needs to follow the code style and use existing hashing and cryptographic functionality if possible. Adding new dependencies is something we want to avoid at all costs. We want to keep the codebase and the binary library as small as possible.

If you do need to add a new cryptographic function or other building block please do so as a separate PR from the blockchain implementation.

Address encoding/decoding

The first step is to support the address format specific to the blockchain. Create src/Blockchain/Address.h and src/Blockchain/Address.cpp where Blockchain is the blockchain name. Put this in the header file:

// Copyright © 2017-2019 Trust.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

#pragma once

namespace TW {
namespace Blockchain {

class Address {
    /// Determines whether a string makes a valid address.
    static bool isValid(const std::string& string);

    /// Initializes an address from a string representation.
    Address(const std::string& string);

    /// Initializes an address from a public key.
    Address(const PublicKey& publicKey);

    /// Returns a string representation of the address.
    std::string string() const;
};

}} // namespace

Replace Blockchain with the actual blockchain name.

At minimum the address needs a string validation static method, a string constructor, a constructor from a public key, and a method to convert back to a string. Make sure you can parse a string representation of an address and detect invalid addresses. Write unit tests for this. Also make sure that you can derive an address string from a private key. Write unit tests for this as well.

For an example of this have a look at Tendermint's Addres.h and Address.cpp.

Transaction signing

The second step is supporting signing of transactions. Create src/Blockchain/Signer.h and src/Blockchain/Signer.cpp where Blockchain is the blockchain name. Make sure you can generate a valid signature and a valid signed and encoded transaction. Write a unit tests for this.

For an example of this have a look at Binance's Signer.h and Signer.cpp.

Tests

The tests should be put in tests/X where X is the name of the blockchain. All C++ code needs to be unit tested. Make sure the tests pass by running build/tests/tests after building.

The C++ implementation with tests should be the first commit of your PR.

C Interface

Once you are satisfied with your C++ implementation write a C interface for it. The C interface needs to be as small as possible so that clients don't need to worry about implementation details. If you are implementing blockchain X create a TWXAddress.h to handle addresses associated to the blockchain and TWXSigner.h to handle transaction signing. Try reusing existing Protobuf models, in particular Bitcoin::SigningInput and Bitcoin::SigningOutput.

Add relevant constants in TWCointType, TWP2SHPrefix, TWEthereymChainID, TWCoinTypeConfiguration etc. as necessary. Implement your coin type in src/interface/TWCoinType.cpp to return the right elliptic curve and HD wallet derivation purpose. Make this the second commit of your PR.

Generate code

Lastly, generate the idiomatic interface code by running codegen/bin/codegen. Currently the iOS project requires manual updates when new files are added. Please open the Xcode project in the swift folder and add any new files (sorted by name). Make sure the Android and iOS projects run and their tests pass.

Add integration tests for your blockchain if you think it is necessary, for instance if your interface is different than other blockchain interfaces.

Make this the third commit of your PR.

Clone this wiki locally
You can’t perform that action at this time.