How do I build an HTML solution in Solidity

There is a code for a simple conversion (Farenheit to Celcius) in HTML. I am working to recreate the same thing in Solidity. I would need some pointers in making it work. The solidity Code is as follows: 

There is a code for a simple conversion (Farenheit to Celcius) in HTML. I am working to recreate the same thing in Solidity. I would need some pointers in making it work. The solidity Code is as follows: 


contract TemperatureSolution{
    uint16 input,
function convertTemp(uint16 _input) public{
    return (document.getElementById("outputCelcius").innerHTML=(valNum-32)/1.8)
}
convertTemp(_input);

}

For reference, the HTML that it is based upon is as follows (this works, but I intend to create it on a blockchain):

<html>
<body>
<p>Type a value in the Fahrenheit field to convert the value to Celsius:</p>
<p>
<label>Fahrenheit</label>
<input id="inputFahrenheit" type="number" placeholder="Fahrenheit" oninput="temperatureConverter(this.value)" onchange="temperatureConverter(this.value)">
</p>
<p>Celcius: <span id="outputCelcius"></span></p>

&lt;script&gt;
function temperatureConverter(valNum) {

valNum = parseFloat(valNum);
document.getElementById("outputCelcius").innerHTML=(valNum-32)/1.8;
}
</script>
</body>


The javascript function is returning a promise and the promise is used in the solidity function which gives the error of invalid arguments

I am trying to make a Dapp which maintains the various versions of a trade(new versions are created when we update the data of trade). I am using javascript we3.js api to call my solidity functions through javascript. I have a function which displays the data when we search using a field called 'client'. When I am clicking the button to search on basis of client, it returns an error. I am using promises in javascript, but it seems to me that the javascript function is returning a promise which is undefined to solidity function. But after the error the javascript function is returning the expected values(I checked using console.log).

I am trying to make a Dapp which maintains the various versions of a trade(new versions are created when we update the data of trade). I am using javascript we3.js api to call my solidity functions through javascript. I have a function which displays the data when we search using a field called 'client'. When I am clicking the button to search on basis of client, it returns an error. I am using promises in javascript, but it seems to me that the javascript function is returning a promise which is undefined to solidity function. But after the error the javascript function is returning the expected values(I checked using console.log).

I have tried using async/await but that doesnt seems to do the trick.

My Javascript code:-

getVersion:async function(i){

var returnedLatestVersion;

App.contracts.Trades.deployed().then(async function(instance){

await instance.getLatestVersionOfTrade.call(i).then(function(a){      

  returnedLatestVersion = a;
  console.log("test:"+returnedLatestVersion+",a:"+a);

}).catch(function(err){
console.log(err)
})
console.log("test2="+returnedLatestVersion);
return await returnedLatestVersion;    

})
},

searchByClient: function(){

var tradesInstance;

var client = $('#clientSearch').val();

var tradeResults = $("#tradesResult");
tradeResults.empty();

App.contracts.Trades.deployed().then(function(instance){
tradesInstance = instance;

return tradesInstance.getClientData.call(client).then(async function(returnValues){

console.log("returnValues Array = "+returnValues);

var returnValuesLength = returnValues.length;   

for(var i=0;i&lt;returnValuesLength;i++){     

  var a = returnValues[i];
  var c = returnValues[i];    
  var j;

  j = await App.getVersion(a);

  console.log("version ="+j);
  console.log("tradeId="+a);

  tradesInstance.trades(c,j).then(function(trade){

    var secId = trade[0];
    var notional = trade [1];
    var price = trade[2];
    var client = trade[3];

    var tradeTemplate = "&lt;tr&gt;&lt;td&gt;" + secId + "&lt;/td&gt;&lt;td&gt;" + notional + "&lt;/td&gt;&lt;td&gt;" + price + "&lt;/td&gt;&lt;td&gt;" + client +"&lt;/td&gt;&lt;/tr&gt;" 
    tradeResults.append(tradeTemplate);
  })
}

}).catch(function(err){
console.log(err);
})
}).catch(function(err){
console.warn(err);
})

}

};

My Smart Contract :-

pragma solidity 0.5.0;

contract Trades {
struct Trade {
string secId;
uint notional;
uint price;
string client;
uint version;
uint index;
uint tradeId;
}

mapping(uint =>mapping(uint => Trade)) public trades;

uint public tradeId;
uint[] tradeIndex;
uint[] tradeID;
uint[] public totalNum;

function updateTrade(uint _tradeId, string memory _secId,uint _notional,uint _price,string memory _client) public{

uint j;
j= (getLatestVersionOfTrade(_tradeId)) + 1;
trades[_tradeId][j].secId = _secId;
trades[_tradeId][j].notional = _notional;
trades[_tradeId][j].price = _price;
trades[_tradeId][j].client = _client;
trades[_tradeId][j].version = j;
trades[_tradeId][j].index = tradeIndex.push(tradeIndex[tradeIndex.length-1]+1);

}

function setTrade(string memory _secId,uint _notional,uint _price,string memory _client) public {

uint  version  = 0;
tradeId++;

trades[tradeId][version].secId = _secId;
trades[tradeId][version].notional = _notional;
trades[tradeId][version].price = _price;
trades[tradeId][version].client = _client;
trades[tradeId][version].version = version;
trades[tradeId][version].index = tradeIndex.push(tradeId);
tradeID.push(tradeId);

}

function getAllTradeData()view public returns(uint[] memory){
return tradeIndex;
}

function getAllTradeDataId()view public returns(uint[] memory){
return tradeID;
}

function getTradeById(uint _tradeId,uint version)view public returns(string memory, uint, uint, string memory, uint, uint){
return (trades[_tradeId][version].secId, trades[_tradeId][version].notional, trades[_tradeId][version].price,
trades[_tradeId][version].client,trades[_tradeId][version].version, trades[_tradeId][version].index);
}

function getLatestVersionOfTrade(uint _tradeId) view public returns (uint) {
uint max = 0;

for (uint i = 0; i &lt; tradeIndex.length; i++) {
    uint ver = trades[_tradeId][i].version;
    if (ver &gt; max) {
        max = ver;
    }
}

return max;
}

function getClientData(string memory _client) public returns (uint[] memory) {

if (totalNum.length &gt; 0){
    delete totalNum;
}
    for(uint i=1; i &lt;= tradeID.length;i++){
    uint j;
    j= (getLatestVersionOfTrade(i));
    if(uint(keccak256(abi.encodePacked(trades[i][j].client))) == uint(keccak256(abi.encodePacked(_client)))){
        totalNum.push(i);
    }             
}  
return totalNum;

}

function getTotalNumLength() public returns (uint){
return totalNum.length;
}
}

