use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.
the class PSBT method getPublicCopy.
public PSBT getPublicCopy() {
try {
PSBT publicCopy = new PSBT(serialize());
publicCopy.extendedPublicKeys.clear();
publicCopy.globalProprietary.clear();
for (PSBTInput psbtInput : publicCopy.getPsbtInputs()) {
psbtInput.getDerivedPublicKeys().clear();
psbtInput.getProprietary().clear();
}
for (PSBTOutput psbtOutput : publicCopy.getPsbtOutputs()) {
psbtOutput.getDerivedPublicKeys().clear();
psbtOutput.getProprietary().clear();
}
return publicCopy;
} catch (PSBTParseException e) {
throw new IllegalStateException("Could not parse PSBT", e);
}
}
use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.
the class PSBT method extractTransaction.
public Transaction extractTransaction() {
boolean hasWitness = false;
for (PSBTInput psbtInput : getPsbtInputs()) {
if (psbtInput.getFinalScriptWitness() != null) {
hasWitness = true;
}
}
Transaction finalTransaction = new Transaction(transaction.bitcoinSerialize());
if (hasWitness && !finalTransaction.isSegwit()) {
finalTransaction.setSegwitFlag(Transaction.DEFAULT_SEGWIT_FLAG);
}
for (int i = 0; i < finalTransaction.getInputs().size(); i++) {
TransactionInput txInput = finalTransaction.getInputs().get(i);
PSBTInput psbtInput = getPsbtInputs().get(i);
txInput.setScriptBytes(psbtInput.getFinalScriptSig() == null ? new byte[0] : psbtInput.getFinalScriptSig().getProgram());
if (hasWitness) {
if (psbtInput.getFinalScriptWitness() != null) {
txInput.setWitness(psbtInput.getFinalScriptWitness());
} else {
txInput.setWitness(new TransactionWitness(finalTransaction));
}
}
}
return finalTransaction;
}
use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.
the class PSBT method getFee.
public Long getFee() {
long fee = 0L;
for (PSBTInput input : psbtInputs) {
TransactionOutput utxo = input.getUtxo();
if (utxo != null) {
fee += utxo.getValue();
} else {
log.error("Cannot determine fee - not enough information provided on inputs");
return null;
}
}
for (int i = 0; i < transaction.getOutputs().size(); i++) {
TransactionOutput output = transaction.getOutputs().get(i);
fee -= output.getValue();
}
return fee;
}
use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.
the class PSBT method parseInputEntries.
private void parseInputEntries(List<List<PSBTEntry>> inputEntryLists, boolean verifySignatures) throws PSBTParseException {
for (List<PSBTEntry> inputEntries : inputEntryLists) {
PSBTEntry duplicate = findDuplicateKey(inputEntries);
if (duplicate != null) {
throw new PSBTParseException("Found duplicate key for PSBT input: " + Utils.bytesToHex(duplicate.getKey()));
}
int inputIndex = this.psbtInputs.size();
PSBTInput input = new PSBTInput(this, inputEntries, transaction, inputIndex);
this.psbtInputs.add(input);
}
if (verifySignatures) {
for (PSBTInput input : psbtInputs) {
boolean verified = input.verifySignatures();
if (!verified && input.getPartialSignatures().size() > 0) {
throw new PSBTSignatureException("Unverifiable partial signatures provided");
}
}
}
}
use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.
the class Wallet method sign.
public void sign(PSBT psbt) throws MnemonicException {
Map<PSBTInput, WalletNode> signingNodes = getSigningNodes(psbt);
for (Map.Entry<PSBTInput, WalletNode> signingEntry : signingNodes.entrySet()) {
Wallet signingWallet = signingEntry.getValue().getWallet();
for (Keystore keystore : signingWallet.getKeystores()) {
if (keystore.hasPrivateKey()) {
ECKey privKey = signingWallet.getScriptType().getOutputKey(keystore.getKey(signingEntry.getValue()));
PSBTInput psbtInput = signingEntry.getKey();
if (!psbtInput.isSigned()) {
psbtInput.sign(privKey);
}
}
}
}
}
Aggregations