Class WalletProtobufSerializer
wallet.proto
file in the bitcoinj source distribution.
The most common operations are writeWallet and readWallet, which do
the obvious operations on Output/InputStreams. You can use a ByteArrayInputStream
and equivalent
ByteArrayOutputStream
if you'd like byte arrays instead. The protocol buffer can also be manipulated
in its object form if you'd like to modify the flattened data structure before serialization to binary.
You can extend the wallet format with additional fields specific to your application if you want, but make sure to either put the extra data in the provided extension areas, or select tag numbers that are unlikely to be used by anyone else.
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Current version used for serializing wallets.protected Map<com.google.protobuf.ByteString,
Transaction> -
Constructor Summary
ConstructorDescription -
Method Summary
Modifier and TypeMethodDescriptionstatic Sha256Hash
byteStringToHash
(com.google.protobuf.ByteString bs) static com.google.protobuf.ByteString
hashToByteString
(Sha256Hash hash) static boolean
isWallet
(InputStream is) Cheap test to see if input stream is a wallet.static org.bitcoinj.protobuf.wallet.Protos.Wallet
parseToProto
(InputStream input) Returns the loaded protocol buffer from the given byte stream.readWallet
(InputStream input, boolean forceReset, WalletExtension[] extensions) Loads wallet data from the given protocol buffer and inserts it into the given Wallet object.readWallet
(InputStream input, WalletExtension... walletExtensions) Loads wallet data from the given protocol buffer and inserts it into the given Wallet object.readWallet
(Network network, WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto) Loads wallet data from the given protocol buffer and inserts it into the given Wallet object.readWallet
(Network network, WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto, boolean forceReset) Loads wallet data from the given protocol buffer and inserts it into the given Wallet object.readWallet
(NetworkParameters params, WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto) Deprecated.readWallet
(NetworkParameters params, WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto, boolean forceReset) Deprecated.void
setKeyChainFactory
(KeyChainFactory keyChainFactory) void
setRequireAllExtensionsKnown
(boolean value) If this property is set to true, the wallet will fail to load if any found extensions are unknown..void
setRequireMandatoryExtensions
(boolean value) If this property is set to false, then unknown mandatory extensions will be ignored instead of causing load errors.void
setWalletWriteBufferSize
(int walletWriteBufferSize) Change buffer size for writing wallet to output stream.org.bitcoinj.protobuf.wallet.Protos.Wallet
walletToProto
(Wallet wallet) Converts the given wallet to the object representation of the protocol buffers.walletToText
(Wallet wallet) Returns the given wallet formatted as text.void
writeWallet
(Wallet wallet, OutputStream output) Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.
-
Field Details
-
CURRENT_WALLET_VERSION
public static final int CURRENT_WALLET_VERSIONCurrent version used for serializing wallets. A version higher than this is considered from the future. -
txMap
-
-
Constructor Details
-
WalletProtobufSerializer
public WalletProtobufSerializer() -
WalletProtobufSerializer
-
-
Method Details
-
setKeyChainFactory
-
setRequireMandatoryExtensions
public void setRequireMandatoryExtensions(boolean value) If this property is set to false, then unknown mandatory extensions will be ignored instead of causing load errors. You should only use this if you know exactly what you are doing, as the extension data will NOT be round-tripped, possibly resulting in a corrupted wallet if you save it back out again. -
setRequireAllExtensionsKnown
public void setRequireAllExtensionsKnown(boolean value) If this property is set to true, the wallet will fail to load if any found extensions are unknown.. -
setWalletWriteBufferSize
public void setWalletWriteBufferSize(int walletWriteBufferSize) Change buffer size for writing wallet to output stream. Default isCodedOutputStream.DEFAULT_BUFFER_SIZE
- Parameters:
walletWriteBufferSize
- - buffer size in bytes
-
writeWallet
Formats the given wallet (transactions and keys) to the given output stream in protocol buffer format.Equivalent to
walletToProto(wallet).writeTo(output);
- Throws:
IOException
-
walletToText
Returns the given wallet formatted as text. The text format is that used by protocol buffers and it is designed more for debugging than storage. It is not well specified and wallets are largely binary data structures anyway, consisting as they do of keys (large random numbers) andTransaction
s which also mostly contain keys and hashes. -
walletToProto
Converts the given wallet to the object representation of the protocol buffers. This can be modified, or additional data fields set, before serialization takes place. -
hashToByteString
-
byteStringToHash
-
readWallet
public Wallet readWallet(InputStream input, @Nullable WalletExtension... walletExtensions) throws UnreadableWalletException Loads wallet data from the given protocol buffer and inserts it into the given Wallet object. This is primarily useful when you wish to pre-register extension objects. Note that if loading fails the provided Wallet object may be in an indeterminate state and should be thrown away.
A wallet can be unreadable for various reasons, such as inability to open the file, corrupt data, internally inconsistent data, a wallet extension marked as mandatory that cannot be handled and so on. You should always handle
UnreadableWalletException
and communicate failure to the user in an appropriate manner.- Throws:
UnreadableWalletException
- thrown in various error conditions (see description).
-
readWallet
public Wallet readWallet(InputStream input, boolean forceReset, @Nullable WalletExtension[] extensions) throws UnreadableWalletException Loads wallet data from the given protocol buffer and inserts it into the given Wallet object. This is primarily useful when you wish to pre-register extension objects. Note that if loading fails the provided Wallet object may be in an indeterminate state and should be thrown away. Do not simply call this method again on the same Wallet object with
forceReset
settrue
. It won't work.If
forceReset
istrue
, then no transactions are loaded from the wallet, and it is configured to replay transactions from the blockchain (as if the wallet had been loaded andWallet.reset()
had been called immediately thereafter).A wallet can be unreadable for various reasons, such as inability to open the file, corrupt data, internally inconsistent data, a wallet extension marked as mandatory that cannot be handled and so on. You should always handle
UnreadableWalletException
and communicate failure to the user in an appropriate manner.- Throws:
UnreadableWalletException
- thrown in various error conditions (see description).
-
readWallet
public Wallet readWallet(Network network, @Nullable WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto) throws UnreadableWalletException Loads wallet data from the given protocol buffer and inserts it into the given Wallet object. This is primarily useful when you wish to pre-register extension objects. Note that if loading fails the provided Wallet object may be in an indeterminate state and should be thrown away.
A wallet can be unreadable for various reasons, such as inability to open the file, corrupt data, internally inconsistent data, a wallet extension marked as mandatory that cannot be handled and so on. You should always handle
UnreadableWalletException
and communicate failure to the user in an appropriate manner.- Throws:
UnreadableWalletException
- thrown in various error conditions (see description).
-
readWallet
@Deprecated public Wallet readWallet(NetworkParameters params, @Nullable WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto) throws UnreadableWalletException Deprecated.- Throws:
UnreadableWalletException
-
readWallet
public Wallet readWallet(Network network, @Nullable WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto, boolean forceReset) throws UnreadableWalletException Loads wallet data from the given protocol buffer and inserts it into the given Wallet object. This is primarily useful when you wish to pre-register extension objects. Note that if loading fails the provided Wallet object may be in an indeterminate state and should be thrown away. Do not simply call this method again on the same Wallet object with
forceReset
settrue
. It won't work.If
forceReset
istrue
, then no transactions are loaded from the wallet, and it is configured to replay transactions from the blockchain (as if the wallet had been loaded andWallet.reset()
had been called immediately thereafter).A wallet can be unreadable for various reasons, such as inability to open the file, corrupt data, internally inconsistent data, a wallet extension marked as mandatory that cannot be handled and so on. You should always handle
UnreadableWalletException
and communicate failure to the user in an appropriate manner.- Throws:
UnreadableWalletException
- thrown in various error conditions (see description).
-
readWallet
@Deprecated public Wallet readWallet(NetworkParameters params, @Nullable WalletExtension[] extensions, org.bitcoinj.protobuf.wallet.Protos.Wallet walletProto, boolean forceReset) throws UnreadableWalletException Deprecated.- Throws:
UnreadableWalletException
-
parseToProto
public static org.bitcoinj.protobuf.wallet.Protos.Wallet parseToProto(InputStream input) throws IOException Returns the loaded protocol buffer from the given byte stream. You normally wantWallet.loadFromFile(File, WalletExtension...)
instead - this method is designed for low level work involving the wallet file format itself.- Throws:
IOException
-
isWallet
Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.- Parameters:
is
- input stream to test- Returns:
- true if input stream is a wallet
-
readWallet(Network, WalletExtension[], Protos.Wallet)