The error shown on brower console : -

returnValues Array = 3,4,5
app.js:113 version =undefined
app.js:114 tradeId=3
app.js:113 version =undefined
app.js:114 tradeId=4
app.js:113 version =undefined
app.js:114 tradeId=5

3inpage.js:1 Uncaught (in promise) Error: Invalid number of arguments to
Solidity function
at Object.InvalidNumberOfSolidityArgs (inpage.js:1)
at u.validateArgs (inpage.js:1)
at u.toPayload (inpage.js:1)
at u.call (inpage.js:1)
at u.execute (inpage.js:1)
at truffle-contract.js:136
at new Promise (<anonymous>)
at truffle-contract.js:127

app.js:77 test:0,a:0
app.js:82 test2=0
app.js:77 test:0,a:0
app.js:82 test2=0
app.js:77 test:1,a:1
app.js:82 test2=1

The variable 'j' in searchByClient is returning an undefined value when it is passed in 'trades(c,j)'. But in the function getVersion the value returning seems to be fine. Please help me out as I have no other options left. Also I am extremly new to javascript, so please do tell me if I had make any mistakes. I was able to write this code with help of a tutorial.

How to Build a Blockchain in JavaScript

How to Build a Blockchain in JavaScript

For the purpose of this blog, let’s build a blockchain written in JavaScript. Hopefully, this doesn’t just serve as a simple code example but also gives a very basic understanding of how a blockchain actually works.

Blockchain is all the buzz now. And for good reason! A distributed public ledger that is extremely secure through encryption?! Imagine the millions of applicable use cases. A blockchain can be created and maintained using any modern language.

For the purpose of this blog, let’s dive into a blockchain written in JavaScript. Hopefully, this doesn’t just serve as a simple code example but also gives a very basic understanding of how a blockchain actually works.

Creating A Block

Let’s first create a block and give it some attributes.

  • BlockIndex – the position of the block on the blockchain.
  • BlockCreatedWhen – when the block was created.
  • BlockTransaction – a type of data to be stored on the block. This could be any exchange of value really. Perhaps an array of sorts.
  • BlockPreviousHash – hash of the previous block. This ties one block to another.
  • Hash – calculated hash function using the properties of the block. This is the block identifier on the blockchain. There is currently no built-in functionality for hash functions outside of the WebCryptoAPI. But there are plenty of other external libraries such as object-hash, crypto-hs, SJCL, forge SHA-256, etc.
  • Nonce – Proof of work
  • Merkle Root – A Merkle Root hash that houses all hashed transactions tied to a particular block.
const hash = require('crypto-js/sha256'); // could also use object-hash, Stanford Javascript Crypto Library (SJCL), forge SHA-256, jsSHA, etc
 
class VincentsBlock{
         
    constructor(blockIndex, blockCreatedWhen, blockTransactions, blockPreviousHash) {
        this.blockIndex = blockIndex;
        this.blockCreatedWhen = blockCreatedWhen;
        this.blockTransactions = blockTransactions;  // an array of transactions
        this.blockPreviousHash = blockPreviousHash;
        this.hash = this.calculateBlockHash();
        this.nonce = 0;  // needed for proof of work
        this.merkleRoot = this.computeMerkleRoot()  // used to validate transactions within block
    } 
 
    calculateBlockHash() { 
        return hash(this.blockIndex + JSON.stringify(this.blockTransaction) + this.blockPreviousHash + this.blockCreatedWhen + this.nonce + this.merkleTreeRoot).toString();
    }
     
    computeMerkleRoot() {
        let treeList = this.generateMerkleTreeRoot();
         
        return treeList[treeList.length-1];
    }
 
    generateMerkleTreeRoot() {
        let tree = [];
        let transactionCount = this.blockTransactions.length;
 
        for (var i=0; i<transactionCount; i++) { tree.push(this.blockTransactions[i].hash); } let levelOffset = 0; for (let levelSize = transactionCount; levelSize > 1; levelSize = (levelSize + 1) / 2) {          
            for (let left = 0; left < levelSize; left += 2) {            
                let right = Math.min(left + 1, levelSize - 1);
                let tleft = tree[levelOffset + left];
                let tright = tree[levelOffset + right];
                tree.push(hash(tleft + tright));
            }            
            levelOffset += levelSize;
        }
 
        return tree;
    }
}
 
module.exports = VincentsBlock;

Notice the Merkle Root property on the block. Given that a transaction has its own hash generated, we can create a Merkle Tree out of the transaction list. This tree can then be used to validate the block transactions. Now that a block is generated, let’s go back and look at a transaction object. This should illustrate that a transaction can be anything really. This is where the many applications of blockchain come into play.

