Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let topicBytes;
let userBytes;
let protocolVersion = 0;
let topicLength = 32;
let userLength = 20;
let timeLength = 7;
let levelLength = 1;
let headerLength = 8;
let updateMinLength = topicLength + userLength + timeLength + levelLength + headerLength;
protocolVersion = metaResponse.protocolVersion;
try {
topicBytes = hexToBytes(metaResponse.feed.topic);
} catch (err) {
return undefined;
}
try {
userBytes = hexToBytes(metaResponse.feed.user);
} catch (err) {
return undefined;
}
const buf = new ArrayBuffer(updateMinLength + dataBytes.length);
const view = new DataView(buf);
let cursor = 0;
view.setUint8(cursor, protocolVersion); // first byte is protocol version.
cursor += headerLength; // leave the next 7 bytes (padding) set to zero
Accounts.prototype.hashMessage = function hashMessage(data) {
var message = utils.isHexStrict(data) ? utils.hexToBytes(data) : data;
var messageBuffer = Buffer.from(message);
var preamble = "\x19Ethereum Signed Message:\n" + message.length;
var preambleBuffer = Buffer.from(preamble);
var ethMessage = Buffer.concat([preambleBuffer, messageBuffer]);
return Hash.keccak256s(ethMessage);
};
web3.eth.accounts.hashMessage = function hashMessage(data: string | Buffer) {
const message = web3Utils.isHexStrict(data) ? web3Utils.hexToBytes(data) : data
const messageBuffer = Buffer.from(message)
const prefix = '\u0019VeChain Signed Message:\n' + message.length.toString()
const prefixBuffer = Buffer.from(prefix)
const prefixedMessage = Buffer.concat([prefixBuffer, messageBuffer])
return utils.toPrefixedHex(cry.blake2b256(prefixedMessage).toString('hex'))
}
sendRequest(url, requestType, responseType, data = false, pinHeader = false) {
Trace.time('sendRequest//SwarmFeeds');
if(data){
data = Buffer.from(hexToBytes(data));
}
let headers = {
'Accept': 'application/octet-stream',
}
if(pinHeader){
headers['x-swarm-pin'] = true;
}
return request({
responseType: responseType,
url: `${this.swarmNode}${url}`,
method: requestType,
headers: headers,
data: data,
get _signedMessageDigest() {
return hexToBytes(
soliditySha3('\x19Ethereum Signed Message:\n32', this._messageHash),
);
}
export const hex2bytes = (num: string) => {
num = num.replace(/^0x/, '')
num = num.length % 2 ? '0x0' + num : '0x' + num
return utils.hexToBytes(num)
}
export const bytes2hex = (bytes: Uint8Array) => {
dataToBuffer(data) {
return Buffer.from(data, "hex");
const dataBytes = hexToBytes(data);
var buf = new ArrayBuffer(dataBytes.length);
var dataView = new DataView(buf);
for (var i = 0; i < dataBytes.length; i++) {
dataView.setUint8(i, dataBytes[i]);
}
return buf;
}
signData(data, privateKey) {
const sigObj = secp256k1.sign(Buffer.from(hexToBytes(data)), Buffer.from(hexToBytes(privateKey)));
return `0x${sigObj.signature.toString('hex')}0${sigObj.recovery.toString()}`;
}