Ejemplo n.º 1
0
        public async Task <Asset> CreateAssetAsync(Asset asset, NexusUser user, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            user.Validate();
            asset.Validate();

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "pin", user.Pin.ToString() },
                { "session", user.GenesisId.Session },
                { "data", asset.Data },
                { "name", asset.Name }
            });

            var response = await PostAsync <NexusCreationResponse>("assets/create", request, token);

            if (string.IsNullOrWhiteSpace(response?.Address))
            {
                throw new InvalidOperationException($"{asset.Name} creation failed");
            }

            asset.Address   = response.Address;
            asset.TxId      = response.TxId;
            asset.Genesis   = user.GenesisId.Genesis;
            asset.CreatedOn = DateTime.Now;

            return(asset);
        }
Ejemplo n.º 2
0
        public async Task <bool> LockAsync(string sessionId, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            if (ApiSessions)
            {
                throw new InvalidOperationException("Cannot lock with API sessions enabled");
            }

            var request = new NexusRequest(new Dictionary <string, string> {
                { "session", sessionId }
            });

            return(await PostAsync <bool>("accounts/lock", request, token));
        }
Ejemplo n.º 3
0
        private async Task <Block> GetBlockAsync(object retVal, TxVerbosity txVerbosity, CancellationToken token, bool logOutput)
        {
            token.ThrowIfCancellationRequested();

            var useHash = retVal is string;

            var key = useHash ? "hash" : "height";
            var val = retVal.ToString();

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { key, val },
                { "verbose", ((int)txVerbosity).ToString() }
            });

            return(await GetAsync <Block>("ledger/block", request, token, logOutput));
        }
Ejemplo n.º 4
0
        public async Task <IEnumerable <Transaction> > GetNotificationsAsync(GenesisId genesis,
                                                                             TxVerbosity txVerbosity = TxVerbosity.PubKeySign, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            if (string.IsNullOrWhiteSpace(genesis?.Genesis))
            {
                throw new ArgumentException("Genesis is required");
            }

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "genesis", genesis.Genesis },
                { "verbose", ((int)txVerbosity).ToString() }
            });

            var txs = await PostAsync <IEnumerable <Transaction> >("accounts/notifications", request, token);

            return(txs ?? new List <Transaction>());
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <Transaction> > GetTransactionsAsync(string userName,
                                                                            TxVerbosity txVerbosity = TxVerbosity.PubKeySign, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("Username is required");
            }

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "username", userName },
                { "verbose", ((int)txVerbosity).ToString() }
            });

            var txs = await PostAsync <IEnumerable <Transaction> >("accounts/transactions", request, token);

            return(txs ?? new List <Transaction>());
        }
Ejemplo n.º 6
0
        public async Task <bool> UnlockAsync(int pin, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            if (ApiSessions)
            {
                throw new InvalidOperationException("Cannot unlock with API sessions enabled");
            }

            if (pin.ToString().Length < 4)
            {
                throw new ArgumentException("A valid PIN is required to unlock the account");
            }

            var request = new NexusRequest(new Dictionary <string, string> {
                { "pin", pin.ToString() }
            });

            return(await PostAsync <bool>("accounts/unlock", request, token));
        }
Ejemplo n.º 7
0
        public async Task <object> TokeniseAsset(Asset asset, Token nexusToken, NexusUser user, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            user.Validate();
            asset.Validate();
            nexusToken.Validate();

            var(aKey, aVal) = asset.GetKeyVal("asset_address", "asset_name");
            var(tKey, tVal) = nexusToken.GetKeyVal("token_address", "token_name");

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "pin", user.Pin.ToString() },
                { "session", user.GenesisId.Session },
                { aKey, aVal },
                { tKey, tVal }
            });

            return(await PostAsync <Asset>("assets/tokenize", request, token));
        }
Ejemplo n.º 8
0
        public async Task <string> GetBlockHashAsync(int height, CancellationToken token = default, bool logOutput = true)
        {
            token.ThrowIfCancellationRequested();

            if (height <= 0)
            {
                throw new ArgumentException("Height must be greater than 0");
            }

            var request = new NexusRequest(new Dictionary <string, string> {
                { "height", height.ToString() }
            });

            var block = await GetAsync <Block>("ledger/blockhash", request, token, logOutput);

            if (string.IsNullOrWhiteSpace(block?.Hash))
            {
                throw new InvalidOperationException($"Get block hash {height} failed");
            }

            return(block.Hash);
        }
Ejemplo n.º 9
0
        public async Task <GenesisId> CreateAccountAsync(NexusUserCredential userCredential, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            userCredential.Validate();

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "username", userCredential.Username },
                { "password", userCredential.Password },
                { "pin", userCredential.Pin.ToString() }
            });

            var tx = await PostAsync <Transaction>("accounts/create", request, token);

            if (string.IsNullOrWhiteSpace(tx?.Genesis))
            {
                throw new InvalidOperationException($"Create account {userCredential.Username} failed");
            }

            return(new GenesisId {
                Genesis = tx.Genesis
            });
        }
Ejemplo n.º 10
0
        public async Task <TokenInfo> GetTokenInfo(Token token, CancellationToken cToken = default)
        {
            cToken.ThrowIfCancellationRequested();

            token.Validate();

            var(key, val) = token.GetKeyVal();

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "identifier", token.Identifier },
                { key, val },
                { "type", token.Type }
            });

            var tokenInfo = await PostAsync <TokenInfo>("tokens/get", request, cToken);

            if (tokenInfo == null)
            {
                throw new InvalidOperationException($"{token.Name} get failed");
            }

            return(tokenInfo);
        }
Ejemplo n.º 11
0
        public async Task <AssetInfo> GetAssetAsync(Asset asset, CancellationToken token = default)
        {
            token.ThrowIfCancellationRequested();

            if (string.IsNullOrWhiteSpace(asset?.Address))
            {
                throw new ArgumentException("Address is required");
            }

            var(key, val) = asset.GetKeyVal();

            var request = new NexusRequest(new Dictionary <string, string> {
                { key, val }
            });

            var assetInfo = await PostAsync <AssetInfo>("assets/get", request, token);

            if (assetInfo == null)
            {
                throw new InvalidOperationException($"{key} retrieval failed");
            }

            return(assetInfo);
        }
Ejemplo n.º 12
0
        public async Task <Transaction> GetTransactionAsync(string hash, TxVerbosity txVerbosity = TxVerbosity.PubKeySign, CancellationToken token = default, bool logOutput = true)
        {
            token.ThrowIfCancellationRequested();

            if (string.IsNullOrWhiteSpace(hash))
            {
                throw new ArgumentException("Hash must have a value");
            }

            var request = new NexusRequest(new Dictionary <string, string>
            {
                { "hash", hash },
                { "verbose", ((int)txVerbosity).ToString() }
            });

            var tx = await GetAsync <Transaction>("ledger/transaction", request, token, logOutput);

            if (string.IsNullOrWhiteSpace(tx?.Hash))
            {
                throw new InvalidOperationException($"Get tx {hash} failed");
            }

            return(tx);
        }