Пример #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 static FileScript ParseScript(NBitcoin.Script script)
        {
            var ops = script.ToOps().ToList();

            if (
                ops.Count < 5 ||
                ops[0].Code != NBitcoin.OpcodeType.OP_RETURN ||
                !Encoding.UTF8.GetBytes(FileScript.Address).SequenceEqual(ops[1].PushData)
                )
            {
                return(null);
            }

            return(new FileScript
            {
                Body = ops[2].PushData,
                MimeType = Encoding.UTF8.GetString(ops[3].PushData),
                Name = Encoding.UTF8.GetString(ops[4].PushData)
            });
        }
Пример #3
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);
        }
Пример #4
0
        public void IsSmartContractInternalCall_Failure()
        {
            var script = new NBitcoin.Script(new byte[] { 0xFF, (byte)ScOpcodeType.OP_INTERNALCONTRACTTRANSFER, 0xFF });

            Assert.False(script.IsSmartContractInternalCall());
        }
Пример #5
0
        public void IsSmartContractInternalCall_Success()
        {
            var script = new NBitcoin.Script(new byte[] { (byte)ScOpcodeType.OP_INTERNALCONTRACTTRANSFER });

            Assert.True(script.IsSmartContractInternalCall());
        }
Пример #6
0
        public void IsSmartContractSpend_Failure()
        {
            var script = new NBitcoin.Script(new byte[] { 0xFF, (byte)ScOpcodeType.OP_SPEND, 0xFF });

            Assert.False(script.IsSmartContractSpend());
        }
Пример #7
0
        public void IsSmartContractSpend_Success()
        {
            var script = new NBitcoin.Script(new byte[] { (byte)ScOpcodeType.OP_SPEND });

            Assert.True(script.IsSmartContractSpend());
        }
Пример #8
0
        public void IsSmartContractExec_CallContract_Success()
        {
            var script = new NBitcoin.Script(new byte[] { (byte)ScOpcodeType.OP_CALLCONTRACT });

            Assert.True(script.IsSmartContractExec());
        }
Пример #9
0
        public void IsSmartContractCreate_Failure()
        {
            var script = new NBitcoin.Script(new byte[] { 0xFF, (byte)ScOpcodeType.OP_CREATECONTRACT, 0xFF });

            Assert.False(script.IsSmartContractCreate());
        }
Пример #10
0
        public void IsSmartContractCreate_Success()
        {
            var script = new NBitcoin.Script(new byte[] { (byte)ScOpcodeType.OP_CREATECONTRACT });

            Assert.True(script.IsSmartContractCreate());
        }
Пример #11
0
        public void IsSmartContractExec_CallContract_Failure()
        {
            var script = new NBitcoin.Script(new byte[] { 0xFF, (byte)ScOpcodeType.OP_CALLCONTRACT, 0xFF });

            Assert.False(script.IsSmartContractExec());
        }