示例#1
0
        public static String GetScriptScript(this Contracts.Table value)
        {
            List <NBitcoin.PubKey> publicKeys = new List <NBitcoin.PubKey>();

            foreach (Models.Peer peer in value.Peers)
            {
                NBitcoin.PubKey pubKey = new NBitcoin.PubKey(peer.PublicKey);
                publicKeys.Add(pubKey);
            }

            NBitcoin.Script tableScript = NBitcoin.PayToMultiSigTemplate
                                          .Instance
                                          .GenerateScriptPubKey(value.MinPlayers, publicKeys.ToArray());

            return(tableScript.ToString());
        }
示例#2
0
        public async Task <Transaction> GetTransaction(string id)
        {
            RawTransactionResult tx = await _client.GetRawTransactionAsync(id);

            if (tx == null)
            {
                return(null);
            }

            TransactionType transactiontype = GetTransactionType(tx);

            var transaction = new Transaction
            {
                OriginalJson    = tx.OriginalJson,
                TransactionType = transactiontype,
                Blockhash       = tx.Blockhash,
                TransactionId   = tx.Txid,
                Size            = tx.Size,
                TransactionsIn  = new List <Transaction.TransactionIn>(),
                TransactionsOut = new List <Transaction.TransactionOut>(),
                Time            = tx.GetTime()
            };

            int index = 0;

            foreach (var input in tx.Input)
            {
                var vIn = new Transaction.TransactionIn
                {
                    Index        = index,
                    Coinbase     = input.Coinbase,
                    Sequence     = input.Sequence,
                    ScriptSigHex = input.ScriptSig?.Hex,
                    AssetId      = null,
                    // pointer to previous tx/vout:
                    PrevTxIdPointer = input.Txid,
                    PrevVOutPointer = (int)input.Vout,
                    // we'll try to fetch this id possible
                    PrevVOutFetchedAddress = null,
                    PrevVOutFetchedValue   = 0
                };

                if (input.Txid != null)
                {
                    // Retrieve the origin address by retrieving the previous transaction and extracting the receive address and value
                    var previousTx = await _client.GetRawTransactionAsync(input.Txid);

                    if (previousTx != null)
                    {
                        var n = input.Vout;
                        vIn.PrevVOutFetchedAddress = previousTx.Output[(int)n].ScriptPubKey.Addresses.First();
                        vIn.PrevVOutFetchedValue   = previousTx.Output[(int)n].Value;
                    }
                }
                transaction.TransactionsIn.Add(vIn);
            }

            index = 0;
            foreach (var output in tx.Output)
            {
                var vOut = new Transaction.TransactionOut
                {
                    TransactionId = transaction.TransactionId,
                    Value         = output.Value,
                    Quantity      = output.Index,
                    AssetId       = null,
                    Index         = index++,
                };

                if (output.ScriptPubKey.Addresses != null) // Satoshi 14.2
                {
                    vOut.Address = output.ScriptPubKey.Addresses.FirstOrDefault();
                }
                else
                {
                    string hexScript = output.ScriptPubKey.Hex;

                    if (!string.IsNullOrEmpty(hexScript))
                    {
                        byte[]          decodedScript = Encoders.Hex.DecodeData(hexScript);
                        NBitcoin.Script script        = new NBitcoin.Script(decodedScript);
                        var             pubKey        = NBitcoin.PayToPubkeyTemplate.Instance.ExtractScriptPubKeyParameters(script);
                        if (pubKey != null)
                        {
                            NBitcoin.BitcoinPubKeyAddress address = pubKey.GetAddress(NetworkSpecs.Gravium.Main());
                            vOut.Address = address.ToString();
                        }
                        else
                        {
                            vOut.Address = script.ToString();
                        }
                    }
                    else
                    {
                        vOut.Address = output.ScriptPubKey.Type;
                    }
                }
                transaction.TransactionsOut.Add(vOut);
            }

            return(transaction);
        }