Пример #1
0
    async Task <EvmContract> GetContract(byte[] privateKey, byte[] publicKey)
    {
        var writer = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     // .WithHTTP("http://127.0.0.1:46658/rpc")
                     .WithWebSocket("ws://127.0.0.1:46657/websocket")
                     .Create();

        var reader = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     // .WithHTTP("http://127.0.0.1:46658/query")
                     .WithWebSocket("ws://127.0.0.1:9999/queryws")
                     .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };

        // required middleware
        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[] {
            new NonceTxMiddleware {
                PublicKey = publicKey,
                Client    = client
            },
            new SignedTxMiddleware(privateKey)
        });

        string abi          = simpleStoreABI.ToString();
        var    contractAddr = await client.ResolveContractAddressAsync("SimpleStore");

        var callerAddr = Address.FromPublicKey(publicKey);

        return(new EvmContract(client, contractAddr, callerAddr, abi));
    }
Пример #2
0
        public async Task CreateContract(byte[] privateKey)
        {
            byte[]  publicKey  = CryptoUtils.PublicKeyFromPrivateKey(privateKey);
            Address callerAddr = Address.FromPublicKey(publicKey);

            IRpcClient writer = RpcClientFactory.Configure().WithLogger(Debug.unityLogger).WithWebSocket(WriterHost)
                                .Create();

            IRpcClient reader = RpcClientFactory.Configure().WithLogger(Debug.unityLogger).WithWebSocket(ReaderHost)
                                .Create();

            DAppChainClient client = new DAppChainClient(writer, reader)
            {
                Logger = Debug.unityLogger
            };

            client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[]
            {
                new NonceTxMiddleware(publicKey, client), new SignedTxMiddleware(privateKey)
            });

            client.AutoReconnect = false;

            await client.ReadClient.ConnectAsync();

            await client.WriteClient.ConnectAsync();

            Address contractAddr = await client.ResolveContractAddressAsync("ZombieBattleground");

            Contract oldContract = Contract;

            Contract = new Contract(client, contractAddr, callerAddr);
            ContractCreated?.Invoke(oldContract, Contract);
        }
Пример #3
0
        private async Task <EvmContract> GetContract(byte[] privateKey, byte[] publicKey, string abi)
        {
            ILogger    logger = NullLogger.Instance;
            IRpcClient writer = RpcClientFactory.Configure()
                                .WithLogger(logger)
                                .WithWebSocket("ws://127.0.0.1:46657/websocket")
                                .Create();

            IRpcClient reader = RpcClientFactory.Configure()
                                .WithLogger(logger)
                                .WithWebSocket("ws://127.0.0.1:9999/queryws")
                                .Create();

            DAppChainClient client = new DAppChainClient(writer, reader)
            {
                Logger = logger
            };

            // required middleware
            client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[]
            {
                new NonceTxMiddleware(publicKey, client),
                new SignedTxMiddleware(privateKey)
            });

            Address contractAddr = await client.ResolveContractAddressAsync("Tests");

            Address callerAddr = Address.FromPublicKey(publicKey);

            return(new EvmContract(client, contractAddr, callerAddr, abi));
        }
    async Task <EvmContract> GetContract(byte[] privateKey, byte[] publicKey)
    {
        var writer = RpcClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithWebSocket("ws://127.0.0.1:46658/websocket")
                     .Create();

        var reader = RpcClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithWebSocket("ws://127.0.0.1:46658/queryws")
                     .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };

        // required middleware
        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[]
        {
            new NonceTxMiddleware(publicKey, client),
            new SignedTxMiddleware(privateKey)
        });

        // ABI of the Solidity contract
        const string abi          = "[{\"constant\":false,\"inputs\":[{\"name\":\"_tileState\",\"type\":\"string\"}],\"name\":\"SetTileMapState\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"GetTileMapState\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"state\",\"type\":\"string\"}],\"name\":\"OnTileMapStateUpdate\",\"type\":\"event\"}]\r\n";
        var          contractAddr = await client.ResolveContractAddressAsync("TilesChain");

        var callerAddr = Address.FromPublicKey(publicKey);

        return(new EvmContract(client, contractAddr, callerAddr, abi));
    }
