Categorygithub.com/cryptogarageinc/cfd-go
modulepackage
0.4.7
Repository: https://github.com/cryptogarageinc/cfd-go.git
Documentation: pkg.go.dev

# README

Crypto Finance Development Kit for Go (CFD-GO)

CFD library for Go.

Overview

This library is development kit for crypto finance application. Useful when developing applications for cryptocurrencies.

Target Network

  • Bitcoin
  • Liquid Network

Support function by cfd

  • Bitcoin
    • Bitcoin Script (builder, viewer)
    • Transaction
      • Create, Parse, Decode
      • Simple pubkey-hash sign / verify
      • Estimate Fee
      • Coin Selection (FundRawTransaction)
    • PSBT (v0. v2 & taproot is not yet.)
      • Create, Parse, Decode
      • Simple pubkey-hash sign / verify
      • Estimate Fee
      • Coin Selection (FundRawTransaction)
    • ECDSA Pubkey/Privkey (TweakAdd/Mul, Negate, Sign, Verify)
    • BIP32, BIP39
    • Output Descriptor (contains miniscript parser)
    • Schnorr/Taproot
    • Bitcoin Address (Segwit-v0, Segwit-v1, P2PKH/P2SH)
  • Liquid Network
    • Confidential Transaction
      • Blind, Unblind
      • Reissuance
    • Confidential Address

Libraries for each language

  • Go : cfd-go
    • C/C++ : cfd
      • Extend the cfd-core library. Defines the C language API and extension classes.
    • C++ : cfd-core
      • Core library. Definition base class.
  • other language:
    • JavaScript : cfd-js
    • WebAssembly : cfd-js-wasm
    • Python : cfd-python
    • C# : cfd-csharp
    • Rust : cfd-rust

Dependencies

  • Go (1.17 or higher)
  • C/C++ Compiler   - can compile c++11
  • CMake (3.14.3 or higher)

Windows (MinGW)

attention: Cgo can only be used on the make platform.

(Recommended to use wsl(Windows Subsystem for Linux), because it can be cumbersome.)

download and install files.

  • go (1.17 or higher)
  • CMake (3.14.3 or higher)
  • MinGW (Add to PATH after install)

MacOS

# xcode cli tools
xcode-select --install

# install dependencies using Homebrew
brew install cmake go

Linux(Ubuntu)

# install dependencies using APT package Manager
apt-get install -y build-essential golang cmake

cmake version 3.14.2 or lower, download from website and install cmake.

go version 1.11 or lower, get golang.org/dl/go1.12 or higher.


Build

Using CMake

# recommend out of source build
mkdir build && cd $_
# configure & build
cmake .. -DENABLE_SHARED=on -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=off -DENABLE_JS_WRAPPER=off -DENABLE_CAPI=on -DTARGET_RPATH=/usr/local/lib/
make
cd ..
go mod download
go build

CMake options

cmake .. (CMake options) -DENABLE_JS_WRAPPER=off

  • -DENABLE_ELEMENTS: Enable functionalies for elements sidechain. [ON/OFF] (default:ON)
  • -DENABLE_SHARED: Enable building a shared library. [ON/OFF] (default:OFF)
  • -DENABLE_TESTS: Enable building a testing codes. If enables this option, builds testing framework submodules(google test) automatically. [ON/OFF] (default:ON)
  • -DCMAKE_BUILD_TYPE=Release: Enable release build.
  • -DCMAKE_BUILD_TYPE=Debug: Enable debug build.
  • -DCFDCORE_DEBUG=on: Enable cfd debug mode and loggings log files. [ON/OFF] (default:OFF)

install / uninstall

On Linux or MacOS, can use install / uninstall.

On Windows, can use releases asset.

Using releases asset

The fastest and easiest way. Target is amd64(x86_64) only.

  • macos / linux(ubuntu)
(cleanup)
./tools/cmake_cleanup.sh
sudo ./tools/cleanup_install_files.sh

(download)
wget https://github.com/cryptogarageinc/cfd-go/releases/download/v0.3.2/cfdgo-v0.3.2-ubuntu2004-gcc-x86_64.zip

(unzip)
sudo unzip -q cfdgo-v0.3.2-ubuntu2004-gcc-x86_64.zip -d /
  • windows
    1. cleanup: c:/usr/local directory.
    2. download asset.
    3. Unzip and extract to c:/usr/local directory.

install (after build)

install for /usr/local/lib.

Using CMake install

Attention: Currently, there is a problem with ExternalProject, so a problem occurs when performing update processing. Please perform cleanup when building before installation.

(cleanup)
./tools/cmake_cleanup.sh
sudo ./tools/cleanup_install_files.sh

(build)
mkdir build && cd build && cmake .. -DENABLE_SHARED=on -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=off -DENABLE_JS_WRAPPER=off -DENABLE_CAPI=on -DTARGET_RPATH=/usr/local/lib && make

(install by using makefile)
cd build && sudo make install

(install by using ninja)
cd build && sudo ninja install

cmake version is 3.15 or higher: cmake --install build

Using releases asset (for install)

Target is amd64(x86_64) only.

  • Ubuntu / MacOS
(cleanup)
./tools/cmake_cleanup.sh
sudo ./tools/cleanup_install_files.sh

(download)
wget https://github.com/cryptogarageinc/cfd-go/releases/download/v0.3.2/cfdgo-v0.3.2-ubuntu2004-gcc-x86_64.zip

(unzip)
sudo unzip -q cfdgo-v0.3.2-ubuntu2004-gcc-x86_64.zip -d /

uninstall

(uninstall by using makefile)
cd build && sudo make uninstall

(uninstall by using ninja)
cd build && sudo ninja uninstall

(uninstall by using script)
sudo ./tools/cleanup_install_files.sh

How to use cfd-go as go module

  1. Once, clone this repository.

  2. Build & install cfd-go(and dependencies).

  3. Modify go.mod file adding cfd-go as go moudle

    go.mod

    require (
      github.com/cryptogarageinc/cfd-go v0.3.2
      ...
    )
    

    Reference github commit:

    require (
      github.com/cryptogarageinc/cfd-go v1.0.0-0.20191205091101-a48a6a8b1a24
      ...
    )
    

    (version format: UnknownVersionTag-UtcDate-CommitHash)

  4. Download cfd-go module

    go mod download
    

Test and Example

Test

test file is cfdgo_test.go . Execute by the following method.

  • shell script or bat file

    (linux/macos)
    ./go_test.sh
    
    (Windows)
    .\go_test.bat
    
  • go command (linux/macos)

    LD_LIBRARY_PATH=./build/Release go test
    

Example

  • cfdgo_test.go

Information for developers

managed files

  • cfdgo.go, cfdgo.cxx: generated from swig.
  • swig.i: swig file.

using library

develop tools

generate from swig.i

attention: At first, install swig and set PATH.

(linux/macos)
./tools/gen_swig.sh

(Windows)
.\tools\gen_swig.bat

formatter

  • go fmt
  • goimports

use by makefile:

make

(windows)
mingw32-make

mockgen

make generate

(windows)
mingw32-make generate

develop tools by docker compose

generate from swig.i by docker compose

docker-compose run swig

formatter and mockgen by docker compose

docker-compose run formatter

support compilers

  • GCC (contains MinGW) (5.x or higher)
  • Clang (7.x or higher)

code coverage

(Windows)
.\go_coverage.bat

(Ubuntu / MacOS)
./go_coverage.sh

Note

Git connection

Git repository connections default to HTTPS. However, depending on the connection settings of GitHub, you may only be able to connect via SSH. As a countermeasure, forcibly establish SSH connection by setting CFD_CMAKE_GIT_SSH=1 in the environment variable.

  • Windows: (On the command line. Or set from the system setting screen.)

    set CFD_CMAKE_GIT_SSH=1
    
  • MacOS & Linux(Ubuntu):

    export CFD_CMAKE_GIT_SSH=1
    

Ignore git update for CMake External Project

