コード例 #1
0
        private static async Task <TransactionSigningResult> CreateAsync(
            ITransactionSignerFactory transactionSignerFactory,
            string protocolCode,
            IWalletRepository walletRepository,
            IReadOnlyCollection <string> signingAddresses,
            IEncryptionService encryptionService,
            NetworkType networkType,
            byte[] builtTransaction)
        {
            ITransactionSigner transactionSigner;

            try
            {
                transactionSigner = transactionSignerFactory.Create(protocolCode);
            }
            catch (ArgumentOutOfRangeException e)
            {
                throw new BlockchainIsNotSupportedException(e);
            }

            var wallets = await walletRepository.GetByAddressesAsync(signingAddresses);

            var privateKeys = wallets.Select(x => encryptionService.Decrypt(x.PrivateKey)).ToArray();

            try
            {
                return(transactionSigner.Sign(builtTransaction, privateKeys, networkType));
            }
            catch (Exception exception)
            {
                throw new TransactionSigninFailedException("An error occured while signing transaction.", exception);
            }
        }
コード例 #2
0
        private static async Task <TransactionSigningResult> CreateCoinsAsync(
            ITransactionSignerFactory transactionSignerFactory,
            string protocolCode,
            IWalletRepository walletRepository,
            IReadOnlyCollection <string> signingAddresses,
            IEncryptionService encryptionService,
            NetworkType networkType,
            byte[] builtTransaction,
            IReadOnlyCollection <Coin> coinsToSpend)
        {
            ICoinsTransactionSigner transactionSigner;

            try
            {
                transactionSigner = transactionSignerFactory.CreateCoinsSigner(protocolCode);
            }
            catch (ArgumentOutOfRangeException e)
            {
                throw new BlockchainIsNotSupportedException(e);
            }

            var wallets = await walletRepository.GetByAddressesAsync(signingAddresses);

            var walletDict  = wallets.ToDictionary(x => x.Address);
            var privateKeys = wallets.Select(x => encryptionService.Decrypt(x.PrivateKey)).ToArray();

            try
            {
                return(transactionSigner.Sign(builtTransaction,
                                              coinsToSpend?.Select(x =>
                {
                    if (!walletDict.TryGetValue(x.Address, out var wallet))
                    {
                        throw new InvalidOperationException($"No wallet is stored for {x.Address}");
                    }

                    return new Swisschain.Sirius.Sdk.Crypto.Coin(x.Id,
                                                                 x.Asset,
                                                                 x.Value,
                                                                 wallet.PublicKey,
                                                                 x.Redeem);
                })
                                              .ToArray(),
                                              privateKeys,
                                              networkType));
            }
            catch (Exception exception)
            {
                throw new TransactionSigninFailedException("An error occured while signing transaction.", exception);
            }
        }