These particular transactions consist of a

  • transactionId (id of transaction),
  • inputAccount (sender),
  • outputAccount (receiver),
  • fee (perhaps in BTC or any other currency),
  • the amount (processed amount),
  • type,
  • misc (notes)
  • timestamp (timestamp of transaction)
  • hash (hash of transaction)

Creating a Block Transaction Object

I created a block transaction object as follows:

const hash = require('crypto-js/sha256');
 
class VincentsTransaction{
         
    constructor(transactionId, inputAccount, outputAccount, fee, amount, type, misc, timestamp) {
        this.transactionId = transactionId,   // could be hex address
        this.inputAccount = inputAccount;    // sender
        this.outputAccount = outputAccount;   // receiver
        this.fee = fee;    // fee of transaction
        this.amount = amount;    // amount of transaction processed
        this.type = type;      // type of transaction
        this.misc = misc;     // misc note text
        this.timestamp = timestamp    // timestamp of block
        this.hash = this.calculateTransactionHash();  // hash of the transaction
    } 
 
    calculateTransactionHash() { 
        return hash(this.transactionId + this.inputAccount + this.outputAccount + this.fee + this.amount + this.type + this.misc + this.timestamp).toString();
    }
}
 
module.exports = VincentsTransaction;
The Chain

Moving on to the good part, let’s take a look at a basic immutable blockchain using the above blocks.

Below, in the constructor, you will notice a manual creation of the first block. This is needed to get the ball rolling. Every chain needs a first link!

Let’s manually create it and give it an index of 0 to show its placement on the chain. Many functions can be added to this class to assist on tasks such as adding blocks, checking for an empty chain, grabbing the latest block, validation, etc.

The process of creating additional blocks can be a complex one involving tons of validation if needed. We must keep in mind to regenerate the hash if any other property changes on a block.

In regards to validation of the blockchain, for now, we will focus on blocks linked together. Since index 0 corresponds to the manually created first block, we can skip it and check the hash of corresponding linked blocks.

let VincentsBlock = require('./VincentsBlock');
 
class VincentsBlockChain {
     
    constructor() {
        this.chain = [this.createBrandNewBlock()];
 
        //this.current_transactions = [];
    }
 
    createBrandNewBlock() {
        return new VincentsBlock(0, "01/01/2019", "Initial Block", ""); //no hash needed here since this is a manual creation of the first block.
    }
 
    createAdditionalBlock(newBlock) {
        ///very simplistic... could have more validation in place here.  
        let previousBlock = this.getLatestBlock();
        newBlock.index = previousBlock.index + 1; 
        newBlock.blockPreviousHash = this.getLatestBlock().hash;
        newBlock.hash = newBlock.calculateBlockHash(); //need to regenerate hash if any properties in our block changes
        this.chain.push(newBlock);
    }
 
    getLatestBlock() {
        return this.chain.slice(-1)[0];        
    }
 
    isEmpty() {
        return this.chain.length == 0;
    } 
 
    isBlockChainValid() { 
        for(let i = 1;i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
 
            if (currentBlock.blockPreviousHash != previousBlock.hash) { // does our block match up to correct previous hash property? 
                return false;
            }            
 
            if (currentBlock.hash != currentBlock.calculateBlockHash()) { // does the actual hash of the block match up to it's current hash property?
                return false;
            }     
              
            if (previousBlock.index + 1 !== currentBlock.index) { // do the indexes line up in order?
                return false;
            }  
        }
 
        return true;
    }
};
 
module.exports = VincentsBlockChain;
Adding to the Chain

For this simplistic example, let’s create 10 blocks, each with multiple transactions, and add them to the chain.

let VincentsBlockChain = require( "./blockchain/VincentsBlockChain");
let VincentsBlock = require( "./blockchain/VincentsBlock");
let VincentsTransaction = require( "./blockchain/VincentsTransaction");
 
