Creating a Private Key
There are several classes for working with private keys, all with slightly different intent.
|PrivateKey||A simple wrapper around an ECKey|
|DumpedPrivateKey||Parses and generates private keys in bitcoin’s serialised text format|
|ECKey||Represents an elliptic curve public and (optionally) private key|
import org.twostack.bitcoin4j.ECKey import org.twostack.bitcoin4j.PrivateKey //creates random private key and public keys (generates random point on elliptic curve) val ecKey = ECKey() //create a PrivateKey to wrap the ECKey val privateKey = PrivateKey(ecKey);
Private Keys do not directly have anything to do with network selection. However, the PrivateKey() and DumpedPrivateKey() classes have convenience methods for serializing to user-friendly text representation (WIF), which does encapsulate the NetworkType in it’s envelope as a signal to Wallets.
You can select from the following networks (note that all the TEST networks are functionally equivalent in terms of Private Keys. i.e. selecting any of them results in the same serialised WIF text )
|MAIN||The Bitcoin Mainnet|
|REGTEST||The Regression Test Network (local node)|
|TEST||The Public Test network|
|SCALING_TESTNET||The Public High-volume test network|
Exporting to WIF
The Wallet-import-format (WIF) is a widely used standard for serialising private keys to base58 text encoding that make them less prone to errors during copying since they include a built-in checksum.
import org.twostack.bitcoin4j.ECKey import org.twostack.bitcoin4j.PrivateKey val privateKey = ECKey(); val dpk = DumpedPrivateKey(NetworkType.TEST, privateKey.getPrivKeyBytes(), true); var wifString = dpk.toBase58();
Importing from WIF
import org.twostack.bitcoin4j.params.NetworkType; import org.twostack.bitcoin4j.crypto.DumpedPrivateKey; val privkeyWif = "92shANodC6Y4evT5kFzjNFQAdjqTtHAnDTLzqBBq4BbKUPyx6CD"; val key: ECKey = DumpedPrivateKey.fromBase58(NetworkType.TEST, privkeyWif).getKey();