Skip to content

Instantly share code, notes, and snippets.

@erickva
Created May 10, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickva/c346fca5ca3c2500cfcfe9a961e9ec6e to your computer and use it in GitHub Desktop.
Save erickva/c346fca5ca3c2500cfcfe9a961e9ec6e to your computer and use it in GitHub Desktop.
val mainParams = MainNetParams.get()
// Create transaction
val tx = Transaction(mainParams)
val wallet = Wallet(mainParams)
// decode private key
val privateKey = BIP38PrivateKey.fromBase58(mainParams, encryptedKey).decryptWithParams(decryptKey, 2048, 8, 8)
// Import private key
wallet.importKey(privateKey)
// Create address
val senderAddress = Address.fromString(mainParams, address)
val merchantAddress = Address.fromString(mainParams, recipient)
// Create Script (scriptPubKey)
var sc = Script(Hex.decode("76a914f374716268eb1d461228fb1e0169863b9933720e88ac"))
// Add Output
val amountLong = round(amount) * 100000000
tx.addOutput(Coin.valueOf( amountLong ), senderAddress)
tx.addOutput(Coin.valueOf( amountLong ), merchantAddress)
SAPIService.getInputsAndFee(this, address, amount) { txIdArr, indexArr, fee, error ->
println(txIdArr)
println(indexArr)
println(fee)
println(error)
if (error != null || txIdArr.size != indexArr.size) {
showToast(getString(R.string.Something_went_wrong_Try_again))
enableSpinner(false)
} else {
for (i in txIdArr.indices) {
val prevOut = TransactionOutPoint(mainParams, indexArr[i].toLong(), Sha256Hash(txIdArr[i]))
tx.addSignedInput(prevOut, sc, privateKey)
}
Log.d("SmartCash", HEX.encode(tx.bitcoinSerialize()))
enableSpinner(false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment