2018-09-01 01:16:34 +00:00
|
|
|
export const Contracts = {
|
|
|
|
'Identity': 'IdentityGasRelay',
|
|
|
|
'SNT': 'SNTController'
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Functions = {
|
|
|
|
'Identity': {
|
|
|
|
'call': 'callGasRelayed',
|
|
|
|
'approveAndCall': 'approveAndCallGasRelayed'
|
|
|
|
}
|
2018-09-01 04:45:28 +00:00
|
|
|
};
|
2018-09-01 01:16:34 +00:00
|
|
|
|
|
|
|
export const Actions = {
|
|
|
|
'Availability': 'availability',
|
|
|
|
'Transaction': 'transaction'
|
2018-09-01 04:45:28 +00:00
|
|
|
};
|
2018-09-01 01:16:34 +00:00
|
|
|
|
|
|
|
const relayerSymmmetricKeyID = "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b";
|
|
|
|
|
|
|
|
class StatusGasRelayer {
|
|
|
|
constructor(build, web3) {
|
2018-09-01 11:23:24 +00:00
|
|
|
if (arguments.length !== 2 || !this.validateBuild(build)) throw new Error("Invalid build");
|
|
|
|
|
2018-09-01 01:16:34 +00:00
|
|
|
this.web3 = web3;
|
2018-09-01 11:23:24 +00:00
|
|
|
|
|
|
|
Object.defineProperties(this, {
|
|
|
|
message: {
|
|
|
|
value: build._getMessage(web3),
|
|
|
|
writable: false
|
|
|
|
},
|
|
|
|
topic: {
|
|
|
|
value: web3.utils.toHex(build.contractName).slice(0, 10),
|
|
|
|
writable: false
|
|
|
|
},
|
|
|
|
kid: {
|
|
|
|
value: build.kid,
|
|
|
|
writable: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(build.operation === Actions.Transaction){
|
2018-09-01 01:16:34 +00:00
|
|
|
Object.defineProperties(this, {
|
2018-09-01 11:23:24 +00:00
|
|
|
pubKey: {
|
|
|
|
value: build.relayer,
|
2018-09-01 01:16:34 +00:00
|
|
|
writable: false
|
2018-09-01 11:23:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Object.defineProperties(this, {
|
2018-09-01 01:16:34 +00:00
|
|
|
skid: {
|
|
|
|
value: build.skid,
|
|
|
|
writable: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
post = async (options) => {
|
|
|
|
options = options || {};
|
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
let pubKey = options.pubKey || this.pubKey;
|
2018-09-01 01:16:34 +00:00
|
|
|
let skid = options.skid || this.skid;
|
|
|
|
|
|
|
|
let kid = options.kid || this.kid;
|
|
|
|
if(!kid){
|
|
|
|
kid = await this.web3.shh.newKeyPair();
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendOptions = {
|
|
|
|
ttl: options.ttl || 1000,
|
|
|
|
sig: kid,
|
|
|
|
powTarget: options.powTarget || 1,
|
|
|
|
powTime: options.powTime || 20,
|
|
|
|
topic: this.topic,
|
|
|
|
payload: this.web3.utils.toHex(this.message)
|
|
|
|
};
|
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
if(pubKey){
|
|
|
|
sendOptions.pubKey = pubKey;
|
|
|
|
} else {
|
|
|
|
if(!skid){
|
|
|
|
skid = await this.web3.shh.addSymKey(relayerSymmmetricKeyID);
|
|
|
|
}
|
|
|
|
sendOptions.symKeyID = skid;
|
|
|
|
}
|
|
|
|
|
2018-09-01 04:45:28 +00:00
|
|
|
const msgId = await this.web3.shh.post(sendOptions);
|
|
|
|
return msgId;
|
2018-09-01 01:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validateBuild = (build) => {
|
2018-09-01 04:45:28 +00:00
|
|
|
return (String(build.constructor) === String(StatusGasRelayer.AvailableRelayers) ||
|
|
|
|
String(build.constructor) === String(StatusGasRelayer.Identity)
|
|
|
|
);
|
2018-09-01 01:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get AvailableRelayers() {
|
|
|
|
return AvailableRelayersAction;
|
|
|
|
}
|
2018-09-01 04:45:28 +00:00
|
|
|
|
|
|
|
static get Identity() {
|
|
|
|
return IdentityGasRelayedAction;
|
|
|
|
}
|
2018-09-01 01:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Action {
|
|
|
|
setGas(token, price, limit){
|
|
|
|
this.gasToken = token;
|
|
|
|
this.gasPrice = price;
|
|
|
|
this.gasLimit = limit;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
setOperation(operation){
|
|
|
|
this.operation = operation;
|
|
|
|
return this;
|
|
|
|
}
|
2018-09-01 03:22:01 +00:00
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
setRelayer(relayer){
|
|
|
|
this.relayer = relayer;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-01 03:22:01 +00:00
|
|
|
setRelayersSymKeyID = (skid) => {
|
|
|
|
this.skid = skid;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAsymmetricKeyID = (kid) => {
|
|
|
|
this.kid = kid;
|
|
|
|
return this;
|
|
|
|
}
|
2018-09-01 01:16:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 03:22:01 +00:00
|
|
|
class IdentityGasRelayedAction extends Action {
|
|
|
|
constructor(contractAddress, accountAddress) {
|
2018-09-01 01:16:34 +00:00
|
|
|
super();
|
2018-09-01 03:22:01 +00:00
|
|
|
this.contractName = Contracts.Identity;
|
2018-09-01 01:16:34 +00:00
|
|
|
this.contractAddress = contractAddress;
|
|
|
|
this.accountAddress = accountAddress;
|
|
|
|
|
2018-09-01 03:22:01 +00:00
|
|
|
this.operation = Actions.Transaction;
|
2018-09-01 01:16:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-01 11:42:44 +00:00
|
|
|
setContractFunction = contractFunction => {
|
|
|
|
this.contractFunction = contractFunction;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-01 03:22:01 +00:00
|
|
|
setTransaction = (to, value, data) => {
|
|
|
|
this.to = to;
|
|
|
|
this.value = value;
|
|
|
|
this.data = data;
|
|
|
|
|
2018-09-01 01:16:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-01 11:42:44 +00:00
|
|
|
setBaseToken = baseToken => {
|
|
|
|
this.baseToken = baseToken;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-01 04:45:28 +00:00
|
|
|
_nonce = async(contract) => {
|
2018-09-01 03:22:01 +00:00
|
|
|
const nonce = await contract.methods.nonce().call();
|
|
|
|
return nonce;
|
|
|
|
}
|
|
|
|
|
2018-09-01 04:45:28 +00:00
|
|
|
getNonce = async (web3) => {
|
|
|
|
const contract = new web3.eth.Contract(identityGasRelayABI, this.contractAddress);
|
|
|
|
const nonce = await this._nonce(contract);
|
|
|
|
return nonce;
|
|
|
|
}
|
2018-09-01 03:22:01 +00:00
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
sign = async web3 => {
|
2018-09-01 04:45:28 +00:00
|
|
|
const contract = new web3.eth.Contract(identityGasRelayABI, this.contractAddress);
|
|
|
|
const nonce = await this._nonce(contract);
|
2018-09-01 11:42:44 +00:00
|
|
|
let hashedMessage;
|
|
|
|
|
|
|
|
switch(this.contractFunction){
|
|
|
|
case Functions.Identity.call:
|
|
|
|
hashedMessage = await contract.methods.callGasRelayHash(
|
|
|
|
this.to,
|
|
|
|
this.value,
|
|
|
|
web3.utils.soliditySha3({t: 'bytes', v: this.data}),
|
|
|
|
nonce,
|
|
|
|
this.gasPrice,
|
|
|
|
this.gasLimit,
|
|
|
|
this.gasToken
|
|
|
|
).call();
|
|
|
|
break;
|
|
|
|
case Functions.Identity.approveAndCall:
|
|
|
|
hashedMessage = await contract.methods.deployGasRelayHash(
|
|
|
|
this.value,
|
|
|
|
web3.utils.soliditySha3({t: 'bytes', v: this.data}),
|
|
|
|
nonce,
|
|
|
|
this.gasPrice,
|
|
|
|
this.gasLimit,
|
|
|
|
this.gasToken
|
|
|
|
).call();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Function not allowed");
|
|
|
|
}
|
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
const signedMessage = await web3.eth.sign(hashedMessage, this.accountAddress);
|
2018-09-01 04:45:28 +00:00
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
return signedMessage;
|
2018-09-01 03:22:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
post = async (signature, web3, options) => {
|
|
|
|
this.nonce = await this.getNonce(web3);
|
|
|
|
this.signature = signature;
|
|
|
|
|
|
|
|
const s = new StatusGasRelayer(this, web3);
|
|
|
|
return s.post(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getMessage = web3 => {
|
2018-09-01 11:42:44 +00:00
|
|
|
let jsonAbi = identityGasRelayABI.find(x => x.name == this.contractFunction);
|
|
|
|
let funCall;
|
|
|
|
|
|
|
|
switch(this.contractFunction){
|
|
|
|
case Functions.Identity.call:
|
|
|
|
funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
|
|
|
|
this.to,
|
|
|
|
this.value,
|
|
|
|
this.data,
|
|
|
|
this.nonce,
|
|
|
|
this.gasPrice,
|
|
|
|
this.gasLimit,
|
|
|
|
this.gasToken,
|
|
|
|
this.signature
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
case Functions.Identity.approveAndCall:
|
|
|
|
funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [
|
|
|
|
this.baseToken,
|
|
|
|
this.to,
|
|
|
|
this.value,
|
|
|
|
this.data,
|
|
|
|
this.nonce,
|
|
|
|
this.gasPrice,
|
|
|
|
this.gasLimit,
|
|
|
|
this.gasToken,
|
|
|
|
this.signature
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Function not allowed");
|
|
|
|
}
|
2018-09-01 03:22:01 +00:00
|
|
|
|
2018-09-01 11:23:24 +00:00
|
|
|
return {
|
|
|
|
'contract': this.contractAddress,
|
|
|
|
'address': this.accountAddress,
|
|
|
|
'action': Actions.Transaction,
|
|
|
|
'encodedFunctionCall': funCall
|
2018-09-01 03:22:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AvailableRelayersAction extends Action {
|
|
|
|
constructor(contractName, contractAddress, accountAddress) {
|
|
|
|
super();
|
|
|
|
this.contractName = contractName;
|
|
|
|
this.contractAddress = contractAddress;
|
|
|
|
this.accountAddress = accountAddress;
|
|
|
|
|
|
|
|
this.operation = Actions.Availability;
|
|
|
|
|
2018-09-01 01:16:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-01 03:22:01 +00:00
|
|
|
_getMessage = web3 => {
|
2018-09-01 01:16:34 +00:00
|
|
|
return {
|
|
|
|
contract: this.contractAddress,
|
|
|
|
address: this.accountAddress || web3.eth.defaultAccount,
|
|
|
|
action: Actions.Availability,
|
|
|
|
gasToken: this.gasToken,
|
|
|
|
gasPrice: this.gasPrice
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
post(web3, options) {
|
|
|
|
const s = new StatusGasRelayer(this, web3);
|
|
|
|
return s.post(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 03:22:01 +00:00
|
|
|
const identityGasRelayABI = [
|
|
|
|
{
|
|
|
|
"constant": true,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "_to",
|
|
|
|
"type": "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_value",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_dataHash",
|
|
|
|
"type": "bytes32"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_nonce",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasPrice",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasLimit",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasToken",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "callGasRelayHash",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "_callGasRelayHash",
|
|
|
|
"type": "bytes32"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"payable": false,
|
|
|
|
"stateMutability": "view",
|
|
|
|
"type": "function",
|
|
|
|
"signature": "0xe27e2e5c"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": true,
|
|
|
|
"inputs": [],
|
|
|
|
"name": "nonce",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"payable": false,
|
|
|
|
"stateMutability": "view",
|
|
|
|
"type": "function",
|
|
|
|
"signature": "0xaffed0e0"
|
2018-09-01 11:23:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": false,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "_to",
|
|
|
|
"type": "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_value",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_data",
|
|
|
|
"type": "bytes"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_nonce",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasPrice",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasLimit",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasToken",
|
|
|
|
"type": "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_messageSignatures",
|
|
|
|
"type": "bytes"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "callGasRelayed",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "success",
|
|
|
|
"type": "bool"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"payable": false,
|
|
|
|
"stateMutability": "nonpayable",
|
|
|
|
"type": "function",
|
|
|
|
"signature": "0xfd0dded5"
|
2018-09-01 11:42:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": false,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "_baseToken",
|
|
|
|
"type": "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_to",
|
|
|
|
"type": "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_value",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_data",
|
|
|
|
"type": "bytes"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_nonce",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasPrice",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasLimit",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasToken",
|
|
|
|
"type": "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_messageSignatures",
|
|
|
|
"type": "bytes"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "approveAndCallGasRelayed",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "success",
|
|
|
|
"type": "bool"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"payable": false,
|
|
|
|
"stateMutability": "nonpayable",
|
|
|
|
"type": "function",
|
|
|
|
"signature": "0x59f4ac61"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"constant": true,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "_value",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_dataHash",
|
|
|
|
"type": "bytes32"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_nonce",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasPrice",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasLimit",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "_gasToken",
|
|
|
|
"type": "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"name": "deployGasRelayHash",
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "_callGasRelayHash",
|
|
|
|
"type": "bytes32"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"payable": false,
|
|
|
|
"stateMutability": "view",
|
|
|
|
"type": "function",
|
|
|
|
"signature": "0x86962d85"
|
2018-09-01 11:23:24 +00:00
|
|
|
}
|
2018-09-01 03:22:01 +00:00
|
|
|
];
|
|
|
|
|
2018-09-01 04:45:28 +00:00
|
|
|
export default StatusGasRelayer;
|