I am new to bitcoins, I want to create an offline wallet on Android, to scan transaction QR code from online Desktop wallet, Sign that transaction with private key on my offline wallet.
1 Creating offline wallet. 2.Signing transaction offline with private key.
I want to sign transaction and generate QR code from that sign transaction.But after signing i get a sha256 in this form "MEQCIGBVDN/PkbESZdWkG6/KzrDRAEpDXVdsjKMzErBfFIWYAiA8JJOv97Dlp8Acg/L8JHI3RzoW eYNxPW1Lx4wQaORNNQ==", so what will i do with this. So please help and review my code. Is my signing process ok or if not please comeup with some code.Thanks
So for i did the following.
// For creating wallet
private void InitilizeWallet()
throws IOException {
BriefLogFormatter.init();
params = TestNet3Params.get();
filePrefix = "forwarding-service-testnet";
walletAppKit = new WalletAppKit(params, getCacheDir(), filePrefix) {
@Override
protected void onSetupCompleted() {
if (wallet().getKeyChainGroupSize() < 1)
wallet().importKey(new ECKey());
deterministicKey = wallet().getWatchingKey().dropPrivateBytes();
deterministicKey = HDKeyDerivation.createMasterPubKeyFromBytes(deterministicKey.getPubKey(), deterministicKey.getChainCode());
xPublicKey = deterministicKey.serializePubB58(params); privateKey=wallet().getKeyByPath(DeterministicKeyChain.ACCOUNT_ZERO_PATH).getPrivateKeyAsWiF(params);
Log.e("key", xPublicKey.toString());
Log.e("privatekey", privateKey.toString());
}
};
if (params == RegTestParams.get()) {
// Regression test mode is designed for testing and development only, so there's no public network for it.
// If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
walletAppKit.connectToLocalHost();
}
// Download the block chain and wait until it's done.
walletAppKit.startAsync();
walletAppKit.awaitRunning();
}
And for signing transaction i do the following.
public void Createtransictionhash(String recipientAddress, String amount) {
try {
// i am getting address and coins from QR code
SendRequest request = SendRequest.to(Address.fromBase58(params,
recipientAddress), Coin.parseCoin(amount));
Signingtrasaction(MainActivity.privateKey,request.tx.getHashAsString());
Log.e("txhash", request.tx.getHashAsString());
} catch (Exception e) {
Log.e("msgError", e.getMessage().toString());
Toast.makeText(getApplicationContext(), " Version code of address did not match", Toast.LENGTH_SHORT).show();
}
}
public void Signingtrasaction(String wif, String msg) {
try {
// message (hash) to be signed with private key
//String msg = "15953935a135031bfec37d36a9d662aea43e1deb0ea463d6932ac6e537cb3e81";
//my hash = 09b14f746bd0a93b71907ba0070a103adbee7b1a260e053a21aa0b660ad8de57
// an example of WiF for private key (taken from 'Mastering Bitcoin')
// wif ="KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ";
// creating a key object from WiF
DumpedPrivateKey dpk = DumpedPrivateKey.fromBase58(params, wif);
ECKey key = dpk.getKey();
// checking our key object
// NetworkParameters main = MainNetParams.get();
String check = key.getPrivateKeyAsWiF(params);
System.out.println(wif.equals(check)); // true
Log.e("wif check", String.valueOf(wif.equals(check)));
// creating Sha object from string
Sha256Hash hash = Sha256Hash.wrap(msg);
// creating signature
ECKey.ECDSASignature sig = key.sign(hash);
// encoding
byte[] res = sig.encodeToDER();
// converting to hex
//String hex = DatatypeConverter.printHexBinary(res);
// String hex = new String(res);
String hex = android.util.Base64.encodeToString(res, 16);
Log.e("sigendTransiction", hex.toString());
Log.e("decrypttx",""+ Hex.decode(sig.encodeToDER()));
} catch (Exception e) { //signingkey = ecdsa.from_string(privateKey.decode('hex'), curve=ecdsa.SECP256k1)
Log.e("signing exception", e.getMessage().toString());
}
}