Simone Romano
Published © Apache-2.0

Blockchain-Based Verifiable Data Marketplace

Generate and Sell verifiable IoT data with blockchain!

AdvancedFull instructions provided3 hours840
Blockchain-Based Verifiable Data Marketplace

Things used in this project

Hardware components

Pebble Tracker
IoTeX Pebble Tracker
×1

Software apps and online services

Blockchain
IoTeX Blockchain

Story

Read more

Code

Interat with IoTeX Contract

JavaScript
// Create the global antenna SDK object
async function loadAntenna() {
	if (window.antenna) return;
	
	if (mobileAndTabletcheck()) {
		window.antenna = new Antenna("http://api.iotex.one:80", {		
			signer: new window.Antenna.modules.WvSigner()
		});		
	}
	else {
		window.antenna = new Antenna("http://api.iotex.one:80", {		
			signer: new window.Antenna.modules.WsSignerPlugin()			
		});
		await sleep(3000);
	} 	
}

// Create the global object to interact with the contract
async function loadContract(address) {
	if (window.contract && window.contract.address == address) return;
	const prophecyContarctAbi = [{ /* CONTRACT ABI FILE HERE */}];
	const prophecyContarctAddress = address;
	await loadAntenna();
	window.contract = new window.Antenna.modules.Contract(prophecyContarctAbi, prophecyContarctAddress, {
		provider: window.antenna.iotx,
		signer: window.antenna.iotx.signer
	});
}

async function accessIoPay() {
	try {
		if (!window.antenna) await loadAntenna();

		let iopayAccount = window.antenna.iotx.accounts[0].address;
		return iopayAccount;
	} catch (err) {
		console.log(err);
		alert("Please make sure IoPay Wallet is running and unlocked.");
		return "";
	}
}

// Calls the registerDevice or updateDevice contract functions
async function registerUpdateDevice(imei, freq, price, rsaN, rsaE, register) {
	if (register && !confirm("Please notice that your device must be allowed to register, otherwise registration will fail.\nPlease contact <a href='https://t.me/zimne'>@zimne</a> to allow your device.")) 
	{ 
		return; 
	}
	let optsArr = [{ gasLimit: "20000000" }, { gasPrice: "1000000000000" }];
	let optsObj = { gasLimit: "20000000", gasPrice: "1000000000000"	};

	var resp;
	if (register)
	  resp = await window.contract.methods.registerDevice(hexEncodeImei(imei), freq, price, "mydevice.specs", rsaN, rsaE,	optsArr, optsObj);
	else 
	  resp = await window.contract.methods.updateDevice(hexEncodeImei(imei), freq, price, "mydevice.specs", rsaN, rsaE,	optsArr, optsObj);
}

// Calls the preRegisterDevice contract func to whitelist a device
async function preRegisterDevice(imei) {
	if (!confirm("Please notice that only the owner of the contract can allow new devices to register, otherwise this action will fail.")) 
	{ 
		return; 
	}
	let optsArr = [{ gasLimit: "20000000" }, { gasPrice: "1000000000000" }];
	let optsObj = { gasLimit: "20000000", gasPrice: "1000000000000"	};

	var web3 = new Web3();
	var hash = web3.utils.keccak256(web3.eth.abi.encodeParameter("bytes32",hexEncodeImei(imei)));
	var resp = await window.contract.methods.preRegisterDevice(hash, optsArr, optsObj);
}

// Calls the subscribe contract func to subscribe to a data stream
async function subscribeToDevice(imei, blocks, amountRau, endpoint, token, rsaN, rsaE) {
	let optsArr = [{ gasLimit: "20000000" }, { gasPrice: "1000000000000" }, { amount: amountRau }];
	let optsObj = { gasLimit: "20000000", gasPrice: "1000000000000", amount: amountRau	};
 
	imei = hexEncodeImei(imei);

	if (token == "") token = "abcde";
	var resp = await window.contract.methods.subscribe(imei, blocks, endpoint, token, optsArr, optsObj);
}

// Calls the claim contract func to claim device owner earnings!
async function claim(imei) {
	let optsArr = [{ gasLimit: "20000000" }, { gasPrice: "1000000000000" }];
	let optsObj = { gasLimit: "20000000", gasPrice: "1000000000000"	};
    
	var resp = await window.contract.methods.claim(hexEncodeImei(imei),optsArr, optsObj);
}

// Utility function
function hexEncode(str)
{
var arr1 = [];
for (var n = 0, l = str.length; n < l; n ++) 
	{
	var hex = 0;
	if (n < l) hex = Number(str.charCodeAt(n)).toString(16);
	arr1.push(hex);
	}
return "0x"+(arr1.join('')).padStart(64,'0');
}

// Utility function
function hexEncodeImei(imei) {
	if (imei.length == 15) imei = hexEncode(imei.toString());
	if (imei.slice(0,2) != "0x") imei = '0x'+imei;
	return imei;
}

Credits

Simone Romano

Simone Romano

5 projects • 25 followers
Developers Community Growth @ IoTeX

Comments