コード例 #1
0
ファイル: AElfKeyStoreTests.cs プロジェクト: woxihuanjia/AElf
        public async Task Account_Create_And_Open()
        {
            var keyPair = await _keyStore.CreateAsync("123", "AELF");

            keyPair.ShouldNotBe(null);
            _keyStore.ListAccountsAsync().Result.Count.ShouldBeGreaterThanOrEqualTo(1);

            var address   = Address.FromPublicKey(keyPair.PublicKey);
            var addString = address.GetFormatted();

            address.ShouldNotBe(null);

            //Open account
            var errResult = await _keyStore.OpenAsync(addString, "12", true);

            errResult.ShouldBe(AElfKeyStore.Errors.WrongPassword);

            errResult = await _keyStore.OpenAsync(addString, "123");

            errResult.ShouldBe(AElfKeyStore.Errors.None);

            errResult = await _keyStore.OpenAsync(addString, "123");

            errResult.ShouldBe(AElfKeyStore.Errors.AccountAlreadyUnlocked);

            errResult = await _keyStore.OpenAsync(addString, "123", false);

            errResult.ShouldBe(AElfKeyStore.Errors.AccountAlreadyUnlocked);

            Directory.Delete("/tmp/keys", true);

            await Should.ThrowAsync <KeyStoreNotFoundException>(() => _keyStore.ReadKeyPairAsync(addString + "_fake", "123"));
        }
コード例 #2
0
        public async Task Wallet_SignHash_Failed()
        {
            var chain = await _blockchainService.GetChainAsync();

            var walletStorePath = Path.Combine(ApplicationHelper.AppDataPath, "rpc-managed-wallet");
            var store           = new AElfKeyStore(walletStorePath);
            var keyPair         = await store.CreateAsync("123", chain.Id.ToString());

            var addressString = Address.FromPublicKey(keyPair.PublicKey).GetFormatted();

            var response = await JsonCallAsJObject("/wallet", "SignHash",
                                                   new { address = addressString, password = "******", hash = Hash.Generate().ToHex() });

            response.ShouldNotBeNull();
            response["error"]["code"].To <long>().ShouldBe(Wallet.Error.WrongPassword);
            response["error"]["message"].ToString().ShouldBe(Wallet.Error.Message[Wallet.Error.WrongPassword]);

            response = await JsonCallAsJObject("/wallet", "SignHash",
                                               new { address = addressString + "test", password = "******", hash = Hash.Generate().ToHex() });

            response.ShouldNotBeNull();
            response["error"]["code"].To <long>().ShouldBe(Wallet.Error.AccountNotExist);
            response["error"]["message"].ToString().ShouldBe(Wallet.Error.Message[Wallet.Error.AccountNotExist]);

            Directory.Delete(walletStorePath, true);
        }
コード例 #3
0
        public async Task Wallet_SignHash_Success()
        {
            var chain = await _blockchainService.GetChainAsync();

            var walletStorePath = Path.Combine(ApplicationHelper.AppDataPath, "rpc-managed-wallet");
            var store           = new AElfKeyStore(walletStorePath);
            var keyPair         = await store.CreateAsync("123", chain.Id.ToString());

            var addressString = Address.FromPublicKey(keyPair.PublicKey).GetFormatted();

            var response = await JsonCallAsJObject("/wallet", "SignHash",
                                                   new { address = addressString, password = "******", hash = Hash.Generate().ToHex() });

            response.ShouldNotBeNull();
            response["result"].ToString().ShouldNotBeEmpty();

            Directory.Delete(walletStorePath, true);
        }
コード例 #4
0
        public async Task Wallet_ListAccounts()
        {
            var chain = await _blockchainService.GetChainAsync();

            var walletStorePath = Path.Combine(ApplicationHelper.AppDataPath, "rpc-managed-wallet");
            var store           = new AElfKeyStore(walletStorePath);
            var keyPair         = await store.CreateAsync("123", chain.Id.ToString());

            var addressString = Address.FromPublicKey(keyPair.PublicKey).GetFormatted();

            var response = await JsonCallAsJObject("/wallet", "ListAccounts");

            response.ShouldNotBeNull();
            response["result"].ToList().Count.ShouldBeGreaterThanOrEqualTo(1);
            response["result"].ToList().Contains(addressString).ShouldBeTrue();

            Directory.Delete(walletStorePath, true);
        }
コード例 #5
0
ファイル: OSAElfModule.cs プロジェクト: woxihuanjia/AElf
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            context.Services.AddAssemblyOf <OSAElfModule>();

            context.Services.AddSingleton <PeerConnectedEventHandler>();
            context.Services.AddTransient <BlockSyncJob>();

            //TODO: make ApplicationHelper as a provider, inject it into key store
            var keyStore = new AElfKeyStore(ApplicationHelper.AppDataPath);

            context.Services.AddSingleton <IKeyStore>(keyStore);

            Configure <AccountOptions>(option =>
            {
                configuration.GetSection("Account").Bind(option);

                if (string.IsNullOrWhiteSpace(option.NodeAccount))
                {
                    AsyncHelper.RunSync(async() =>
                    {
                        var accountList = await keyStore.ListAccountsAsync();

                        option.NodeAccountPassword = string.Empty;
                        if (accountList.Count == 0)
                        {
                            var blockChainService = context.Services.GetRequiredServiceLazy <IBlockchainService>().Value;
                            var chainId           = blockChainService.GetChainId();
                            var keyPair           = await keyStore.CreateAsync(option.NodeAccountPassword, chainId.ToString());
                            option.NodeAccount    = Address.FromPublicKey(keyPair.PublicKey).GetFormatted();
                        }
                        else
                        {
                            option.NodeAccount = accountList.First();
                        }
                    });
                }
            });
        }
コード例 #6
0
        private async Task <Dictionary <string, string> > GetAndCreateAccountKey(DeployArg arg)
        {
            if (string.IsNullOrWhiteSpace(arg.ChainAccount))
            {
                var keyStore = new AElfKeyStore(ApplicationHelper.AppDataPath);

                var chainPrefixBase58 = Base58CheckEncoding.Encode(ByteArrayHelpers.FromHexString(arg.SideChainId));
                var chainPrefix       = chainPrefixBase58.Substring(0, 4);

                var key = await keyStore.CreateAsync(arg.AccountPassword, chainPrefix);

                // todo clean
                //arg.ChainAccount = "ELF_" + chainPrefix + "_" + key.GetEncodedPublicKey();
            }

            var fileName   = arg.ChainAccount + ".ak";
            var filePath   = Path.Combine(ApplicationHelper.AppDataPath, "keys", fileName);
            var keyContent = File.ReadAllText(filePath);

            return(new Dictionary <string, string> {
                { fileName, keyContent }
            });
        }