Skip to content

6.0 IOTA Hello World

bds-iota-hackathon edited this page Nov 18, 2017 · 7 revisions

hello-world-iota.js Code Example

const IOTA = require('iota.lib.js');

const iota = new IOTA({
  host: 'http://p103.iotaledger.net',
  port: 14700
});

// iota.api.getNodeInfo((error, nodeInfo) => {
//   if (error) {
//     console.error('getNodeInfo error', error)
//   } else {
//     console.log('getNodeInfo result', nodeInfo)
//   }
// });

const seed = 'FNCWNXJWJIVDGPRWNZYKOMKNIIATPPDKEVCZEWSZTEVIWJFCOUV9PJD9AUCEVQLFEAI9UBUAVQKVEBLKN';

iota.api.getNewAddress(seed, (error, address) => {
  if (error) {
    console.error('getNewAddress error', error)
  } else {
    console.log('new address generated: ' + address)
  }
});





const Depth = 3; // constant defined by IOTA - how deep to look for the tips in the Tangle
const MinWeightMagnitude = 16; // constant defined by IOTA - the difficulty of PoW

const transfers = [
  {
    // where are we sending the transaction to?
    address: 'JEMEHHS9EEASH9UFOLYAKBCTNKGBADCLTKCEOEKPZVILIGTZOGYMFMWKQABVVNKHGHBHTJNRVDDMMUQOX',

    // how many tokens are we transferring?
    value: 0,

    // do we want to comment on this transaction?
    message: iota.utils.toTrytes('Hello World!')
  }
];

const options = {
  // addresses of the wallets we're using to fund this transfer
  inputs: [
    {
      keyIndex: 0,
      security: 0,
      address: 'DDLAERGKJZCFCICUTHTPZNACYCNCLJMBFK9OKI9BMMWXYVKBYJZRCW9CIIKFJHOCPOQHNAMOOUERZZIHD'
    }   ]
};

iota.api.sendTransfer(seed, Depth, MinWeightMagnitude, transfers, options, (error, transactions) => {
  if (error) {
    console.error('sendTransfer error', error)
  } else {
    console.log('transactions sent!', transactions)
  }
});

hello.world.python Code Example

from iota import *
from requests.exceptions import ConnectionError

url = "https://testnet140.tangle.works:443"
seed = "YOUR SEED HERE"
depth = 3
min_weight_magniude = 16

api = Iota(url, seed)
try:
    node_info = api.get_node_info()
    print(node_info)
    print api.get_new_addresses()

    api.send_transfer(
        depth=depth,
        transfers=[
                    ProposedTransaction(
                        address=Address(
                            "CHNLHJCYBZCYUI9DTHINDDWHNJWFCHQOTGABXFVZQHXF9BROTOIJZJSBXOVKCDGCXZTDDJUVTYBJZYAOH"),
                        value=0,
                        message=TryteString.from_string("Damn it works in Python!")
                    )
                ],
        min_weight_magnitude=min_weight_magniude,
        inputs=[Address("ZISNLNSKMPDOORSSFRCBGQOPY9BI9SONMTDHJJDWBTTCYLFV9PQ9VSWNI9FHEAEFGROGZ9YHSMZYOGFQG", key_index=0, security_level=0)]
    )
except ConnectionError as e:
    print("Connection Error: {e}".format(e=e))
except BadApiResponse as e:
    print("Bad Api: {e}".format(e=e))
else:
    print("Success!")