let blockChain = new VincentsBlockChain();
blockChain.createAdditionalBlock(new VincentsBlock(1, "01/02/2019", [new VincentsTransaction(123, "abcxyz", "xyzabc", 30, "btc"), new VincentsTransaction(999, "uyyuyy", "xxdzkj", 550, "btc"), new VincentsTransaction(653, "drerea", "kioolk", 70, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(2, "01/03/2019", [new VincentsTransaction(234, "sfgfsd", "rrttyu", 50, "eth"), new VincentsTransaction(555, "iuiuii", "rrttyu", 50, "eth"), new VincentsTransaction(553, "hghghg", "ffgfff", 80, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(3, "01/04/2019", [new VincentsTransaction(345, "srthhf", "oouyuu", 65, "btc"), new VincentsTransaction(452, "jhghgg", "rrttyu", 11, "eth"), new VincentsTransaction(230, "wewfgf", "ghjgtt", 230, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(4, "01/05/2019", [new VincentsTransaction(456, "sfgfss", "xzsdee", 4, "btc"), new VincentsTransaction(780, "fgtfdd", "rrttyu", 22, "eth"), new VincentsTransaction(112, "qwewue", "hgyuyu", 280, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(5, "01/06/2019", [new VincentsTransaction(567, "xxcbbf", "nbvbcx", 545, "eth"), new VincentsTransaction(677, "fdfsaa", "uytttt", 33, "eth"), new VincentsTransaction(111, "hjhjh", "huiuii", 5480, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(6, "01/07/2019", [new VincentsTransaction(678, "uiyuyy", "dfgree", 5, "btc"), new VincentsTransaction(553, "ooouio", "yuyuuu", 55, "eth"), new VincentsTransaction(467, "eeeewq", "jgftft", 880, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(7, "01/08/2019", [new VincentsTransaction(789, "iughff", "opoiii", 125, "btc"), new VincentsTransaction(988, "aqQQQQ", "trewew", 01, "eth"), new VincentsTransaction(432, "eeeee", "khuuuu", 40, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(8, "01/09/2019", [new VincentsTransaction(890, "sdsrer", "sdfser", 99, "eth"), new VincentsTransaction(886, "gfgfdg", "qqqqqq", 88, "eth"), new VincentsTransaction(444, "gfgfd", "llllkl", 70, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(9, "01/10/2019", [new VincentsTransaction(901, "mnghjy", "dfgxbg", 65, "btc"), new VincentsTransaction(644, "dddsds", "wwwwww", 55, "eth"), new VincentsTransaction(333, "sdsaqq", "ererew", 50, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(10, "01/11/2019", [new VincentsTransaction(012, "vccvff", "uiyfvv", 74, "btc"), new VincentsTransaction(994, "mnmnmm", "qqqqhh", 66, "eth"), new VincentsTransaction(323, "qwqwqq", "wreret", 30, "eth")]));
 
console.log(JSON.stringify(blockChain, null, 2)); // let's view entire blockchain
 
console.log('Validate entire blockchain: ' + blockChain.isBlockChainValid()); // let's validate the blockchain is secure
 
blockChain.chain[5].hash = blockChain.chain[5].calculateBlockHash();  // let's try to recalculate hash for block 5
 
console.log('Is blockchain valid? ' + blockChain.isBlockChainValid()); 

Alright, let’s run this bad boy and see the blockchain we created.

{
  "chain": [
    {
      "blockIndex": 0,
      "blockCreatedWhen": "01/01/2019",
      "blockTransactions": "Initial Block",
      "blockPreviousHash": "",
      "hash": "46d4d81e6047cccb6e856a4bc800ed344fbe4b78df5f5a7fe5862e8773cd022a",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      }
    },
    {
      "blockIndex": 1,
      "blockCreatedWhen": "01/02/2019",
      "blockTransactions": [
        {
          "transactionId": 123,
          "inputAccount": "abcxyz",
          "outputAccount": "xyzabc",
          "fee": 30,
          "amount": "btc",
          "hash": "324cc491e4436b69299a905847124308c6dfc272642d9dbb3808bd79048bc110"
        },
        {
          "transactionId": 999,
          "inputAccount": "uyyuyy",
          "outputAccount": "xxdzkj",
          "fee": 550,
          "amount": "btc",
          "hash": "3beef24348997907eaa0263d3eb1393b1e74cb365e730649c60a910ecbbee0d5"
        },
        {
          "transactionId": 653,
          "inputAccount": "drerea",
          "outputAccount": "kioolk",
          "fee": 70,
          "amount": "eth",
          "hash": "a30e34266150f7b616aa4ebe88461406308280c9821425dbe0bfbc1fa7e6843a"
        }
      ],
      "blockPreviousHash": "46d4d81e6047cccb6e856a4bc800ed344fbe4b78df5f5a7fe5862e8773cd022a",
      "hash": "54ee7657f991b1341ecfa7d2ecfe7cf0501751f147ecc7b9ca08b2354068eeab",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 2,
      "blockCreatedWhen": "01/03/2019",
      "blockTransactions": [
        {
          "transactionId": 234,
          "inputAccount": "sfgfsd",
          "outputAccount": "rrttyu",
          "fee": 50,
          "amount": "eth",
          "hash": "f2378fced4de261dc5c531441d867b385f5726b76eb76d6ece8b739de3f6d2f7"
        },
        {
          "transactionId": 555,
          "inputAccount": "iuiuii",
          "outputAccount": "rrttyu",
          "fee": 50,
          "amount": "eth",
          "hash": "cb42ae591f275f7d265f749489c0830ef985252d256ad176430a9acee0b7c525"
        },
        {
          "transactionId": 553,
          "inputAccount": "hghghg",
          "outputAccount": "ffgfff",
          "fee": 80,
          "amount": "eth",
          "hash": "edb71da69f72d01fdefe7939665d8fc59489bb16e5f3d7e662a187b1d437ef8f"
        }
      ],
      "blockPreviousHash": "54ee7657f991b1341ecfa7d2ecfe7cf0501751f147ecc7b9ca08b2354068eeab",
      "hash": "f58b63c0fbe00f69b6b1bd04560ffdd72ebe7b71e70a16224bf45aad01308e0f",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 3,
      "blockCreatedWhen": "01/04/2019",
      "blockTransactions": [
        {
          "transactionId": 345,
          "inputAccount": "srthhf",
          "outputAccount": "oouyuu",
          "fee": 65,
          "amount": "btc",
          "hash": "eaa995a8b3a64237c3133fbb744ed1761ba716781b7026f7f956043173c452f6"
        },
        {
          "transactionId": 452,
          "inputAccount": "jhghgg",
          "outputAccount": "rrttyu",
          "fee": 11,
          "amount": "eth",
          "hash": "e867bcef38e0272776fc3fbeff0aac19cb366ca513945ddfa1491adc2f3257b2"
        },
        {
          "transactionId": 230,
          "inputAccount": "wewfgf",
          "outputAccount": "ghjgtt",
          "fee": 230,
          "amount": "eth",
          "hash": "d25b62651513ee902e063970b5b7baed30de4f9832b7a78db3247a09414a1495"
        }
      ],
      "blockPreviousHash": "f58b63c0fbe00f69b6b1bd04560ffdd72ebe7b71e70a16224bf45aad01308e0f",
      "hash": "288b9f37d503b22a5f4535e086ab84fdbff2e22f8880fb2f24baa29b5151235d",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 4,
      "blockCreatedWhen": "01/05/2019",
      "blockTransactions": [
        {
          "transactionId": 456,
          "inputAccount": "sfgfss",
          "outputAccount": "xzsdee",
          "fee": 4,
          "amount": "btc",
          "hash": "af15f83e9c599347351019f62309fb1b9077447a4e0fc8b48a6b5c487b1e144e"
        },
        {
          "transactionId": 780,
          "inputAccount": "fgtfdd",
          "outputAccount": "rrttyu",
          "fee": 22,
          "amount": "eth",
          "hash": "8984871129b02f0b09f83abd5c1161f6a0dee64f84c336a54f5c34e5454f02a7"
        },
        {
          "transactionId": 112,
          "inputAccount": "qwewue",
          "outputAccount": "hgyuyu",
          "fee": 280,
          "amount": "eth",
          "hash": "2631aa030dfbc44fe084d86f69c1d24ca64a154c59d13b42718211ed02e3b1dc"
        }
      ],
      "blockPreviousHash": "288b9f37d503b22a5f4535e086ab84fdbff2e22f8880fb2f24baa29b5151235d",
      "hash": "07205c43afea9e53d02dedce994c891babccd8397d082b431f986045cbc18e44",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 5,
      "blockCreatedWhen": "01/06/2019",
      "blockTransactions": [
        {
          "transactionId": 567,
          "inputAccount": "xxcbbf",
          "outputAccount": "nbvbcx",
          "fee": 545,
          "amount": "eth",
          "hash": "8030161e3fdb7038fb7e751236063996152897f9647c654fb47334da7f7295bb"
        },
        {
          "transactionId": 677,
          "inputAccount": "fdfsaa",
          "outputAccount": "uytttt",
          "fee": 33,
          "amount": "eth",
          "hash": "b327dfa8769ec04bc447313af98feeb783c95c198e926737ea916700f4b71994"
        },
        {
          "transactionId": 111,
          "inputAccount": "hjhjh",
          "outputAccount": "huiuii",
          "fee": 5480,
          "amount": "eth",
          "hash": "aae165434e054c42e7da129b08a7c99f6c544351b7383459e322ef253570f4a4"
        }
      ],
      "blockPreviousHash": "07205c43afea9e53d02dedce994c891babccd8397d082b431f986045cbc18e44",
      "hash": "55c88c98e497a9f300ca3ec8bec0129652a8e5e9b49e6ecaf4ab0c63b42ebf4f",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 6,
      "blockCreatedWhen": "01/07/2019",
      "blockTransactions": [
        {
          "transactionId": 678,
          "inputAccount": "uiyuyy",
          "outputAccount": "dfgree",
          "fee": 5,
          "amount": "btc",
          "hash": "134269524a221afb1f5ef218584b063ab4bea3aaa5dc2cdbfa66f439f2d1e36b"
        },
        {
          "transactionId": 553,
          "inputAccount": "ooouio",
          "outputAccount": "yuyuuu",
          "fee": 55,
          "amount": "eth",
          "hash": "19076e1cb35d8357b16f92794c5bd1ff020c8cb9e8ce5fcf03b560256a363c16"
        },
        {
          "transactionId": 467,
          "inputAccount": "eeeewq",
          "outputAccount": "jgftft",
          "fee": 880,
          "amount": "eth",
          "hash": "461ad65962d2d1cc1f767c1b9c0f569d19ef48be60ef2fe8c08898cc01d34492"
        }
      ],
      "blockPreviousHash": "55c88c98e497a9f300ca3ec8bec0129652a8e5e9b49e6ecaf4ab0c63b42ebf4f",
      "hash": "e1087468c2719f967584cab0a22938b81e9e956762f2c5a2d2dd91da2c92cfbd",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 7,
      "blockCreatedWhen": "01/08/2019",
      "blockTransactions": [
        {
          "transactionId": 789,
          "inputAccount": "iughff",
          "outputAccount": "opoiii",
          "fee": 125,
          "amount": "btc",
          "hash": "7149930eae17b35f22c02acb0765b829d79e5539c6e2e80b837045f89d0422c2"
        },
        {
          "transactionId": 988,
          "inputAccount": "aqQQQQ",
          "outputAccount": "trewew",
          "fee": 1,
          "amount": "eth",
          "hash": "d752f91eb8010b5c9d6066afb92a5fa6c4667f9fc18b6f8d4a7c1a32b104919c"
        },
        {
          "transactionId": 432,
          "inputAccount": "eeeee",
          "outputAccount": "khuuuu",
          "fee": 40,
          "amount": "eth",
          "hash": "c5c20ac19253cd6782f0a95eb628c719cafeac22d4c8bacbf2863b06e0e366db"
        }
      ],
      "blockPreviousHash": "e1087468c2719f967584cab0a22938b81e9e956762f2c5a2d2dd91da2c92cfbd",
      "hash": "63302ff671cee7c659e1bf9eb1cd659f054d4615c4692d5b425ad04b2f82deb6",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 8,
      "blockCreatedWhen": "01/09/2019",
      "blockTransactions": [
        {
          "transactionId": 890,
          "inputAccount": "sdsrer",
          "outputAccount": "sdfser",
          "fee": 99,
          "amount": "eth",
          "hash": "c4abc25fcc9ea1e78be488aac5ee9a3ee1f16b0915e23700ff6bb11e1df02c51"
        },
        {
          "transactionId": 886,
          "inputAccount": "gfgfdg",
          "outputAccount": "qqqqqq",
          "fee": 88,
          "amount": "eth",
          "hash": "fd454dbdfb263db527ed335116aab73a7dab8468d3d443aa0fd8e4c7e438f465"
        },
        {
          "transactionId": 444,
          "inputAccount": "gfgfd",
          "outputAccount": "llllkl",
          "fee": 70,
          "amount": "eth",
          "hash": "c9b9ebdcb469f0f5ae8c6653c10f45d88a59b807c87b9febe1be986844d558f4"
        }
      ],
      "blockPreviousHash": "63302ff671cee7c659e1bf9eb1cd659f054d4615c4692d5b425ad04b2f82deb6",
      "hash": "fec255eb642f1dd02ab7c5185bfd78f9574b50c0188a888b4227b8cef5497e0e",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 9,
      "blockCreatedWhen": "01/10/2019",
      "blockTransactions": [
        {
          "transactionId": 901,
          "inputAccount": "mnghjy",
          "outputAccount": "dfgxbg",
          "fee": 65,
          "amount": "btc",
          "hash": "6e94502eb38d14410141d87bd27a2369e14e62bf8efece8adbde9873a8e88777"
        },
        {
          "transactionId": 644,
          "inputAccount": "dddsds",
          "outputAccount": "wwwwww",
          "fee": 55,
          "amount": "eth",
          "hash": "7072d7ec51bf41a1f36744f7abf8e9e4f70d195b708420f77b5141bcb7567f45"
        },
        {
          "transactionId": 333,
          "inputAccount": "sdsaqq",
          "outputAccount": "ererew",
          "fee": 50,
          "amount": "eth",
          "hash": "199c710224da1e16f597c2eae088acd5c3d4b6fd9a12d84c0b76a735eed541a2"
        }
      ],
      "blockPreviousHash": "fec255eb642f1dd02ab7c5185bfd78f9574b50c0188a888b4227b8cef5497e0e",
      "hash": "fcec05a7f1afb3c91ddbf90f36e04d17e4eabd7e47545a9f08173767cd4d74c8",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 10,
      "blockCreatedWhen": "01/11/2019",
      "blockTransactions": [
        {
          "transactionId": 10,
          "inputAccount": "vccvff",
          "outputAccount": "uiyfvv",
          "fee": 74,
          "amount": "btc",
          "hash": "8f2abd539ef073b7d4053fe6090196f682f6361a619329e45a3a0df10919623c"
        },
        {
          "transactionId": 994,
          "inputAccount": "mnmnmm",
          "outputAccount": "qqqqhh",
          "fee": 66,
          "amount": "eth",
          "hash": "040dda42beee5673e882884e3e963292015253348b1db5c3feb5d54af4e4b728"
        },
        {
          "transactionId": 323,
          "inputAccount": "qwqwqq",
          "outputAccount": "wreret",
          "fee": 30,
          "amount": "eth",
          "hash": "af54b136d57904e80823c53649d4149092fb6ebb4bf1e8578facaae8aca2cf1c"
        }
      ],
      "blockPreviousHash": "fcec05a7f1afb3c91ddbf90f36e04d17e4eabd7e47545a9f08173767cd4d74c8",
      "hash": "9074846c6a4153802d86cd19b6d95266c56d082329c6f0f52ded270fae269c48",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    }
  ]
}

NOTE: Within the merkleTreeRoot property, crypto-js utilizes word arrays encoded as UTF-8 when given a string.

Above, notice the hash and blockPreviousHash property of each block. This serves as the link for the chain.

But what happens if one were to modify a block somehow? Perhaps change the fee property or attempt to recalculate a block hash?

console.log('Validate entire blockchain: ' + blockChain.isBlockChainValid()); // let's validate the blockchain is secure
 
blockChain.chain[5].hash = blockChain.chain[5].generateBlockHash();  // let's try to recalculate hash for block 5
 
console.log('Is blockchain valid? ' + blockChain.isBlockChainValid()); 
 
Validate entire blockchain: true
Is blockchain valid? false

It appears that the blockchain validation function isBlockChainValid() caught an attempt to recalculate its hash. Of course, this is pretty basic validation, but it serves its purpose to illustrate the secure nature of blockchain.

Final Thoughts

In summary, a blockchain can be written in various languages. I hope this basic JavaScript version not only illustrates that but gives a little more overview of how blockchains work.

Thank for reading ! Please share if you liked it!

How to set up face verification the easy way using HTML5 + JavaScript

How to set up face verification the easy way using HTML5 + JavaScript

<strong>Originally published by </strong><a href="https://medium.com/@matthewenubuje" target="_blank">Matthew Enubuje</a><strong> </strong><em>at&nbsp;</em><a href="https://medium.com/@matthewenubuje/how-to-set-up-face-verification-the-easy-way-using-html5-javascript-5301235e495f" target="_blank"><em>Medium</em></a>

I’ve created a very simple way to face match two images using HTML5 and JavaScript. You upload the verification picture you’d like to use, take a snapshot from the video streaming from your camera/webcam, then use a face matching API to retrieve the results. Simple.

The Github Repo

What You’ll Need:

  • A Webcam/Camera
  • Free Face Recognition API Key
  • Web Server

Before We Begin

Create a Directory For This Project

This directory will be where we put all the files.

Create a Local Web Server

In order to have full control of images, a web server is required otherwise we’d be getting a tainted canvas security error. There are several ways to do this and I’ve listed below how to do it with Python.

Python

cd C:/DIRECTORY_LOCATION & py -m http.server 8000

You should be able to access the directory through http://localhost:8000

Get Your Free API Key

We’re going to be using Facesoft’s face recognition API. Quickly sign up here to access your free API key so you can get unlimited API calls with up to two requests per minute.

Once you’ve logged in, your API key will be visible in the dashboard area.

1. Setup

Create these three files:

  • index.html
  • style.css
  • verify.js

Next right click and save the files below into that directory. These image files will be the default images for uploaded and verification pics.

SAVE AS “defaultupload.png”

SAVE AS “defaultphoto.png”

2. The HTML

Layout

Copy and paste the layout code below into your “index.html” file. This gives us our framework and design with the help of bootstrap.

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Style CSS -->
  <link rel="stylesheet" href="style.css">
<title>Face Verification</title>
</head>
<body>
  <!-- Page Content -->
  <div class="container">
    <div class="row">
      <div class="col-lg-12 text-center">
        <h1 class="mt-5">Face Verification</h1>
        <p class="lead">Quick and simple face verification using HTML5 and JavaScript</p>
      </div>
    </div>
    <!-- INSERT NEXT CODE HERE -->
  </div>
<!-- Verify JS -->
 <script src="verify.js"></script>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

Images, Canvases & Buttons

We’re now creating a row and having three columns for the verification photo, the video stream, and the photo taken from the video stream. Add the code below right after the row.

Check for “INSERT NEXT CODE HERE” tag in previous code.

<div class="row justify-content-md-center">
  <div class="col-lg-4 text-center">
    <p><strong>Verification Photo</strong></p>
      <!-- Canvas For Uploaded Image -->
      <canvas id="uploadCanvas" width="300"  height="300"></canvas>
      <!-- Default Canvas Image -->
      <img src="defaultupload.png" id="uploadedPhoto" alt="upload"/>
      <!-- Upload Image Input & Upload Photo Button -->
      <input type="file" name="image-upload" accept="image/png, image/jpeg">
      <button id="upload" type="button" class="btn btn-outline-primary btn-lg">Upload Photo</button>
  </div>
  <div class="col-lg-4 text-center">
    <p><strong>Video</strong></p>
    <!-- Camera -->
    <div class="camera-container">
      <video id="video" width="100%" height="300" autoplay="true">
      </video>
    </div>
    <!-- Take Photo Button -->
    <button id="capture" type="button" class="btn btn-outline-primary btn-lg">Take Photo</button>
  </div>
  <div class="col-lg-4 text-center">
    <p><strong>Photo Taken</strong></p>
  
    <!-- Canvas For Capture Taken -->
    <canvas id="captureCanvas" width="300"  height="300"></canvas>
    <!-- Default Canvas Image -->
    <img src="defaultphoto.png" id="capturedPhoto" alt="capture" />
    <!-- Verify Photos Button -->
    <button id="verify" type="button" class="btn btn-outline-success btn-lg">Verify Photo</button>
  </div>
</div>
<!-- INSERT NEXT CODE HERE -->

API Response and Warnings

The code we’re going to add is to display the match result, score percentage, errors and warnings. Right under the last code we added, add the code below.

<div class="row">
  <div class="col-lg-12 text-center">
    <!-- API Match Result & API Percentage Score -->
    <h2 id="match" class="mt-5"></h2>
    <p id="score" class="lead"></p>
  </div>
  <div class="col-lg-12 text-center">
    <!-- Error & Warning Alerts -->
    <div class="alert alert-danger" id="errorAlert"></div>
    <div class="alert alert-warning" id="warningAlert"></div>
  </div>
</div>

3. The CSS

Add the code below to your style.css file.

.camera-container {
  max-width: 100%;
  border: 1px solid black;
}
.verification-image {
  width: 300px;
  height: auto;
  max-width: 100%;
}
.btn {
  margin-top: 10px;
}
#captureCanvas, #uploadCanvas {
  display: none;
}
input[name="image-upload"] {
  display: none;
}
#errorAlert, #warningAlert {
  display: none;
}

We’ve set the image upload input to display none as we’ll be triggering it using the upload button. Also, the IDs for the canvases have been set to display none so the default images are initially displayed.

Here is what your screen should look like:

4. The JavaScript

document.addEventListener("DOMContentLoaded", function() {
});

In the verify.js file we want to start off by adding an event listener that will run after the page loads. Every code we enter should be inside this function.

Variables

var video = document.getElementById('video'), 
captureCanvas = document.getElementById('captureCanvas'), 
uploadCanvas = document.getElementById('uploadCanvas'), 
captureContext = captureCanvas.getContext('2d'),
uploadContext = uploadCanvas.getContext('2d'),
uploadedPhoto = document.getElementById('uploadedPhoto'),
capturedPhoto = document.getElementById('capturedPhoto'),
imageUploadInput = document.querySelector('[name="image-upload"]'),
apiKey = 'INSERT_YOUR_FACESOFT_API_KEY',
errorAlert = document.getElementById('errorAlert'), AlertwarningAlert = document.getElementById('warningAlert'),
matchText = document.getElementById('match'),
scoreText = document.getElementById('score');

The variables are:

  • IDs for the video element, canvases, photos & API response
  • Selector for image input
  • Canvas contexts
  • API key

Video Stream

Here is a very simple code to access your webcam/camera and stream it into the video element. Add underneath variables.

// Stream Camera To Video Element
if(navigator.mediaDevices.getUserMedia){
  navigator.mediaDevices.getUserMedia({ video: true })
  .then(function(stream) {
    video.srcObject = stream;
  }).catch(function(error) {
    console.log(error)
  })
}

If you refresh your page, this is what you’ll see:

:D

Function 1: Set Photo To Canvas

// Set Photo To Canvas Function
function setImageToCanvas(image, id, canvas, context, width=image.width, height=image.height) {
  var ratio = width / height;
  var newWidth = canvas.width;
  var newHeight = newWidth / ratio;
  if (newHeight > canvas.height) {
    newHeight = canvas.height;
    newWidth = newHeight * ratio;
  }
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.drawImage(image, 0, 0, newWidth, newHeight);
  id.setAttribute('src', canvas.toDataURL('image/png'));
}

In this function, we take in the image, id, canvas, context, width and height. We take in the width and height because to get the dimensions of the video, we must use video.videoWidth & video.videoHeight.

We also get the aspect ratio of the image so that when we assign an image, it fits right into the canvas. The rest of the code clears the canvas and draws the new image into the canvas.

Function 2: Verify If The Photos Match By Sending Them To The API.

// Facesoft Face Match API Function
function verifyImages(image1, image2, callback){
  var params = {
    image1: image1,
    image2: image2,
  }
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "https://api.facesoft.io/v1/face/match");
  xhr.setRequestHeader("apikey", apiKey);
  xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  xhr.onload = function(){
    callback(xhr.response);
  }
  xhr.send(JSON.stringify(params));
}

In this function we make an XMLHttpRequest to the face match API endpoint. We’ve added the API key for authorisation and content type into the header. For the body we’re passing in an object containing the two images.

Now we’re done with functions :)

Upload Photo Button Click Event

// On Upload Photo Button Click
document.getElementById('upload').addEventListener('click', function(){
  imageUploadInput.click();
})

This click event listener for the upload button triggers a click for the image input.

Image Upload Input Change Event

// On Uploaded Photo Change
imageUploadInput.addEventListener('change', function(){
  // Get File Extension
  var ext = imageUploadInput.files[0]['name'].substring(imageUploadInput.files[0]['name'].lastIndexOf('.') + 1).toLowerCase();
  // If File Exists & Image
  if (imageUploadInput.files && imageUploadInput.files[0] && (ext == "png" || ext == "jpeg" || ext == "jpg")) {
    // Set Photo To Canvas
    var reader = new FileReader();
    reader.onload = function (e) {
      var img = new Image();
      img.src = event.target.result;
      img.onload = function() {
      setImageToCanvas(img, uploadedPhoto, uploadCanvas, uploadContext);
      }
    }
    reader.readAsDataURL(imageUploadInput.files[0]);
  }
})

In this change event listener, we retrieve the file extension and perform an if statement to check if there is an image file in the input. Then we use FileReader to load the image onto the canvas.

Take Photo Button Click Event

// On Take Photo Button Click
document.getElementById('capture').addEventListener('click', function(){
  setImageToCanvas(video, capturedPhoto, captureCanvas, captureContext, video.videoWidth, video.videoHeight);
})

This event listener now executes the set image to canvas to capture a still frame from the video and assign it into a canvas.

Verify Photo Button Click Event

// On Verify Photo Button Click
document.getElementById('verify').addEventListener('click', function(){
  // Remove Results & Alerts
  errorAlert.style.display = "none";
  warningAlert.style.display = "none";
  matchText.innerHTML = "";
  scoreText.innerHTML = "";
  // Get Base64
  var image1 = captureCanvas.toDataURL().split(',')[1];
  var image2 = uploadCanvas.toDataURL().split(',')[1]; 
  // Verify if images are of the same person
  verifyImages(image1, image2, function(response){
    if(response){
      var obj = JSON.parse(response);
      
      // If Warning Message
     
      if(obj.message){
        errorAlert.style.display = "none";
        warningAlert.style.display = "block";
        warningAlert.innerHTML = obj.message;
        matchText.innerHTML = "";
        scoreText.innerHTML = "";
      }
      // If Error
      else if(obj.error){
        errorAlert.style.display = "block";
        errorAlert.innerHTML = obj.error;
        warningAlert.style.display = "none";
        matchText.innerHTML = "";
        scoreText.innerHTML = "";
      }
      // If Valid
      else{
        errorAlert.style.display = "none";
        warningAlert.style.display = "none";
        matchText.innerHTML = obj.match;
        scoreText.innerHTML = (obj.score*100).toFixed(2)+"% Score";
      }
    }
  })
})

In this event, we first hide the error/warning alerts and remove any text from the match result and score percentage.

We then get the base64 of the images from the canvases and use the split method to only get the part without “ data:image/png;base64” so the API won’t return an error.

Lastly, we call the verify images function to send the data to the API and our response will be an object either containing the results, an error, or a message.

Final Code

// Set Default Images For Uploaded & Captured Photo
setImageToCanvas(uploadedPhoto, uploadedPhoto, uploadCanvas, uploadContext);
setImageToCanvas(capturedPhoto, capturedPhoto, captureCanvas, captureContext);

This will allow us to verify the default images by assigning them to their canvas.

5. The Rock vs Dwayne Johnson

If you click verify we shall now see if the API can tell the difference between The Rock at a young age and Dwayne Johnson we usually see in films…

The API correctly identifies the 2 as the same with a 96.53% match score!

Upload, Capture, Verify

In Closing

You should now be able to think of better, secure and more complex ways to implement face matching such as logging in, 2FA, authorisation, payments etc.

Accuracy & Reliability?

The results for the best face recognition algorithm in the world came out. The algorithm we’re using came in the top 10 beating Toshiba, Microsoft & VisionLabs, and came 2nd worldwide in the wild image test (detection at difficult angles).