Ejemplo n.º 1
0
    public byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase)
    {
//		if (nep2 == null)
//			throw new ArgumentNullException (nameof (nep2));
//		if (passphrase == null)
//			throw new ArgumentNullException (nameof (passphrase));

        byte[] data = nep2.Base58CheckDecode();

        if (data.Length != 39 || data [0] != 0x01 || data [1] != 0x42 || data [2] != 0xe0)
        {
            throw new FormatException();
        }

        Debug.Log("nep2 " + nep2);

        byte[] addresshash = new byte[4];

        Buffer.BlockCopy(data, 3, addresshash, 0, 4);

        Debug.Log("in nep2: " + passphrase);

        byte[] derivedkey   = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, 16384, 8, 8, 64);
        byte[] derivedhalf1 = derivedkey.Take(32).ToArray();
        byte[] derivedhalf2 = derivedkey.Skip(32).ToArray();
        byte[] encryptedkey = new byte[32];
        Buffer.BlockCopy(data, 7, encryptedkey, 0, 32);
        byte[] prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1);
        Neo.Cryptography.ECC.ECPoint pubkey = Neo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;

        var bytes = pubkey.EncodePoint(true).ToArray();
//		byte[] CompressedPublicKey = bytes;

        // byte[] PublicKeyHash = Crypto.Default.ToScriptHash (bytes);

        string  signatureScript = KeyPair.CreateSignatureScript(bytes);
        UInt160 signatureHash   = Crypto.Default.ToScriptHash(signatureScript.HexToBytes());

        byte[] publickey = pubkey.EncodePoint(false).Skip(1).ToArray();

        string address = Crypto.Default.ToAddress(signatureHash);

        Debug.Log("decrypted private key: " + prikey.ByteToHex());
        Debug.Log("decrypted public key: " + publickey.ByteToHex());
        Debug.Log("decrypted address: " + address);

        return(prikey);
    }
Ejemplo n.º 2
0
        public static KeyPair Mnemonickeypair(string menimonic)
        {
            var getmenimonicBytes = Encoding.ASCII.GetBytes(menimonic);
            var lengthofmenimonic = getmenimonicBytes.Length;

            byte[] data = new byte[lengthofmenimonic];
            data[0] = 0x80;
            Buffer.BlockCopy(getmenimonicBytes, 0, data, 1, lengthofmenimonic - 2);
            data[lengthofmenimonic - 1] = 0x01;
            string wif = data.Base58CheckEncode();

            Array.Clear(data, 0, data.Length);

            if (wif == null)
            {
                throw new ArgumentNullException();
            }
            byte[] data1 = wif.Base58CheckDecode();
            if (data1.Length != lengthofmenimonic || data1[0] != 0x80 || data1[lengthofmenimonic - 1] != 0x01)
            {
                throw new FormatException();
            }
            byte[] PrivateKeyOfmenimonic = new byte[32];
            Buffer.BlockCopy(data1, 1, PrivateKeyOfmenimonic, 0, PrivateKeyOfmenimonic.Length);
            Array.Clear(data1, 0, data1.Length);

            KeyPair menimonickeypair = new KeyPair(PrivateKeyOfmenimonic);


            var privatekeyBytes = menimonickeypair.PrivateKey;
            var pubKeyBytes     = menimonickeypair.PublicKey;
            var privateKey      = Base58.Encode(privatekeyBytes);
            var pubKey          = Base58.Encode(pubKeyBytes);
            var sc      = KeyPair.CreateSignatureScript(privatekeyBytes);
            var address = menimonickeypair.address;

            Console.WriteLine("\n In Function ................. : Private Key: " + privateKey + "\nPublic key:" + pubKey + "\n Address :" + address);
            return(menimonickeypair);
        }