-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
282 additions
and
57 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
export const Blockchains = { | ||
EOS:'EOS', | ||
ETH:'ETH' | ||
EOS:'eos', | ||
ETH:'eth' | ||
}; |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
export default class Plugin { | ||
|
||
constructor(_name = '', _type = ''){ | ||
this.name = _name; | ||
this.type = _type; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as PluginTypes from './PluginTypes'; | ||
import EOS from './defaults/eos'; | ||
|
||
/*** | ||
* Setting up for plugin based generators, | ||
* this will add more blockchain compatibility in the future. | ||
*/ | ||
|
||
class PluginRepositorySingleton { | ||
|
||
constructor(){ | ||
this.plugins = []; | ||
|
||
this.loadPlugins(); | ||
} | ||
|
||
loadPlugins(){ | ||
this.plugins.push(new EOS()); | ||
|
||
// TODO: Get from store repo | ||
} | ||
|
||
signatureProviders(){ | ||
return this.plugins.filter(plugin => plugin.type === PluginTypes.BLOCKCHAIN_SUPPORT); | ||
} | ||
|
||
findPlugin(name){ | ||
return this.plugins.find(plugin => plugin.name === name); | ||
} | ||
|
||
} | ||
|
||
const PluginRepository = new PluginRepositorySingleton(); | ||
export default PluginRepository; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const BLOCKCHAIN_SUPPORT = 'blockchain_support'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import Plugin from '../Plugin'; | ||
import * as PluginTypes from '../PluginTypes'; | ||
import {Blockchains} from '../../models/Blockchains' | ||
import * as NetworkMessageTypes from '../../messages/NetworkMessageTypes' | ||
const ecc = require('eosjs-ecc'); | ||
|
||
let networkBinder = new WeakMap(); | ||
let internalMessageSender = new WeakMap(); | ||
let throwIfNoIdentity = new WeakMap(); | ||
|
||
const proxy = (dummy, handler) => new Proxy(dummy, handler); | ||
|
||
export default class EOS extends Plugin { | ||
|
||
constructor(){ | ||
super(Blockchains.EOS, PluginTypes.BLOCKCHAIN_SUPPORT) | ||
} | ||
|
||
accountFormatter(account){ | ||
return `${account.name}@${account.authority}` | ||
} | ||
|
||
signer(bgContext, payload, publicKey, callback){ | ||
const buf = Buffer.from(payload.buf.data, 'utf8'); | ||
bgContext.publicToPrivate(privateKey => { | ||
callback(privateKey ? ecc.sign(buf, privateKey) : null); | ||
}, publicKey); | ||
} | ||
|
||
signatureProvider(...args){ | ||
|
||
networkBinder = args[0]; | ||
internalMessageSender = args[1]; | ||
throwIfNoIdentity = args[2]; | ||
|
||
return (_eos, network, _options = {}) => { | ||
|
||
networkBinder(network); | ||
const httpEndpoint = `http://${network.host}:${network.port}`; | ||
|
||
// The proxy stands between the eosjs object and scatter. | ||
// This is used to add special functionality like adding `requiredFields` arrays to transactions | ||
return proxy(_eos(), { | ||
get(eosInstance, method) { | ||
|
||
let returnedFields = null; | ||
|
||
return (...args) => { | ||
|
||
if(args.find(arg => arg.hasOwnProperty('keyProvider'))) throw Error.usedKeyProvider(); | ||
|
||
let requiredFields = args.find(arg => arg.hasOwnProperty('requiredFields')); | ||
requiredFields = requiredFields ? requiredFields.requiredFields : []; | ||
|
||
// The signature provider which gets elevated into the user's Scatter | ||
const signProvider = async signargs => { | ||
throwIfNoIdentity(); | ||
|
||
// Friendly formatting | ||
signargs.messages = await messagesBuilder(_eos, signargs, httpEndpoint, args[0]); | ||
|
||
const payload = Object.assign(signargs, { domain:location.host, network, requiredFields }); | ||
const result = await internalMessageSender(NetworkMessageTypes.REQUEST_SIGNATURE, payload); | ||
|
||
// No signature | ||
if(!result) return null; | ||
|
||
if(result.hasOwnProperty('signatures')){ | ||
// Holding onto the returned fields for the final result | ||
returnedFields = result.returnedFields; | ||
|
||
// Grabbing buf signatures from local multi sig sign provider | ||
let multiSigKeyProvider = args.find(arg => arg.hasOwnProperty('signProvider')); | ||
if(multiSigKeyProvider){ | ||
result.signatures.push(multiSigKeyProvider.signProvider(signargs.buf, signargs.sign)); | ||
} | ||
|
||
// Returning only the signatures to eosjs | ||
return result.signatures; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// TODO: We need to check about the implications of multiple eosjs instances | ||
return new Promise((resolve, reject) => { | ||
_eos(Object.assign(JSON.stringify(_options), {httpEndpoint, signProvider}))[method](...args) | ||
.then(result => { | ||
|
||
// Standard method ( ie. not contract ) | ||
if(!result.hasOwnProperty('fc')){ | ||
result = Object.assign(result, {returnedFields}); | ||
resolve(result); | ||
return; | ||
} | ||
|
||
// Catching chained promise methods ( contract .then action ) | ||
const contractProxy = proxy(result, { | ||
get(instance,method){ | ||
if(method === 'then') return instance[method]; | ||
return (...args) => { | ||
return new Promise(async (res, rej) => { | ||
instance[method](...args).then(actionResult => { | ||
res(Object.assign(actionResult, {returnedFields})); | ||
}).catch(rej); | ||
}) | ||
|
||
} | ||
} | ||
}); | ||
|
||
resolve(contractProxy); | ||
}).catch(error => reject(error)) | ||
}) | ||
} | ||
} | ||
}); // Proxy | ||
} | ||
} | ||
} | ||
|
||
|
||
const messagesBuilder = async (_eos, signargs, httpEndpoint, contractName) => await Promise.all(signargs.transaction.actions.map(async action => { | ||
let data = null; | ||
|
||
const eos = _eos({httpEndpoint}); | ||
if(action.account === 'eosio') data = eos.fc.fromBuffer(action.name, action.data); | ||
else { | ||
const abi = await eos.contract(contractName); | ||
data = abi.fc.fromBuffer(action.name, action.data); | ||
} | ||
|
||
return { | ||
data, | ||
code:action.account, | ||
type:action.name, | ||
authorization:action.authorization | ||
}; | ||
})); |
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
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
Oops, something went wrong.