Skip to content

Commit

Permalink
Merge pull request #19 from rsksmart/revert-to-last-version
Browse files Browse the repository at this point in the history
Revert to last version
  • Loading branch information
ilanolkies authored Aug 22, 2022
2 parents c283371 + 24e4c06 commit db52b61
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 559 deletions.
9 changes: 3 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions dist/lib/Constants.js

This file was deleted.

15 changes: 1 addition & 14 deletions dist/lib/ContractParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ var _Contract = _interopRequireDefault(require("./Contract"));
var _EventDecoder = _interopRequireDefault(require("./EventDecoder"));
var _Abi = _interopRequireDefault(require("./Abi"));
var _types = require("./types");
var _utils = require("./utils");
var _utils = require("./utils");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}






var _strings = require("@rsksmart/rsk-utils/dist/strings");
var _Constants = require("./Constants");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

class ContractParser {
constructor({ abi, log, initConfig, nod3 } = {}) {
Expand Down Expand Up @@ -162,24 +160,13 @@ class ContractParser {
}
let interfaces;
if (isErc165) interfaces = await this.getInterfacesERC165(contract);else
if (this.isEIP1167(txInputData)) interfaces = { EIP1167: true };else
interfaces = this.getInterfacesByMethods(methods);
interfaces = Object.keys(interfaces).
filter(k => interfaces[k] === true).
map(t => _types.contractsInterfaces[t] || t);
return { methods, interfaces };
}

getEip1167MasterCopy(bytecode) {
const implementationAddress = bytecode.replace(_Constants.EIP_1167_PREFIX, '').replace(_Constants.EIP_1167_SUFFIX, '');
return implementationAddress;
}

isEIP1167(bytecode) {
const re = new RegExp(`^${_Constants.EIP_1167_PREFIX}[a-f0-9]{40}${_Constants.EIP_1167_SUFFIX}$`, 'i');
return re.test((0, _strings.remove0x)(bytecode));
}

async getInterfacesERC165(contract) {
let ifaces = {};
let keys = Object.keys(_interfacesIds.default);
Expand Down
42 changes: 12 additions & 30 deletions dist/lib/EventDecoder.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _utils = require("./utils");
var _rskUtils = require("@rsksmart/rsk-utils");
var _web3EthAbi = _interopRequireDefault(require("web3-eth-abi"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _ethereumjsAbi = _interopRequireDefault(require("ethereumjs-abi"));
var _utils = require("./utils");
var _rskUtils = require("@rsksmart/rsk-utils");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

function EventDecoder(abi) {
abi = (0, _utils.addSignatureDataToAbi)(abi);

const rawDecode = (types, data) => {
const decoded = _web3EthAbi.default.decodeParameters(types, data);
delete decoded['__length__'];
const arrDecoded = Object.keys(decoded).map(key => decoded[key]);
return arrDecoded;
};

const formatDecoded = decoded => {
return (0, _rskUtils.add0x)(Buffer.isBuffer(decoded) ? (0, _rskUtils.bufferToHex)(decoded) : decoded.toString(16));
};
Expand All @@ -29,31 +22,20 @@ function EventDecoder(abi) {
};

const decodeElement = (data, types) => {
try {
let decoded = rawDecode(types, data);
if (Array.isArray(decoded)) {
decoded = decoded.map(d => formatDecoded(d));
if (decoded.length === 1) decoded = decoded.join();
} else {
decoded = formatDecoded(decoded);
}
return decoded;
} catch (e) {
console.log(e);
return '';
let decoded = _ethereumjsAbi.default.rawDecode(types, (0, _rskUtils.toBuffer)(data));
if (Array.isArray(decoded)) {
decoded = decoded.map(d => formatDecoded(d));
if (decoded.length === 1) decoded = decoded.join();
} else {
decoded = formatDecoded(decoded);
}
return decoded;
};

const decodeData = (data, types) => {
try {
let decoded = rawDecode(types, data);
return decoded.map(d => formatDecoded(d));
} catch (e) {
console.log(e);
return [''];
}
let decoded = _ethereumjsAbi.default.rawDecode(types, (0, _rskUtils.toBuffer)(data));
return decoded.map(d => formatDecoded(d));
};

const decodeLog = log => {
log = Object.assign({}, log);
const { eventABI, topics } = getEventAbi(log.topics);
Expand Down
39 changes: 39 additions & 0 deletions dist/lib/NativeContracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.NativeContracts = NativeContracts;exports.default = exports.parseNativeContracts = exports.defaultNativeContracts = void 0;
var _rskUtils = require("rsk-utils");

const defaultNativeContracts = {
bridge: '0x0000000000000000000000000000000001000006',
remasc: '0x0000000000000000000000000000000001000008' };exports.defaultNativeContracts = defaultNativeContracts;


const parseNativeContracts = nativeContracts => {
if (typeof nativeContracts !== 'object') throw new TypeError(`nativeContracts must be an object`);
if (Object.keys(nativeContracts) < 1) throw new Error(`Empty native contracts list`);
for (let name in nativeContracts) {
let address = nativeContracts[name];
if (!(0, _rskUtils.isAddress)(address)) throw new Error(`Address of ${name} is not an address`);
nativeContracts[name] = address.toLowerCase();
}
return nativeContracts;
};exports.parseNativeContracts = parseNativeContracts;

function NativeContracts({ nativeContracts } = {}) {
nativeContracts = parseNativeContracts(nativeContracts || defaultNativeContracts);
const names = Object.keys(nativeContracts);

const getNativeContractAddress = contractName => {
return nativeContracts[contractName];
};
const getNativeContractName = address => {
address = address.toLowerCase();
return names.find(name => nativeContracts[name] === address);
};

const isNativeContract = address => !!getNativeContractName(address);

const list = () => nativeContracts;

return Object.freeze({ getNativeContractAddress, getNativeContractName, isNativeContract, list });
}var _default =

NativeContracts;exports.default = _default;
213 changes: 213 additions & 0 deletions dist/lib/NativeContractsEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.NativeContractsEvents = NativeContractsEvents;exports.default = void 0;var _rskUtils = require("rsk-utils");
var btcUtils = _interopRequireWildcard(require("./btcUtils"));
var _utils = require("./utils");function _getRequireWildcardCache() {if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function () {return cache;};return cache;}function _interopRequireWildcard(obj) {if (obj && obj.__esModule) {return obj;}if (obj === null || typeof obj !== "object" && typeof obj !== "function") {return { default: obj };}var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {return cache.get(obj);}var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) {var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {Object.defineProperty(newObj, key, desc);} else {newObj[key] = obj[key];}}}newObj.default = obj;if (cache) {cache.set(obj, newObj);}return newObj;}

function NativeContractsEvents({ bitcoinNetwork } = {}) {
const network = bitcoinNetwork || 'testnet';
const decodeAddress = address => {
address = Buffer.from((0, _rskUtils.remove0x)(address), 'hex');
return (0, _rskUtils.add0x)(address.toString('hex').slice(-40));
};

const decodeEventName = name => {
return Buffer.from((0, _rskUtils.remove0x)(name), 'hex').toString('ascii').replace(/\0/g, '');
};

const removeEmptyStartBytes = d => {
d = !Buffer.isBuffer(d) ? Buffer.from(d, 'hex') : d;
return d.slice(d.findIndex(x => x > 0));
};

const decodeData = data => {
let decoded = _rskUtils.rlp.decode(data);
if (!Array.isArray(decoded)) decoded = [decoded];
return decoded.map(d => (0, _rskUtils.add0x)(removeEmptyStartBytes(d).toString('hex')));
};

const decodeBtcTxHash = data => {
if ((0, _rskUtils.remove0x)(data).length === 128) {
let buffer = Buffer.from((0, _rskUtils.remove0x)(data), 'hex');
data = (0, _rskUtils.add0x)(buffer.toString('ascii'));
}
return data;
};
const decodeArray = data => data.map(d => Array.isArray(d) ? decodeArray(d) : (0, _rskUtils.add0x)(d.toString('hex')));

const decodeFederationData = data => {
let [a160, keys] = data;
let address = btcUtils.h160toAddress(a160, { prefixKey: 'scriptHash', network }).toString('hex');
keys = keys.map(d => btcUtils.rskAddressFromBtcPublicKey(d.toString('hex')));
return [address, keys];
};

const commitFederationDecoder = data => {
const decoded = _rskUtils.rlp.decode(data);
let [oldData, newData, block] = decoded;
let [oldFederationAddress, oldFederationMembers] = decodeFederationData(oldData);
let [newFederationAddress, newFederationMembers] = decodeFederationData(newData);
block = block.toString('ascii');
return [oldFederationAddress, oldFederationMembers, newFederationAddress, newFederationMembers, block];
};
const fakeAbi = Object.freeze((0, _utils.addSignatureDataToAbi)([
{ // Remasc events
anonymous: false,
inputs: [
{
indexed: true,
name: 'to',
type: 'address' },

{
indexed: false,
name: 'blockHash',
type: 'string' },

{
indexed: false,
name: 'value',
type: 'uint256' }],


name: 'mining_fee_topic',
type: 'event' },

{ // Bridge events
inputs: [
{
indexed: false,
name: 'btcTxHash',
type: 'string' },

{
indexed: false,
name: 'btcTx', // raw tx?
type: 'string' }],


name: 'release_btc_topic',
type: 'event' },

{
inputs: [
{
indexed: false,
name: 'sender',
type: 'address' }],


name: 'update_collections_topic',
type: 'event' },

{
inputs: [
{
indexed: false,
name: 'btcTxHash',
type: 'string',
_filter: decodeBtcTxHash },

{
indexed: false,
name: 'federatorPublicKey',
type: 'string' },

{
indexed: false,
name: 'rskTxHash',
type: 'string' }],


name: 'add_signature_topic',
type: 'event' },

{
inputs: [
{
indexed: false,
name: 'oldFederationAddress',
type: 'string' },

{
indexed: false,
name: 'oldFederationMembers',
type: 'address[]' },

{
indexed: false,
name: 'newFederationAddress',
type: 'string' },

{
indexed: false,
name: 'newFederationMembers',
type: 'address[]' },

{
indexed: false,
name: 'activationBlockNumber',
type: 'string' }],


name: 'commit_federation_topic',
type: 'event',
_decoder: commitFederationDecoder }]));



const getEventAbi = eventName => fakeAbi.find(a => a.name === eventName && a.type === 'event');

const decodeByType = (type, value) => {
if (type === 'address') return decodeAddress(value);
return value;
};

const decodeInput = (input, value) => {
let { type, _filter } = input;
if (_filter && typeof _filter === 'function') {
value = _filter(value);
}
return decodeByType(type, value);
};

const removeCustomProperties = obj => {
const res = Object.assign({}, obj);
for (let p in res) {
if (p[0] === '_') delete res[p];
}
return res;
};

const cleanAbi = abi => {
abi = removeCustomProperties(abi);
let { inputs } = abi;
if (Array.isArray(inputs)) abi.inputs = inputs.map(input => removeCustomProperties(input));
return abi;
};

const decodeLog = log => {
let topics = [...log.topics];
let event = decodeEventName(topics.shift());
let abi = getEventAbi(event);
if (event && abi) {
const { signature } = (0, _utils.getSignatureDataFromAbi)(abi);
log.event = event;
log.signature = signature;
log.abi = cleanAbi(abi);
log.args = [];
const decoder = abi._decoder || decodeData;
let dataDecoded = decoder(log.data);
if (!Array.isArray(dataDecoded)) dataDecoded = [dataDecoded];
for (let i in abi.inputs) {
let input = abi.inputs[i];
let { indexed } = input;
let value = indexed === true ? topics[i] : dataDecoded[i - topics.length];
let decoded = decodeInput(input, value);
if (decoded) log.args.push(decoded);
}
}
return log;
};
return Object.freeze({ decodeLog, abi: fakeAbi });
}var _default =

NativeContractsEvents;exports.default = _default;
Empty file modified dist/lib/nativeContracts/NativeContractsEvents.js
100755 → 100644
Empty file.
3 changes: 1 addition & 2 deletions dist/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const contractsInterfaces = {
ERC20: 'ERC20',
ERC677: 'ERC677',
ERC165: 'ERC165',
ERC721: 'ERC721',
EIP1167: 'EIP1167' };exports.contractsInterfaces = contractsInterfaces;
ERC721: 'ERC721' };exports.contractsInterfaces = contractsInterfaces;


const ci = contractsInterfaces;
Expand Down
Loading

0 comments on commit db52b61

Please sign in to comment.