Adding an external key pair to a local bitcoinj wallet

0

I need to add a vanity address to a local wallet that I maintain using bitcoinj APIs. I'm not able to search for the vanity using bitcoinj APIs directly, on my local PC, because my vanity is too fancy. However, I'm able to generate the pair online easily. What is the API I should use to add this key pair to my local wallet ?

I tried the following,

Say the required vanity is 1meowvYmB7ughhsRvUWfsac9LKDonh4cn (private key: 5JXFsJEAiqtTv95rSmw3KSJeAZ97SsmKWTQq5yqBzioSJ1guUHK). I generated this at bitcoinvanitygen, I converted this to hex at, Base58 Encode, Decode, and Validate. I tried the following.

Address vanity = new Address(networkParameters, networkParameters.getAddressHeader(), Hex.decode("000871DD31D517D15D7352139A44B07EBE97B1772C7F291357"));

I need to construct an Address from this string. I get the following error:

exception decoding Hex string: String index out of range: 47

rranjik

Posted 2018-10-08T18:20:05.757

Reputation: 11

Note: generating private keys online is very risky! Doing so may leave your funds at risk.chytrik 2018-10-08T20:56:10.887

Answers

1

After searching for a while I found this type that takes a private key (in base58), required network parameters, and constructs the public address. Please refer: DumpedPrivateKey. This was enough for me at least. I construct an address from the key so formed using the toAddress method exported from the type key.

DumpedPrivateKey vanityKey = DumpedPrivateKey.fromBase58(networkParameters, "5JXFsJEAiqtTv95rSmw3KSJeAZ97SsmKWTQq5yqBzioSJ1guUHK");
ECKey ECVanityKey = vanityKey.getKey();
Address vanityAddress = ECVanityKey.toAddress(networkParameters);

rranjik

Posted 2018-10-08T18:20:05.757

Reputation: 11