Ejemplo n.º 1
0
        private object RouteConvertAddressETH(HTTPRequest request)
        {
            var ethKey  = request.GetVariable("ethKey");
            var context = InitContext(request);

            EthereumKey ethKeys;

            ethKeys = EthereumKey.FromWIF(ethKey);

            var result = $"{ethKeys.ToString()}";

            return(result);
        }
Ejemplo n.º 2
0
        public string SignTransaction(EthereumKey keys, int nonce, string receiveAddress, BigInteger amount, BigInteger gasPrice, BigInteger gasLimit, string data = null)
        {
            var realAmount = System.Numerics.BigInteger.Parse(amount.ToString());

            //Create a transaction from scratch
            var tx = new Phantasma.Ethereum.Signer.Transaction(receiveAddress, realAmount, nonce,
                                                               System.Numerics.BigInteger.Parse(gasPrice.ToString()),
                                                               System.Numerics.BigInteger.Parse(gasLimit.ToString()),
                                                               data);

            tx.Sign(new EthECKey(keys.PrivateKey, true));

            var encoded = tx.GetRLPEncoded();

            return("0x" + Base16.Encode(encoded));
        }
Ejemplo n.º 3
0
        private object RouteConvertAddressETHHEX(HTTPRequest request)
        {
            var ethKey  = request.GetVariable("ethKey");
            var context = InitContext(request);

            EthereumKey ethKeys;

            var bytes = Base16.Decode(ethKey);
            var temp  = new PhantasmaKeys(bytes);

            ethKeys = EthereumKey.FromWIF(temp.ToWIF());

            var result = $"{ethKeys.ToString()}";

            return(result);
        }
Ejemplo n.º 4
0
        public string SignTokenTransaction(EthereumKey keys, int nonce, string tokenContract, string receiveAddress, BigInteger amount, BigInteger gasPrice, BigInteger gasLimit)
        {
            var transferMethodHash = "a9059cbb";
            var to        = receiveAddress.Substring(2).PadLeft(64, '0');
            var amountHex = amount.ToHex().PadLeft(64, '0');

            //Create a transaction from scratch
            var tx = new Phantasma.Ethereum.Signer.Transaction(tokenContract,
                                                               0, // Ammount of ETH to be transfered (0)
                                                               nonce,
                                                               System.Numerics.BigInteger.Parse(gasPrice.ToString()),
                                                               System.Numerics.BigInteger.Parse(gasLimit.ToString()),
                                                               transferMethodHash + to + amountHex);

            tx.Sign(new EthECKey(keys.PrivateKey, true));

            var encoded = tx.GetRLPEncoded();

            return("0x" + Base16.Encode(encoded));
        }
Ejemplo n.º 5
0
        private object RouteInvokeSettleTxETH(HTTPRequest request)
        {
            var txHash        = request.GetVariable("txHash");
            var ethKey        = request.GetVariable("ethKey");
            var assetSymbol   = request.GetVariable("assetSymbol");
            var context       = InitContext(request);
            var phantasmaKeys = GetLoginKey(request);

            InvalidateCache(phantasmaKeys.Address);
            if (context["holdings"] is Holding[] balance)
            {
                EthereumKey ethKeys;

                ethKeys = EthereumKey.FromWIF(ethKey);

                var result = AccountController.InvokeSettleTxETH(ethKeys, phantasmaKeys, txHash, assetSymbol).Result;
                ResetSessionSendFields(request);
                return(result);
            }
            return(null);
        }
Ejemplo n.º 6
0
        protected void onShowInteropKeys(string[] args)
        {
            var wif = args[0];

            if (string.IsNullOrEmpty(wif))
            {
                Console.WriteLine("Wif cannot be empty");
                return;
            }

            var platformName = args[1];

            if (string.IsNullOrEmpty(platformName))
            {
                Console.WriteLine("Wif cannot be empty");
                return;
            }

            var genesisHash = _cli.Nexus.GetGenesisHash(_cli.Nexus.RootStorage);
            var interopKeys = InteropUtils.GenerateInteropKeys(PhantasmaKeys.FromWIF(wif), genesisHash, platformName);

            switch (platformName)
            {
            case EthereumWallet.EthereumPlatform:
                var ethKeys = EthereumKey.FromWIF(interopKeys.ToWIF());
                Console.WriteLine($"Platfrom:    {platformName}");
                Console.WriteLine($"WIF:         {ethKeys.GetWIF()}");
                Console.WriteLine($"Private key: {ethKeys.PrivateKey.ToHex()}");
                Console.WriteLine($"Address:     {ethKeys.Address}");
                break;

            case NeoWallet.NeoPlatform:
                Console.WriteLine($"Not yet added, feel free to add.");
                break;
            }
        }
Ejemplo n.º 7
0
        public IEnumerator ECDsaSecP256k1()
        {
            // Eth address: "0x66571c32d77c4852be4c282eb952ba94efbeac20";
            var key = "6f6784731c4e526c97fa6a97b6f22e96f307588c5868bc2c545248bc31207eb1";

            Assert.IsTrue(key.Length == 64);

            var privBytes     = Base16.Decode(key);
            var phantasmaKeys = new PhantasmaKeys(privBytes);

            var wif     = phantasmaKeys.ToWIF();
            var ethKeys = EthereumKey.FromWIF(wif);

            Debug.Log("Eth address: " + ethKeys);

            var pKey = ECCurve.Secp256k1.G * privBytes;
            var ethPublicKeyCompressed = pKey.EncodePoint(true).ToArray();

            Debug.Log("Eth compressed public key: " + Base16.Encode(ethPublicKeyCompressed));
            var ethPublicKeyUncompressed = pKey.EncodePoint(false).Skip(1).ToArray();

            Debug.Log("Eth uncompressed public key: " + Base16.Encode(ethPublicKeyUncompressed));

            var msgBytes  = Encoding.ASCII.GetBytes("Phantasma");
            var signature = ethKeys.Sign(msgBytes, (message, prikey, pubkey) =>
            {
                return(Phantasma.Neo.Utils.CryptoUtils.Sign(message, prikey, pubkey, Phantasma.Cryptography.ECC.ECDsaCurve.Secp256k1));
            });

            var ecdsaSignature      = (ECDsaSignature)signature;
            var signatureSerialized = signature.Serialize(); // signature.ToByteArray() gives same result

            Debug.Log("\nSignature (RAW concatenated r & s, hex):\n" + Base16.Encode(ecdsaSignature.Bytes));
            // Curve byte: ECDsaCurve enum: Secp256r1 = 0, Secp256k1 = 1.
            // Following is the format we use for signature:
            Debug.Log("\nSignature (curve byte + signature length + concatenated r & s, hex):\n" + Base16.Encode(signatureSerialized));

            var signatureDEREncoded = ethKeys.Sign(msgBytes, (message, prikey, pubkey) =>
            {
                return(Phantasma.Neo.Utils.CryptoUtils.Sign(message, prikey, pubkey, Phantasma.Cryptography.ECC.ECDsaCurve.Secp256k1, Neo.Utils.CryptoUtils.SignatureFormat.DEREncoded));
            });

            var ecdsaSignatureDEREncoded = (ECDsaSignature)signatureDEREncoded;

            Debug.Log("\nSignature (RAW DER-encoded, hex):\n" + Base16.Encode(ecdsaSignatureDEREncoded.Bytes));
            Debug.Log("\nSignature (curve byte + signature length + DER-encoded, hex):\n" + Base16.Encode(signatureDEREncoded.Serialize()));

            // Since ECDsaSignature class not working for us,
            // we use signature .Bytes directly to verify it with Bouncy Castle.
            // Verifying concatenated signature / compressed Eth public key.
            Assert.IsTrue(Phantasma.Neo.Utils.CryptoUtils.Verify(msgBytes, ecdsaSignature.Bytes, ethPublicKeyCompressed, ECDsaCurve.Secp256k1));

            // Verifying concatenated signature / uncompressed Eth public key.
            // Not working with Bouncy Castle.
            // Assert.IsTrue(Phantasma.Neo.Utils.CryptoUtils.Verify(msgBytes, ecdsaSignature.Bytes, ethPublicKeyUncompressed, ECDsaCurve.Secp256k1));

            // Verifying DER signature.
            Assert.IsTrue(Phantasma.Neo.Utils.CryptoUtils.Verify(msgBytes, ecdsaSignatureDEREncoded.Bytes, ethPublicKeyCompressed, ECDsaCurve.Secp256k1, Neo.Utils.CryptoUtils.SignatureFormat.DEREncoded));

            // This method we cannot use, it gives "System.NotImplementedException : The method or operation is not implemented."
            // exception in Unity, because Unity does not fully support .NET cryptography.
            // Assert.IsTrue(((ECDsaSignature)signature).Verify(msgBytes, Address.FromKey(ethKeys)));

            // Failes for same reason: "System.NotImplementedException".
            // Assert.IsTrue(CryptoExtensions.VerifySignatureECDsa(msgBytes, signatureSerialized, ethPublicKeyCompressed, ECDsaCurve.Secp256k1));

            yield return(null);
        }