Пример #5
0
    async Task <Contract> GetContract(byte[] privateKey, byte[] publicKey)
    {
        var writer = RpcClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithHttp("http://127.0.0.1:46658/rpc")
                     //.WithWebSocket("ws://127.0.0.1:46658/websocket")
                     .Create();

        var reader = RpcClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithHttp("http://127.0.0.1:46658/query")
                     //.WithWebSocket("ws://127.0.0.1:46658/queryws")
                     .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };

        // required middleware
        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[] {
            new NonceTxMiddleware(publicKey, client),
            new SignedTxMiddleware(privateKey)
        });

        var contractAddr = await client.ResolveContractAddressAsync("BluePrint");

        var callerAddr = Address.FromPublicKey(publicKey);

        return(new Contract(client, contractAddr, callerAddr));
    }
Пример #6
0
    public async void SignIn()
    {
        var privateKey = CryptoUtils.GeneratePrivateKey();
        var publicKey  = CryptoUtils.PublicKeyFromPrivateKey(privateKey);
        var callerAddr = Address.FromPublicKey(publicKey);

        this.statusTextRef.text = "Signed in as " + callerAddr.ToAddressString();

        var writer = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     //.WithHTTP("http://127.0.0.1:46658/rpc")
                     .WithWebSocket("ws://127.0.0.1:46657/websocket")
                     .Create();

        var reader = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     //.WithHTTP("http://127.0.0.1:46658/query")
                     .WithWebSocket("ws://127.0.0.1:9999/queryws")
                     .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };

        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[] {
            new NonceTxMiddleware {
                PublicKey = publicKey,
                Client    = client
            },
            new SignedTxMiddleware(privateKey)
        });

        var contractAddr = await client.ResolveContractAddressAsync("BluePrint");

        this.contract = new Contract(client, contractAddr, callerAddr);

        // Subscribe to DAppChainClient.OnChainEvent to receive all events

        /*
         * client.OnChainEvent += (sender, e) =>
         * {
         *  var jsonStr = System.Text.Encoding.UTF8.GetString(e.Data);
         *  var data = JsonConvert.DeserializeObject<SampleEvent>(jsonStr);
         *  Debug.Log(string.Format("Chain Event: {0}, {1}, {2} from block {3}", data.Method, data.Key, data.Value, e.BlockHeight));
         * };
         */

        // Subscribe to DAppChainClient.OnEvent to receive events from a specific smart contract
        this.contract.OnEvent += (sender, e) =>
        {
            var jsonStr = System.Text.Encoding.UTF8.GetString(e.Data);
            var data    = JsonConvert.DeserializeObject <SampleEvent>(jsonStr);
            Debug.Log(string.Format("Contract Event: {0}, {1}, {2} from block {3}", data.Method, data.Key, data.Value, e.BlockHeight));
        };
    }
Пример #7
0
    public async void SignIn()
    {
#if !UNITY_WEBGL
        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
        var authClient = this.CreateAuthClient();
        this.identity = await authClient.GetIdentityAsync("", null);
#endif
        this.statusTextRef.text = "Signed in as " + this.identity.Username;

        var writer = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithHTTP("http://127.0.0.1:46658/rpc")
                     //.WithWebSocket("ws://127.0.0.1:46657/websocket")
                     .Create();

        var reader = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithHTTP("http://127.0.0.1:46658/query")
                     //.WithWebSocket("ws://127.0.0.1:47000/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)
        });

        var contractAddr = await client.ResolveContractAddressAsync("BluePrint");

        var callerAddr = this.identity.ToAddress("default");
        this.contract = new Contract(client, contractAddr, callerAddr);
    }
