Skip to content

Instantly share code, notes, and snippets.

@yathin017
Last active April 7, 2023 06:30
Show Gist options
  • Save yathin017/ec50576dcd706e5868f4d96edcd5a00c to your computer and use it in GitHub Desktop.
Save yathin017/ec50576dcd706e5868f4d96edcd5a00c to your computer and use it in GitHub Desktop.
Implementation of Mobile Theft Prevention Smart Contract in JavaScript and Solidity
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545'); // your Blockchain client endpoint
const contractAddress = '0x123456789...'; // your contract address
const abi = [/* Smart Contract ABI */]; // your contract ABI
const contract = new web3.eth.Contract(abi, contractAddress);
const isIMEIexist = {};
const isPhoneNumberexist = {};
const mapAPI = {};
const isIMEIlost = {};
function hash(value) {
return web3.utils.keccak256(web3.eth.abi.encodeParameter('uint256', value));
}
async function addIMEI(IMEI, phoneNumber) {
if (!isIMEIexist[hash(IMEI)] && !isPhoneNumberexist[hash(phoneNumber)]) {
isIMEIexist[hash(IMEI)] = true;
isPhoneNumberexist[hash(phoneNumber)] = true;
mapAPI[web3.eth.defaultAccount][hash(phoneNumber)] = hash(IMEI);
await contract.methods.addIMEI(IMEI, phoneNumber).send({ from: web3.eth.defaultAccount });
}
}
async function activateLost(phoneNumber) {
const IMEI = mapAPI[web3.eth.defaultAccount][hash(phoneNumber)];
if (isIMEIexist[IMEI]) {
isIMEIlost[IMEI] = true;
await contract.methods.activateLost(phoneNumber).send({ from: web3.eth.defaultAccount });
}
}
async function deactivateLost(phoneNumber) {
const IMEI = mapAPI[web3.eth.defaultAccount][hash(phoneNumber)];
if (isIMEIexist[IMEI]) {
isIMEIlost[IMEI] = false;
await contract.methods.deactivateLost(phoneNumber).send({ from: web3.eth.defaultAccount });
}
}
async function changeIMEI(IMEI, phoneNumber, newIMEI) {
if (isIMEIexist[hash(IMEI)] && isPhoneNumberexist[hash(phoneNumber)] && mapAPI[web3.eth.defaultAccount][hash(phoneNumber)] === hash(IMEI)) {
mapAPI[web3.eth.defaultAccount][hash(phoneNumber)] = hash(newIMEI);
isIMEIexist[hash(IMEI)] = false;
if (!isIMEIexist[hash(newIMEI)]) {
isIMEIexist[hash(newIMEI)] = true;
}
await contract.methods.changeIMEI(IMEI, phoneNumber, newIMEI).send({ from: web3.eth.defaultAccount });
}
}
async function changePhoneNumber(IMEI, phoneNumber, newPhoneNumber) {
if (isIMEIexist[hash(IMEI)] && isPhoneNumberexist[hash(phoneNumber)] && mapAPI[web3.eth.defaultAccount][hash(phoneNumber)] === hash(IMEI)) {
mapAPI[web3.eth.defaultAccount][hash(phoneNumber)] = hash(0);
mapAPI[web3.eth.defaultAccount][hash(newPhoneNumber)] = hash(IMEI);
isPhoneNumberexist[hash(phoneNumber)] = false;
await contract.methods.changePhoneNumber(IMEI, phoneNumber, newPhoneNumber).send({ from: web3.eth.defaultAccount });
}
}
async function checkIMEI(IMEI) {
return isIMEIlost[hash(IMEI)];
}
module.exports = {
addIMEI,
activateLost,
deactivateLost,
changeIMEI,
changePhoneNumber,
checkIMEI
};
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract MTP{
mapping(bytes32=>bool) private isIMEIexist;
mapping(bytes32=>bool) private isPhoneNumberexist;
mapping(address=>mapping(bytes32=>bytes32)) private mapAPI;
mapping(bytes32=>bool) private isIMEIlost;
function hash(uint _value) private pure returns(bytes32){
return keccak256(abi.encodePacked(_value));
}
function addIMEI(uint _IMEI, uint _phoneNumber) public{
require(isIMEIexist[hash(_IMEI)] == false);
require(isPhoneNumberexist[hash(_phoneNumber)] == false);
isIMEIexist[hash(_IMEI)] = true;
isPhoneNumberexist[hash(_phoneNumber)] = true;
mapAPI[msg.sender][hash(_phoneNumber)] = hash(_IMEI);
}
function activateLost(uint _phoneNumber) public{
require(isIMEIexist[mapAPI[msg.sender][hash(_phoneNumber)]] == true);
isIMEIlost[mapAPI[msg.sender][hash(_phoneNumber)]] = true;
}
function deactivateLost(uint _phoneNumber) public{
require(isIMEIexist[mapAPI[msg.sender][hash(_phoneNumber)]] == true);
isIMEIlost[mapAPI[msg.sender][hash(_phoneNumber)]] = false;
}
function changeIMEI(uint _IMEI, uint _phoneNumber, uint _newIMEI) public{
require(isIMEIexist[hash(_IMEI)] == true);
require(isPhoneNumberexist[hash(_phoneNumber)] == true);
require(mapAPI[msg.sender][hash(_phoneNumber)] == hash(_IMEI));
mapAPI[msg.sender][hash(_phoneNumber)] = hash(_newIMEI);
isIMEIexist[hash(_IMEI)] = false;
if(isIMEIexist[hash(_newIMEI)] == false){
isIMEIexist[hash(_IMEI)] = true;
}
}
function changePhoneNumber(uint _IMEI, uint _phoneNumber, uint _newPhoneNumber) public{
require(isIMEIexist[hash(_IMEI)] == true);
require(isPhoneNumberexist[hash(_phoneNumber)] == true);
require(mapAPI[msg.sender][hash(_phoneNumber)] == hash(_IMEI));
mapAPI[msg.sender][hash(_phoneNumber)] = hash(uint(0));
mapAPI[msg.sender][hash(_newPhoneNumber)] = hash(_IMEI);
isPhoneNumberexist[hash(_phoneNumber)] = false;
}
function checkIMEI(uint _IMEI) public view returns(bool){
return isIMEIlost[hash(_IMEI)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment