private async void Start()
        {
            // Load private & public key from storage, if possible
            const string privateKeyKey = "blackjack_privateKey";
            const string publicKeyKey  = "blackjack_publicKey";

            byte[] privateKey;
            byte[] publicKey;
            if (PlayerPrefs.HasKey(privateKeyKey) && PlayerPrefs.HasKey(publicKeyKey))
            {
                privateKey = CryptoUtils.HexStringToBytes(PlayerPrefs.GetString(privateKeyKey));
                publicKey  = CryptoUtils.HexStringToBytes(PlayerPrefs.GetString(publicKeyKey));
            }
            else
            {
                privateKey = CryptoUtils.GeneratePrivateKey();
                publicKey  = CryptoUtils.PublicKeyFromPrivateKey(privateKey);
                PlayerPrefs.SetString(privateKeyKey, CryptoUtils.BytesToHexString(privateKey));
                PlayerPrefs.SetString(publicKeyKey, CryptoUtils.BytesToHexString(publicKey));
            }

            this.client               = new BlackjackContractClient(this.BackendHost, this.ContractAbi.text, privateKey, publicKey, NullLogger.Instance);
            this.client.RoomCreated  += ClientOnRoomCreated;
            this.client.PlayerJoined += ClientOnPlayerJoined;
            this.client.PlayerLeft   += ClientOnPlayerLeft;
            this.client.PlayerBetted += ClientOnPlayerBetted;
            this.client.PlayerReadyForNextRoundChanged += ClientOnPlayerReadyForNextRoundChanged;
            this.client.GameStageChanged          += ClientOnGameStageChanged;
            this.client.GameRoundResultsAnnounced += ClientOnGameRoundResultsAnnounced;
            this.client.CurrentPlayerIndexChanged += ClientOnCurrentPlayerIndexChanged;
            this.client.PlayerDecisionReceived    += ClientOnPlayerDecisionReceived;

            await UpdateRoomList();
        }
Пример #2
0
        public void Ed25519Test()
        {
            byte[] seed =
            {
                22, 218, 117,  80,  91, 159,  10, 154, 78,  89, 67, 85,  7, 57, 215, 103,
                68, 178, 222, 219, 152, 180, 172, 239, 75, 116, 88, 17, 42, 67, 227, 172,
            };

            byte[] privateKey = CryptoUtils.GeneratePrivateKey(seed);
            string hex        = CryptoUtils.BytesToHexString(privateKey);

            Assert.AreEqual("16DA75505B9F0A9A4E5943550739D76744B2DEDB98B4ACEF4B7458112A43E3AC25AE76342B3E06911DC2CDFF70C60736A45C9DC40D7D14BBC455501779DCB04D", hex);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            AddressJsonModel jsonModel = serializer.Deserialize <AddressJsonModel>(reader);

            return(new Address(CryptoUtils.BytesToHexString(jsonModel.Local), String.IsNullOrEmpty(jsonModel.ChainId) ? Address.kDefaultChainId : jsonModel.ChainId));
        }
Пример #4
0
    public async void SignIn()
    {
        if (dAppChainCfg == null)
        {
            Start();
        }

        if (this.identity != null)
        {
            return;
        }
#if !UNITY_WEBGL
        if (PlayerPrefs.GetString("identityString") == "" && PlayerPrefs.GetString("usernameString") == "")
        {
            try
            {
                CertValidationBypass.Enable();
                var authClient  = this.CreateAuthClient();
                var accessToken = await authClient.GetAccessTokenAsync();

                var keyStore = await this.CreateKeyStore(accessToken);

                this.identity = await authClient.GetIdentityAsync(accessToken, keyStore);
            }
            finally
            {
                CertValidationBypass.Disable();
            }
        }
        else
        {
            this.identity = new Identity {
                Username   = PlayerPrefs.GetString("usernameString"),
                PrivateKey = CryptoUtils.HexStringToBytes(PlayerPrefs.GetString("identityString"))
            };
        }
#else
        var authClient = this.CreateAuthClient();
        this.identity = await authClient.GetIdentityAsync("", null);
#endif

        PlayerPrefs.SetString("identityString", CryptoUtils.BytesToHexString(this.identity.PrivateKey));
        PlayerPrefs.SetString("usernameString", this.identity.Username);

        var writer = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithHTTP(dAppChainCfg.write_host)
                     //.WithWebSocket("ws://etherboy-stage.loomapps.io/websocket")
                     .Create();

        var reader = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithHTTP(dAppChainCfg.read_host)
                     //.WithWebSocket("ws://etherboy-stage.loomapps.io/queryws")
                     .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };
        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[] {
            new NonceTxMiddleware {
                PublicKey = this.identity.PublicKey,
                Client    = client
            },
            new SignedTxMiddleware(this.identity.PrivateKey)
        });

        // There is only one contract address at the moment...
        var contractAddr = Address.FromHexString("0xe288d6eec7150D6a22FDE33F0AA2d81E06591C4d");
        var callerAddr   = this.identity.ToAddress("default");
        this.contract = new Contract(client, contractAddr, callerAddr);

        //call create account, if it's a new user, an account will be created for Etherboy
        if (PlayerPrefs.GetInt("hasAccount") == 0)
        {
            CreateAccount();
        }
    }
Пример #5
0
        public IEnumerator MultipleSimultaneousRpcCallsMustSucceedTest()
        {
            return(AsyncEditorTestUtility.AsyncTest(async() =>
            {
                byte[] privateKey = CryptoUtils.GeneratePrivateKey();
                byte[] publicKey = CryptoUtils.PublicKeyFromPrivateKey(privateKey);
                EvmContract contract = await ContractTestUtility.GetEvmContract(privateKey, publicKey, this.testsAbi);

                using (contract.Client)
                {
                    const int callCount = 20;
                    Task[] callTasks = new Task[callCount];
                    for (int i = 0; i < callTasks.Length; i++)
                    {
                        // Any request will do. Use the nonce request, simple it is a simple one
                        callTasks[i] = new Task <ulong>(() => contract.Client.GetNonceAsync(CryptoUtils.BytesToHexString(publicKey)).Result);
                    }

                    foreach (Task task in callTasks)
                    {
                        task.Start();
                    }

                    await Task.WhenAll(callTasks);
                }
            }));
        }