public void Export( [Argument("KEY-ID", Description = "A key UUID to export.")] Guid keyId, PassphraseParameters passphrase, [Option('P', Description = "Export a public key instead of private key.")] bool publicKey = false, [Option( 'b', Description = "Print raw bytes instead of hexadecimal. No trailing LF appended." )] bool bytes = false ) { PrivateKey key = UnprotectKey(keyId, passphrase); byte[] rawKey = publicKey ? key.PublicKey.Format(true) : key.ToByteArray(); if (bytes) { using Stream stdout = Console.OpenStandardOutput(); stdout.Write(rawKey, 0, rawKey.Length); } else { Console.WriteLine(ByteUtil.Hex(rawKey)); } }
public void Create( PassphraseParameters passphrase, [Option( Description = "Print the created private key as Web3 Secret Storage format." )] bool json = false, [Option(Description = "Do not add to the key store, but only show the created key.")] bool dryRun = false ) => Add(new PrivateKey(), passphrase, json, dryRun);
public void Remove( [Argument(Name = "KEY-ID", Description = "A key UUID to remove.")] Guid keyId, PassphraseParameters passphrase, [Option(Description = "Remove without asking passphrase.")] bool noPassphrase = false ) { try { if (!noPassphrase) { UnprotectKey(keyId, passphrase); } KeyStore.Remove(keyId); } catch (NoKeyException) { throw Utils.Error($"No such key ID: {keyId}"); } }
public void Import( [Argument( "PRIVATE-KEY", Description = "A raw private key to import in hexadecimal string." )] string rawKeyHex, PassphraseParameters passphrase, [Option( Description = "Print the created private key as Web3 Secret Storage format." )] bool json = false, [Option(Description = "Do not add to the key store, but only show the created key.")] bool dryRun = false ) { PrivateKey key; try { key = PrivateKey.FromString(rawKeyHex); } catch (FormatException) { throw Utils.Error("A raw private key should be hexadecimal."); } catch (ArgumentOutOfRangeException) { throw Utils.Error("Hexadecimal characters should be even (not odd)."); } catch (Exception) { throw Utils.Error("Invalid private key."); } Add(key, passphrase, json, dryRun); }
public void Sign( [Argument(Name = "KEY-ID", Description = "A private key to use for signing.")] Guid keyId, [Argument(Name = "VERSION", Description = "A version number to sign.")] int version, PassphraseParameters passphrase, [Option( 'E', ValueName = "FILE", Description = "Bencodex file to use for extra data. " + "For standard input, use a hyphen (`-'). " + "For an actual file named a hyphen, prepend `./', i.e., `./-'." )] string?extraFile = null, [Option( 'e', ValueName = "KEY=VALUE", Description = "Set a value to a key on extra Bencodex dictionary. " + "Can be applied multiple times (e.g., `-e foo=1 -e bar=baz'). " + "This option implies the extra data to be a Bencodex dictionary, " + "hence cannot be used together with -E/--extra-file option." )] string[]?extra = null ) { if (passphrase.PassphraseFile == "-" && extraFile == "-") { throw Utils.Error( "-P/--passphrase-file and -E/--extra-file cannot read standard input at a " + "time. Please specify one of both to read from a regular file." ); } PrivateKey key = new KeyCommand().UnprotectKey(keyId, passphrase, ignoreStdin: true); IValue? extraValue = null; if (extraFile is string path) { if (extra is string[] e && e.Length > 0) { throw Utils.Error( "-E/--extra-file and -e/--extra cannot be used together at a time." ); } var codec = new Codec(); if (path == "-") { // Stream for stdin does not support .Seek() using MemoryStream buffer = new MemoryStream(); using (Stream stream = Console.OpenStandardInput()) { stream.CopyTo(buffer); } buffer.Seek(0, SeekOrigin.Begin); extraValue = codec.Decode(buffer); } else { using Stream stream = File.Open(path, FileMode.Open, FileAccess.Read); extraValue = codec.Decode(stream); } } else if (extra is string[] e && e.Length > 0) { var dict = Bencodex.Types.Dictionary.Empty; foreach (string pair in e) { int sepPos = pair.IndexOf('='); if (sepPos < 0) { throw Utils.Error( "-e/--extra must be a pair of KEY=VALUE, but no equal (=) separator: " + $"`{pair}'." ); } string key_ = pair.Substring(0, sepPos); string value = pair.Substring(sepPos + 1); dict = dict.SetItem(key_, value); } extraValue = dict; } AppProtocolVersion v = AppProtocolVersion.Sign(key, version, extraValue); Console.WriteLine(v.Token); }