subscriber.js source code

const dotenv = require('dotenv');
const ethers = require("ethers");
const Web3 = require('web3');
const axios = require('axios');
dotenv.config();
const testNetHTTPUrl = process.env.HTTP;
const webhookUrl = process.env.WEBHOOK;
const provider = new ethers.providers.JsonRpcProvider(testNetHTTPUrl);
const wallet = new ethers.Wallet(process.env.TRUST_ANCHOR_PK, provider);
const TrustAnchorManagerArtifact = require('./build/contracts/TrustAnchorManager.json');
const TrustAnchorStorageArtifact = require('./build/contracts/TrustAnchorStorage.json');
const TrustAnchorExtraData_GenericArtifact = require('./build/contracts/TrustAnchorExtraData_Generic.json');
const TrustAnchorExtraData_UniqueArtifact = require('./build/contracts/TrustAnchorExtraData_Unique.json');
let web3 = new Web3(new Web3.providers.HttpProvider(testNetHTTPUrl));
function convertComponentsFromHex(hex) {
return web3.utils.hexToAscii(hex);
}
function sendWebsocketMessage(obj) {
axios.post(webhookUrl, {
todo: obj
})
.then((res) => {
console.log('sendWebsocket success');
})
.catch((error) => {
console.log('sendWebsocket error');
console.error(error)
});
}
(async function() {
let tas_factory = new ethers.Contract(process.env.TRUST_ANCHOR_STORAGE_CONTRACT_ADDRESS, TrustAnchorStorageArtifact.abi, wallet);
let tas = await tas_factory.deployed();
tas.on("EVT_setAttestation", async (attestationKeccak, msg_sender, _identifiedAddress, _jurisdiction, _effectiveTime, _expiryTime, _publicData_0, _documentsMatrixEncrypted_0, _availabilityAddressEncrypted, _isManaged, _publicDataLength, _documentsMatrixEncryptedLength, event) => {
console.log("event EVT_setAttestation");
data = {};
data['transactionHash'] = event.transactionHash;
data['event'] = "EVT_setAttestation";
data['returnValues'] = {};
data['returnValues']['attestationKeccak'] = attestationKeccak;
data['returnValues']['msg_sender'] = msg_sender;
data['returnValues']['_identifiedAddress'] = _identifiedAddress;
data['returnValues']['_jurisdiction'] = _jurisdiction;
data['returnValues']['_effectiveTime'] = _effectiveTime.toString();
data['returnValues']['_expiryTime'] = _expiryTime.toString();
data['returnValues']['_publicData_0'] = _publicData_0;
data['returnValues']['_documentsMatrixEncrypted_0'] = _documentsMatrixEncrypted_0;
data['returnValues']['_availabilityAddressEncrypted'] = _availabilityAddressEncrypted;
data['returnValues']['_isManaged'] = _isManaged;
data['returnValues']['_publicDataLength'] = _publicDataLength;
data['returnValues']['_documentsMatrixEncryptedLength'] = _documentsMatrixEncryptedLength;
data['type'] = convertComponentsFromHex(data['returnValues']['_publicData_0']);
data['document'] = convertComponentsFromHex(data['returnValues']['_documentsMatrixEncrypted_0']);
data['document_decrypt'] = convertComponentsFromHex(data['returnValues']['_documentsMatrixEncrypted_0']);
data['memo'] = convertComponentsFromHex(data['returnValues']['_availabilityAddressEncrypted']);
var obj = {
message: "tas-event",
data: data
};
sendWebsocketMessage(obj);
console.log(data);
});
provider.on("block", (blockNumber) => {
console.log("block# " + blockNumber);
});
provider.on("pending", (tx) => {
console.log("pending");
console.log(tx);
var data = {};
data['hash'] = tx['hash'];
data['nonce'] = tx['nonce'];
data['blockHash'] = tx['blockHash'];
data['blockNumber'] = tx['blockNumber'];
data['transactionIndex'] = tx['transactionIndex'];
data['from'] = tx['from'];
data['to'] = tx['to'];
data['value'] = tx['value'].toString();
data['gas'] = tx['gasPrice'].toString();
data['gasPrice'] = tx['gasPrice'].toString();
var obj = {
message: "smart-contract-transaction",
data: data
};
sendWebsocketMessage(obj);
});
})();