/* return Command object * operands are different: * DoubleOperand: SourceMod,Source,DestMod,Dest * TwoOperand: Register, Mod, Src/Dest * SingleOperand: Mode, Register, * ConditionalBranch: Offset * If ERROR then operands has atribute ERR and MnemonicType has value ERR and\or Mnemonic has value ERR */ public static Command Decode(ushort input) { Mnemonic mnemonic = GetMnemonic(input); MnemonicType type = GetMnemonicType(mnemonic); Dictionary <String, UInt16> operands = new Dictionary <String, UInt16>(); if (type == MnemonicType.DoubleOperand) { operands.Add(SOURCE_MODE, Positioner.GetBits(input, 9, 11)); operands.Add(SOURCE, Positioner.GetBits(input, 6, 8)); operands.Add(DEST_MODE, Positioner.GetBits(input, 3, 5)); operands.Add(DEST, Positioner.GetBits(input, 0, 2)); } else if (type == MnemonicType.TwoOperand) { operands.Add(REG, Positioner.GetBits(input, 6, 8)); operands.Add(MODE, Positioner.GetBits(input, 3, 5)); operands.Add(SRC_DEST, Positioner.GetBits(input, 0, 2)); } else if (type == MnemonicType.SingleOperand) { operands.Add(MODE, Positioner.GetBits(input, 3, 5)); operands.Add(REG, Positioner.GetBits(input, 0, 2)); } else if (type == MnemonicType.ConditionalBranch) { operands.Add(OFFSET, Positioner.GetBits(input, 0, 7)); } else { operands.Add(ERR, 1); } return(new Command(mnemonic, type, operands)); }
/// <summary> /// Initializes a new instance of <see cref="ElectrumMnemonic"/> with a randomly generated entropy /// using the given <see cref="IRandomNumberGenerator"/> instance, world list and an the passphrase. /// </summary> /// <exception cref="ArgumentException"/> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentOutOfRangeException"/> /// <param name="rng">Random number generator to use</param> /// <param name="mnType">Type of the mnemonic to create (anything but <see cref="MnemonicType.Undefined"/>)</param> /// <param name="wl">[Defaultvalue = <see cref="BIP0039.WordLists.English"/> Word list to use</param> /// <param name="passPhrase"> /// [Default value = null] Optional passphrase to use for computing <see cref="BIP0032"/> entropy /// </param> public ElectrumMnemonic(IRandomNumberGenerator rng, MnemonicType mnType, BIP0039.WordLists wl = BIP0039.WordLists.English, string passPhrase = null) { if (rng is null) { throw new ArgumentNullException(nameof(rng), "Random number generator can not be null."); } if (!Enum.IsDefined(typeof(MnemonicType), mnType) || mnType == MnemonicType.Undefined) { throw new ArgumentException("Undefined mnemonic type.", nameof(mnType)); } MnType = mnType; allWords = BIP0039.GetAllWords(wl); byte[] entropy = new byte[EntropyByteLen]; rng.GetBytes(entropy); SetWordsFromEntropy(entropy); SetBip32(passPhrase); }
/// <summary> /// Initializes a new instance of <see cref="ElectrumMnemonic"/> with the given entropy, world list and the passphrase. /// </summary> /// <exception cref="ArgumentException"/> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentOutOfRangeException"/> /// <param name="entropy">Entropy to use (must be 17 bytes or 132 bits)</param> /// <param name="mnType">Type of the mnemonic to create (anything but <see cref="MnemonicType.Undefined"/>)</param> /// <param name="wl">[Defaultvalue = <see cref="BIP0039.WordLists.English"/> Word list to use</param> /// <param name="passPhrase"> /// [Default value = null] Optional passphrase to use for computing <see cref="BIP0032"/> entropy /// </param> public ElectrumMnemonic(byte[] entropy, MnemonicType mnType, BIP0039.WordLists wl = BIP0039.WordLists.English, string passPhrase = null) { if (entropy == null) { throw new ArgumentNullException(nameof(entropy), "Entropy can not be null."); } if (entropy.Length != EntropyByteLen) { throw new ArgumentOutOfRangeException(nameof(entropy), $"Entropy must be {EntropyByteLen} bytes or 132 bits."); } if (!Enum.IsDefined(typeof(MnemonicType), mnType) || mnType == MnemonicType.Undefined) { throw new ArgumentException("Undefined mnemonic type.", nameof(mnType)); } MnType = mnType; allWords = BIP0039.GetAllWords(wl); SetWordsFromEntropy(entropy); SetBip32(passPhrase); }
public Command(Mnemonic mnemonic, MnemonicType mnemonicType, Dictionary <String, ushort> operands) { Mnemonic = mnemonic; MnemonicType = mnemonicType; Operands = operands; }
public static Command GetCommand(String textCommand) { String[] parts = textCommand.Trim().Split(OPPERAND_DELIMETER); Mnemonic mnemonic = (Mnemonic)Enum.Parse(typeof(Mnemonic), parts[0], true); MnemonicType type = Decoder.GetMnemonicType(mnemonic); if (type == MnemonicType.DoubleOperand) { String[] operands = parts[1].Trim().Split(OPERANDS_DELIMETER); if (operands.Length != 2) { return(new Command());//ERROR } var op1 = operands[0].Trim().Split(MOD_DELIMETER); var op2 = operands[1].Trim().Split(MOD_DELIMETER); if (op1.Length != 2 || op2.Length != 2) { return(new Command());//ERROR } Dictionary <string, ushort> opps = new Dictionary <string, ushort>(); opps.Add(Decoder.SOURCE_MODE, UInt16.Parse(op1[0])); opps.Add(Decoder.SOURCE, UInt16.Parse(op1[1])); opps.Add(Decoder.DEST_MODE, UInt16.Parse(op2[0])); opps.Add(Decoder.DEST, UInt16.Parse(op2[1])); return(new Command(mnemonic: mnemonic, mnemonicType: type, operands: opps)); } if (type == MnemonicType.TwoOperand) { var operands = parts[1].Trim().Split(OPERANDS_DELIMETER); if (operands.Length != 2) { return(new Command());//ERROR } var op1 = operands[0].Trim().Split(MOD_DELIMETER); var op2 = operands[1].Trim().Split(MOD_DELIMETER); if (op1.Length != 2) { return(new Command());//ERROR } var opps = new Dictionary <string, ushort>(); opps.Add(Decoder.REG, UInt16.Parse(op1[0])); opps.Add(Decoder.MODE, UInt16.Parse(op1[1])); opps.Add(Decoder.SRC_DEST, UInt16.Parse(op2[0])); return(new Command(mnemonic: mnemonic, mnemonicType: type, operands: opps)); } if (type == MnemonicType.SingleOperand) { String[] operand = parts[1].Trim().Split(MOD_DELIMETER); if (operand.Length != 2) { return(new Command()); } Dictionary <string, ushort> opps = new Dictionary <string, ushort>(); opps.Add(Decoder.MODE, UInt16.Parse(operand[0])); opps.Add(Decoder.REG, UInt16.Parse(operand[1])); return(new Command(mnemonic: mnemonic, mnemonicType: type, operands: opps)); } if (type == MnemonicType.TwoOperand) { Dictionary <string, ushort> opps = new Dictionary <string, ushort>(); opps.Add(Decoder.OFFSET, UInt16.Parse(parts[1])); return(new Command(mnemonic: mnemonic, mnemonicType: type, operands: opps)); } return(new Command());//error }