Complete dapp in 1 week!
Welcome to my 1-Week-Aepp-Challange.
The goal is to develop complete decentralized application (dapp) with Aeternity blockchain (aepp).
I’m documenting the whole design and development process.
Feel free to follow the progress.

Day 4: the introduction of the aepp-sdk-js and ways of communication and interaction with the Aeternity Blockchain.
Day 3: https://www.mobycrypt.com/1-week-aepp-challange-day-3-sophia-smart-contract/

Blockchain communication

The Blockchain API is the way to communicate with it. The entry gate to the Blockchain always will be the node (one of many creating the network) which allows to use the built-in API. In the blockchains like Bitcoin, Ethereum or Aeternity – the API is HTTP based. We interact with the Blockchain simply by invoking HTTP calls (like GET or POST) to the concrete node URL and port. Some of give possibility to connect via websocket which allows to subscribe – the node notifies about interesting (for us) events.

The Blockchain HTTP API is the direct way of interaction. In most cases developers use the abstraction layer which is dedicated library, mostly called SDK (Software Development Kit) written in specific programming language and dedicated to use in applications (like dapps). The library API is simpler to use, has many built-in helper features (like compilation of the code) and hides a lot of redundant details from the perspective of dapp development. Imagine you want to make a contract function call. This is of course possible via HTTP API however all the responsibility to prepare correct transaction (including all the cryptographic operations behind) is much harder than simply invoke something like callContractFunction(<contract_address>, <contract_function_name>, <arguments>). The whole magic is hidden behind the function and will be done by the library. Using the library, application code stays clean and development is much easier.

aepp-sdk-js

Aeternity provides SDK for Python and Javascript.

In context of dapps based on web technologies (where the web browser is the part of the application) we talk about Javascript SDK, the aepp-sdk-js. This library is also used in the Logonity.

The SDK is dedicated for both: client and server. It means that whenever you want to interact with the blockchain if it’s browser or for example Node.js server – you install the same package. Installation the SDK via npm is simply running in the folder of the project:

npm i @aeternity/aepp-sdk

Interacting with Aeternity blockchain via SDK

The code below shows very simple Vue.js component. The Aeternity SDK is initialized when the component will render in the browser. The interesting part for us is initializing the SDK in order to get ae instance.

<script>
  import Ae from '@aeternity/aepp-sdk/es/ae/universal';

  export default {
    name: 'PageIndex',
    mounted() {
      Ae({
        url: 'https://sdk-testnet.aepps.com',
        internalUrl: 'https://sdk-testnet.aepps.com',
      }).then(ae => {
        // Ae instance
        ae.height().then(height => console.log(`Current block height: ${height}.`));
      });
    }
  }
</script>

In the most basic scenario the only needed config parameter is the address of the node we want to connect. The https://sdk-testnet.aepps.com is the address of TESTNET (test network) node maintained by the Aeternity. If you have the own node, or want to connect to some other node – just provide different node address. Since the Ae() returns the Promise, we call then and provide the callback to get ae instance when it’s ready.

The ae instance allows us to interact with the blockchain. It actually wraps all the neccessary methods responsible for .e.g. getting blockchain info (height, last blocks, transactions), processing transactions, compile and deploy smart contracts, calling smart contracts functions.

Find out more about using SDK: https://github.com/aeternity/aepp-sdk-js/blob/develop/docs/usage.md

Signing transactions with SDK

Saving data in the blockchain – no matter if it’s simple tokens send or interaction with the smart contract which results in the state change – requires to own Blockchain account (simply public/private keys needed to encryption operations).

Since the idea of dapp is to offer possibility of independent blockchain interaction (not made by any third party) – it’s crucial that SDK wrapping the blockchain interaction functions – will also give the possibility to integrate the account information while using it.

At current (January 2019) stage Aeternity doesn’t (YET) provide the safe and production-ready ways to interact with the blockchain (through the SDK). What I mean by that? Solution like Metamask for Ethereum, where the built-in browser extension – which is the secure cryptocurrency wallet with the possibility to interact with blockchain SDK.

Interaction with the blockchain using aepp-sdk-js is possible by providing public & private keys. In order to use SDK function which change the blockchain state, providing keys is required:

import Ae from '@aeternity/aepp-sdk/es/ae/universal' // or any other flavor

Ae({
  url: 'https://sdk-testnet.aepps.com',
  internalUrl: 'https://sdk-testnet.aepps.com',
  keypair: {
    publicKey: 'ak_2igXQ7pQgH5YDv4zP9ciq1R3qW8pNaGgywqYnwRPrChKhYxgeG',
    secretKey: '02a9f1e976b83b5cdf34275e9068fc7f352a5b49144fd96e7546390ff20a7a14e256c3ecea141842c37f9686fa0555faf71388dc473f5d69891d9ea2ee2cb671'
  },
}).then(ae => {
  //1 Ae will be send from address ak_2igXQ7pQgH5YDv4zP9ciq1R3qW8pNaGgywqYnwRPrChKhYxgeG to ak_2paxkmqp5wgAc76oxsR6UDLwk4tscTSfMpsNtYyGMZrkX74bdC
  ae.spend(parseInt(1000000000000000000),'ak_2paxkmqp5wgAc76oxsR6UDLwk4tscTSfMpsNtYyGMZrkX74bdC');
});

The main flaw of such solution is of course providing private key. In order to develop safe and efficient dapps also having in mind good user experience – the cryptographic aspects of blockchain & crypto should be hidden from the user. In context of Aeternity we can expect that better solution will be delivered very soon.

Summary

SDK library is the preferred method of interacting with the Blockchain through the client side or any middlware. Dapps approach, where the client is making calls to the blockchain mostly directly from own device (like web browser) requires secure architecture and tools (wallet which stores the keys and can cooperate with the SDK).
In the next article I will describe the Logonity client side and it’s SDK usage in concerte examples.