예제 #1
0
        public static (bool ok, KzScript script) ParseHex(string rawScriptHex, bool withoutLength = false)
        {
            var bytes = rawScriptHex.HexToBytes();
            var s     = new KzScript();
            var ros   = new ReadOnlySequence <byte>(bytes);
            var sr    = new SequenceReader <byte>(ros);

            return(s.TryReadScript(ref sr, withoutLength), s);
        }
예제 #2
0
파일: KzMinerId.cs 프로젝트: wy000000/KzBsv
        public static KzMinerId Parse(KzScript scriptPub)
        {
            var m     = (KzMinerId)null;
            var count = -1;

            foreach (var op in scriptPub.Decode())
            {
                count++;
                switch (count)
                {
                case 0: if (op.Code != KzOpcode.OP_0)
                    {
                        goto fail;
                    }
                    break;

                case 1: if (op.Code != KzOpcode.OP_RETURN)
                    {
                        goto fail;
                    }
                    break;

                case 2: if (op.Code != KzOpcode.OP_PUSH4 || op.Data.AsUInt32BigEndian() != _ProtocolPrefix)
                    {
                        goto fail;
                    }
                    m = new KzMinerId();
                    break;

                case 3: m._StaticJson = op.GetDataBytes().ToUTF8(); break;

                case 4: m._StaticSig = op.GetDataBytes(); break;

                case 5: m._DynamicJson = op.GetDataBytes().ToUTF8(); break;

                case 6: m._DynamicSig = op.GetDataBytes(); break;

                default: break;     // Ignore additional ops.
                }
            }
            if (m._StaticJson == null || m._StaticSig == null)
            {
                goto fail;
            }

            m._Static = JsonConvert.DeserializeObject <MinerIdStatic>(m._StaticJson);

            if (!m.Verify())
            {
                goto fail;
            }

            return(m);

fail:
            return(null);
        }
예제 #3
0
        /// <summary>
        /// Implements brfc 759684b1a19a, paymentDestination: bsvalias Payment Addressing (Basic Address Resolution)
        ///
        /// </summary>
        /// <param name="key">Private key with which to sign this request. If null, signature will be blank. Else, must match public key returned by GetPubKey(senderHandle).</param>
        /// <param name="receiverHandle"></param>
        /// <param name="senderHandle"></param>
        /// <param name="senderName"></param>
        /// <param name="amount"></param>
        /// <param name="purpose"></param>
        /// <returns></returns>
        public async Task <KzScript> GetOutputScript(KzPrivKey key, string receiverHandle, string senderHandle, string senderName = null, KzAmount?amount = null, string purpose = null)
        {
            if (!amount.HasValue)
            {
                amount = KzAmount.Zero;
            }
            var dt        = DateTime.UtcNow.ToString("o");
            var message   = $"{senderHandle}{amount.Value.Satoshis}{dt}{purpose}";
            var signature = key?.SignMessageToB64(message);

            // var ok = key.GetPubKey().VerifyMessage(message, signature);

            var request = new GetOutputScriptRequest
            {
                senderHandle = senderHandle,
                amount       = amount.Value.Satoshis,
                dt           = dt,
                purpose      = purpose ?? "",
                senderName   = senderName ?? "",
                signature    = signature ?? ""
            };

            var jsonContent = JsonConvert.SerializeObject(request);
            var httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            var uri = await GetAddressUrl(receiverHandle);

            var rm = await _HttpClient.PostAsync(uri, httpContent);

            if (rm.StatusCode == HttpStatusCode.OK)
            {
                var response = await rm.Content.ReadAsStringAsync();

                // e.g. {"output":"76a914bdfbe8a16162ba467746e382a081a1857831811088ac"}

                var r = JsonConvert.DeserializeObject <GetOutputScriptResponse>(response);

                var s = new KzScript(r.output);

                return(s);
            }

            if (rm.StatusCode == HttpStatusCode.NotFound)
            {
                throw new ArgumentException($"Paymail \"{receiverHandle}\" was not found by this service.");
            }

            throw new Exception($"Unhandled HTTP Post StatusCode {rm.StatusCode}.");
        }
예제 #4
0
        KzBlock CreateGenesisBlock(string pszTimestamp,
                                   KzScript genesisOutputScript,
                                   UInt32 nTime, UInt32 nNonce,
                                   UInt32 nBits, Int32 nVersion,
                                   Int64 genesisReward)
        {
            var txs = new KzTransaction[] {
                new KzTransaction(
                    version: 1,
                    vin: new KzTxIn[] {
                    new KzTxIn(
                        prevout: new KzOutPoint(KzUInt256.Zero, -1),
                        scriptSig: new KzScript(""),
                        sequence: 0)
                },
                    vout: new KzTxOut[] {
                    new KzTxOut(
                        value: 0,
                        scriptPub: new KzScript("")
                        )
                },
                    lockTime: 0
                    )
            };
            var hashMerkleRoot = KzMerkleTree.ComputeMerkleRoot(txs);
            var genesis        = new KzBlock(
                txs: txs,
                version: 1,
                hashPrevBlock: KzUInt256.Zero,
                hashMerkleRoot: hashMerkleRoot,
                time: 1231006506,
                bits: 0x1d00ffff,
                nonce: 2083236893
                );

            return(genesis);
        }
예제 #5
0
 public bool Equals(KzScript o) => Length == o.Length && _script.CompareTo(o._script) == 0;
예제 #6
0
 /// <summary>
 /// Decode a script segment and convert the Ops to builder BOps.
 /// </summary>
 /// <param name="script"></param>
 /// <returns>Sequence of builder BOps</returns>
 public static IEnumerable <KzBOp> ToBOps(this KzScript script) => script.Decode().Cast <KzBOp>();
예제 #7
0
 /// <summary>
 /// Decode a script segment and convert the Ops to builder BOps.
 /// </summary>
 /// <param name="script"></param>
 /// <returns>Sequence of builder BOps</returns>
 public static IEnumerable <KzBOp> ToBOps(this KzScript script) => script.Decode().Select(o => new KzBOp(o));
예제 #8
0
파일: KzTxIn.cs 프로젝트: wy000000/KzBsv
 public KzTxIn(KzOutPoint prevout, KzScript scriptSig, UInt32 sequence)
 {
     _prevout = prevout;
     _scriptSig = scriptSig;
     _sequence = sequence;
 }
예제 #9
0
파일: KzTxOut.cs 프로젝트: wy000000/KzBsv
 public KzTxOut(Int64 value, KzScript scriptPub)
 {
     _value = value; _scriptPub = scriptPub;
 }
예제 #10
0
파일: KzTxOut.cs 프로젝트: TagsRocks/KzBsv
 public KzTxOut(Int64 value, KzScript scriptPubKey)
 {
     _value = value; _scriptPubKey = scriptPubKey;
 }