def signTx txBytes Array Byte partialSigning Boolean Try Array Byte Tr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def signTx(txBytes: Array[Byte], partialSigning: Boolean): Try[Array[Byte]] = Try {
val possibleOutputs = wallet.getWatchedOutputs(false) ++ injectedTx.getOutputs
val tx = new Transaction(params, txBytes)
tx.getInputs.foreach { input =>
Option(possibleOutputs.get(0)) match { // .find(_.getHash == input.getOutpoint.getHash) match {
case Some(output) =>
input.connect(output)
val script = output.getScriptPubKey
if (partialSigning) {
if (!script.isPayToScriptHash) {
throw new IllegalStateException("Not a p2sh output script!")
} else {
val redeem = input.getScriptSig
val secondKeyBytes = EncodingUtils.hex2bytes("c6b56218d28b36b9c043e1ef6009c7a1d3c896b24cf41c8f5dc784df44dcc5ba")
val secondKey = ECKey.fromPrivate(secondKeyBytes)
val signature = tx.calculateSignature(0, key, redeem, SigHash.ALL, false)
val signature2 = tx.calculateSignature(0, secondKey, redeem, SigHash.ALL, false)
val ss = ScriptBuilder.createP2SHMultiSigInputScript(List(signature,signature2), redeem)
input.setScriptSig(ss)
}
} else {
val signature = tx.calculateSignature(0, key, script, Transaction.SigHash.ALL, false)
val ss = new ScriptBuilder().data(signature.encodeToBitcoin()).data(key.getPubKey).build()
input.setScriptSig(ss)
}
case None => throw new IllegalStateException("No output to connect")
}
}
tx.unsafeBitcoinSerialize().ensuring(!_.sameElements(txBytes))
}