|
| 1 | +using Keysmith.Net.BIP; |
| 2 | +using Keysmith.Net.EC; |
| 3 | +using Keysmith.Net.ED; |
| 4 | +using System.CommandLine; |
| 5 | + |
| 6 | +namespace KeyUtils.Cli.Derive; |
| 7 | + |
| 8 | +public sealed class DeriveCommand : Command |
| 9 | +{ |
| 10 | + public DeriveCommand() : base("derive", "derive a key for a supported chain") |
| 11 | + { |
| 12 | + Add(new DeriveChainLeafCommand( |
| 13 | + "solana", |
| 14 | + "derive a Solana key", |
| 15 | + BIP44.Solana, |
| 16 | + ED25519.Instance, |
| 17 | + [OutputFormat.Hex, OutputFormat.Base58, OutputFormat.Json], |
| 18 | + OutputFormat.Base58)); |
| 19 | + |
| 20 | + Add(new DeriveChainLeafCommand( |
| 21 | + "evm", |
| 22 | + "derive an EVM key", |
| 23 | + accountIndex => BIP44.Ethereum((uint) accountIndex), |
| 24 | + Secp256k1.Instance, |
| 25 | + [OutputFormat.Hex], |
| 26 | + OutputFormat.Hex)); |
| 27 | + |
| 28 | + Add(new DeriveChainLeafCommand( |
| 29 | + "cosmos", |
| 30 | + "derive a Cosmos key", |
| 31 | + BIP44.Cosmos, |
| 32 | + Secp256k1.Instance, |
| 33 | + [OutputFormat.Hex], |
| 34 | + OutputFormat.Hex)); |
| 35 | + } |
| 36 | + |
| 37 | + private sealed class DeriveChainLeafCommand : Command |
| 38 | + { |
| 39 | + private readonly Func<int, string> _pathFactory; |
| 40 | + private readonly ECCurve _curve; |
| 41 | + private readonly OutputFormat[] _supportedFormats; |
| 42 | + |
| 43 | + private readonly Option<FileInfo> _mnemonicFileOption = new("--mnemonic-file", "-i") |
| 44 | + { |
| 45 | + Description = "Path to the file containing the mnemonic", |
| 46 | + Required = true |
| 47 | + }; |
| 48 | + |
| 49 | + private readonly Option<int> _accountIndexOption = new("--account-index") |
| 50 | + { |
| 51 | + Description = "The account index to derive", |
| 52 | + DefaultValueFactory = _ => 0 |
| 53 | + }; |
| 54 | + |
| 55 | + private readonly Option<string> _formatOption; |
| 56 | + |
| 57 | + private readonly Option<FileInfo?> _outputOption = new("--output", "-o") |
| 58 | + { |
| 59 | + Description = "Path to the file where the derived key will be saved" |
| 60 | + }; |
| 61 | + |
| 62 | + public DeriveChainLeafCommand( |
| 63 | + string name, |
| 64 | + string description, |
| 65 | + Func<int, string> pathFactory, |
| 66 | + ECCurve curve, |
| 67 | + OutputFormat[] supportedFormats, |
| 68 | + OutputFormat defaultFormat) : base(name, description) |
| 69 | + { |
| 70 | + _pathFactory = pathFactory; |
| 71 | + _curve = curve; |
| 72 | + _supportedFormats = supportedFormats; |
| 73 | + _formatOption = new Option<string>("--format", "-f") |
| 74 | + { |
| 75 | + Description = $"Output format ({OutputFormatExtensions.Describe(supportedFormats)}). Defaults to {defaultFormat.ToCliValue()}", |
| 76 | + DefaultValueFactory = _ => defaultFormat.ToCliValue() |
| 77 | + }; |
| 78 | + _formatOption.AcceptOnlyFromAmong([.. supportedFormats.Select(format => format.ToCliValue())]); |
| 79 | + |
| 80 | + Add(_mnemonicFileOption); |
| 81 | + Add(_accountIndexOption); |
| 82 | + Add(_formatOption); |
| 83 | + Add(_outputOption); |
| 84 | + |
| 85 | + SetAction(Handle); |
| 86 | + } |
| 87 | + |
| 88 | + private Task<int> Handle(ParseResult parseResult) |
| 89 | + { |
| 90 | + var mnemonicFile = parseResult.GetValue(_mnemonicFileOption)!; |
| 91 | + int accountIndex = parseResult.GetValue(_accountIndexOption); |
| 92 | + string formatText = parseResult.GetValue(_formatOption)!; |
| 93 | + var outputFile = parseResult.GetValue(_outputOption); |
| 94 | + |
| 95 | + if(!OutputFormatExtensions.TryParse(formatText, out var format) || !_supportedFormats.Contains(format)) |
| 96 | + { |
| 97 | + Console.WriteLine($"Error: Unsupported format '{formatText}'. Supported formats: {OutputFormatExtensions.Describe(_supportedFormats)}"); |
| 98 | + return Task.FromResult(1); |
| 99 | + } |
| 100 | + |
| 101 | + return DeriveKeySupport.DeriveAsync(mnemonicFile, _pathFactory(accountIndex), _curve, format, outputFile); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments