In this tutorial, you will implement a Hydra transaction with the SDK. A demo passphrase will give you access to a pre-generated vault filled with test HYD's. You will send HYD's using code from this wallet to your personal vault.
- NodeJS 12
make
and eitherpython
orpython3
- Download the project template and setup the environment as described in the readme.
- Flutter
- A sample Flutter project. Please follow their Test Drive page to create it. In the end, you'll have a simple counter application.
This sample project has a lib/main.dart
file.
This is the file where you will work. Except for the imports, we will write our code into the _incrementcounter
method, which is changed to async, as follows:
Future<void> _incrementCounter() async {
// our code comes here...
};
First, you need to access the SDK in the code.
The Typescript package is available on npmjs.com. After putting it into your package.json, you can start using it.
In Typescript you need to use multiple modules from the sdk. Please read more about Typescript modules here.
// Import the necessary modules from our SDK
import { Crypto, Layer1, Network, NetworkConfig } from '@internet-of-people/sdk';
To be able to use our SDK in your Flutter Android application, you need to run our installer script first, that does the followings:
- It downloads the dynamic libraries you need and puts those files in the right place. Those files are required because the SDK's crypto codebase is implemented in Rust and uses Dart FFI.
- It adds our Dart SDK into your
pubspec.yaml
file.
You just have to run this under your project's root on your Linux or macOS (Windows is not yet supported):
curl https://raw.githubusercontent.com/Internet-of-People/iop-dart/master/tool/init-flutter-android.sh | sh
When the script is finished, the only remaining task is to import the SDK in the lib/main.dart
.
// Import the necessary modules from our SDK
import 'package:iop_sdk/crypto.dart';
import 'package:iop_sdk/layer1.dart';
import 'package:iop_sdk/network.dart';
// Instantiate the demo vault that acts as a source of funds
const sourcePassword = 'correct horse battery staple';
const sourceVault = Crypto.Vault.create(Crypto.Seed.demoPhrase(), '', sourcePassword);
// Instantiate the demo vault that acts as a source of funds
final sourcePassword = 'correct horse battery staple';
final sourceVault = Vault.create(Bip39.DEMO_PHRASE, '', sourcePassword);
hydra
plugin initialized with the correct parameters to interact with the Hydra blockchain.
If initialized correctly, it can generate the appropriate keypairs. The first parameter describes the network and the account number (0)
of the account for which the plugin generates keys. The plugin consists of a public part accessible without a password
and a private interface that requires the password explicitly.
- The vault resembles a hierarchical deterministic wallet with additional functionalities. Read more about it here
// Initialize the Hydra plugin on the source vault.
const parameters = new Crypto.HydraParameters(
Crypto.Coin.Hydra.Testnet,
0
);
Crypto.HydraPlugin.init(sourceVault, sourcePassword, parameters);
// Get the private interface of the Hydra plugin
const sourcePlugin = Crypto.HydraPlugin.get(sourceVault, parameters);
const hydraPrivate = sourcePlugin.priv(sourcePassword);
// The address from which funds are sent from
const sourceAddress = sourcePlugin.pub.key(0).address;
// Initialize the Hydra plugin on the vault object
final accountNumber = 0;
final network = Network.TestNet;
HydraPlugin.init(sourceVault, sourcePassword, network, accountNumber);
// Get the private interface of the Hydra plugin
final hydra = HydraPlugin.get(sourceVault, network, accountNumber);
final hydraPrivate = hydra.private(sourcePassword);
// The address from which funds are sent from
final sourceAddress = hydra.public.key(0).address;
// Initialize your personal vault that will act as the target
const mnemonic = new Crypto.Bip39('en').generate().phrase;
const targetPassword = 'horse battery staple correct'
const targetVault = Crypto.Vault.create(
mnemonic,
'', // 25th word
targetPassword,
);
Crypto.HydraPlugin.init(targetVault, targetPassword, parameters)
// The address to which the funds are sent to
const targetHydra = Crypto.HydraPlugin.get(targetVault, parameters);
// Initialize the second key on the private hydra interface
targetHydra.priv(targetPassword).key(1);
const targetAddress = targetHydra.pub.key(1).address;
// Initialize your personal vault that will act as the target
final mnemonic = Bip39('en').generatePhrase();
final targetPassword = 'horse battery staple correct';
final targetVault = Vault.create(mnemonic, '', targetPassword);
HydraPlugin.init(targetVault, targetPassword, network, accountNumber);
// The address to which the funds are sent to
final targetHydra = HydraPlugin.get(targetVault, network, accountNumber);
// Initialize the second key on the private hydra interface
targetHydra.private(targetPassword).key(1);
final targetAddress = targetHydra.public.key(1).address;
// Return an api that can interact with the hydra blockchain
const networkConfig = NetworkConfig.fromNetwork(Network.Testnet);
const layer1Api = await Layer1.createApi(networkConfig);
// Send a hydra transaction using the hydra private object.
const amount = 1e8; // 1 HYD in flakes
const txId = await layer1Api.sendTransferTx(
sourceAddress,
targetAddress,
BigInt(amount),
hydraPrivate
);
// Prints the transaction ID
console.log('Transaction ID: ', txId);
Outputs:
Transaction ID: de7542ab693080dc1d51de23b20fd3611dac6a60c7a081634010f1f4aa413547
// Return an api that can interact with the hydra blockchain
final networkConfig = NetworkConfig.fromNetwork(network);
final layer1Api = Layer1Api.createApi(networkConfig);
// Send a hydra transaction using the hydra private object.
final amount = 1e8; // 1 HYD in flakes
final txId = await layer1Api.sendTransferTx(
sourceAddress,
targetAddress,
amount.toInt(),
hydraPrivate
);
// Prints the transaction ID
print('Transaction ID: $txId');
Outputs:
Transaction ID: de7542ab693080dc1d51de23b20fd3611dac6a60c7a081634010f1f4aa413547
Congratulations, you sent your first hydra transactions using our SDK! Don't forget, that if you need more detailed or technical information, visit the SDK's source code on GitHub (Typescript/Flutter) or contact us here.