protected override async Task InvokeAsyncBase(InvocationContext context) { var attestation = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("attestation")?.Trim(); if (attestation is null) { throw new CommandOptionRequiredException("attestation"); } var bytes = Encoders.Hex.DecodeData(attestation); if (bytes.Length != 32) { throw new CommandException("attestation", "The attestation must be 32 bytes"); } var attestationKey = new Key(bytes); EventFullName evt = context.GetEventName(); var oracle = await Repository.GetOracle(evt.OracleName); if (oracle is null) { throw new CommandException("name", "This oracle does not exists"); } var outcome = await Repository.AddAttestation(evt, attestationKey); if (outcome?.OutcomeString is null) { throw new CommandException("attestation", "This attestation does not attest known outcomes"); } context.Console.Out.Write(outcome.OutcomeString); }
protected override async Task InvokeAsyncBase(InvocationContext context) { EventFullName evt = context.GetEventName(); if (await TryGetEvent(evt) is Event) { throw new CommandException("eventfullname", "This event already exists"); } var outcomes = context.GetOutcomes(); var oracle = await this.GetOracle("eventfullname", evt.OracleName); if (oracle.RootedKeyPath is null) { throw new CommandException("eventfullname", "You do not own the keys of this oracle"); } var k = await Repository.CreatePrivateKey(); var nonce = k.PrivateKey.ToECPrivKey().CreateSchnorrNonce(); var evtId = new OracleInfo(oracle.PubKey !, nonce); if (!await Repository.AddEvent(evtId, outcomes, k.KeyPath)) { throw new CommandException("eventfullname", "This event already exists"); } await NameRepository.AsEventRepository().SetMapping(evtId, evt.Name); context.Console.Out.Write(nonce.ToString()); }
protected override async Task InvokeAsyncBase(InvocationContext context) { EventFullName evt = context.GetEventName(); if (await Repository.GetEvent(evt) is Event) { throw new CommandException("name", "This event already exists"); } var outcomes = context.GetOutcomes(); var oracle = await Repository.GetOracle(evt.OracleName); if (oracle is null) { throw new CommandException("name", "This oracle does not exists"); } if (oracle.RootedKeyPath is null) { throw new CommandException("name", "You do not own the keys of this oracle"); } var k = await Repository.CreatePrivateKey(); var nonce = k.PrivateKey.ToECPrivKey().CreateSchnorrNonce(); if (!await Repository.AddEvent(evt, nonce, outcomes, k.KeyPath)) { throw new CommandException("name", "This event already exists"); } context.Console.Out.Write(nonce.ToString()); }
protected override async Task InvokeAsyncBase(InvocationContext context) { EventFullName evt = context.GetEventName(); var rNonce = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("nonce")?.ToLowerInvariant().Trim(); if (rNonce is null) { throw new CommandOptionRequiredException("nonce"); } if (!SchnorrNonce.TryParse(rNonce, out var nonce) || nonce is null) { throw new CommandException("nonce", "Invalid nonce"); } var oracle = await GetOracle("eventfullname", evt.OracleName); var outcomes = context.GetOutcomes(); var evtId = new OracleInfo(oracle.PubKey !, nonce); if (!await Repository.AddEvent(evtId, outcomes.ToArray())) { throw new CommandException("eventfullname", "The specified event already exists"); } await NameRepository.AsEventRepository().SetMapping(evtId, evt.Name); }
protected override async Task InvokeAsyncBase(InvocationContext context) { var eventName = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("name")?.Trim(); if (eventName is null) { throw new CommandOptionRequiredException("name"); } if (!EventFullName.TryParse(eventName, out EventFullName? evt) || evt is null) { throw new CommandException("name", "Invalid event full name, should be in the form 'oracleName/eventName'"); } var rNonce = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("nonce")?.ToLowerInvariant().Trim(); if (rNonce is null) { throw new CommandOptionRequiredException("nonce"); } if (!SchnorrNonce.TryParse(rNonce, out var nonce) || nonce is null) { throw new CommandException("nonce", "Invalid nonce"); } var outcomes = context.GetOutcomes(); if (!await Repository.OracleExists(evt.OracleName)) { throw new CommandException("name", "The specified oracle do not exists"); } if (!await Repository.AddEvent(evt, nonce, outcomes.ToArray())) { throw new CommandException("name", "The specified event already exists"); } }
protected override async Task InvokeAsyncBase(InvocationContext context) { var outcome = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("outcome")?.Trim(); var force = context.ParseResult.ValueForOption <bool>("force"); if (outcome is null) { throw new CommandOptionRequiredException("outcome"); } EventFullName evt = context.GetEventName(); var oracle = await Repository.GetOracle(evt.OracleName); if (oracle is null) { throw new CommandException("name", "This oracle does not exists"); } var discreteOutcome = new DiscreteOutcome(outcome); var evtObj = await Repository.GetEvent(evt); if (evtObj?.Nonce is null) { throw new CommandException("name", "This event does not exists"); } if (evtObj?.NonceKeyPath is null) { throw new CommandException("name", "You did not generated this event"); } outcome = evtObj.Outcomes.FirstOrDefault(o => o.Equals(outcome, StringComparison.OrdinalIgnoreCase)); if (outcome is null) { throw new CommandException("outcome", "This outcome does not exists in this event"); } var key = oracle.RootedKeyPath is RootedKeyPath ? await Repository.GetKey(oracle.RootedKeyPath) : null; if (key is null) { throw new CommandException("name", "You do not own the keys of this oracle"); } if (evtObj.Attestations?.ContainsKey(outcome) is true) { throw new CommandException("outcome", "This outcome has already been attested"); } if (evtObj.Attestations != null && evtObj.Attestations.Count > 0 && !force) { throw new CommandException("outcome", "An outcome has already been attested, attesting another one could leak the private key of your oracle. Use -f to force your action."); } var kValue = await Repository.GetKey(evtObj.NonceKeyPath); key.ToECPrivKey().TrySignBIP140(discreteOutcome.Hash, new PrecomputedNonceFunctionHardened(kValue.ToECPrivKey().ToBytes()), out var sig); var oracleAttestation = new Key(sig !.s.ToBytes()); if (await Repository.AddAttestation(evt, oracleAttestation) != outcome) { throw new InvalidOperationException("Error while validating reveal"); } context.Console.Out.Write(oracleAttestation.ToHex()); }
public static EventFullName GetEventName(this InvocationContext context) { var eventName = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("eventfullname")?.Trim(); if (eventName is null) { throw new CommandOptionRequiredException("eventfullname"); } if (!EventFullName.TryParse(eventName, out EventFullName? evt) || evt is null) { throw new CommandException("eventfullname", "Invalid event full name, should be in the form 'oracleName/eventName'"); } return(evt); }
public static bool TryParse(string fullName, out EventFullName?evt) { if (fullName == null) { throw new ArgumentNullException(nameof(fullName)); } fullName = fullName.Trim(); evt = null; var i = fullName.IndexOf('/'); if (i == -1) { return(false); } evt = new EventFullName(fullName.Substring(0, i), fullName.Substring(i + 1)); return(true); }
protected override async Task InvokeAsyncBase(InvocationContext context) { var oracleName = context.ParseResult.ValueForOption <string>("oracle"); foreach (var oracle in (await Repository.ListOracles()).OrderBy(o => o.Name)) { if (oracleName is string && !oracle.Name.Equals(oracleName, StringComparison.OrdinalIgnoreCase)) { continue; } foreach (var evts in (await Repository.ListEvents(oracle.Name)).OrderBy(o => o.Name)) { var fullName = new EventFullName(oracle.Name, evts.Name); context.Console.Out.WriteLine(fullName.ToString()); } } }
protected override async Task InvokeAsyncBase(InvocationContext context) { var name = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("name")?.Trim(); if (name is null) { throw new CommandOptionRequiredException("name"); } if (!EventFullName.TryParse(name, out var evtName) || evtName is null) { throw new CommandException("name", "Invalid event full name, should be in the form 'oracleName/eventName'"); } var evt = await Repository.GetEvent(evtName); if (evt is null) { throw new CommandException("name", "Event not found"); } var oracle = await Repository.GetOracle(evtName.OracleName); if (oracle is null) { throw new CommandException("name", "Event not found"); } context.Console.Out.WriteLine($"Full Name: {evtName}"); context.Console.Out.WriteLine($"Oracle: {oracle.Name}"); context.Console.Out.WriteLine($"Name: {evtName.Name}"); context.Console.Out.WriteLine($"Nonce: {evt.Nonce}"); context.Console.Out.WriteLine($"Can reveal: {oracle.RootedKeyPath is RootedKeyPath}"); int i = 0; foreach (var outcome in evt.Outcomes) { context.Console.Out.WriteLine($"Outcome[{i}]: {outcome}"); i++; } if (evt.Attestations is Dictionary <string, Key> ) { foreach (var kv in evt.Attestations) { context.Console.Out.WriteLine($"Attestation[\"{kv.Key}\"]: {kv.Value.ToHex()}"); } } }