Phani
Phani
Jan 14 · 2 min read

A proof of work is a piece of data which is difficult (costly, time-consuming) to produce but easy for others to verify and which satisfies certain requirements.

The purpose of Proof of Work is to prevent spam. IOTA ledger is secured by a heavily distributed form of CCurl Proof of Work. The IoT devices create signed transactions and compute a Proof of Work in a distributed fashion.

Without going in-depth of an IOTA transaction mechanics, we will see how a transaction is created to understand where PoW comes into play. To create a transaction:

First, we construct a transaction bundle. The bundle groups together all the individual transactions which relate to a single intent of value transfer. The constructed bundle is signed with your private keys.

Second, we randomly choose two previous unapproved transactions, or tips to approve. This will allow us to join the Tangle. We can get two tips from IRI via getTransactionsToApprove.

Third, is Proof of Work(PoW). To join Tangle every transaction in the bundle requires a nonce. The nonce is the result of PoW.

The Proof of Work can be done on the device locally or it can be delegated to external devices.

Local Proof of Work — Using CCurl WebGL2 implementation

const IOTA = require('iota.lib.js')
const curl = require('curl.lib.js')
const iota = new IOTA({ provider: 'http://localhost:14265' })curl.overrideAttachToTangle(iota.api)
curl.init()

Remote Proof of Work — Delegate to a 3rd party PoW service

const { composeAPI } = require('@iota/core')async function attachToTangle(
trunkTransaction, branchTransaction, mWM, trytes, cb
) {
//3rd Party PoW
const resp = await makeRequestToPoWService(...);
if ((resp[0]) != null) {
return resp[0]
} else {
return resp[1].trytes
}
}
const iota = composeAPI({
provider: 'http://localhost:14265',
attachToTangle
})

If you are using IOTA.js (version ≥ 1.x.x) this library will come handy: https://github.com/pasupulaphani/iota.js-remote-pow

The library has 2 providers to choose from. ATM it supports

For older IOTA.js (version <1.x.x) please use the iota.lib.js.powsrvio.


Happy coding!

Bytes

P2P Internet Access Trading

Phani

Written by

Phani

Fintech and Blockchain Entrepreneur

Bytes

Bytes

P2P Internet Access Trading

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade