pweb3
Version:
Pchain JavaScript API
206 lines (200 loc) • 7.71 kB
JavaScript
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn'));
var _get = _interopDefault(require('@babel/runtime/helpers/get'));
var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf'));
var _set = _interopDefault(require('@babel/runtime/helpers/set'));
var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
var pweb3Core = require('pweb3-core');
var pweb3Providers = require('pweb3-providers');
var Utils = require('pweb3-utils');
var pweb3Eth = require('pweb3-eth');
var pweb3Pi = require('pweb3-pi');
var pweb3Shh = require('pweb3-shh');
var pweb3Net = require('pweb3-net');
var pweb3EthPersonal = require('pweb3-eth-personal');
var pweb3Tdm = require('pweb3-tdm');
var pweb3Chain = require('pweb3-chain');
var pweb3Del = require('pweb3-del');
var version = "1.0.0-beta.62";
var CryptoJS = require('crypto-js');
var _sha = require('crypto-js/sha3');
var Web3 =
function (_AbstractWeb3Module) {
_inherits(Web3, _AbstractWeb3Module);
function Web3(provider, net) {
var _this;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, Web3);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Web3).call(this, provider, options, null, net));
_this.eth = new pweb3Eth.Eth(_this.currentProvider, net, options);
_this.shh = new pweb3Shh.Shh(_this.currentProvider, net, options);
_this.tdm = new pweb3Tdm.Tdm(_this.currentProvider, net, options);
_this.chain = new pweb3Chain.Chain(_this.currentProvider, net, options);
_this.del = new pweb3Del.Del(_this.currentProvider, net, options);
_this.pi = new pweb3Pi.Pi(_this.currentProvider, net, options);
_this.utils = Utils;
_this.version = version;
return _this;
}
_createClass(Web3, [{
key: "setProvider",
value: function setProvider(provider, net) {
return _get(_getPrototypeOf(Web3.prototype), "setProvider", this).call(this, provider, net) && this.eth.setProvider(provider, net) && this.shh.setProvider(provider, net);
}
}, {
key: "getVoteHash",
value: function getVoteHash(from, pubKey, amount, salt) {
if (pubKey.substr(0, 2) === '0x') pubKey = pubKey.substr(2);
if (typeof amount == 'string' && amount.substr(0, 2) === '0x') {
amount = amount.substr(2);
} else {
amount = amount.toString('16');
}
amount = amount.length % 2 == 0 ? amount : '0' + amount;
var saltCode = '';
for (var i = 0; i < salt.length; i++) {
saltCode += salt[i].charCodeAt().toString('16');
}
var concatString = from + pubKey + amount + saltCode;
return '16' + _sha(concatString.toLowerCase(), {
encoding: 'hex'
});
}
}, {
key: "sha3",
value: function sha3(value, options) {
if (options && options.encoding === 'hex') {
if (value.length > 2 && value.substr(0, 2) === '0x') {
value = value.substr(2);
}
value = CryptoJS.enc.Hex.parse(value);
}
return _sha(value, {
outputLength: 256
}).toString();
}
}, {
key: "accounts",
get: function get() {
return this.eth.accounts;
}
}, {
key: "defaultGasPrice",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "defaultGasPrice", value, this, true);
this.eth.defaultGasPrice = value;
this.shh.defaultGasPrice = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "defaultGasPrice", this);
}
}, {
key: "defaultGas",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "defaultGas", value, this, true);
this.eth.defaultGas = value;
this.shh.defaultGas = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "defaultGas", this);
}
}, {
key: "transactionBlockTimeout",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "transactionBlockTimeout", value, this, true);
this.eth.transactionBlockTimeout = value;
this.shh.transactionBlockTimeout = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "transactionBlockTimeout", this);
}
}, {
key: "transactionConfirmationBlocks",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "transactionConfirmationBlocks", value, this, true);
this.eth.transactionConfirmationBlocks = value;
this.shh.transactionConfirmationBlocks = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "transactionConfirmationBlocks", this);
}
}, {
key: "transactionPollingTimeout",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "transactionPollingTimeout", value, this, true);
this.eth.transactionPollingTimeout = value;
this.shh.transactionPollingTimeout = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "transactionPollingTimeout", this);
}
}, {
key: "defaultAccount",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "defaultAccount", value, this, true);
this.eth.defaultAccount = value;
this.shh.defaultAccount = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "defaultAccount", this);
}
}, {
key: "defaultBlock",
set: function set(value) {
_set(_getPrototypeOf(Web3.prototype), "defaultBlock", value, this, true);
this.eth.defaultBlock = value;
this.shh.defaultBlock = value;
}
,
get: function get() {
return _get(_getPrototypeOf(Web3.prototype), "defaultBlock", this);
}
}], [{
key: "givenProvider",
get: function get() {
return pweb3Providers.ProviderDetector.detect();
}
}, {
key: "modules",
get: function get() {
var providerResolver = new pweb3Providers.ProvidersModuleFactory().createProviderResolver();
return {
Eth: function Eth(provider, options, net) {
return new pweb3Eth.Eth(providerResolver.resolve(provider, net), options);
},
Net: function Net(provider, options, net) {
return new pweb3Net.Network(providerResolver.resolve(provider, net), options);
},
Personal: function Personal(provider, options, net) {
return new pweb3EthPersonal.Personal(providerResolver.resolve(provider, net), options);
},
Shh: function Shh(provider, options, net) {
return new pweb3Shh.Shh(providerResolver.resolve(provider, net), options);
},
Tdm: function Tdm(provider, options, net) {
return new pweb3Tdm.Tdm(providerResolver.resolve(provider, net), options);
},
Chain: function Chain(provider, options, net) {
return new pweb3Chain.Chain(providerResolver.resolve(provider, net), options);
},
Del: function Del(provider, options, net) {
return new pweb3Del.Del(providerResolver.resolve(provider, net), options);
},
Pi: function Pi(provider, options, net) {
return new pweb3Pi.Pi(providerResolver.resolve(provider, net), options);
}
};
}
}]);
return Web3;
}(pweb3Core.AbstractWeb3Module);
module.exports = Web3;