Пример #8
0
    public async void SignIn()
    {
        var privateKey = CryptoUtils.GeneratePrivateKey();
        var publicKey  = CryptoUtils.PublicKeyFromPrivateKey(privateKey);
        var callerAddr = Address.FromPublicKey(publicKey);

        this.statusTextRef.text = "Signed in as " + callerAddr.QualifiedAddress;

        var writer = RpcClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithWebSocket("ws://127.0.0.1:46658/websocket")
                     .Create();

        var reader = RpcClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     .WithWebSocket("ws://127.0.0.1:46658/queryws")
                     .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };

        // required middleware
        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[]
        {
            new NonceTxMiddleware(publicKey, client),
            new SignedTxMiddleware(privateKey)
        });

        const string abi          = "[{\"constant\":false,\"inputs\":[{\"name\":\"_tileState\",\"type\":\"string\"}],\"name\":\"SetTileMapState\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"GetTileMapState\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"state\",\"type\":\"string\"}],\"name\":\"OnTileMapStateUpdate\",\"type\":\"event\"}]\r\n";
        var          contractAddr = await client.ResolveContractAddressAsync("TilesChain");

        contract = new EvmContract(client, contractAddr, callerAddr, abi);

        // Subscribe to DAppChainClient.ChainEventReceived to receive events from a specific smart contract
        this.contract.EventReceived += (sender, e) =>
        {
            OnTileMapStateUpdateEvent tileMapStateUpdateEvent = e.DecodeEventDto <OnTileMapStateUpdateEvent>();
            Debug.Log(string.Format("Contract Event: {0}, {1}, from block {2}", e.EventName, tileMapStateUpdateEvent.State, e.BlockHeight));
        };
    }
Пример #9
0
        public static async Task <EvmContract> GetEvmContract(byte[] privateKey, byte[] publicKey, string abi, CustomTxMiddlewareFunc customTxMiddlewareFunc = null)
        {
            ILogger    logger = Debug.unityLogger;
            IRpcClient writer = RpcClientFactory.Configure()
                                .WithLogger(logger)
                                .WithWebSocket("ws://127.0.0.1:46658/websocket")
                                .Create();

            IRpcClient reader = RpcClientFactory.Configure()
                                .WithLogger(logger)
                                .WithWebSocket("ws://127.0.0.1:46658/queryws")
                                .Create();

            DAppChainClient client = new DAppChainClient(writer, reader)
            {
                Logger = logger
            };

            // required middleware
            ITxMiddlewareHandler[] txMiddlewareHandlers;
            if (customTxMiddlewareFunc != null)
            {
                txMiddlewareHandlers = customTxMiddlewareFunc(client, privateKey, publicKey);
            }
            else
            {
                txMiddlewareHandlers = new ITxMiddlewareHandler[]
                {
                    new NonceTxMiddleware(publicKey, client),
                    new SignedTxMiddleware(privateKey)
                };
            }

            client.TxMiddleware = new TxMiddleware(txMiddlewareHandlers);

            Address contractAddress = await client.ResolveContractAddressAsync("Tests");

            Address callerAddress = Address.FromPublicKey(publicKey);

            return(new EvmContract(client, contractAddress, callerAddress, abi));
        }
Пример #10
0
    public async void SignIn()
    {
#if !UNITY_WEBGL
        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
        var authClient = this.CreateAuthClient();
        this.identity = await authClient.GetIdentityAsync("", null);
#endif
        this.statusTextRef.text = "Signed in as " + this.identity.Username;

        var writer = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     //.WithHTTP("http://127.0.0.1:46658/rpc")
                     .WithWebSocket("ws://127.0.0.1:46657/websocket")
                     .Create();

        var reader = RPCClientFactory.Configure()
                     .WithLogger(Debug.unityLogger)
                     //.WithHTTP("http://127.0.0.1:46658/query")
                     .WithWebSocket("ws://127.0.0.1:9999/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)
        });

        var contractAddr = await client.ResolveContractAddressAsync("BluePrint");

        var callerAddr = this.identity.ToAddress("default");
        this.contract = new Contract(client, contractAddr, callerAddr);

        // Subscribe to DAppChainClient.OnChainEvent to receive all events

        /*
         * client.OnChainEvent += (sender, e) =>
         * {
         *  var jsonStr = System.Text.Encoding.UTF8.GetString(e.Data);
         *  var data = JsonConvert.DeserializeObject<SampleEvent>(jsonStr);
         *  Debug.Log(string.Format("Chain Event: {0}, {1}, {2} from block {3}", data.Method, data.Key, data.Value, e.BlockHeight));
         * };
         */

        // Subscribe to DAppChainClient.OnEvent to receive events from a specific smart contract
        this.contract.OnEvent += (sender, e) =>
        {
            var jsonStr = System.Text.Encoding.UTF8.GetString(e.Data);
            var data    = JsonConvert.DeserializeObject <SampleEvent>(jsonStr);
            Debug.Log(string.Format("Contract Event: {0}, {1}, {2} from block {3}", data.Method, data.Key, data.Value, e.BlockHeight));
        };
    }