Skip to content

Instantly share code, notes, and snippets.

@adonnini
Last active August 27, 2018 14:49
Show Gist options
  • Save adonnini/114b37e4ec80ec97a227b0b8d6d469be to your computer and use it in GitHub Desktop.
Save adonnini/114b37e4ec80ec97a227b0b8d6d469be to your computer and use it in GitHub Desktop.
DigitalTwin
package com.android.commmanager.continuouslocationtrackingservice;
import com.bigchaindb.builders.BigchainDbConfigBuilder;
public class bigchaindbConnection {
public bigchaindbConnection(String network, String appId, String appKey)
{
try {
BigchainDbConfigBuilder
.baseUrl(network)
.addToken("appId", appId)
.addToken("appKey", appKey)
.setup();
} catch (Exception e6) {
// TODO Auto-generated catch block
e6.printStackTrace();
}
}
}
public static void digitalTwin(String appId, String appKey, AssetType assetType)
{
int bigchainAssetIdToMyAssetIdMapSize = bigchainAssetIdToMyAssetIdMap.size();
userLocationInformationDatabaseExtract = "";
apLocationInformationDatabaseExtract = "";
userLocationInformationDatabaseExtract = toStringFour(mUserLocationInformation);
ArrayList<apLocationInformation> apLocationOfflineDatabaseHolder = new ArrayList<apLocationInformation>();
apLocationOfflineDatabaseHolder.addAll(apLocationOffLineDatabaseRetrieve());
int apLocationOfflineDatabaseHolderSize = apLocationOfflineDatabaseHolder.size();
Log.i(TAG, "- digitalTwin - apLocationOfflineDatabaseHolderSize - "+apLocationOfflineDatabaseHolderSize);
apLocationInformationDatabaseExtract = toStringFour(apLocationOfflineDatabaseHolder);
String[] apLocationOfflineDatabaseRecordHolder = new String[27];
ArrayList<apLocationInformation> apLocationOfflineDatabaseSingleRecord = new ArrayList<apLocationInformation>();
int apLocationOfflineDatabaseSingleRecordSize = apLocationOfflineDatabaseSingleRecord.size();
String API_PATH = "https://test.bigchaindb.com";
String appIdDigitalTwin = appId;
String appKeyDigitalTwin = appKey;
net.i2p.crypto.eddsa.KeyPairGenerator edDsaKpg = new net.i2p.crypto.eddsa.KeyPairGenerator();
KeyPair keyPairUser = edDsaKpg.generateKeyPair();
KeyPair keyPairAp = edDsaKpg.generateKeyPair();
KeyPair keyPairPoi = edDsaKpg.generateKeyPair();
class DID extends Orm
{
EdDSAPublicKey entity;
public DID(EdDSAPublicKey entity)
{
super(API_PATH, appIdDigitalTwin, appKeyDigitalTwin);
this.entity = entity;
}
}
KeyPair keyPairDevice = edDsaKpg.generateKeyPair();
KeyPair keyPairGPSLocation = edDsaKpg.generateKeyPair();
DID userDID = new DID((EdDSAPublicKey) keyPairUser.getPublic());
DID apDID = new DID((EdDSAPublicKey) keyPairAp.getPublic());
DID poiDID = new DID((EdDSAPublicKey) keyPairPoi.getPublic());
OrmObject userDIDOrmObject = userDID.define("myModel", "https://schema.org/v1/myModel");
OrmObject apDIDOrmObject = apDID.define("myModel", "https://schema.org/v1/myModel");
OrmObject poiDIDOrmObject = poiDID.define("myModel", "https://schema.org/v1/myModel");
String userAssetId = "";
String apAssetId = "";
String poiAssetId = "";
for(int i = 0; i < 50; ++i)
// for(int i = 0; i < apLocationOfflineDatabaseHolderSize; ++i)
{
try {
// Log.i(TAG, "- digitalTwin - apLocationOfflineDatabaseHolder.get(i).bssid - "+apLocationOfflineDatabaseHolder.get(i).bssid);
apLocationInformation apLocationOfflineDatabaseSingleRecordHolder = new apLocationInformation();
apLocationOfflineDatabaseSingleRecordHolder.bssid = apLocationOfflineDatabaseHolder.get(i).bssid;
apLocationOfflineDatabaseSingleRecordHolder.ssid = apLocationOfflineDatabaseHolder.get(i).ssid;
apLocationOfflineDatabaseSingleRecordHolder.capabilities = apLocationOfflineDatabaseHolder.get(i).capabilities;
// apLocationOfflineDatabaseSingleRecordHolder.apRssi = apLocationOfflineDatabaseHolder.get(i).apRssi;
// apLocationOfflineDatabaseSingleRecordHolder.distance = apLocationOfflineDatabaseHolder.get(i).distance;
apLocationOfflineDatabaseSingleRecordHolder.lat = apLocationOfflineDatabaseHolder.get(i).lat;
apLocationOfflineDatabaseSingleRecordHolder.lon = apLocationOfflineDatabaseHolder.get(i).lon;
apLocationOfflineDatabaseSingleRecordHolder.lat2 = apLocationOfflineDatabaseHolder.get(i).lat2;
apLocationOfflineDatabaseSingleRecordHolder.lon2 = apLocationOfflineDatabaseHolder.get(i).lon2;
apLocationOfflineDatabaseSingleRecordHolder.alternateLatitude = apLocationOfflineDatabaseHolder.get(i).alternateLatitude;
apLocationOfflineDatabaseSingleRecordHolder.alternateLongitude = apLocationOfflineDatabaseHolder.get(i).alternateLongitude;
apLocationOfflineDatabaseSingleRecordHolder.myLat = apLocationOfflineDatabaseHolder.get(i).myLat;
apLocationOfflineDatabaseSingleRecordHolder.myLon = apLocationOfflineDatabaseHolder.get(i).myLon;
apLocationOfflineDatabaseSingleRecordHolder.previousBearing = apLocationOfflineDatabaseHolder.get(i).previousBearing;
apLocationOfflineDatabaseSingleRecordHolder.bearing = apLocationOfflineDatabaseHolder.get(i).bearing;
apLocationOfflineDatabaseSingleRecordHolder.deltaBearing = apLocationOfflineDatabaseHolder.get(i).deltaBearing;
apLocationOfflineDatabaseSingleRecordHolder.locationAltitude = apLocationOfflineDatabaseHolder.get(i).locationAltitude;
apLocationOfflineDatabaseSingleRecordHolder.locationTime = apLocationOfflineDatabaseHolder.get(i).locationTime;
apLocationOfflineDatabaseSingleRecordHolder.locationSpeed = apLocationOfflineDatabaseHolder.get(i).locationSpeed;
apLocationOfflineDatabaseSingleRecordHolder.locationAccuracy = apLocationOfflineDatabaseHolder.get(i).locationAccuracy;
apLocationOfflineDatabaseSingleRecordHolder.locationBearing = apLocationOfflineDatabaseHolder.get(i).locationBearing;
apLocationOfflineDatabaseSingleRecordHolder.nanoTime = apLocationOfflineDatabaseHolder.get(i).nanoTime;
apLocationOfflineDatabaseSingleRecordHolder.currentTimeMillis = apLocationOfflineDatabaseHolder.get(i).currentTimeMillis;
apLocationOfflineDatabaseSingleRecordHolder.macroMovementLocationChangeFlag = apLocationOfflineDatabaseHolder.get(i).macroMovementLocationChangeFlag;
apLocationOfflineDatabaseSingleRecordHolder.macroMovementScanCount = apLocationOfflineDatabaseHolder.get(i).macroMovementScanCount;
// apLocationOfflineDatabaseSingleRecordHolder.estimatedLocations = apLocationOfflineDatabaseHolder.get(i).estimatedLocations;
// apLocationOfflineDatabaseSingleRecordHolder.myLocations = apLocationOfflineDatabaseHolder.get(i).myLocations;
apLocationOfflineDatabaseSingleRecord.add(apLocationOfflineDatabaseSingleRecordHolder);
apLocationOfflineDatabaseSingleRecordHolder = new apLocationInformation();
apDIDOrmObject = apDIDOrmObject.create(keyPairAp, apLocationOfflineDatabaseSingleRecord);
apAssetId = apDIDOrmObject.transactionList.get(0).getId();
try {
Log.i(TAG, "- digitalTwin - 1 - apAssetId - "+apAssetId);
} catch (Exception e1) {
e1.printStackTrace();
}
bigchainAssetIdToMyAssetIdMap.put(apAssetId, apLocationOfflineDatabaseHolder.get(i).bssid);
Log.i(TAG, "- digitalTwin - bigchainAssetIdToMyAssetIdMap.size() - "+bigchainAssetIdToMyAssetIdMap.size());
} catch (Exception e) {
e.printStackTrace();
}
apLocationOfflineDatabaseSingleRecord.clear();
}
bigchainAssetIdToMyAssetIdMapSize = bigchainAssetIdToMyAssetIdMap.size();
Log.i(TAG, "- digitalTwin - bigchainAssetIdToMyAssetIdMapSize - "+bigchainAssetIdToMyAssetIdMapSize);
try {
Log.i(TAG, " - digitalTwin - thread sleeping --- ");
// Thread.sleep((long)(Math.random() * 4000));
Thread.sleep(5000);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String bigchainAssetIdToMyAssetIdMapKeyHolder = "";
TransactionsApi apTransactionRetrieve = new TransactionsApi();
for(String bigchainAssetIdToMyAssetIdMapKey : bigchainAssetIdToMyAssetIdMap.keySet())
{
bigchainAssetIdToMyAssetIdMapKeyHolder = bigchainAssetIdToMyAssetIdMapKey;
Log.i(TAG, "- digitalTwin - bigchainAssetIdToMyAssetIdMapKeyHolder - "+bigchainAssetIdToMyAssetIdMapKeyHolder);
Transaction apTransaction = new Transaction();
try {
apTransaction = apTransactionRetrieve.getTransactionById(bigchainAssetIdToMyAssetIdMapKeyHolder);
} catch (IOException e) {
e.printStackTrace();
}
try {
Log.i(TAG, "- digitalTwin - bigchainAssetIdToMyAssetIdMapKeyHolder - "+apTransaction.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.android.commmanager.continuouslocationtrackingservice;
import java.util.ArrayList;
import java.util.List;
import com.android.commmanager.continuouslocationtrackingservice.bigchaindbConnection;
public class Orm {
String appId;
String appKey;
public List<OrmObject> models = new ArrayList<OrmObject>();
bigchaindbConnection connection;
public Orm(String connectionUrl, String appId, String appKey) {
this.connection = new bigchaindbConnection(connectionUrl, appId, appKey);
this.appId = appId;
this.appKey = appKey;
this.models = new ArrayList<OrmObject>();
}
public OrmObject define(String modelName, String modelSchema) {
OrmObject ormObjectHolder = new OrmObject(
modelName,
modelSchema,
this.connection,
this.appId,
null
);
this.models.add(new OrmObject(
modelName,
modelSchema,
this.connection,
this.appId,
null
));
return ormObjectHolder;
}
}
package com.android.commmanager.continuouslocationtrackingservice;
import android.util.Log;
import com.bigchaindb.api.AssetsApi;
import com.bigchaindb.builders.BigchainDbTransactionBuilder;
import com.bigchaindb.constants.Operations;
import com.bigchaindb.model.Assets;
import com.bigchaindb.model.FulFill;
import com.bigchaindb.model.MetaData;
import com.bigchaindb.model.Transaction;
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
import java.io.IOException;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static com.bigchaindb.api.TransactionsApi.getTransactionsByAssetId;
import static com.bigchaindb.util.KeyPairUtils.decodeKeyPair;
public class OrmObject {
final static String TAG = "OrmObject";
// The likelihood to generate a vanity address that is 11 times "Burn" is extremely low:
// - https://en.bitcoin.it/wiki/Vanitygen#Use_of_vanitygen_to_try_to_attack_addresses
static String BURN_ADDRESS = "BurnBurnBurnBurnBurnBurnBurnBurnBurnBurnBurn";
static String modelName = "";
static String modelSchema = "";
static String appId = "";
public static String id = "";
static EdDSAPublicKey publicKey;
static EdDSAPrivateKey privateKey;
static bigchaindbConnection connection;
public static List<Transaction> transactionHistory = new ArrayList<Transaction>();
public static List<Transaction> transactionList = new ArrayList<Transaction>();
static List<Object> metaData = new ArrayList<Object>();
public OrmObject(String modelName, String modelSchema, bigchaindbConnection connection, String appId, List<Transaction> transactionList) {
this.modelName = modelName;
this.modelSchema = modelSchema;
this.appId = appId;
OrmObject.connection = connection;
if(transactionList != null)
{
if (transactionList.size() > 0)
{
transactionHistory = transactionList;
this.id = transactionList.get(0).getAsset().getId();
for(int i = 0; i < transactionList.size(); ++i)
{
metaData.add(i, transactionList.get(i).getMetaData());
}
}
}
}
public static Assets retrieve(String searchKey) {
AssetsApi mAssetApi = new AssetsApi();
try {
return AssetsApi.getAssets(searchKey);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static OrmObject create(KeyPair keyPair, Object data) {
if (data == null || keyPair == null) {
Log.i(TAG, "inputs missing");
return null;
}
publicKey = (EdDSAPublicKey) keyPair.getPublic();
privateKey = (EdDSAPrivateKey) keyPair.getPrivate();
String[] assetPayload;
assetPayload = new String[]{modelSchema, appId};
Transaction transaction = new Transaction();
try {
transaction = BigchainDbTransactionBuilder
.init()
.addAssets(data, Object.class)
.addMetaData(assetPayload)
.operation(Operations.CREATE)
.buildAndSign((EdDSAPublicKey) keyPair.getPublic(), (EdDSAPrivateKey) keyPair.getPrivate())
// .buildAndSignOnly((EdDSAPublicKey) keyPair.getPublic(), (EdDSAPrivateKey) keyPair.getPrivate())
.sendTransaction();
} catch (IOException e) {
e.printStackTrace();
}
transactionList = new ArrayList<Transaction>();
transactionList.add(transaction);
int transactionListSize = transactionList.size();
Log.i(TAG, "- Create - transactionListSize - "+transactionListSize);
return new OrmObject(modelName, modelSchema, connection, appId, transactionList);
}
public static Transaction append(MetaData metaData) {
if (metaData == null) {
Log.i(TAG, "inputs missing");
return null;
}
String transactionId = transactionHistory.get(transactionHistory.size() - 1).getId();
FulFill fulfill = new FulFill();
//stil running 1.0
fulfill.setOutputIndex("0");
fulfill.setTransactionId(transactionId);
Transaction transactionUpdate = BigchainDbTransactionBuilder
.init()
.addInput(null, fulfill, publicKey)
.addOutput("1", publicKey)
.addMetaData(metaData)
.operation(Operations.TRANSFER)
.buildAndSignOnly(publicKey, privateKey);
// .sendTransaction();
return transactionUpdate;
}
public static Transaction burn(MetaData metadata) {
if (metaData == null) {
Log.i(TAG, "inputs missing");
return null;
}
String transactionId = transactionHistory.get(transactionHistory.size() - 1).getId();
FulFill fulfill = new FulFill();
//stil running 1.0
fulfill.setOutputIndex("0");
fulfill.setTransactionId(transactionId);
KeyPair burnKeyPair = decodeKeyPair(BURN_ADDRESS);
EdDSAPublicKey burnPublicKey = (EdDSAPublicKey) burnKeyPair.getPublic();
EdDSAPrivateKey burnPrivateKey = (EdDSAPrivateKey) burnKeyPair.getPrivate();
Transaction transactionUpdate = BigchainDbTransactionBuilder
.init()
.addInput(null, fulfill, publicKey)
.addOutput("1", burnPublicKey)
.addMetaData(metaData)
.operation(Operations.TRANSFER)
.buildAndSignOnly(publicKey, privateKey);
// .sendTransaction();
return transactionUpdate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment