Skip to content

Instantly share code, notes, and snippets.

@Alexhuszagh
Last active October 5, 2018 05:03
Show Gist options
  • Save Alexhuszagh/d934a7aa57b84a2e7eb939880d128777 to your computer and use it in GitHub Desktop.
Save Alexhuszagh/d934a7aa57b84a2e7eb939880d128777 to your computer and use it in GitHub Desktop.
NEM SDK Walkthrough

NEM SDK Walkthrough

Table of Contents

Getting Started

// Import the NEM SDK
var nem = require('nem-sdk').default

// Create an endpoint to a supernode
var url = "http://176.9.68.110"
var port = 7890
var endpoint = nem.model.objects.create("endpoint")(url, port)

// Get the heartbeat
nem.com.requests.endpoint.heartbeat(endpoint)
    .then(res => {
        console.log(res)
    }, err => {
        console.error(err)
    })

Transfer Transaction

// Config
var recipient = "TALICELCD3XPH4FFI5STGGNSNSWPOTG5E4DS2TOS"
var amount = 1000000000
var message = "Sample message."

// Create transaction
var transaction = nem.model.objects.create("transferTransaction")(recipient, amount, message)

// Prepare the transaction
var common = {
   ...nem.model.objects.get("common"),
   privateKey: "68e4f79f886927de698df4f857de2aada41ccca6617e56bb0d61623b35b08cc0"
}
var mode = nem.model.network.data.mainnet.id
var prepared = nem.model.transactions.prepare("transferTransaction")(common, transaction, mode)


// Create an endpoint to a supernode
var url = "http://176.9.68.110"
var port = 7890
var endpoint = nem.model.objects.create("endpoint")(url, port)

// Sign and post the transaction
nem.model.transactions.send(common, prepared, endpoint)
    .then(res => {
        console.log(res)
    }, err => {
        console.error(err)
    })

Getting NEM Address

// Generate private key from a CSPRNG
var privateKey = nem.utils.convert.ua2hex(nem.crypto.nacl.randomBytes(32))

// Derive the public key from the private key
var publicKey = nem.crypto.keyPair.create(privateKey).publicKey.toString()

// Derive the address from the public key and mode
var mode = nem.model.network.data.mainnet.id
var address = nem.model.address.toAddress(publicKey, mode)

Apostille

// Create an endpoint to a supernode
var url = "http://176.9.68.110"
var port = 7890
var endpoint = nem.model.objects.create("endpoint")(url, port)

// Generate the apostille content
var privateKey = "68e4f79f886927de698df4f857de2aada41ccca6617e56bb0d61623b35b08cc0"
var mode = nem.model.network.data.mainnet.id
var common = nem.model.objects.create("common")("", privateKey)
var content = nem.crypto.js.enc.Utf8.parse('Sample document contents')
var apostille = nem.model.apostille.create(common, "File.txt", content, "File title", nem.model.apostille.hashing["SHA256"], false, true, nem.model.network.data.mainnet.id)

// Generate and post the apostille
var apostille = nem.model.apostille.create(
    common, 
    "File.txt", 
    content, 
    "File title", 
    nem.model.apostille.hashing["SHA256"], 
    false, 
    true, 
    Nem.model.network.data.mainnet.id
)

// Post apostille to network
nem.model.transactions.send(common, apostille.transaction, endpoint)
.then(res => {
        console.log(res)
    }, err => {
        console.error(err)
    })
    
// Transaction hash of the apostille
var txHash = "9b2dc096fb55e610c97a870b1d385458ca3d60b6f656428a981069ab8edd9a28"

// Look up and verify apostille
nem.com.requests.transaction.byHash(endpoint, txHash)
    .then(res => {
        if (nem.model.apostille.verify(content, res.transaction)) {
            // Valid apostille, contents match
        } else {
            // Invalid apostille, contents don't match
        }
    })
    .catch(err => {
        // Transaction hash not valid
    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment