Documentation update in progress
libnspv – a portable C library for creating and manipulating data structures and interacting with the p2p network on Komodo-compatible Smart Chains including the KMD main chain.
Based on Libbtc
=============================================================
Libbtc is a very portable C library for creating and manipulating bitcoin data structures and interacting with the p2p network.
- Generating and storing private and public keys
- ECDSA secp256k1 signing and verification (through libsecp256k1 included as git subtree)
- Generate recoverable signatures (and recover pubkey from signatures)
- BIP32 hierarchical deterministic key derivation
- Transaction generation, manipulation, signing and ser-/deserialization including P2PKH, P2SH, multisig
- Address generation
- Base58check encoding
- Native implementation of SHA256, SHA512, SHA512_HMAC, RIPEMD-160 including NIST testvectors
- Native constant time AES (+256CBC) cipher implementation including NIST testvectors
- Keystore (wallet) databases (through logdb https://github.com/liblogdb/liblogdb)
- Event based bitcoin P2P client capable of connecting to multiple nodes in a single thread (requires libevent)
- No dependencies in case no p2p network client is required (only dependency is libsecp256k1 added as git subtree)
- The only dependency for the p2p network client is libevent (very portable)
- optimized for MCU and low mem environments
- ~full test coverage
- mem leak free (valgrind check during CI)
./bitcointool -command genkey
> privatekey WIF: KwmAqzEiP7nJbQi6ofQywSEad4j5b9BXDJvyypQDDLSvrV6wACG8
> privatekey HEX: 102f1d9d91fa1c8d816ef469e74c1153a6b453d2a991e77fe187e5514a7b18ac
/bitcointool -command pubfrompriv -p KwmAqzEiP7nJbQi6ofQywSEad4j5b9BXDJvyypQDDLSvrV6wACG8
> pubkey: 023d86ca58e2519cce1729b4d36dfe5a053ad5f4ae6f7ef9360bee4e657f7e41c9
> p2pkh address: 1N5ZkjyabcZLLHMweJrSkn3qedsPGzAx9m
./bitcointool -command addrfrompub -pubkey 023d86ca58e2519cce1729b4d36dfe5a053ad5f4ae6f7ef9360bee4e657f7e41c9
> p2pkh address: 1N5ZkjyabcZLLHMweJrSkn3qedsPGzAx9m
./bitcointool -command hdgenmaster
> masterkey: xprv9s21ZrQH143K3C5hLMq2Upsh8mf9Z1p5C4QuXJkiodSSihp324YnWpFfRjvP7gqocJKz4oakVwZn5cUgRYTHtNRvGqU5DU2Gn8MPM9jHvfC
./bitcointool -command hdprintkey -privkey xprv9s21ZrQH143K3C5hLMq2Upsh8mf9Z1p5C4QuXJkiodSSihp324YnWpFfRjvP7gqocJKz4oakVwZn5cUgRYTHtNRvGqU5DU2Gn8MPM9jHvfC
> ext key: xprv9s21ZrQH143K3C5hLMq2Upsh8mf9Z1p5C4QuXJkiodSSihp324YnWpFfRjvP7gqocJKz4oakVwZn5cUgRYTHtNRvGqU5DU2Gn8MPM9jHvfC
> depth: 0
> p2pkh address: 1Fh1zA8mD6S2LBbCqdViEGuV3oDhggX3k4
> pubkey hex: 0394a83fcfa131afc47a3fcd1d32db399a0ffa7e68844546b2df7ed9f5ebd07b09
> extended pubkey: xpub661MyMwAqRbcFgAASPN2qxpRgoVdxUXvZHLWKhALMxyRbW9BZbs34ca9H3LrdsKxdMD4o5Fc7eqDg19cRTj3V9dCCeM4R1DRn8DvUq3rMva
./bitcointool -command hdderive -keypath m/1h -privkey xprv9s21ZrQH143K3C5hLMq2Upsh8mf9Z1p5C4QuXJkiodSSihp324YnWpFfRjvP7gqocJKz4oakVwZn5cUgRYTHtNRvGqU5DU2Gn8MPM9jHvfC
> ext key: xprv9v5qiRbzrbhUzAVBdtfqi1tQx5tiRJ2jpNtAw8bRec8sTivLw55H85SoRTizNdx2JSVL4sNxmjvseASZkwpUopby3iGiJWnVH3Wjg2GkjrD
> depth: 1
> p2pkh address: 1DFBGZdcADGTcWwDEgf15RGPqnjmW2gokC
> pubkey hex: 0203a85ec401e66a218bf1583112599ee2a1268ebc90d91b7f457c87a50f2b011b
> extended pubkey: xpub695C7w8tgyFnCeZejvCr59q9W7jCpkkbBbomjX13CwfrLXFVUcPXfsmHGiSfpYds2JuHrXAFEoikMX6725W8VgrVL5x4ojBw9QFAPgtdw1G
This tools can be used to broadcast a raw transaction to peers retrived from a dns seed or specified by ip/port. The application will try to connect to max 6 peers, send the transaction two two of them and listens on the remaining ones if the transaction has been relayed back.
./bitcoin-send-tx <txhex>
./bitcoin-send-tx -d -t <txhex>
./bitcoin-send-tx -d -s 5 -i 192.168.1.110:8333,127.0.0.1:8333 <txhex>
You will need to have some dev tools prior to building libnspv for your system.
# Install brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install Xcode, opens a pop-up window to install CLT without installing the entire Xcode package
xcode-select --install
# Update brew and install dependencies
brew update
brew upgrade
brew install libsodium libevent automake libtool git wget
brew tap jl777/homebrew-libnspv
brew install libnspv
# Install Termux
https://play.google.com/store/apps/details?id=com.termux
# Open and run the following commands
pkg upgrade
pkg install build-essential git wget libsodium libevent
sudo apt-get -y install build-essential pkg-config libc6-dev m4 autoconf \
libtool unzip git wget automake
Once prepared your system, get the source code:
git clone https://github.com/jl777/libnspv/
cd libnspv
cd ./src/tools/cryptoconditions
./autogen.sh
./configure
make
cd ./src/tools/cryptoconditions
./build_win.sh
Go to libnspv directory and run:
On Linux and MacOS:
./autogen.sh
./configure
make check
On Windows run
./build_win.sh
Go to libnspv directory and run:
./autogen.sh
./configure --disable-wallet --disable-tools
make check
cd libnspv
git pull
./autogen.sh
./configure
make
libnspv does all the above, it also can launch a superlite nSPV client
./nspv KMD
launches a KMD superlite
./nspv BTC
launches the normal libbtc headers only SPV mode
for superlite clients, it will daemonize the nspv executable and it is accessible via JSON requests sent into the localhost rpc port. you can specify any available rpc port with -p option at the end.
there is a coins JSON file that comes from the https://github.com/jl777/coins/coins which will define the default behavior of the superlite if it is a komodo "asset" and also has the following fields:
"p2p": 12985,
"magic":"feb4cb23",
"nSPV":"5.9.102.210, 5.9.253.195, 5.9.253.196, 5.9.253.197, 5.9.253.198, 5.9.253.199, 5.9.253.200, 5.9.253.201, 5.9.253.202, 5.9.253.203"
the "p2p" field is the coin's peer to peer port, the "magic" is the netmagic of that chain. the decimal value of this can be obtained from the getinfo call, convert to hex and serialize it into the 4 hexbytes. if you got the direction wrong, just flip it around. finally, the "nSPV" field is a list of one or more ip addresses of a nSPV fullnode
the JSON api is very simple and almost a direct mapping of the nSPV=1 rpc commands in the komodod, the testvectors file at ~/libnspv/src/tools/testvectors will show specifically how to call it using curl. any other method to post the JSON to the rpc port will suffice.
https://docs.komodoplatform.com/nSPV has initial docs
Check if your device will use the new Termux repos: https://github.com/termux/termux-packages#information-for-android-7-users
pkg upgrade && pkg install build-essential git wget libsodium libevent
Other packages may be required.
git clone https://github.com/jl777/libnspv/
cd libnspv
Update scripts so target environment determined (from https://wiki.termux.com/wiki/Building_packages):
find . -name 'config.sub' -exec chmod u+w '{}' \; -exec cp -f "${PREFIX}/share/libtool/build-aux/config.sub" '{}' \;
find . -name 'config.guess' -exec chmod u+w '{}' \; -exec cp -f "${PREFIX}/share/libtool/build-aux/config.guess" '{}' \;
./androidonetime.sh
Start nspv
Then visit 127.0.0.1:7771 in your Android browser.