Depending on your git environment, you may get the following error when checking out external:

  Performing update step for 'libwally-core-download'
  Current branch cmake_build is up to date.
  No stash entries found.
  No stash entries found.
  No stash entries found.
  CMake Error at /workspace/cfd-core/build/external/libwally-core/download/libwally-core-download-prefix/tmp/libwally-core-download-gitupdate.cmake:133 (message):


    Failed to unstash changes in:
    '/workspace/cfd-core/external/libwally-core/'.

    You will have to resolve the conflicts manually

This phenomenon is due to the git update related command. Please set an environment variable that skips update processing.

  • Windows: (On the command line. Or set from the system setting screen.)

    set CFD_CMAKE_GIT_SKIP_UPDATE=1
    
  • MacOS & Linux(Ubuntu):

    export CFD_CMAKE_GIT_SKIP_UPDATE=1
    

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

AddPeginInput This function add a pegin input.
AddPegoutOutput This function add a pegout output.
AddTaprootSchnorrSign This function add a taproot schnorr sign.
AddTapScriptSign This function add a tapscript sign.
AddTransactionInput : add createrawtransaction input data.
AddTransactionInput : add createrawtransaction output data.
AppendConfidentialTx : append confidential transaction.
* * Add blind transaction txin data.
* * Add blind transaction txout data.
* * Convert to der encode, and add sign data to confidential transaction.
* * Add txin to confidential transaction.
* * Add multisig sign to confidential transaction.
* * Add txout to confidential transaction.
* * add transaction output for elements.
* * add transaction output by locking script for elements.
* * add transaction output for destroy amount.
* * add transaction output by fee for elements.
* * Add pubkey hash sign data to confidential transaction.
* * Add script hash sign data to confidential transaction.
* * Add sign data to confidential transaction.
* * Generate sign with privkey, and add sign data to confidential transaction.
* * Add unlocking script to confidential transaction input.
* * Add unlocking script to confidential transaction input by index.
* * Add output for destroying the specified amount of the specified asset.
* * Add output for destroying the specified amount of the specified asset.
* * Add multisig sign data.
* * Convert to der encode, and add multisig sign data.
CfdGoAddPeginInput This function add a pegin input.
CfdGoAddPegoutOutput This function add a pegout output.
CfdGoAddTaprootSchnorrSign This function add a taproot schnorr sign.
CfdGoAddTapScriptSign This function add a tapscript sign.
* * add transaction input.
* * Add multisig sign to transaction.
* * add transaction output for bitcoin.
* * add transaction output by locking script for bitcoin.
* * Add pubkey hash sign data to transaction.
* * Add script hash sign data to transaction.
* * Generate sign with privkey, and add sign data to confidential transaction.
CfdGoAddTxSignWithPrivkeyByUtxoList This function add sign with prikey.
* CfdGoBlindRawTransaction * Execute blindrawtransaction.
* CfdGoBlindRawTransactionAndGetBlinder * Execute blindrawtransaction.
* CfdGoBlindRawTransactionInternal * Execute blindrawtransaction.
* * Calculate ec-signature from privkey.
CfdGoClearCustomPrefix clear custom prefix.
* * Select coins.
* CfdGoCombinePubkey * Combine pubkey.
* CfdGoCombinePubkeyPair * Compress pubkey.
* CfdGoCompressPubkey * Compress pubkey.
* CfdGoConvertEntropyToMnemonic * Convert entropy to mnemonic.
* CfdGoConvertMnemonicToSeed * Convert mnemonic to seed.
* CfdGoConvertMnemonicWordsToSeed * Convert mnemonic to seed.
* * Convert script asm to hex.
* * Create Address.
* * Create confidential address.
* * Create sighash from confidential transaction.
* CfdGoCreateExtkey * Create extkey.
CfdGoCreateExtkeyByFormat create extended key by format.
CfdGoCreateExtkeyByFormatFromParent create extended key by format.
CfdGoCreateExtkeyByFormatFromSeed create extended key by format.
* CfdGoCreateExtkeyFromParent * Create extkey from parent key.
* * Create extkey from parent path.
* * Create extkey from seed.
* * Create extpubkey from extprivkey.
* * Create cfd handle.
* * Create key pair.
* * Create multisig script and address.
* * Create multisig scriptsig.
* * Create script from script items.
CfdGoCreateScriptTreeElementsHandle This function is create a script tree handle.
CfdGoCreateScriptTreeHandle This function is create a script tree handle.
* * Create sighash from transaction.
CfdGoDecodeBase58 ...
CfdGoDecodeBase64 ...
* * Decode transaction hex.
* * Decode der encoded signature.
CfdGoDecryptAES ...
CfdGoEncodeBase58 ...
CfdGoEncodeBase64 ...
* * Encode ec signature by der encoding.
CfdGoEncryptAES ...
* * Estimate fee amount.
* * Estimate fee amount.
CfdGoExistTxidInBlock This function get a exist tx in a block.
* * Generate blind transaction.
* * Add multisig sign data to confidential transaction.
* * finalize transaction.
* * Free blind handle.
* * Free cfd handle.
* * Free multisig sign handle.
CfdGoFreeScriptTreeHandle This function is free a script tree handle.
* * free transaction handle.
No description provided by the author
* CfdGoFundRawTransaction * Execute fundrawtransacdtion.
No description provided by the author
* CfdGoFundRawTransactionBtc * Execute fundrawtransacdtion on bitcoin.
* * Get multisig pubkeys address.
* * Get address from locking script.
* * Get address information.
CfdGoGetAmountCommitment get amount commitment.
CfdGoGetAssetCommitment get asset commitment.
* * Get blind data.
CfdGoGetBlockHeaderData This function get a block header data.
* * Get confidential transaction data.
No description provided by the author
* * Get txin on confidential transaction.
* * Get txin count on confidential transaction.
* * Get txin index on confidential transaction.
* * Get pegin witness stack on confidential transaction input.
* * Get witness stack count on confidential transaction input.
* * Get witness stack on confidential transaction input.
* * Get witness stack count on confidential transaction input.
* * Get txout on confidential transaction.
No description provided by the author
* * Get txout count on confidential transaction.
* * Get txout index on confidential transaction.
No description provided by the author
* * Get default blinding key by locking script.
* * Get default blinding key by address.
* * Get outputDescriptor's checksum.
CfdGoGetExtkeyInfo This function returns extkey info and network.
* * Get extkey information.
* * Get issuance blinding key.
* CfdGoGetMnemonicWordList * Get mnemonic word list.
* * Get parent key path data.
CfdGoGetPeginAddress This function get a pegin address.
CfdGoGetPegoutAddress This function get a pegout address.
CfdGoGetPegoutAddressFromTransaction This function get a pegout address from transaction.
* * Get privkey from extprivkey.
* * Get privkey from WIF.
* * Get privkey WIF from hex.
* * Get pubkey from extkey.
* * Get pubkey from privkey.
CfdGoGetSighash This function return a sighash.
CfdGoGetSighashByKey This function return a sighash by pubkey.
CfdGoGetSighashByScript This function return a sighash by redeem script.
CfdGoGetSighashByTapScript This function return a sighash by tapscript.
* * Get supported function.
CfdGoGetTransactionDataFromBlock This function get a tx data.
CfdGoGetTxCountInBlock This function get a tx count.
CfdGoGetTxidListFromBlock This function get txid list from block.
* * Get txin on transaction.
No description provided by the author
* * Get txin count on transaction.
No description provided by the author
* * Get transaction data.
No description provided by the author
* * Get txin index on transaction.
No description provided by the author
* * Get txin issuance on confidential transaction.
No description provided by the author
* * Get witness stack on transaction input.
No description provided by the author
* * Get witness stack count on transaction input.
No description provided by the author
* * Get txout on transaction.
No description provided by the author
* * Get txout count on transaction.
No description provided by the author
* * Get txout index on transaction.
No description provided by the author
CfdGoGetTxOutIndexes This function get txout index list.
CfdGoHash160 ...
CfdGoHash256 ...
* * Get blind transaction handle.
* * initialize elements createrawtransaction with version & locktime.
* * initialize elements createrawtransaction with hex.
* * Get initialized confidential transaction.
* * Generate multisig sign handle.
* * initialize bitcoin createrawtransaction with version & locktime.
* * initialize bitcoin createrawtransaction with hex.
No description provided by the author
* CfdGoNegatePrivkey * Negate privkey.
* CfdGoNegatePubkey * Negate pubkey.
* * Normalize ec signature to low-s form * param: signature ec signature to normalize * return: normalizeSignature normalized signature * return: err error */.
* * Get address and confidentialKey from confidentialAddress.
* * Parse Output Descriptor.
* * Parse Output Descriptor.
* * Parse privkey WIF data.
* * Parse script items from script.
* CfdGoPrivkeyTweakAdd * TweakAdd privkey.
* CfdGoPrivkeyTweakMul * TweakMul privkey.
* CfdGoPubkeyTweakAdd * TweakAdd pubkey.
* CfdGoPubkeyTweakMul * TweakMul pubkey.
CfdGoRipemd160 ...
* CfdGoSerializeByteData * Serialize byte data.
* * Serialize transaction for ledger.
CfdGoSetBlindTxOption is set blinding optional data.
CfdGoSetCustomPrefix set custom prefix by json.
* * Set multisig scriptsig to locking script.
CfdGoSetGenesisBlockHashGlobal ...
CfdGoSetRawIssueAsset This function set a issuance information.
* * Set reissuance asset to confidential transaction.
CfdGoSha256 ...
CfdGoSignMessage sign messsage.
CfdGoSplitConfidentialTxOut This function set the split outputs.
CfdGoSplitTxOut This function set the split outputs.
CfdGoUnblindData This function returns unblind txout data.
* * Unblind txin issuance on confidential transaction.
* * Unblind txout on confidential transaction.
* CfdGoUncompressPubkey * Compress pubkey.
* * Update txout of confidential transaction.
CfdGoUpdatePeginWitnessStack This function set a witness stack item.
CfdGoUpdateTxInSequence This function set a sequence number.
* * Update txout amount on transaction.
CfdGoUpdateWitnessStack This function set a witness stack item.
* * Verify sign in transaction input.
* * Verify signature in transaction input.
* * Verify signature in transaction input by index.
* * Verify sign in transaction input with error message.
* CfdGoVerifyEcSignature * Verify ec signature.
CfdGoVerifyMessage verify message.
CfdGoVerifySign This function return a verify sign results.
* CfdGoVerifySignature * Verify signature.
* CfdGoVerifyTxSign * Verify transaction sign.
* CfdGoVerifyTxSignReason * Verify transaction sign.
* * Convert return code to golang built-in error struct.
CreateConfidentialTx : create confidential transaction.
EncryptEcdsaAdaptor This function return a ecdsa-adaptor's signature.
ExistTxidInBlock This function get a exist tx in a block.
FinalizeTransaction : finalize createrawtransaction.
FreeBlockHandle This function is free block handle.
FreeTransactionHandle : free createrawtransaction handle.
No description provided by the author
GetBlockHash This function get a block hash.
GetBlockHeaderData This function get a block header data.
No description provided by the author
No description provided by the author
GetPeginAddress This function get a pegin address.
GetPegoutAddress This function get a pegout address.
GetPegoutAddressFromTransaction This function is getting pegout address from tx.
GetSighash This function return a sighash.
No description provided by the author
GetTransactionFromBlock This function get a transaction data.
GetTransactionHex This function return a transacton hex.
GetTxCountInBlock This function get a transaction count in this block.
GetTxidFromBlock This function get a txid from block.
GetTxidListFromBlock This function get txid list from block.
GetTxOutIndexes This function get txout index list.
GetTxOutProof This function get a txoutproof.
HasPegoutOutput This function check pegout output.
InitializeTransactionByHex This function is open block handle.
InitializeTransaction : initialize createrawtransaction with version & locktime.
InitializeTransactionByHex : initialize createrawtransaction with hex.
NewAdaptorSignature This function return a ecdsa-adaptor's signature.
NewByteData This function create a bytedata from a byte array.
NewByteDataFromHex This function create a bytedata from a hex string.
NewByteDataFromHex This function create a bytedata from a hex string.
* NewCfdBlindTxOption * Create CfdBlindTxOption struct set default value.
* * Create CfdCoinSelectOption struct set default value.
* * Create CfdFeeEstimateOption struct set default value.
* NewCfdFundRawTxOption * Create CfdFundRawTxOption struct set default value.
NewDescriptorFromAddress This function return a Descriptor from address.
NewDescriptorFromLockingScript This function return a Descriptor from locking script.
NewDescriptorFromMultisig This function return a Descriptor from multisig.
NewDescriptorFromAddress This function return a Descriptor from pubkey.
NewDescriptor This function return a Descriptor.
NewElementsTapBranchFromControlBlock This function return a TapBranch from control block.
NewElementsTapBranchFromString This function return a TapBranch from tree string.
NewElementsTapBranchFromStringByNodes This function return a TapBranch from tree string and nodes.
NewElementsTapBranchFromTapScript This function return a TapBranch from tapscript.
NewSchnorrUtil This function return a SchnorrUtil.
NewScript This function create a script from a byte array.
NewScriptFromAsm This function create a script from an asm string.
NewScriptFromAsmList This function create a script from the asm string list.
NewScriptFromHex This function create a script from a hex string.
NewScriptFromHex This function create a script from a hex string.
NewSigHashType This function return a SigHashType.
NewTapBranchFromControlBlock This function return a TapBranch from control block.
NewTapBranchFromControlBlockWithNetwork This function return a TapBranch from control block.
NewTapBranchFromHash This function return a TapBranch.
NewTapBranchFromHashWithNetwork This function return a TapBranch.
NewTapBranchFromString This function return a TapBranch from tree string.
NewTapBranchFromStringByNodes This function return a TapBranch from tree string and nodes.
NewTapBranchFromStringByNodesWithNetwork This function return a TapBranch from tree string and nodes.
NewTapBranchFromStringByNodesWithNetwork This function return a TapBranch from tree string.
NewTapBranchFromTapScript This function return a TapBranch from tapscript.
NewTapBranchFromTapScriptWithNetwork This function return a TapBranch from tapscript.
SetConfidentialTxGenesisBlockHashByHandle set the genesis block hash for tx handle.
SetElementsUtxoListByHandle This function has adding utxo list.
SetIssueAsset This function set a issuance information.
SetReissueAsset This function set a reissuance information.
SetUtxoListByHandle This function has adding utxo list.
SignWithPrivkeyByHandle This function has adding sign with prikey.
SplitConfidentialTxOut This function set the split outputs.
SplitTxOut This function set the split outputs.
UpdatePeginWitnessStack This function set a witness stack item.
UpdateTxInSequence This function set a sequence number.
UpdateWitnessStack This function set a witness stack item.
VerifySign This function return a verify sign results.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

