Last active
February 6, 2019 22:34
-
-
Save clockworkgr/cc12861a5dc1a113777ad77f956293ab to your computer and use it in GitHub Desktop.
Beet-JS wrappers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const OTPAuth = require('otpauth'); | |
import CryptoJS from "crypto-js"; | |
import browser from 'browser-detect'; | |
import BeetClientDB from './lib/BeetClientDB'; | |
import "isomorphic-fetch"; | |
import { ec as EC } from "elliptic"; | |
var ec = new EC('curve25519'); | |
class BeetJS{ | |
static _beetAppInstances; | |
constructor() { | |
this._beetAppInstances = {}; | |
} | |
async get(appName) { | |
if (this._beetAppInstances[appName]) { | |
return this._beetAppInstances[appName]; | |
}else{ | |
let appInstance=new BeetApp(appName); | |
await appInstance.init(); | |
this._beetAppInstances[appName]=appInstance; | |
return this._beetAppInstances[appName]; | |
} | |
} | |
} | |
class BeetApp{ | |
static _beetConnections; | |
constructor(appName){ | |
this.appName = appName; | |
this.origin = appName; // FIXME put in actual origin | |
if (typeof location !== 'undefined') { | |
if (location.hasOwnProperty('hostname') && location.hostname.length && location.hostname !== 'localhost') { | |
this.origin = location.hostname; | |
} | |
} | |
this.detected = browser(); | |
this.keyhash = CryptoJS.SHA256(this.detected.name + ' ' + this.origin + ' ' + this.appName).toString(); | |
} | |
async init() { | |
this.appstore = await BeetClientDB.apps.where("keyhash").equals(this.keyhash).toArray(); | |
this._beetConnections={}; | |
} | |
list() { | |
return this.appstore; | |
} | |
async getConnection(identity) { | |
if (this._beetConnections[identity.apphash]) { | |
return this._beetConnections[identity.apphash]; | |
}else{ | |
let beetConnection = new BeetConnection(this.appName); | |
try { | |
beetConnection.connect(identity); | |
this._beetConnections[identity.apphash]=beetConnection; | |
return this._beetConnections[identity.apphash]; | |
}catch(err) { | |
throw new Error (err); | |
// TODO: if linking error, re-link transparently instead | |
} | |
} | |
} | |
async getChainConnection(chainType, existing=true) { | |
if (existing) { | |
let compatibleIdentities = this.appstore.filter( id => { return id.chain==chainType }); | |
if (compatibleIdentities.length>0) { | |
try { | |
let beetConnection = this.getConnection(compatibleIdentities[0]); | |
return beetConnection; | |
}catch(err) { | |
return this.getChainConnection(chainType,false); | |
} | |
} | |
}else{ | |
let beetConnection = new BeetConnection(this.appName); | |
try { | |
let beetAccount = await beetConnection.link(chainType); | |
// Create and store new apphash here | |
this._beetConnections[apphash] = beetConnection; | |
return this._beetConnections[apphash]; | |
}catch(err) { | |
throw new Error(err); | |
} | |
} | |
} | |
async getAnyConnection(existing=true) { | |
if (existing) { | |
if (this.appstore.length>0) { | |
try { | |
let beetConnection = this.getConnection(this.appstore[0]); | |
return beetConnection; | |
}catch(err) { | |
return this.getAnyConnection(false); | |
} | |
} | |
}else{ | |
let beetConnection = new BeetConnection(this.appName); | |
try { | |
let beetAccount = await beetConnection.link(); // Need to modify link to allow for no chain preference | |
// Create and store new apphash here | |
this._beetConnections[apphash] = beetConnection; | |
return this._beetConnections[apphash]; | |
}catch(err) { | |
throw new Error(err); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment