Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+254
View File
@@ -0,0 +1,254 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file ResolverMethodHandler.js
*
* @author Samuel Furter <samuel@ethereum.org>
* @date 2018
*/
"use strict";
var PromiEvent = require('web3-core-promievent');
var namehash = require('eth-ens-namehash');
var errors = require('web3-core-helpers').errors;
var _ = require('underscore');
var interfaceIds = require('../config').interfaceIds;
/**
* @param {Registry} registry
* @constructor
*/
function ResolverMethodHandler(registry) {
this.registry = registry;
}
/**
* Executes an resolver method and returns an eventifiedPromise
*
* @param {string} ensName
* @param {string} methodName
* @param {array} methodArguments
* @param {function} callback
* @returns {Object}
*/
ResolverMethodHandler.prototype.method = function (ensName, methodName, methodArguments, outputFormatter, callback) {
return {
call: this.call.bind({
ensName: ensName,
methodName: methodName,
methodArguments: methodArguments,
callback: callback,
parent: this,
outputFormatter: outputFormatter
}),
send: this.send.bind({
ensName: ensName,
methodName: methodName,
methodArguments: methodArguments,
callback: callback,
parent: this
})
};
};
/**
* Executes call
*
* @returns {eventifiedPromise}
*/
ResolverMethodHandler.prototype.call = function (callback) {
var self = this;
var promiEvent = new PromiEvent();
var preparedArguments = this.parent.prepareArguments(this.ensName, this.methodArguments);
var outputFormatter = this.outputFormatter || null;
this.parent.registry.getResolver(this.ensName).then(async function (resolver) {
await self.parent.checkInterfaceSupport(resolver, self.methodName);
self.parent.handleCall(promiEvent, resolver.methods[self.methodName], preparedArguments, outputFormatter, callback);
}).catch(function(error) {
if (_.isFunction(callback)) {
callback(error, null);
return;
}
promiEvent.reject(error);
});
return promiEvent.eventEmitter;
};
/**
* Executes send
*
* @param {Object} sendOptions
* @param {function} callback
* @returns {eventifiedPromise}
*/
ResolverMethodHandler.prototype.send = function (sendOptions, callback) {
var self = this;
var promiEvent = new PromiEvent();
var preparedArguments = this.parent.prepareArguments(this.ensName, this.methodArguments);
this.parent.registry.getResolver(this.ensName).then(async function (resolver) {
await self.parent.checkInterfaceSupport(resolver, self.methodName);
self.parent.handleSend(promiEvent, resolver.methods[self.methodName], preparedArguments, sendOptions, callback);
}).catch(function(error) {
if (_.isFunction(callback)) {
callback(error, null);
return;
}
promiEvent.reject(error);
});
return promiEvent.eventEmitter;
};
/**
* Handles a call method
*
* @param {eventifiedPromise} promiEvent
* @param {function} method
* @param {array} preparedArguments
* @param {function} callback
* @returns {eventifiedPromise}
*/
ResolverMethodHandler.prototype.handleCall = function (promiEvent, method, preparedArguments, outputFormatter, callback) {
method.apply(this, preparedArguments).call()
.then(function (result) {
if (outputFormatter){
result = outputFormatter(result);
}
if (_.isFunction(callback)) {
// It's required to pass the receipt to the second argument to be backwards compatible and to have the required consistency
callback(result, result);
return;
}
promiEvent.resolve(result);
}).catch(function (error) {
if (_.isFunction(callback)) {
callback(error, null);
return;
}
promiEvent.reject(error);
});
return promiEvent;
};
/**
* Handles a send method
*
* @param {eventifiedPromise} promiEvent
* @param {function} method
* @param {array} preparedArguments
* @param {Object} sendOptions
* @param {function} callback
* @returns {eventifiedPromise}
*/
ResolverMethodHandler.prototype.handleSend = function (promiEvent, method, preparedArguments, sendOptions, callback) {
method.apply(this, preparedArguments).send(sendOptions)
.on('sending', function () {
promiEvent.eventEmitter.emit('sending');
})
.on('sent', function () {
promiEvent.eventEmitter.emit('sent');
})
.on('transactionHash', function (hash) {
promiEvent.eventEmitter.emit('transactionHash', hash);
})
.on('confirmation', function (confirmationNumber, receipt) {
promiEvent.eventEmitter.emit('confirmation', confirmationNumber, receipt);
})
.on('receipt', function (receipt) {
promiEvent.eventEmitter.emit('receipt', receipt);
promiEvent.resolve(receipt);
if (_.isFunction(callback)) {
// It's required to pass the receipt to the second argument to be backwards compatible and to have the required consistency
callback(receipt, receipt);
}
})
.on('error', function (error) {
promiEvent.eventEmitter.emit('error', error);
if (_.isFunction(callback)) {
callback(error, null);
return;
}
promiEvent.reject(error);
});
return promiEvent;
};
/**
* Adds the ENS node to the arguments
*
* @param {string} name
* @param {array} methodArguments
*
* @returns {array}
*/
ResolverMethodHandler.prototype.prepareArguments = function (name, methodArguments) {
var node = namehash.hash(name);
if (methodArguments.length > 0) {
methodArguments.unshift(node);
return methodArguments;
}
return [node];
};
/**
*
*
* @param {Contract} resolver
* @param {string} methodName
*
* @returns {Promise}
*/
ResolverMethodHandler.prototype.checkInterfaceSupport = async function (resolver, methodName) {
// Skip validation for undocumented interface ids (ex: multihash)
if (!interfaceIds[methodName]) return;
var supported = false;
try {
supported = await resolver
.methods
.supportsInterface(interfaceIds[methodName])
.call();
} catch(err) {
console.warn('Could not verify interface of resolver contract at "' + resolver.options.address + '". ');
}
if (!supported){
throw errors.ResolverMethodMissingError(resolver.options.address, methodName);
}
};
module.exports = ResolverMethodHandler;
+111
View File
@@ -0,0 +1,111 @@
/*
Adapted from ensdomains/ui
https://github.com/ensdomains/ui/blob/3e62e440b53466eeec9dd1c63d73924eefbd88c1/src/utils/contents.js#L1-L85
BSD 2-Clause License
Copyright (c) 2019, Ethereum Name Service
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var contentHash = require('content-hash');
function decode(encoded) {
var decoded = null;
var protocolType = null;
var error = null;
if (encoded && encoded.error) {
return {
protocolType: null,
decoded: encoded.error
};
}
if (encoded) {
try {
decoded = contentHash.decode(encoded);
var codec = contentHash.getCodec(encoded);
if (codec === 'ipfs-ns') {
protocolType = 'ipfs';
} else if (codec === 'swarm-ns') {
protocolType = 'bzz';
} else if (codec === 'onion') {
protocolType = 'onion';
} else if (codec === 'onion3') {
protocolType = 'onion3';
} else {
decoded = encoded;
}
} catch (e) {
error = e.message;
}
}
return {
protocolType: protocolType,
decoded: decoded,
error: error
};
}
function encode(text) {
var content, contentType;
var encoded = false;
if (!!text) {
var matched = text.match(/^(ipfs|bzz|onion|onion3):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/);
if (matched) {
contentType = matched[1];
content = matched[2];
}
try {
if (contentType === 'ipfs') {
if(content.length >= 4) {
encoded = '0x' + contentHash.fromIpfs(content);
}
} else if (contentType === 'bzz') {
if(content.length >= 4) {
encoded = '0x' + contentHash.fromSwarm(content);
}
} else if (contentType === 'onion') {
if(content.length === 16) {
encoded = '0x' + contentHash.encode('onion', content);
}
} else if (contentType === 'onion3') {
if(content.length === 56) {
encoded = '0x' + contentHash.encode('onion3', content);
}
} else {
throw new Error('Could not encode content hash: unsupported content type');
}
} catch (err) {
throw err;
}
}
return encoded;
}
module.exports = {
decode: decode,
encode: encode
};