diff --git a/app/src/main/java/com/tangem/wallet/BTCUtils.java b/app/src/main/java/com/tangem/wallet/BTCUtils.java index 3985201cc7..1b3c8fe894 100644 --- a/app/src/main/java/com/tangem/wallet/BTCUtils.java +++ b/app/src/main/java/com/tangem/wallet/BTCUtils.java @@ -13,6 +13,10 @@ import com.tangem.wallet.btc.BitcoinException; import com.tangem.wallet.btc.BitcoinOutputStream; import com.tangem.wallet.btc.BtcData; +import org.bitcoinj.core.Address; +import org.bitcoinj.core.SegwitAddress; +import org.bitcoinj.script.ScriptBuilder; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigInteger; @@ -103,7 +107,15 @@ public final class BTCUtils { //8 bytes forSign.writeInt64(amount); //amount - byte[] sendScript = Transaction.Script.buildOutput(outputAddress).bytes; // build out + + byte[] sendScript; + if (outputAddress.startsWith("bc1") || outputAddress.startsWith("tb1")) { + Address address = SegwitAddress.fromBech32(null, outputAddress); + sendScript = ScriptBuilder.createOutputScript(address).getProgram(); + } else { + sendScript = Transaction.Script.buildOutput(outputAddress).bytes; // build out + } + //hex str 1976a914....88ac forSign.write((byte) sendScript.length); forSign.write(sendScript); diff --git a/app/src/main/java/com/tangem/wallet/btc/BtcEngine.java b/app/src/main/java/com/tangem/wallet/btc/BtcEngine.java index 0fe90dd9f0..d37f6a18a1 100644 --- a/app/src/main/java/com/tangem/wallet/btc/BtcEngine.java +++ b/app/src/main/java/com/tangem/wallet/btc/BtcEngine.java @@ -37,6 +37,10 @@ import com.tangem.wallet.TangemContext; import com.tangem.wallet.Transaction; import com.tangem.wallet.UnspentOutputInfo; +import org.bitcoinj.core.SegwitAddress; +import org.bitcoinj.params.MainNetParams; +import org.bitcoinj.params.TestNet3Params; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigDecimal; @@ -148,42 +152,52 @@ public class BtcEngine extends CoinEngine { return false; } - if (address.length() < 25) { - return false; - } - - if (address.length() > 35) { - return false; - } - - if (!address.startsWith("1") && !address.startsWith("2") && !address.startsWith("3") && !address.startsWith("n") && !address.startsWith("m")) { - return false; - } - - byte[] decAddress = Base58.decodeBase58(address); - - if (decAddress == null || decAddress.length == 0) { - return false; - } - - byte[] rip = new byte[21]; - for (int i = 0; i < 21; ++i) { - rip[i] = decAddress[i]; - } - - byte[] kcv = CryptoUtil.doubleSha256(rip); - - for (int i = 0; i < 4; ++i) { - if (kcv[i] != decAddress[21 + i]) + if (address.startsWith("1") || address.startsWith("2") || address.startsWith("3") || address.startsWith("n") || address.startsWith("m")) { + if (address.length() < 25) { return false; - } + } - if (ctx.getBlockchain() != Blockchain.BitcoinTestNet && ctx.getBlockchain() != Blockchain.Bitcoin) { - return false; - } + if (address.length() > 35) { + return false; + } - if (ctx.getBlockchain() == Blockchain.BitcoinTestNet && (address.startsWith("1") || address.startsWith("3"))) { - return false; + byte[] decAddress = Base58.decodeBase58(address); + + if (decAddress == null || decAddress.length == 0) { + return false; + } + + byte[] rip = new byte[21]; + for (int i = 0; i < 21; ++i) { + rip[i] = decAddress[i]; + } + + byte[] kcv = CryptoUtil.doubleSha256(rip); + + for (int i = 0; i < 4; ++i) { + if (kcv[i] != decAddress[21 + i]) + return false; + } + + if (ctx.getBlockchain() != Blockchain.BitcoinTestNet && ctx.getBlockchain() != Blockchain.Bitcoin) { + return false; + } + + if (ctx.getBlockchain() == Blockchain.BitcoinTestNet && (address.startsWith("1") || address.startsWith("3"))) { + return false; + } + } else { + try { + if (ctx.getBlockchain() == Blockchain.Bitcoin) { + SegwitAddress.fromBech32(new MainNetParams(), address); + } else if (ctx.getBlockchain() == Blockchain.BitcoinTestNet) { + SegwitAddress.fromBech32(new TestNet3Params(), address); + } else { + return false; + } + } catch (Exception e) { + return false; + } } return true;