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); }
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); }
/// <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}."); }
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); }
public bool Equals(KzScript o) => Length == o.Length && _script.CompareTo(o._script) == 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>();
/// <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));
public KzTxIn(KzOutPoint prevout, KzScript scriptSig, UInt32 sequence) { _prevout = prevout; _scriptSig = scriptSig; _sequence = sequence; }
public KzTxOut(Int64 value, KzScript scriptPub) { _value = value; _scriptPub = scriptPub; }
public KzTxOut(Int64 value, KzScript scriptPubKey) { _value = value; _scriptPubKey = scriptPubKey; }