public TxOut GetOutputFor(TxIn input) { Coins coins = GetCoins(input.PrevOut.Hash); if(!coins.IsAvailable(input.PrevOut.N)) { return null; } return coins.Outputs[(int)input.PrevOut.N]; }
public static AddressType GetAddressType(this NBitcoin.TxIn tx) { var type = tx.ScriptSig.FindTemplate().Type; switch (type) { case TxOutType.TX_PUBKEY: return(AddressType.PublicKey); case TxOutType.TX_PUBKEYHASH: return(AddressType.PublicKeyHash); case TxOutType.TX_MULTISIG: return(AddressType.MultiplePublicKeyHashes); case TxOutType.TX_SCRIPTHASH: return(AddressType.ScriptHash); } throw new NotSupportedException("Not supported yet"); }
public TxIn AddInput(Transaction prevTx, int outIndex) { if(outIndex >= prevTx.Outputs.Count) throw new InvalidOperationException("Output " + outIndex + " is not present in the prevTx"); var @in = new TxIn(); @in.PrevOut.Hash = prevTx.GetHash(); @in.PrevOut.N = (uint)outIndex; AddInput(@in); return @in; }
public TxIn AddInput(TxIn @in) { this.vin.Add(@in); return @in; }
internal TxOut GetOutputFor(TxIn txIn) { TxOut txout = null; _TxOutByOutpoint.TryGetValue(txIn.PrevOut, out txout); return txout; }
public TxIn AddInput(TxIn @in) { this.vin.Add(@in); return(@in); }
internal PSBTInput(BitcoinStream stream, PSBT parent, uint index, TxIn input) : base(parent) { TxIn = input; Index = index; originalScriptSig = TxIn.ScriptSig ?? Script.Empty; originalWitScript = TxIn.WitScript ?? WitScript.Empty; byte[] k = new byte[0]; byte[] v = new byte[0]; try { stream.ReadWriteAsVarString(ref k); } catch (EndOfStreamException e) { throw new FormatException("Invalid PSBTInput. Failed to Parse key.", e); } while (k.Length != 0) { try { stream.ReadWriteAsVarString(ref v); } catch (EndOfStreamException e) { throw new FormatException("Invalid PSBTInput. Failed to parse key.", e); } switch (k.First()) { case PSBTConstants.PSBT_IN_NON_WITNESS_UTXO: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for NonWitnessUTXO"); } if (non_witness_utxo != null) { throw new FormatException("Invalid PSBTInput. Duplicate non_witness_utxo"); } non_witness_utxo = this.GetConsensusFactory().CreateTransaction(); non_witness_utxo.FromBytes(v); break; case PSBTConstants.PSBT_IN_WITNESS_UTXO: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for WitnessUTXO"); } if (witness_utxo != null) { throw new FormatException("Invalid PSBTInput. Duplicate witness_utxo"); } if (this.GetConsensusFactory().TryCreateNew <TxOut>(out var txout)) { witness_utxo = txout; } else { witness_utxo = new TxOut(); } witness_utxo.FromBytes(v); break; case PSBTConstants.PSBT_IN_PARTIAL_SIG: var pubkey = new PubKey(k.Skip(1).ToArray()); if (partial_sigs.ContainsKey(pubkey)) { throw new FormatException("Invalid PSBTInput. Duplicate key for partial_sigs"); } partial_sigs.Add(pubkey, new TransactionSignature(v)); break; case PSBTConstants.PSBT_IN_SIGHASH: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for SigHash type"); } if (!(sighash_type is null)) { throw new FormatException("Invalid PSBTInput. Duplicate key for sighash_type"); } if (v.Length != 4) { throw new FormatException("Invalid PSBTInput. SigHash Type is not 4 byte"); } var value = Utils.ToUInt32(v, 0, true); if (!Enum.IsDefined(typeof(SigHash), value)) { throw new FormatException($"Invalid PSBTInput Unknown SigHash Type {value}"); } sighash_type = (SigHash)value; break; case PSBTConstants.PSBT_IN_REDEEMSCRIPT: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for redeem script"); } if (redeem_script != null) { throw new FormatException("Invalid PSBTInput. Duplicate key for redeem_script"); } redeem_script = Script.FromBytesUnsafe(v); break; case PSBTConstants.PSBT_IN_WITNESSSCRIPT: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for witness script"); } if (witness_script != null) { throw new FormatException("Invalid PSBTInput. Duplicate key for redeem_script"); } witness_script = Script.FromBytesUnsafe(v); break; case PSBTConstants.PSBT_IN_BIP32_DERIVATION: var pubkey2 = new PubKey(k.Skip(1).ToArray()); if (hd_keypaths.ContainsKey(pubkey2)) { throw new FormatException("Invalid PSBTInput. Duplicate key for hd_keypaths"); } var masterFingerPrint = new HDFingerprint(v.Take(4).ToArray()); KeyPath path = KeyPath.FromBytes(v.Skip(4).ToArray()); hd_keypaths.Add(pubkey2, new RootedKeyPath(masterFingerPrint, path)); break; case PSBTConstants.PSBT_IN_SCRIPTSIG: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for final scriptsig"); } if (final_script_sig != null) { throw new FormatException("Invalid PSBTInput. Duplicate key for final_script_sig"); } final_script_sig = Script.FromBytesUnsafe(v); break; case PSBTConstants.PSBT_IN_SCRIPTWITNESS: if (k.Length != 1) { throw new FormatException("Invalid PSBTInput. Contains illegal value in key for final script witness"); } if (final_script_witness != null) { throw new FormatException("Invalid PSBTInput. Duplicate key for final_script_witness"); } final_script_witness = new WitScript(v); break; default: if (unknown.ContainsKey(k)) { throw new FormatException("Invalid PSBTInput. Duplicate key for unknown value"); } unknown.Add(k, v); break; } stream.ReadWriteAsVarString(ref k); } }
public static TxInHelper GetUnSpentTransactionFromAddress(string address, decimal amount, bool isTest = false) { try { var blockCount = isTest ? GetBTCBlockCount(true) : GetBTCBlockCount(); TxInHelper txInHelper = new TxInHelper(); txInHelper.inputs = new List <NBitcoin.TxIn>(); List <NBitcoin.TxIn> inputs = new List <NBitcoin.TxIn>(); var addressData = BLL.BlockchainCom.API.GetAddressDataByAddress(address, isTest); List <BLL.BlockchainCom.Data.TransactionData> unSpentList = new List <BLL.BlockchainCom.Data.TransactionData>(); foreach (var item in addressData.Txs) { var outFound = item.Out.FirstOrDefault(p => p.Addr == address && p.Spent == false); //all the unspent btc from this address. if (outFound != null) { //Before adding this transaction check this is not a case for me sending to my self. var coinSendToItSelf = false; var meSending = item.Inputs.Count(p => p.Prev_out.Addr == address) == item.Inputs.Count(); if (meSending) //which mean: I am the only one who send this transaction. { var meGetting = item.Out.Count(p => p.Addr == address) == item.Out.Count(); if (meGetting) { coinSendToItSelf = true; } } if (coinSendToItSelf == false) { unSpentList.Add(item); } } } txInHelper.Sum = 0m; foreach (var item in unSpentList) { if (txInHelper.Sum > amount) { return(txInHelper); } var TransactionHash = item.Hash; //Before I sent this money, I need to make sure that it has a few blocks over. var numberOfConfirmation = GetConfirmationNumber(item.Block_height, blockCount); var transactionCreatedByOwner = item.Inputs.FirstOrDefault(p => p.Prev_out.Addr == address); bool trustedTransaction = false; if (transactionCreatedByOwner != null && numberOfConfirmation >= 1) //trust your own transaction change. { trustedTransaction = true; } if (trustedTransaction || numberOfConfirmation >= NumberOfConfirmations) { var outData = item.Out.Where(p => p.Addr == address).FirstOrDefault(); //To my best understanding there can be only one out with my address if (outData != null) { txInHelper.Sum += outData.Value * Satoshi; NBitcoin.TxIn txIn = new NBitcoin.TxIn(); txIn.PrevOut = new NBitcoin.OutPoint(new NBitcoin.uint256(TransactionHash), outData.N); txInHelper.inputs.Add(txIn); } } } return(txInHelper); } catch (Exception ex) { throw ex; } }
private Exception CoinNotFound(TxIn txIn) { return(new KeyNotFoundException("Impossible to find the scriptPubKey of outpoint " + txIn.PrevOut)); }