When i first encountered IOTA, the concept behind it fascinated me. After discovering that it even supports a JavaScript library it fascinated me even more. At that point I decided to program my own project based on IOTA. To save you, dear reader, from the hurdles that I had to overcome, I will provide you with a small guide for making transactions with the IOTA JavaScript library
What is IOTA?
The name IOTA is a construct made of IoT (internet of things) and Tangle (IOTA's "Blockchain") . IOTA is a Cryptocurrency that is supposed to make payments between machines possible. Due to the peculiarities of the Tangle the Transactions are free, which also makes microtransactions possible
Tangle - The innovation behind IOTA
The Tangle is IOTA's blockless distributed ledger. (Theoretically) it is infinitely scalable and thanks to the absence of miners there are no transaction fees.While in Bitcoin for example miners take on the job of confirming transactions, in IOTA every transactions itself confirms two other transactions. This leads to an increase in transaction speed as the amount of transactions is growing.
Programming
Creating an IOTA instance
We start by creating an instance of the IOTA class, the constructor takes in an object containing the following keys as a parameter:
host
:String
Address of the IOTA-node that should be used, default islocalhost
.port
:Int
Port of the IOTA-node that should be used, default is14265
.provider
:String
If neither host nor port have been set, a string containing both can be set.sandbox
:Bool
optional True is provider is a sandbox.token
:String
token to authenticate within the sandbox.
// create new iota object
const iota = new window.IOTA({
'host': 'http://node01.iotatoken.nl',
'port': 14265,
});
You can find a suitable host directly in the IOTA support page: http://iotasupport.com/lightwallet.shtml.
Requesting of current node informations
Once we can successfully connected to the node we can request the current node data by the function iota.api.getNodeInfo
.
const getNodeInfo = () => new Promise((resolve, reject) => {
iota.api.getNodeInfo((error, info) => (error ? reject(error) : resolve(info)));
});
This function will not have to be actively called in the future, instead it will be encapsulated in functions like iota.api.getLatestInclusion
.
The Proof-of-Work
The Proof-of-Work(PoW) is executed by an algorthm invented by IOTA and the JavaScript Library. What we have to implement is the initialization of the library and the attachment of the IOTA instance.
// Proof of Work - webgl2 only
window.curl.init(); // <-- do not forget!
window.curl.overrideAttachToTangle(iota);
We can find the library here. It uses the WebGL2 interface, which is only supported by the firefox and chrome web browsers to this day. So if your requirements include having Internet Explorer support your project you should consider finding a server side solution to the PoW Implementation.
Creating transactions
Once we have created an IOTA instance and embedded the Proof-Of-Work Library, we can prepare the transfer. For that we have to fist create an array of all transfer objects that should be sent in a bundle. Here you have to make sure that the message does not exceed the length of 2673trytes. If a message is shorter than 2673trytes it will be padded with 9s.
// create an array of transfers for each message
const createTransfers = (address, messages = ['']) => messages.map(message => ({
address: address,
value: 0,
message: iota.utils.toTrytes(message),
}));
This Array of transfer objects was the last piece of the jigsaw puzzle for creating a transaction.
We can create our transactions with our seed, the transfer array and the function iota.api.sendTransfer
. Once everything succeeds we will receive an array of the created transactions.
const createTransactions = (seed) => {
const transfers = generateTransfers(address, ['i<3webdev']);
// params: https://github.com/iotaledger/iota.lib.js/#sendtransfer
return new Promise((resolve, reject) => iota.api.sendTransfer(seed, 4, 14, transfers, (error, transactions) => {
if (error) {
reject(error);
} else {
resolve(transactions);
}
}));
};
Conclusion
Programming with the IOTA JavaScript library is not that hard, once you understand which parameters are required. On top of that the preparation of the IOTA developers is remarkable, since such libraries are rarely this well documented.
Sources and further reading
https://iota.org (visited 24.01.2018 21:11)
https://caniuse.com/#search=webgl2 (visited 25.01.2018 11:59)
https://domschiener.gitbooks.io/iota-guide/ (visited 25.01.2018 12:35)
https://github.com/iotaledger/iota.lib.js/ (visited 24.01.2018 21:12)
https://github.com/iotaledger/curl.lib.js (visited 24.01.2018 21:13)
http://iotasupport.com/lightwallet.shtml (visited 24.01.2018 21:51)
Special thanks to @sidem!
With that in mind, happy coding.
(() => {
const colors = [
'001f3f', '0074d9', '7fdbff', '39cccc',
'3d9970', '2ecc40', '01ff70', 'ffdc00',
'ff851b', 'ff4136', '85144b', 'f012be',
];
const contents = ['%cI', '%c❤', '%cweb', '%cdev'];
const options = Array.from(
new Array(contents.length),
() => `
color:#${colors[Math.floor(Math.random() * colors.length)]};
font-size:64px;
`
);
console.log.apply(console, [contents.join('')].concat(options));
})();
IOTA as a concept is fantastic. I also invested a large amount of money on them. If they can actually successfully deliver on their promise I'm expecting to make some great returns. However, it seems a crypto bloodbath is on its way
We will see what the future will bring
Great post thank you man
You got a 64.52% upvote from @whalebuilder courtesy of @drookyn. Let your SP earn for you... Deligate it to @whalebuilder by clicking on one of the ready to delegate links: 50SP | 100SP | 250SP | 500SP | 1000SP | 5000SP | custom amount.