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"; }
private string GetScriptString(NBitcoin.Transaction transaction) { var output = transaction.Outputs.FirstOrDefault(c => c.Value.Equals(Money.Zero)); if (output == null) { transaction.ToString(); //TODO: UnCOmment exception } //throw new Exception("Transaction without integration output: " + transaction.GetHash().ToString()); else { try { var coin = new NBitcoin.Coin(transaction, output); var script = coin.GetScriptCode().ToString(); script = script.Replace("OP_RETURN ", ""); return(HexToString(script)); } catch (Exception e) { throw new Exception("Failed to process transaction output", e); } } return(""); }
private GetTransactionResponse GetTransactionInfo(TransactionEntry tx, bool colored, BlockInformation block) { var response = new GetTransactionResponse() { TransactionId = tx.TransactionId, Transaction = tx.Transaction, IsCoinbase = tx.Transaction.IsCoinBase, Fees = tx.Fees, Block = block, FirstSeen = tx.FirstSeen }; for (int i = 0; i < tx.Transaction.Outputs.Count; i++) { var txout = tx.Transaction.Outputs[i]; NBitcoin.ICoin coin = new NBitcoin.Coin(new NBitcoin.OutPoint(tx.TransactionId, i), txout); if (colored) { var entry = tx.ColoredTransaction.GetColoredEntry((uint)i); if (entry != null) { coin = new NBitcoin.ColoredCoin(entry.Asset, (NBitcoin.Coin)coin); } } response.ReceivedCoins.Add(coin); } if (!response.IsCoinbase) { for (int i = 0; i < tx.Transaction.Inputs.Count; i++) { NBitcoin.ICoin coin = new NBitcoin.Coin(tx.SpentCoins[i].OutPoint, tx.SpentCoins[i].TxOut); if (colored) { var entry = tx.ColoredTransaction.Inputs.FirstOrDefault(ii => ii.Index == i); if (entry != null) { coin = new NBitcoin.ColoredCoin(entry.Asset, (NBitcoin.Coin)coin); } } response.SpentCoins.Add(coin); } } 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; }
static void Main(string[] args) { var goldGuy = new BitcoinSecret("KyuzoVnpsqW529yzozkzP629wUDBsPmm4QEkh9iKnvw3Dy5JJiNg"); var silverGuy = new BitcoinSecret("L4KvjpqDtdGEn7Lw6HdDQjbg74MwWRrFZMQTgJozeHAKJw5rQ2Kn"); var firstPerson = new BitcoinSecret("5Jnw9Td7PaG6PWBrU7ZCfxyVXsHSsNxdZ9sg5dnZstcr12DLVbJ"); var secondPerson = new BitcoinSecret("5Jn4zJkzS2BWNu7AMRTdSJ6mS7JYfJg27oXKAichaRBbp97ZKks"); var exchangeEntity = new BitcoinSecret("5KA7FeABKmMKerWmkJzYM9FdoqScZEMVcS9u6wvT3EhgF5ZUWv5"); var bitcoinProviderEntity = new BitcoinSecret("5Jcz2A17aAt4bcQP5GEn6itt72JsLwrksNRVKqazy7n284b1bKj"); var issuanceCoinsTransaction = new Transaction() { Outputs = { new TxOut("1.0", goldGuy.PubKey), new TxOut("1.0", silverGuy.PubKey), new TxOut("1.0", firstPerson.PubKey), new TxOut("1.0", secondPerson.PubKey), } }; IssuanceCoin[] issuanceCoins = issuanceCoinsTransaction .Outputs .Take(2) .Select((o, i) => new Coin(new OutPoint(issuanceCoinsTransaction.GetHash(), i), o)) .Select(c => new IssuanceCoin(c)) .ToArray(); var goldIssuanceCoin = issuanceCoins[0]; var silverIssuanceCoin = issuanceCoins[1]; var firstPersonInitialCoin = new Coin(new OutPoint(issuanceCoinsTransaction, 2), issuanceCoinsTransaction.Outputs[2]); var secondPersonInitialCoin = new Coin(new OutPoint(issuanceCoinsTransaction, 3), issuanceCoinsTransaction.Outputs[3]); var goldId = goldIssuanceCoin.AssetId; var silverId = silverIssuanceCoin.AssetId; var txRepo = new NoSqlTransactionRepository(); txRepo.Put(issuanceCoinsTransaction.GetHash(), issuanceCoinsTransaction); var ctxRepo = new NoSqlColoredTransactionRepository(txRepo); TransactionBuilder txBuilder = new TransactionBuilder(); // Issuing gold to first person // This happens in gold issuer client Transaction tx = txBuilder .AddKeys(goldGuy.PrivateKey) .AddCoins(goldIssuanceCoin) .IssueAsset(firstPerson.GetAddress(), new AssetMoney(goldId, 20)) .SetChange(goldGuy.GetAddress()) .BuildTransaction(true); txRepo.Put(tx.GetHash(), tx); var ctx = tx.GetColoredTransaction(ctxRepo); var coloredCoins = ColoredCoin.Find(tx, ctx).ToArray(); ColoredCoin firstPersonGoldCoin = coloredCoins[0]; // Issuing silver to second person // This happens in silver issuer client txBuilder = new TransactionBuilder(); tx = txBuilder .AddKeys(silverGuy.PrivateKey) .AddCoins(silverIssuanceCoin) .IssueAsset(secondPerson.GetAddress(), new AssetMoney(silverId, 30)) .SetChange(silverGuy.GetAddress()) .BuildTransaction(true); txRepo.Put(tx.GetHash(), tx); ctx = tx.GetColoredTransaction(ctxRepo); coloredCoins = ColoredCoin.Find(tx, ctx).ToArray(); ColoredCoin secondPersonSilverCoin = coloredCoins[0]; // Sending first person gold to exchange // This happens in first user client var bitcoinProviderCoin = CreateTransactionFeeCoin(bitcoinProviderEntity.PubKey, txRepo); txBuilder = new TransactionBuilder(); tx = txBuilder .AddCoins(bitcoinProviderCoin) .AddKeys(bitcoinProviderEntity.PrivateKey) .AddCoins(firstPersonGoldCoin) .AddKeys(firstPerson.PrivateKey) .SendAssetToExchange(exchangeEntity.GetAddress(), new AssetMoney(goldId, 5)) .SetChange(firstPerson.PubKey) .BuildTransaction(true); txRepo.Put(tx.GetHash(), tx); ctx = tx.GetColoredTransaction(ctxRepo); coloredCoins = ColoredCoin.Find(tx, ctx).ToArray(); ColoredCoin firstPersonColoredCoinInExchange = coloredCoins[1]; // Creating the time-locked transaction which the first user can post to the // network to claim his/her coin from exchange (it works if the exchange does not touch the coins // This happens in exchange and the transaction is delivered to first user client bitcoinProviderCoin = CreateTransactionFeeCoin(bitcoinProviderEntity.PubKey, txRepo); txBuilder = new TransactionBuilder(); tx = txBuilder .AddCoins(bitcoinProviderCoin) .AddKeys(bitcoinProviderEntity.PrivateKey) .AddCoins(firstPersonColoredCoinInExchange) .AddKeys(firstPerson.PrivateKey) .SendAsset(firstPerson.PubKey, new AssetMoney(firstPersonColoredCoinInExchange.Amount.Id, firstPersonColoredCoinInExchange.Amount.Quantity)) .SetChange(exchangeEntity.PubKey) .SetLockTime(new LockTime(1000000)) .BuildTransaction(true); string reclaimTransactionForFirstUser = tx.ToHex(); // Create first person exchange request // This happens in first person client JObject firstPersonRequestToExchange = CreateExchangeRequest("ExactMatch", goldId.ToString(), silverId.ToString(), 5, 2); var firstRequestSignature = firstPerson.PrivateKey.SignMessage(firstPersonRequestToExchange.ToString(Formatting.None)); // Sending second person silver to exchange // This happens in second person client bitcoinProviderCoin = CreateTransactionFeeCoin(bitcoinProviderEntity.PubKey, txRepo); txBuilder = new TransactionBuilder(); tx = txBuilder .AddCoins(bitcoinProviderCoin) .AddKeys(bitcoinProviderEntity.PrivateKey) .AddCoins(secondPersonSilverCoin) .AddKeys(secondPerson.PrivateKey) .SendAssetToExchange(exchangeEntity.GetAddress(), new AssetMoney(silverId, 12)) .SetChange(secondPerson.PubKey) .BuildTransaction(true); txRepo.Put(tx.GetHash(), tx); ctx = tx.GetColoredTransaction(ctxRepo); coloredCoins = ColoredCoin.Find(tx, ctx).ToArray(); ColoredCoin secondPersonColoredCoinInExchange = coloredCoins[1]; // Create second person exchange request // This happens in second person client JObject secondPersonRequestToExchange = CreateExchangeRequest("ExactMatch", silverId.ToString(), goldId.ToString(), 30, 0.5f); var secondRequestSignature = secondPerson.PrivateKey.SignMessage(secondPersonRequestToExchange.ToString(Formatting.None)); // Creating exchange reason // This happens in exchange var exchangeReason = CreateExchangeMatch(firstPersonRequestToExchange, firstRequestSignature, secondPersonRequestToExchange, secondRequestSignature, exchangeEntity.PrivateKey); // Performing the exchange operation // This happens in exchange bitcoinProviderCoin = CreateTransactionFeeCoin(bitcoinProviderEntity.PubKey, txRepo); txBuilder = new TransactionBuilder(); tx = txBuilder .AddCoins(bitcoinProviderCoin) .AddKeys(bitcoinProviderEntity.PrivateKey) .AddCoins(firstPersonColoredCoinInExchange) .AddKeys(exchangeEntity.PrivateKey) .AddCoins(secondPersonColoredCoinInExchange) .AddKeys(exchangeEntity.PrivateKey) .PerformExchangeOperation(firstPerson.GetAddress(), new AssetMoney(silverId, 10), secondPerson.GetAddress(), new AssetMoney(goldId, 5), exchangeReason.ToString(Formatting.None)) .SetChange(exchangeEntity.GetAddress()) .BuildTransaction(true); txRepo.Put(tx.GetHash(), tx); ctx = tx.GetColoredTransaction(ctxRepo); coloredCoins = ColoredCoin.Find(tx, ctx).ToArray(); txRepo.Put(tx.GetHash(), tx); }
/// <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"; }
private static ColoredCoin GetCoin(String assetId, UInt64 amount) { //Gold if (assetId == "") { } var goldCoin = new Coin(fromTxHash: new uint256("dc19133d57bf9013d898bd89198069340d8ca99d71f0d5f6c6e142d724a9ba92"), fromOutputIndex: 0, amount: Money.Satoshis(600), //default fee scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey); BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet); return goldCoin.ToColoredCoin(assetIdx, amount); }
public IssuanceCoin(OutPoint outpoint, TxOut txout) { Bearer = new Coin(outpoint, txout); }
public IssuanceCoin(Coin bearer) { this.Bearer = bearer; }
//take in tx parameters, return tx object public static TxSerial CreateTx(string extPubKey, string pubToAddr, string chgAddr, int walletId, long satToSend, long fee, bool testnet) { CheckNullOrEmpty(new object[] { extPubKey, pubToAddr, ElectrumXhost }, new string[] { "extPubKey", "pubToAddr", "ElectrumXhost" }); string err = ""; if (satToSend == 0) { err += "satoshiToSend = 0, "; } if (fee == 0) { err += "satoshiFee = 0, "; } if (err != "") { throw new Exception("[CreateTx] " + err); } //get first 100+100 child address from ext pub key List <Tuple <string, string> > recAddrList = GetDerivedKeys(extPubKey, 0, 20, false, testnet); //receive addresses List <Tuple <string, string> > chgAddrList = GetDerivedKeys(extPubKey, 0, 20, true, testnet); //change addresses //TODO - create process for getting next change address, so address never used twice if (chgAddr == null || chgAddr == "") //get first chg addr for extPubKey { chgAddr = chgAddrList.First().Item1; } //server status check string info = ElectrumX.GetServerInfo(ElectrumXhost, ElectrumXport); if (info == null) { throw new Exception("[CreateTx] ElectrumX Server Check Failed"); } string[] recAddrListAddr = new string[recAddrList.Count]; //short address string[] recAddrListExt = new string[recAddrList.Count]; //long address int ctr = 0; foreach (var t in recAddrList) { recAddrListAddr[ctr++] = t.Item1; //short } ctr = 0; foreach (var t in recAddrList) { recAddrListExt[ctr++] = t.Item2; //long - hash } string[] chgAddrListAddr = new string[recAddrList.Count]; string[] chgAddrListExt = new string[recAddrList.Count]; ctr = 0; foreach (var t in chgAddrList) { chgAddrListAddr[ctr++] = t.Item1; } ctr = 0; foreach (var t in chgAddrList) { chgAddrListExt[ctr++] = t.Item2; } //get all UTXOs (unspent inputs) from receive addresses UTXO[] recUTXOs = ElectrumX.GetUTXOs(recAddrList, ElectrumXhost, ElectrumXport); UTXO.SetAddressType(recUTXOs, 0); //receiver //get all UTXOs (unspent inputs) from change addresses UTXO[] chgUTXOs = ElectrumX.GetUTXOs(chgAddrList, ElectrumXhost, ElectrumXport); UTXO.SetAddressType(chgUTXOs, 1); //change //start new tx TransactionBuilder bldr = new TransactionBuilder(); bldr.Send(new BitcoinPubKeyAddress(pubToAddr), Money.Satoshis(satToSend)); //amount to send to recipient bldr.SetChange(new BitcoinPubKeyAddress(chgAddr)); //send change to this address bldr.SendFees(Money.Satoshis(fee)); //miner (tx) fee //collect all UTXOs List <UTXO> allUTXOs = new List <UTXO>(); allUTXOs.AddRange(recUTXOs); allUTXOs.AddRange(chgUTXOs); List <ICoin> lstTxCoins = new List <ICoin>(); //Coin is a UTXO //add new coin for each UTXO foreach (UTXO x in allUTXOs) //tx builder will select coins from this list { BitcoinPubKeyAddress fromAddr = new BitcoinPubKeyAddress(x.Address); NBitcoin.Coin cn = null; //create new coin from UTXO bldr.AddCoins(cn = new NBitcoin.Coin( new OutPoint(new uint256(x.Tx_hash), x.Tx_pos), //tx that funded wallet, spend this coin new TxOut(Money.Satoshis(x.Value), fromAddr.ScriptPubKey))); //specify full coin amount, else SetChange ignored lstTxCoins.Add(cn); //add coin to transaction, may not be used x.tmp = cn; //link UTXO with coin } List <UTXO> usedUTXOs = new List <UTXO>(); //coins actually used in tx NBitcoin.Transaction tx = bldr.BuildTransaction(false); //sort\filter coins, some coins will not be needed\used //coin objects not stored in tx, so we need to determine which coins were used //scan tx inputs for matching coins, ignore other coins foreach (UTXO u in allUTXOs) { foreach (TxIn i in tx.Inputs) { if (i.PrevOut == ((NBitcoin.Coin)u.tmp).Outpoint) //this coin in tx { usedUTXOs.Add(u); //this UTXO will be used\spent in tx } } } //populate return object TxSerial txs = new TxSerial() { SendAmt = satToSend, Fee = fee, ExtPublicKey = extPubKey, ToAddress = pubToAddr, ChgAddress = chgAddr, WalletId = walletId }; txs.ExtPublicKey = extPubKey; foreach (UTXO u in usedUTXOs) { u.tmp = null; //don't serialize coin object, will rebuild coins in signing process } txs.InputUTXOs = new List <UTXO>(); txs.InputUTXOs.AddRange(usedUTXOs); //string jsn = Newtonsoft.Json.JsonConvert.SerializeObject(txs, Newtonsoft.Json.Formatting.Indented); return(txs); }
public ColoredCoin(AssetMoney asset, Coin bearer) { Amount = asset; Bearer = bearer; }
public static ColoredCoin MakeColouredCoin(String assetId, String txhash, UInt32 output_index, Int64 output_value, Script scriptPubKey, UInt64 asset_quantity) { //TODO: NEED MULTIPLE COINS //DO THE FROM ////Colour coin utxo that was sent var fromCoin = new Coin(fromTxHash: new uint256(txhash), //fromOutputIndex: Convert.ToUInt32(fromAssetOutput.index), fromOutputIndex: Convert.ToUInt32(output_index), amount: Money.Satoshis(output_value), //default fee scriptPubKey: scriptPubKey); BitcoinAssetId bitcoinAssetId = new BitcoinAssetId(assetId, Network.TestNet); ColoredCoin coin = fromCoin.ToColoredCoin(bitcoinAssetId, asset_quantity); return coin; }
public IssuanceCoin(Coin bearer) { Bearer = bearer; }
public ScriptCoin(Coin coin, Script redeem) : base(coin.Outpoint, coin.TxOut) { Redeem = redeem; AssertCoherent(); }
private void Sign(TransactionSigningContext ctx, ICoin coin, IndexedTxIn txIn) { var input = txIn.TxIn; if(coin is StealthCoin) { var stealthCoin = (StealthCoin)coin; var scanKey = FindKey(ctx, stealthCoin.Address.ScanPubKey.ScriptPubKey); if(scanKey == null) throw new KeyNotFoundException("Scan key for decrypting StealthCoin not found"); var spendKeys = stealthCoin.Address.SpendPubKeys.Select(p => FindKey(ctx, p.ScriptPubKey)).Where(p => p != null).ToArray(); ctx.AdditionalKeys.AddRange(stealthCoin.Uncover(spendKeys, scanKey)); var normalCoin = new Coin(coin.Outpoint, coin.TxOut); if(stealthCoin.Redeem != null) normalCoin = normalCoin.ToScriptCoin(stealthCoin.Redeem); coin = normalCoin; } var scriptSig = CreateScriptSig(ctx, coin, txIn); if(scriptSig == null) return; ScriptCoin scriptCoin = coin as ScriptCoin; Script signatures = null; if(coin.GetHashVersion() == HashVersion.Witness) { signatures = txIn.WitScript; if(scriptCoin != null) { if(scriptCoin.IsP2SH) txIn.ScriptSig = Script.Empty; if(scriptCoin.RedeemType == RedeemType.WitnessV0) signatures = RemoveRedeem(signatures); } } else { signatures = txIn.ScriptSig; if(scriptCoin != null && scriptCoin.RedeemType == RedeemType.P2SH) signatures = RemoveRedeem(signatures); } signatures = CombineScriptSigs(coin, scriptSig, signatures); if(coin.GetHashVersion() == HashVersion.Witness) { txIn.WitScript = signatures; if(scriptCoin != null) { if(scriptCoin.IsP2SH) txIn.ScriptSig = new Script(Op.GetPushOp(scriptCoin.GetP2SHRedeem().ToBytes(true))); if(scriptCoin.RedeemType == RedeemType.WitnessV0) txIn.WitScript = txIn.WitScript + new WitScript(Op.GetPushOp(scriptCoin.Redeem.ToBytes(true))); } } else { txIn.ScriptSig = signatures; if(scriptCoin != null && scriptCoin.RedeemType == RedeemType.P2SH) { txIn.ScriptSig = input.ScriptSig + Op.GetPushOp(scriptCoin.GetP2SHRedeem().ToBytes(true)); } } }
private int EstimateScriptSigSize(ICoin coin) { if(coin is IColoredCoin) coin = ((IColoredCoin)coin).Bearer; int size = 0; if(coin is ScriptCoin) { var scriptCoin = (ScriptCoin)coin; coin = new Coin(scriptCoin.Outpoint, new TxOut(scriptCoin.Amount, scriptCoin.Redeem)); size += new Script(Op.GetPushOp(scriptCoin.Redeem.ToBytes(true))).Length; } var scriptPubkey = coin.GetScriptCode(); foreach(var extension in Extensions) { if(extension.CanEstimateScriptSigSize(scriptPubkey)) { size += extension.EstimateScriptSigSize(scriptPubkey); return size; } } size += coin.TxOut.ScriptPubKey.Length; //Using heurestic to approximate size of unknown scriptPubKey return size; }
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 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 ColoredCoin(AssetMoney asset, Coin bearer) { this.Amount = asset; this.Bearer = bearer; }