public static string Transfer(String fromAddress, Int64 amount, String toAddress, String assetId, String senderWifKey) { NBitcoin.Network network = NBitcoin.Network.TestNet; NBitcoin.BitcoinSecret _key = NBitcoin.Network.TestNet.CreateBitcoinSecret(senderWifKey); //Get bitcoin balance, 0.0006 is required Decimal fundingBalance = 0; const Decimal BITCOIN_TRANSACTION_FEE = 0.00006M; NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress); NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress); //UTXOS CoinPrism txRepo = new CoinPrism(true); var ccutoxs = txRepo.GetTransactions(fromAddress); var utxos = txRepo.Get(fromAddress).Where(ux => ux.value > 10000).ToList(); //hard coded, worked //transaction_hash //var coin = new Coin(fromTxHash: new uint256("f131c91b3ce35f83258a3db54f5547daecc2a61142f598a39a8491ed2844eb28"), // fromOutputIndex: 0, // amount: Money.Satoshis(3000), //20000 // scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914b9ad2f3f358c24ec207abf72125790e67301284488ac"))); //var forfees = new Coin(fromTxHash: new uint256("a86a907f36071754ff4fdc4e27de4ec92272f239de5236af5f4806133ac66d0f"), // fromOutputIndex: 0, // amount: Money.Satoshis(97322000), //9957600 // scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914b9ad2f3f358c24ec207abf72125790e67301284488ac"))); //Coin from Bob var coin = new Coin(fromTxHash: new uint256("dc19133d57bf9013d898bd89198069340d8ca99d71f0d5f6c6e142d724a9ba92"), fromOutputIndex: 0, amount: Money.Satoshis(600), //20000 scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey); //Coin from Alice var forfees = new Coin(fromTxHash: new uint256("b4326462d6d3b522d7e2c06d9c904313f546395eb62661b06b57195691f5fe5f"), fromOutputIndex: 1, amount: Money.Coins(1m), //9957600 scriptPubKey: BitcoinAddress.Create("muJjaSHk99LGMnaFduU9b3pWHdT1ZRPASF").ScriptPubKey); //var coin = new Coin(fromTxHash: new uint256(utxos[0].transaction_hash), // fromOutputIndex: utxos[0].output_index, // amount: Money.Satoshis(60000), //20000 // scriptPubKey: new Script(Encoders.Hex.DecodeData(utxos[0].script_hex))); //var forfees = new Coin(fromTxHash: new uint256(utxos[1].transaction_hash), // fromOutputIndex: utxos[1].output_index, // amount: Money.Satoshis(60000), //20000 // scriptPubKey: new Script(Encoders.Hex.DecodeData(utxos[1].script_hex))); BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); var alice = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet); ulong u = Convert.ToUInt64(amount); ColoredCoin colored = coin.ToColoredCoin(assetIdx, u); //var satoshi = Key.Parse(, Network.TestNet); string aliceWIF = "cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH"; var satoshi = Key.Parse(aliceWIF, Network.TestNet); ; var bobKey = new BitcoinSecret("cMdLBsUCQ92VSRmqfEL4TgJCisWpjVBd8GsP2mAmUZxQ9bh5E7CN"); var aliceKey = new BitcoinSecret("cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH"); var txBuilder = new TransactionBuilder(); var tx = txBuilder .AddKeys(bobKey,aliceKey) .AddCoins(forfees, colored) .SendAsset(alice, new AssetMoney(assetIdx, u)) //.SendAsset(satoshi.PubKey, new NBitcoin.OpenAsset.AssetMoney(assetIdx, u)) .SetChange(bitcoinFromAddress) .SendFees(Money.Coins(0.001m)) .BuildTransaction(true); var ok = txBuilder.Verify(tx); Submit(tx); return "ok"; }
public EncryptedKeyResult(BitcoinEncryptedSecretEC key, BitcoinAddress address, byte[] seed, Func<BitcoinConfirmationCode> calculateConfirmation) { _EncryptedKey = key; _GeneratedAddress = address; _CalculateConfirmation = calculateConfirmation; _Seed = seed; }
public void Lesson3() { Script scriptPubKey = new Script("OP_DUP OP_HASH160 46859ffeab7acb63417674ab3a8f4a187f386cae OP_EQUALVERIFY OP_CHECKSIG"); KeyId hash = (KeyId)scriptPubKey.GetDestination(); Console.WriteLine("Public Key Hash: {0}", hash); BitcoinAddress address = new BitcoinAddress(hash, Network.Main); Console.WriteLine("Bitcoin Address: {0}", address); }
public bool Check(string passphrase, BitcoinAddress expectedAddress) { //Derive passfactor using scrypt with ownerentropy and the user's passphrase and use it to recompute passpoint byte[] passfactor = BitcoinEncryptedSecretEC.CalculatePassFactor(passphrase, LotSequence, OwnerEntropy); //Derive decryption key for pointb using scrypt with passpoint, addresshash, and ownerentropy byte[] passpoint = BitcoinEncryptedSecretEC.CalculatePassPoint(passfactor); byte[] derived = BitcoinEncryptedSecretEC.CalculateDecryptionKey(passpoint, AddressHash, OwnerEntropy); //Decrypt encryptedpointb to yield pointb var pointbprefix = EncryptedPointB[0]; pointbprefix = (byte)(pointbprefix ^ (byte)(derived[63] & (byte)0x01)); //Optional since ArithmeticException will catch it, but it saves some times if(pointbprefix != 0x02 && pointbprefix != 0x03) return false; var pointb = BitcoinEncryptedSecret.DecryptKey(EncryptedPointB.Skip(1).ToArray(), derived); pointb = new byte[] { pointbprefix }.Concat(pointb).ToArray(); //4.ECMultiply pointb by passfactor. Use the resulting EC point as a public key var curve = ECKey.Secp256k1; ECPoint pointbec; try { pointbec = curve.Curve.DecodePoint(pointb); } catch(ArgumentException) { return false; } catch(ArithmeticException) { return false; } PubKey pubkey = new PubKey(pointbec.Multiply(new BigInteger(1, passfactor)).GetEncoded()); //and hash it into address using either compressed or uncompressed public key methodology as specifid in flagbyte. pubkey = IsCompressed ? pubkey.Compress() : pubkey.Decompress(); var actualhash = BitcoinEncryptedSecretEC.HashAddress(pubkey.GetAddress(Network)); var expectedhash = BitcoinEncryptedSecretEC.HashAddress(expectedAddress); return Utils.ArrayEqual(actualhash, expectedhash); }
static void Main(string[] args) { BitcoinSecret secret = new BitcoinSecret("cTfANynUR2MCPC8uBpfo2yUfAzX5PucDNuXaJ8jXT8CpQDgvfUNr", Network); BitcoinAddress address = new BitcoinAddress("n2upyU7axtvHHhAPTMvdaRSkP5Mjufoffx", Network); BlockrTransactionRepository blockr = new BlockrTransactionRepository(Network); List<Coin> unspentCoins = blockr.GetUnspentAsync(secret.GetAddress().ToString(), true).Result; TransactionBuilder builder = new TransactionBuilder(); builder.AddKeys(secret); builder.AddCoins(unspentCoins); builder.Send(address, Money.Coins(0.005m)); builder.SendFees(Money.Coins(0.00001m)); builder.SetChange(secret.ScriptPubKey); Transaction tx = builder.BuildTransaction(true); if (builder.Verify(tx)) SendTransaction(tx); }
/// <summary> /// Extract P2SH or P2PH address from scriptSig /// </summary> /// <param name="network"></param> /// <returns></returns> public BitcoinAddress GetSignerAddress(Network network) { var sig = GetSigner(); return(sig == null ? null : BitcoinAddress.Create(sig, network)); }
public BitcoinColoredAddress(BitcoinAddress address) :base(Build(address),address.Network) { }
/// <summary> /// Code as per nic /// </summary> /// <param name="fromAddress"></param> /// <param name="amount"></param> /// <param name="toAddress"></param> /// <param name="assetId"></param> /// <param name="senderWifKey"></param> /// <returns></returns> public static string TransferAsFromNic(String fromAddress, Int64 amount, String toAddress, String assetId, String senderWifKey) { NBitcoin.Network network = NBitcoin.Network.TestNet; NBitcoin.BitcoinSecret _key = NBitcoin.Network.TestNet.CreateBitcoinSecret(senderWifKey); NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress); NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress); //UTXOS CoinPrism txRepo = new CoinPrism(true); //Get a UTXO to fund. Make sure its large enough, order by size. Maybe order by date? var fundingUTXO = txRepo.GetUnspent(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); var ccutoxs = txRepo.GetTransactions(fromAddress); //Find output which contains an incoming asset var assetOutput = ccutoxs.FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)).outputs.FirstOrDefault(o => o.asset_id == assetId); var coin_nic = new Coin(fromTxHash: new uint256("dc19133d57bf9013d898bd89198069340d8ca99d71f0d5f6c6e142d724a9ba92"), fromOutputIndex: 0, amount: Money.Satoshis(600), //default fee scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey); if (1 == 2) { //Find the coin bob sent //Coin from Bob //Coin from Alice var forfees_nic = new Coin(fromTxHash: new uint256("b4326462d6d3b522d7e2c06d9c904313f546395eb62661b06b57195691f5fe5f"), fromOutputIndex: 1, amount: Money.Coins(1m), //9957600 scriptPubKey: BitcoinAddress.Create("muJjaSHk99LGMnaFduU9b3pWHdT1ZRPASF").ScriptPubKey); } //Colour coin utxo that was sent var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash), fromOutputIndex: Convert.ToUInt32(assetOutput.index), amount: Money.Satoshis(600), //default fee scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey); //Arbitary coin var forfees = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash), fromOutputIndex: fundingUTXO.output_index, amount: Money.Satoshis(fundingUTXO.value), //20000 scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex))); BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); var alice = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet); ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity)); //FROM NIC var bobKey = new BitcoinSecret("cMdLBsUCQ92VSRmqfEL4TgJCisWpjVBd8GsP2mAmUZxQ9bh5E7CN"); var aliceKey = new BitcoinSecret("cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH"); var txBuilder = new TransactionBuilder(); var tx = txBuilder .AddKeys(bobKey, aliceKey) .AddCoins(forfees, colored) .SendAsset(alice, new AssetMoney(assetIdx, Convert.ToUInt64(amount))) .SetChange(bitcoinFromAddress) .SendFees(Money.Coins(0.001m)) .BuildTransaction(true); var ok = txBuilder.Verify(tx); Submit(tx); return "ok"; }
/// <summary> /// Create scriptPubKey from destination address /// </summary> /// <param name="address"></param> /// <returns></returns> public static Script CreateFromDestinationAddress(BitcoinAddress address) { return(CreateFromDestination(address.Hash)); }
public static string Trade(String fromAddress, Int64 fromAmount, String fromAssetId, String toAddress, Int64 toAmount, String toAssetId) { NBitcoin.Network network = NBitcoin.Network.TestNet; NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress, network); NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress, network); //UTXOS CoinPrism txRepo = new CoinPrism(true); //Get a UTXO to fund. Make sure its large enough, order by size. Maybe order by date? var fundingUTXO = txRepo.GetUnspent(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); var fundingToUTXO = txRepo.GetUnspent(toAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); if (fundingUTXO == null | fundingToUTXO == null) { throw new ArgumentNullException("Need more btc"); } //Find unspent utxos with assets var assetFromFundingUtxos = txRepo.GetUnspent(fromAddress).Where(ux => ux.asset_id == fromAssetId).ToList(); var assetToFundingUtxos = txRepo.GetUnspent(toAddress).Where(ux => ux.asset_id == toAssetId).ToList(); //Note last emmitting tx = 55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e //var fromCCutoxs = txRepo.GetTransactions(fromAddress); //var toCCutoxs = txRepo.GetTransactions(toAddress); ////Find new tx first (DEBUG ONLY) //var allOutputsForAsset = fromCCutoxs.OrderBy(a => a.block_time) // .Where(i => i.outputs.Any(o => o.asset_id == fromAssetId)) // && i.outputs.Any(oo => oo.addresses.Contains(fromAddress))) // //.outputs.OrderByDescending(o => o.asset_quantity) // //.Where(o => o.asset_id == assetId && o.asset_quantity >= amount && o.addresses.Contains(fromAddress)); // .ToList(); ////TODO: CHANGE FOR DIVISIBILITY ////Find output which contains an incoming asset //var fromAssetOutput = fromCCutoxs.OrderByDescending(a => a.block_time) // .FirstOrDefault(i => i.outputs.Any(o => o.asset_id == fromAssetId)) // && i.outputs.Any(oo => oo.addresses.Contains(fromAddress))) // //.outputs.OrderByDescending(o => o.asset_quantity) // //.outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount && o.addresses.Contains(fromAddress)); // .outputs.FirstOrDefault(o => o.asset_id == fromAssetId && o.asset_quantity >= fromAmount); //FROM //todo, could be many var fromAssetOutput = assetFromFundingUtxos.SingleOrDefault(u => u.asset_quantity >= fromAmount); var toAssetOutput = assetToFundingUtxos.SingleOrDefault(u => u.asset_quantity >= toAmount); if (fromAssetOutput == null | toAssetOutput == null) { throw new ArgumentNullException("Not enough assets"); } //TODO: NEED MULTIPLE COINS //DO THE FROM ////Colour coin utxo that was sent var fromCoin = new Coin(fromTxHash: new uint256(fromAssetOutput.transaction_hash), //fromOutputIndex: Convert.ToUInt32(fromAssetOutput.index), fromOutputIndex: Convert.ToUInt32(fromAssetOutput.output_index), amount: Money.Satoshis(fromAssetOutput.value), //default fee scriptPubKey: bitcoinFromAddress.ScriptPubKey); BitcoinAssetId fromAssetIdx = new BitcoinAssetId(fromAssetId, Network.TestNet); ColoredCoin fromColored = fromCoin.ToColoredCoin(fromAssetIdx, Convert.ToUInt64(fromAssetOutput.asset_quantity)); ////TODO: NEED MULTIPLE COINS //var toCoin = new Coin(fromTxHash: new uint256(toAssetOutput.transaction_hash), // fromOutputIndex: Convert.ToUInt32(toAssetOutput.output_index), // amount: Money.Satoshis(toAssetOutput.value), //default fee // scriptPubKey: bitcoinToAddress.ScriptPubKey); BitcoinAssetId toAssetIdx = new BitcoinAssetId(toAssetId, Network.TestNet); ColoredCoin toColored = MakeColouredCoin(toAssetOutput, toAssetId, bitcoinToAddress.ScriptPubKey); //Arbitary coin var fromFeeCoin = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash), fromOutputIndex: fundingUTXO.output_index, amount: Money.Satoshis(fundingUTXO.value), //20000 scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex))); var toFeeCoin = new Coin(fromTxHash: new uint256(fundingToUTXO.transaction_hash), fromOutputIndex: fundingToUTXO.output_index, amount: Money.Satoshis(fundingToUTXO.value), //20000 scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingToUTXO.script_hex))); //FROM NIC var key1 = new BitcoinSecret(BobWIFKey); var key2 = new BitcoinSecret(CarolWIFKey); var txBuilder = new TransactionBuilder(); var tx = txBuilder .AddKeys(key1, key2) .AddCoins(fromFeeCoin, fromColored) .SendAsset(bitcoinToAddress, new AssetMoney(fromAssetIdx, Convert.ToUInt64(fromAmount))) .SetChange(bitcoinFromAddress) .SendFees(Money.Coins(0.001m)) .Then() //.AddKeys(key1, key2) .AddCoins(toFeeCoin, toColored) .SendAsset(bitcoinFromAddress, new AssetMoney(toAssetIdx, Convert.ToUInt64(toAmount))) .SetChange(bitcoinToAddress) .SendFees(Money.Coins(0.001m)) .BuildTransaction(true); var ok = txBuilder.Verify(tx); var response = Submit(tx); return response; }
public static string Transfer(String fromAddress, Int64 amount, String toAddress, String assetId) { NBitcoin.Network network = NBitcoin.Network.TestNet; NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress, network); NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress, network); //UTXOS CoinPrism txRepo = new CoinPrism(true); //Get a UTXO to fund. Make sure its large enough, order by size. Maybe order by date? var fundingUTXO = txRepo.GetUnspent(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); if (fundingUTXO == null) { throw new ArgumentNullException("Need more btc"); } //Find unspent utxos with assets var assetFundingUtxos = txRepo.GetUnspent(fromAddress).Where(ux => ux.asset_id == assetId).ToList(); //Note last emmitting tx = 55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e var ccutoxs = txRepo.GetTransactions(fromAddress); //Find new tx first var allOutputsForAsset = ccutoxs.OrderBy(a => a.block_time) .Where(i => i.outputs.Any(o => o.asset_id == assetId)) // && i.outputs.Any(oo => oo.addresses.Contains(fromAddress))) //.outputs.OrderByDescending(o => o.asset_quantity) //.outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount && o.addresses.Contains(fromAddress)); .ToList(); //TODO: CHANGE FOR DIVISIBILITY //Find output which contains an incoming asset var assetOutput = ccutoxs.OrderByDescending(a => a.block_time) .FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)) // && i.outputs.Any(oo => oo.addresses.Contains(fromAddress))) //.outputs.OrderByDescending(o => o.asset_quantity) //.outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount && o.addresses.Contains(fromAddress)); .outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount); if (assetOutput == null) { throw new ArgumentNullException("Not enough assets"); } ////Colour coin utxo that was sent var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash), fromOutputIndex: Convert.ToUInt32(assetOutput.index), amount: Money.Satoshis(assetOutput.value), //default fee scriptPubKey: bitcoinFromAddress.ScriptPubKey); //HACKE FOR 1 USD FROM ALICE BACK TO BOB. TX 47~48 //ADDRESS WAS ALICE //Colour coin utxo that was sent //var coin = new Coin(fromTxHash: new uint256("b52454d26d9020a241d3d3173270bf0b0e218fc4cf21d18c8ac34d8d29f0f988"), // fromOutputIndex: 2, // amount: Money.Satoshis(600), //default fee // scriptPubKey: bitcoinFromAddress.ScriptPubKey); //Arbitary coin var forfees = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash), fromOutputIndex: fundingUTXO.output_index, amount: Money.Satoshis(fundingUTXO.value), //20000 scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex))); BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); //var recipient = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet); //44696 ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity)); //ColoredCoin colored = coin.ToColoredCoin(assetIdx, 44696); //FROM NIC var key1 = new BitcoinSecret(BobWIFKey); var key2 = new BitcoinSecret(CarolWIFKey); var txBuilder = new TransactionBuilder(); var tx = txBuilder .AddKeys(key1, key2) .AddCoins(forfees, colored) .SendAsset(bitcoinToAddress, new AssetMoney(assetIdx, Convert.ToUInt64(amount))) .SetChange(bitcoinFromAddress) .SendFees(Money.Coins(0.001m)) .BuildTransaction(true); var ok = txBuilder.Verify(tx); var response = Submit(tx); return response; }
//public static string Transfer4(String fromAddress, Int64 amount, String toAddress, String assetId, String senderWifKey) //{ // NBitcoin.Network network = NBitcoin.Network.TestNet; // //NBitcoin.BitcoinSecret _key = NBitcoin.Network.TestNet.CreateBitcoinSecret(senderWifKey); // NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress); // NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress); // //UTXOS // NBitcoin.BlockrTransactionRepository blkrRepo = new BlockrTransactionRepository(network); // var x = blkrRepo.GetUnspentAsync(fromAddress).Result.ToList(); // CoinPrism txRepo = new CoinPrism(true); // //Get a UTXO to fund. Make sure its large enough, order by size. Maybe order by date? // var fundingUTXO = txRepo.Get(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); // Debug.Assert(fundingUTXO.spent == false); // //Note last emmitting tx = 55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e // //55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e // var ccutoxs = txRepo.GetTransactions(fromAddress); // // var xxx = ccutoxs.Where(c => c.hash == "55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e").ToList(); // //Find output which contains an incoming asset // var assetOutput = ccutoxs.FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)).outputs.FirstOrDefault(o => o.asset_id == assetId); // //Colour coin utxo that was sent // var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash), // fromOutputIndex: Convert.ToUInt32(assetOutput.index), // amount: Money.Satoshis(600), //default fee // scriptPubKey: BitcoinAddress.Create(fromAddress).ScriptPubKey); // //var coin = new Coin(fromTxHash: new uint256(xxx[0].hash), // // fromOutputIndex: Convert.ToUInt32(1), // // amount: Money.Satoshis(600), //default fee // // scriptPubKey: BitcoinAddress.Create(fromAddress).ScriptPubKey); // //Arbitary coin // var forFees = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash), // fromOutputIndex: fundingUTXO.output_index, // amount: Money.Satoshis(fundingUTXO.value), //20000 // scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex))); // BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); // var alice = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet); // ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity)); // //FROM NIC // var bobKey = new BitcoinSecret(BobWIFKey); // //var aliceKey = new BitcoinSecret("cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH"); // var txBuilder = new TransactionBuilder(); // var tx = txBuilder // .AddKeys(bobKey) // .AddCoins(forFees) // .Send(bitcoinToAddress, "0.0005") // .SetChange(bitcoinFromAddress) // .SendFees(Money.Coins(0.001m)) // .BuildTransaction(true); // var ok = txBuilder.Verify(tx); // Submit(tx); // var hex = tx.ToHex(); // return hex; //} public static string SimpleTransfer(String fromAddress, Int64 amount, String toAddress, String assetId, String senderWifKey) { NBitcoin.Network network = NBitcoin.Network.TestNet; //NBitcoin.BitcoinSecret _key = NBitcoin.Network.TestNet.CreateBitcoinSecret(senderWifKey); NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress, network); NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress, network); //UTXOS CoinPrism txRepo = new CoinPrism(true); //Get a UTXO to fund. Make sure its large enough, order by size. Maybe order by date? var fundingUTXO = txRepo.GetUnspent(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); //Note last emmitting tx = 55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e //55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e //var ccutoxs = txRepo.GetTransactions(fromAddress); //var xxx = ccutoxs.Where(c => c.hash == "55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e").ToList(); //Find output which contains an incoming asset //var assetOutput = ccutoxs.FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)).outputs.FirstOrDefault(o => o.asset_id == assetId); ////Colour coin utxo that was sent //var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash), // fromOutputIndex: Convert.ToUInt32(assetOutput.index), // amount: Money.Satoshis(600), //default fee // scriptPubKey: BitcoinAddress.Create(fromAddress).ScriptPubKey); //var coin = new Coin(fromTxHash: new uint256(xxx[0].hash), // fromOutputIndex: Convert.ToUInt32(1), // amount: Money.Satoshis(600), //default fee // scriptPubKey: BitcoinAddress.Create(fromAddress).ScriptPubKey); //Arbitary coin var coinsToSend = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash), fromOutputIndex: fundingUTXO.output_index, amount: Money.Satoshis(fundingUTXO.value), //20000 scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex))); //BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); //var toAddress = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet); //ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity)); //FROM NIC //var bobKey = new BitcoinSecret(BobWIFKey); var aliceKey = new BitcoinSecret(AliceWIFKey); var txBuilder = new TransactionBuilder(); var tx = txBuilder .AddKeys(aliceKey) .AddCoins(coinsToSend) .Send(bitcoinToAddress, Money.Coins(0.003M)) //.SendAsset(bitcoinToAddress, new AssetMoney(assetIdx, Convert.ToUInt64(amount))) .SetChange(bitcoinFromAddress) .SendFees(Money.Coins(0.001M)) .BuildTransaction(true); var ok = txBuilder.Verify(tx); Submit(tx); String hex = tx.ToHex(); return hex; }
public static String FromNewAddressToAlice() { const String AliceAddress = "muJjaSHk99LGMnaFduU9b3pWHdT1ZRPASF"; String senderKey = "93KgTTe7YrwEFtfcHYL9Bsm1p4PcQEfxaqEw3CBcUCbx17Xc4Nk"; String senderAddress = "mtZk7YUMEK3rGWa91VfvYXAJaK8iw6DMvV"; NBitcoin.Network network = NBitcoin.Network.TestNet; NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(AliceAddress, network); NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(senderAddress, network); //UTXOS CoinPrism txRepo = new CoinPrism(true); //Get a UTXO to fund. Make sure its large enough, order by size. Maybe order by date? var fundingUTXO = txRepo.GetUnspent(bitcoinFromAddress.ToString()).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First(); //var ccutoxs = txRepo.GetTransactions(fromAddress); //Find output which contains an incoming asset //var assetOutput = ccutoxs.FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)).outputs.FirstOrDefault(o => o.asset_id == assetId); var coin_nic = new Coin(fromTxHash: new uint256("5a2359ec87780561306b2d6fbe4151704ef0c97ab09ab51548502d1431a96331"), fromOutputIndex: 1, amount: Money.Satoshis(100000000), //default fee scriptPubKey: BitcoinAddress.Create(senderAddress).ScriptPubKey); ////Colour coin utxo that was sent //var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash), // fromOutputIndex: Convert.ToUInt32(assetOutput.index), // amount: Money.Satoshis(600), //default fee // scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey); ////Arbitary coin //var forfees = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash), // fromOutputIndex: fundingUTXO.output_index, // amount: Money.Satoshis(fundingUTXO.value), //20000 // scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex))); //BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); //var alice = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet); //ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity)); //FROM NIC var senderSecret = new BitcoinSecret(senderKey); //var aliceKey = new BitcoinSecret("cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH"); var txBuilder = new TransactionBuilder(); var tx = txBuilder .AddKeys(senderSecret) .AddCoins(coin_nic) .Send(bitcoinToAddress, new Money(1000)) .SetChange(bitcoinFromAddress) .SendFees(Money.Coins(0.001m)) .BuildTransaction(true); var ok = txBuilder.Verify(tx); Submit(tx); return "ok"; }
public static Script PayToAddress(BitcoinAddress address) { return(PayToPubkeyHash((KeyId)address.Hash)); }
public PaymentRequestManager(bool isMainNet, string destinationAddress) { this.isMainNet = isMainNet; this.destinationAddress = BitcoinAddress.Create(destinationAddress); }
public Block CreateNextBlockWithCoinbase(BitcoinAddress address, int height) { return(CreateNextBlockWithCoinbase(address, height, DateTimeOffset.UtcNow)); }
public static Script PayToAddress(BitcoinAddress address) { return PayToPubkeyHash(address.ID); }
public BitcoinColoredAddress(BitcoinAddress address) : base(Build(address), address.Network) { }
/// <summary> /// Take the first four bytes of SHA256(SHA256(generatedaddress)) and call it addresshash. /// </summary> /// <param name="address"></param> /// <returns></returns> internal static byte[] HashAddress(BitcoinAddress address) { return(Hashes.Hash256(Encoders.ASCII.DecodeData(address.ToString())).ToBytes().Take(4).ToArray()); }
private static byte[] Build(BitcoinAddress address) { var version = address.Network.GetVersionBytes(address.Type); var data = address.ToBytes(); return version.Concat(data).ToArray(); }
public TxOut AddOutput(Money money, BitcoinAddress address) { return(AddOutput(new TxOut(money, address))); }
/// <summary> /// Take the first four bytes of SHA256(SHA256(generatedaddress)) and call it addresshash. /// </summary> /// <param name="address"></param> /// <returns></returns> internal static byte[] HashAddress(BitcoinAddress address) { return Hashes.Hash256(Encoders.ASCII.DecodeData(address.ToString())).ToBytes().Take(4).ToArray(); }
public BitcoinAddress GetAddress(Network network) { return(BitcoinAddress.Create(this, network)); }
public static Script PayToAddress(BitcoinAddress address) { return PayToPubkeyHash((KeyId)address.Hash); }
/// <summary> /// Create scriptPubKey from destination address /// </summary> /// <param name="address"></param> /// <returns></returns> public static Script CreateFromDestinationAddress(BitcoinAddress address) { return CreateFromDestination(address.Hash); }
public void SetDestination(BitcoinAddress address) { ScriptPubKey = new PayToPubkeyHashTemplate().GenerateScriptPubKey(address); }
public BitcoinAddress GetAddress() { return _address ?? (_address = PrivateKey.PubKey.GetAddress(Network)); }
public TxOut(Money value, BitcoinAddress bitcoinAddress) { if(bitcoinAddress == null) throw new ArgumentNullException("bitcoinAddress"); if(value == null) throw new ArgumentNullException("value"); Value = value; SetDestination(bitcoinAddress); }
public bool IsTo(BitcoinAddress address) { if(address == null) throw new ArgumentNullException("address"); return IsTo(address.ID); }
public Block CreateNextBlockWithCoinbase(BitcoinAddress address, int height, DateTimeOffset now) { if (address == null) throw new ArgumentNullException("address"); Block block = new Block(); block.Header.Nonce = RandomUtils.GetUInt32(); block.Header.HashPrevBlock = this.GetHash(); block.Header.BlockTime = now; var tx = block.AddTransaction(new Transaction()); tx.AddInput(new TxIn() { ScriptSig = new Script(Op.GetPushOp(RandomUtils.GetBytes(30))) }); tx.Outputs.Add(new TxOut(address.Network.GetReward(height), address) { Value = address.Network.GetReward(height) }); return block; }
public void AddOutput(Money money, BitcoinAddress address) { AddOutput(new TxOut(money, address)); }
public Block CreateNextBlockWithCoinbase(BitcoinAddress address, int height) { return CreateNextBlockWithCoinbase(address, height, DateTimeOffset.UtcNow); }
/// <summary> /// Extract P2SH or P2PH address from scriptPubKey /// </summary> /// <param name="network"></param> /// <returns></returns> public BitcoinAddress GetDestinationAddress(Network network) { var dest = GetDestination(); return(dest == null ? null : BitcoinAddress.Create(dest, network)); }
public Script GenerateScriptPubKey(BitcoinAddress address) { if(address == null) throw new ArgumentNullException("address"); return GenerateScriptPubKey((KeyId)address.ID); }
public Transaction CreateSend(BitcoinAddress bitcoinAddress, Money value) { if(bitcoinAddress.Network != Network) throw new InvalidOperationException("This bitcoin address does not belong to the network"); Transaction tx = new Transaction(); tx.AddOutput(value, bitcoinAddress); this.CompleteTx(tx); return tx; }
public static void Pay(BitcoinSecret secret, BitcoinAddress toAddress, Money amount, Transaction fundingTransaction) { var fee = Money.Coins(0.0001m); Transaction payment = new Transaction(); payment.Inputs.Add(new TxIn() { PrevOut = new OutPoint(fundingTransaction.GetHash(), 1) }); payment.Outputs.Add(new TxOut() { Value = amount, ScriptPubKey = toAddress.ScriptPubKey }); var output = fundingTransaction.Outputs[0]; var change = output.Value - amount - fee; if (change < 0) { Console.WriteLine("There is not enough BTC in the funding transaction ({0}) to make this payment ({1})", output.Value, amount); Console.WriteLine("No payment being sent"); return; } payment.Outputs.Add(new TxOut() { Value = output.Value - amount - fee, ScriptPubKey = output.ScriptPubKey }); //Feedback ! var message = "Thanks ! :)"; var bytes = Encoding.UTF8.GetBytes(message); payment.Outputs.Add(new TxOut() { Value = Money.Zero, ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes) }); Console.WriteLine(payment); payment.Inputs[0].ScriptSig = fundingTransaction.Outputs[1].ScriptPubKey; payment.Sign(secret, false); using (var node = Node.Connect(Network.Main)) { Console.WriteLine("Doing version handshake"); node.VersionHandshake(); Console.WriteLine("Sending message"); node.SendMessage(new InvPayload(InventoryType.MSG_TX, payment.GetHash())); node.SendMessage(new TxPayload(payment)); Thread.Sleep(500); } }