Exemplo n.º 1
0
        public Response <IOperation[]> Read(string hex)
        {
            Response <IOperation[]> resp = new Response <IOperation[]>();

            if (!Base16.IsValid(hex))
            {
                resp.Errors.Add("Invalid base-16 string.");
            }
            else
            {
                byte[]     data   = Base16.ToByteArray(hex);
                int        offset = 0;
                CompactInt len    = new CompactInt(data.Length);
                data = len.ToByteArray().ConcatFast(data);

                Script scr = new Helper();
                if (scr.TryDeserialize(data, ref offset, out string error))
                {
                    resp.Result = scr.OperationList;
                }
                else
                {
                    resp.Errors.Add(error);
                }
            }

            return(resp);
        }
Exemplo n.º 2
0
 public byte[] Serialize()
 {
     return(ByteArray.ConcatArrays(
                Size.Bytes,
                Base16.ToByteArray(Data)
                ));
 }
 public byte[] Serialize()
 {
     return(ByteArray.ConcatArrays(
                Base16.ToByteArray(TxId).Reverse().ToArray(),
                Index.ToByteArray(false)
                ));
 }
Exemplo n.º 4
0
 public byte[] Serialize()
 {
     return(ByteArray.ConcatArrays(
                Amount.ToByteArray(false),
                PkScriptLength.Bytes,
                Base16.ToByteArray(PkScript)
                ));
 }
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     if (reader.TokenType == JsonToken.String)
     {
         string hex = serializer.Deserialize <string>(reader);
         return(Base16.ToByteArray(hex));
     }
     return(serializer.Deserialize(reader));
 }
Exemplo n.º 6
0
        private static string BuildScriptPub(string addr)
        {
            string hash160 = BitcoinConversions.Base58ToHash160(addr);

            return(ByteArray.ConcatArrays(
                       new byte[] { (byte)OPCodes.OP_DUP, (byte)OPCodes.OP_HASH160 },
                       new byte[] { (byte)(hash160.Length / 2) },
                       Base16.ToByteArray(hash160),
                       new byte[] { (byte)OPCodes.OP_EQUALVERIFY, (byte)OPCodes.OP_CHECKSIG }).ToBase16());
        }
Exemplo n.º 7
0
        private void SetJson()
        {
            if (!Base16.IsValid(_rawTx))
            {
                TxJson = "Invalid hex.";
            }
            else
            {
                Transaction tx     = new Transaction();
                int         offset = 0;

                TxJson = tx.TryDeserialize(Base16.ToByteArray(_rawTx), ref offset, out string error)
                    ? JsonConvert.SerializeObject(tx, Formatting.Indented, jSetting)
                    : "Error while deserializing transaction:" + Environment.NewLine + error;
            }
        }
        public override bool SetOperations()
        {
            Errors = null;

            try
            {
                byte[] ba = new byte[0];
                if (!string.IsNullOrEmpty(Data))
                {
                    switch (SelectedEncoder)
                    {
                    case Encoders.UTF8:
                        ba = Encoding.UTF8.GetBytes(Data);
                        break;

                    case Encoders.Base16:
                        if (!Base16.IsValid(Data))
                        {
                            Errors = "Invalid base16 (hexadecimal) string.";
                            return(false);
                        }
                        else
                        {
                            ba = Base16.ToByteArray(Data);
                        }
                        break;

                    default:
                        Errors = "Undefined encoder.";
                        return(false);
                    }
                }

                ReturnOp op = new ReturnOp(ba, true);
                OpsToAdd = new IOperation[1] {
                    op
                };
            }
            catch (Exception ex)
            {
                Errors = $"An unexpected error happened. Message: {ex.Message}";
                return(false);
            }

            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Makes ScriptSig based on wallet type.
        /// <para/> INCOMPLETE!
        /// </summary>
        /// <param name="addr">Hes string to put inside of ScriptSig.</param>
        /// <param name="type">Type of wallet that is supposed to sign this transaction.</param>
        /// <returns>ScriptSig</returns>
        public static string BuildScriptSig(string addr, WalletType type)
        {
            string hash160 = BitcoinConversions.Base58ToHash160(addr);

            switch (type)
            {
            case WalletType.Electrum:
                //01 ff 16 fd 00
                return(ByteArray.ConcatArrays(
                           new byte[] { 1, (byte)OPCodes.OP_INVALIDOPCODE, 16, (byte)OPCodes.OP_PUBKEYHASH, 0 },
                           Base16.ToByteArray(hash160)
                           ).ToBase16());

            case WalletType.Normal:
            case WalletType.Core:
            default:
                return(BuildScriptPub(addr));
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Deserialize the transaction hex string.
        /// </summary>
        /// <param name="rawTx">Transaction hex string</param>
        /// <returns>Bitcoin Transaction</returns>
        public static TxModel DecodeRawTx(string rawTx)
        {
            if (Transaction.TryParse(Base16.ToByteArray(rawTx), out Transaction tx))
            {
                TxModel result = new TxModel()
                {
                    Version    = tx.Version,
                    TxInCount  = tx.TxInCount.Number,
                    TxInList   = new ObservableCollection <TxInModel>(tx.TxInList.Select(x => new TxInModel(x))),
                    TxOutCount = tx.TxOutCount.Number,
                    TxOutList  = new ObservableCollection <TxOut>(tx.TxOutList),
                    LockTime   = tx.LockTime,
                    TxId       = tx.GetTransactionID(),
                    WtxId      = tx.GetWitnessTransactionID(),
                    IsRbf      = tx.TxInList.Any(x => x.Sequence != uint.MaxValue)
                };

                return(result);
            }
            throw new ArgumentException("Can not parse the given transaction!");
        }
Exemplo n.º 11
0
        public ITransaction Build(uint ver, List <UTXO> txToSpend, List <ReceivingAddress> receiveAddr, uint lockTime)
        {
            TxIn[] tIns = txToSpend.Select(x => new TxIn()
            {
                Outpoint = new Outpoint()
                {
                    Index = x.OutIndex, TxHash = Base16.ToByteArray(Base16.Reverse(x.TxHash))
                },
                SigScript = new SignatureScript(),
                Sequence  = uint.MaxValue
            }).ToArray();

            TxOut[] tOuts = receiveAddr.Select(x => new TxOut()
            {
                Amount    = x.PaymentSatoshi,
                PubScript = BuildPubkeyScript(x.Address)
            }).ToArray();

            Transaction tx = new Transaction((int)ver, tIns, tOuts, lockTime);

            return(tx);
        }