AdaptorSignature This struct use for the accessing to ecdsa-adaptor function.
* * BlindRawTransaction option data struct.
BlockHeader block header information.
ByteData This struct holds a byte array.
* * Address information struct.
* * BlindRawTransaction option data struct.
* * BlindRawTransaction option data struct.
* * BlindRawTransaction option data struct.
* * CoinSelection option data struct.
ConfidentialTxOut : confidential transaction output.
* * Descriptor data struct.
* * Descriptor key data struct.
* * EstimateFee Input data struct.
* * Extkey data struct.
* * EstimateFee option data struct.
* * FundRawTransaction option data struct.
* * Selection target amount struct.
* * Multisig sign data struct.
* Output data struct.
* * Sign parameter data struct.
* * Selection target amount struct.
* * TxData data struct.
TxOut : transaction output.
* * UTXO struct.
ConfidentialTxIn : confidential transaction input.
ConfidentialTxOut : confidential transaction output.
Descriptor This struct use for the output descriptor.
IssuanceData : confidential transaction issuance input.
OutPoint : utxo outpoint struct.
SchnorrUtil This struct use for the accessing to schnorr function.
Script This struct holds a script.
ScriptWitness : witness stack.
SigHashType This struct use for the sighashtype utility function.
TapBranch This struct use for the taproot script tree branch.
* * TransactionData data struct.
TxIn : transaction input.
TxOut : transaction output.