Coverage Summary for Class: P2shP2wshBtcLockSender (co.rsk.peg.btcLockSender)

Class Class, % Method, % Line, %
P2shP2wshBtcLockSender 0% (0/1) 0% (0/5) 0% (0/29)


1 package co.rsk.peg.btcLockSender; 2  3 import co.rsk.bitcoinj.core.Address; 4 import co.rsk.bitcoinj.core.BtcTransaction; 5 import co.rsk.bitcoinj.core.Sha256Hash; 6 import co.rsk.bitcoinj.script.Script; 7 import co.rsk.core.RskAddress; 8 import org.ethereum.crypto.HashUtil; 9 import org.ethereum.util.ByteUtil; 10  11 public class P2shP2wshBtcLockSender implements BtcLockSender { 12  13  private TxSenderAddressType txSenderAddressType; 14  private Address btcAddress; 15  16  public P2shP2wshBtcLockSender() { 17  this.txSenderAddressType = TxSenderAddressType.P2SHP2WSH; 18  } 19  20  @Override 21  public TxSenderAddressType getTxSenderAddressType() { 22  return txSenderAddressType; 23  } 24  25  @Override 26  public Address getBTCAddress() { 27  return this.btcAddress; 28  } 29  30  @Override 31  public RskAddress getRskAddress() { 32  return null; 33  } 34  35  @Override 36  public boolean tryParse(BtcTransaction btcTx) { 37  if (btcTx == null) { 38  return false; 39  } 40  if (!btcTx.hasWitness()) { 41  return false; 42  } 43  if (btcTx.getInput(0).getScriptBytes() == null) { 44  return false; 45  } 46  if (btcTx.getInput(0).getScriptSig().getChunks().size() != 1) { 47  return false; 48  } 49  if (btcTx.getWitness(0).getPushCount() < 3) { //At least 3 pushes: a 0, at least 1 signature, redeem script 50  return false; 51  } 52  53  int pushesLength = btcTx.getWitness(0).getPushCount(); 54  byte[] redeemScript = btcTx.getWitness(0).getPush(pushesLength - 1); //Redeem script is the last push of the witness 55  56  Script redeem = new Script(redeemScript); 57  if (!redeem.isSentToMultiSig()) { 58  return false; 59  } 60  61  try { 62  // Get btc address 63  // witnessVersion = 0x00 64  // push32 = 0x20 65  // scriptPubKey = hash160(sha256(witnessVersion push32 redeemScript)) 66  // Ref: https://github.com/bitcoinbook/bitcoinbook/blob/22a5950abfdd3dbd629c88534e50472822f0e356/ch07.asciidoc#pay-to-witness-public-key-hash-inside-pay-to-script-hash 67  byte[] redeemScriptHash = Sha256Hash.hash(redeemScript); 68  byte[] merged = ByteUtil.merge(new byte[]{0x00, 0x20}, redeemScriptHash); 69  byte[] hashedAgain = Sha256Hash.hash(merged); 70  byte[] scriptPubKey = HashUtil.ripemd160(hashedAgain); 71  72  this.btcAddress = new Address(btcTx.getParams(), btcTx.getParams().getP2SHHeader(), scriptPubKey); 73  } catch (Exception e) { 74  return false; 75  } 76  77  return true; 78  } 79 }