vs-sdk.js source code

const Web3 = require('web3');
const HDWalletProvider = require("@truffle/hdwallet-provider");
const BN = require('bn.js');
const ethers = require("ethers");
const pako = require('pako');
const web3_eth_abi = require("web3-eth-abi");
const dotenv = require('dotenv');
dotenv.config();
const testNetHttpUrl = process.env.HTTP;
const testNetWsUrl = process.env.WS;
const privateKeys = process.env.PKS.split(",");
let hdWalletProvider = new HDWalletProvider(privateKeys, testNetHttpUrl);
let web3 = new Web3(new Web3.providers.HttpProvider(testNetHttpUrl));
const TruffleContract = require('truffle-contract');
const web3Provider = new Web3.providers.WebsocketProvider(testNetWsUrl);
const TrustAnchorManagerArtifact = require('./build/contracts/TrustAnchorManager.json');
const TrustAnchorManager = TruffleContract(TrustAnchorManagerArtifact);
TrustAnchorManager.setProvider(web3Provider);
TrustAnchorManager.setProvider(hdWalletProvider);
const TrustAnchorStorageArtifact = require('./build/contracts/TrustAnchorStorage.json');
const TrustAnchorStorage = TruffleContract(TrustAnchorStorageArtifact);
TrustAnchorStorage.setProvider(web3Provider);
TrustAnchorStorage.setProvider(hdWalletProvider);
const TrustAnchorExtraData_UniqueArtifact = require('./build/contracts/TrustAnchorExtraData_Unique.json');
const TrustAnchorExtraData_Unique = TruffleContract(TrustAnchorExtraData_UniqueArtifact);
TrustAnchorExtraData_Unique.setProvider(web3Provider);
TrustAnchorExtraData_Unique.setProvider(hdWalletProvider);
const TrustAnchorExtraData_GenericArtifact = require('./build/contracts/TrustAnchorExtraData_Generic.json');
const TrustAnchorExtraData_Generic = TruffleContract(TrustAnchorExtraData_GenericArtifact);
TrustAnchorExtraData_Generic.setProvider(web3Provider);
TrustAnchorExtraData_Generic.setProvider(hdWalletProvider);
function decodeDocument(_documentEncoded) {
const hex = Uint8Array.from(Buffer.from(_documentEncoded, 'hex'));
try {
let inflated = pako.inflate(hex);
let inflatedToHex = Buffer.from(inflated).toString('hex');
let unzippedEncodedDocumentsMatrix = "0x" + inflatedToHex;
let decodedDocumentsMatrix = web3_eth_abi.decodeParameter(
{
"DocumentsMatrixStruct": {
"bitsMatrix": 'uint256',
"versionCode": 'uint16',
"encryptedData": 'bytes'
}
},
unzippedEncodedDocumentsMatrix
);
return decodedDocumentsMatrix;
} catch (err) {
console.log(err);
return null;
}
}
function decodeDocumentMatrixInPlace(_dataTemplate) {
if (_dataTemplate.versionCode == 2) {
let bytesDocumentMatrixEncodedPacked = ethers.utils.toUtf8String(_dataTemplate.encryptedData);
let encodedDocumentMatrixDataPacked = JSON.parse(bytesDocumentMatrixEncodedPacked);
_dataTemplate["decryptedData"] = encodedDocumentMatrixDataPacked;
return _dataTemplate;
} else {
return null;
}
}
function unpackCryptoAddress(decodeDocumentMatrix) {
return ethers.utils.toUtf8String(decodeDocumentMatrix.decryptedData.data);
}
function unpackDocumentsMatrixEncrypted(documentMatrix) {
let decodedDocument = decodeDocument(documentMatrix);
let decodeDocumentMatrix = decodeDocumentMatrixInPlace(decodedDocument);
let unpackedCryptoAddress = unpackCryptoAddress(decodeDocumentMatrix);
return unpackedCryptoAddress;
}
function convertComponentsFromHex(hex) {
return web3.utils.hexToAscii(hex);
}
function convertToByte32(string) {
var temp = web3.utils.asciiToHex(string);
temp = web3.utils.hexToBytes(temp);
return temp;
}
function createTravelRuleTemplate(_documentMatrixDataBytes) {
let documentsMatrixBitString = "";
let documentsMatrixBits = new Array(256);
documentsMatrixBitString += "1";
let i = 0;
for (i = 1 ; i < 255; i++) {
documentsMatrixBitString += "0";
}
documentsMatrixBitString += "0";
let documentMatrixBignum = new BN(documentsMatrixBitString, 2);
let versionCode = 2;
return {"bitsMatrix" : documentMatrixBignum, "versionCode" : versionCode, "documentMatrixDataBytes" : _documentMatrixDataBytes};
}
function encodeDocumentMatrixInPlace(_dataTemplate) {
if (_dataTemplate.versionCode === 2 && _dataTemplate.documentMatrixDataBytes != null) {
let encodedDocumentMatrixDataPacked = JSON.stringify(_dataTemplate.documentMatrixDataBytes);
let bytesDocumentMatrixEncodedPacked = ethers.utils.toUtf8Bytes(encodedDocumentMatrixDataPacked);
_dataTemplate["encryptedData"] = bytesDocumentMatrixEncodedPacked;
return _dataTemplate;
} else {
return null;
}
}
function encodeDocument(_bitsMatrix, _versionCode, _encryptedData) {
if (_versionCode === 1 || _versionCode == 2) {
let encodedDocumentsMatrix = web3_eth_abi.encodeParameter(
{
"DocumentsMatrixStruct": {
"bitsMatrix": 'uint256',
"versionCode": 'uint16',
"encryptedData": 'bytes'
}
},
{
"bitsMatrix": _bitsMatrix.toString(),
"versionCode": _versionCode.toString(),
"encryptedData": _encryptedData
}
);
const hex = Uint8Array.from(Buffer.from(encodedDocumentsMatrix.substr(2), 'hex'));
let zippedEncodedDocumentsMatrix = pako.deflate(hex).buffer;
const zippedHex = Buffer.from(zippedEncodedDocumentsMatrix).toString('hex');
return zippedHex;
} else {
return null;
}
}
function packDocumentsMatrixEncrypted(address) {
let addressBuffer = new Buffer(address);
let travelRuleTemplate = createTravelRuleTemplate(addressBuffer);
let encodedDocumentMatrix = encodeDocumentMatrixInPlace(travelRuleTemplate);
let encodedDocument = encodeDocument(encodedDocumentMatrix.bitsMatrix, encodedDocumentMatrix.versionCode, encodedDocumentMatrix.encryptedData);
return encodedDocument;
}
// node -e 'require("./vs-sdk").trustAnchorCreateAccount()'
module.exports.trustAnchorCreateAccount = function () {
trustAnchorCreateAccount();
};
function trustAnchorCreateAccount() {
var result = web3.eth.accounts.create();
console.log(result);
}
// node -e 'require("./vs-sdk").getIsTrustAnchorVerified(account)'
module.exports.getIsTrustAnchorVerified = function (account) {
getIsTrustAnchorVerified(account);
};
function getIsTrustAnchorVerified(account) {
(async () => {
var tam = await TrustAnchorManager.deployed();
result = await tam.isTrustAnchorVerified.call(account)
console.log('isTrustAnchorVerified result');
console.log(result);
})();
}
// node -e 'require("./vs-sdk").getBalance(account)'
module.exports.getBalance = function (account) {
getBalance(account);
};
function getBalance(account) {
web3.eth.getBalance(account).then(console.log);
}
// node -e 'require("./vs-sdk").trustAnchorSetUniqueAddress(account)'
module.exports.trustAnchorSetUniqueAddress = function (account) {
trustAnchorSetUniqueAddress(account);
};
function trustAnchorSetUniqueAddress(account) {
(async () => {
var tam = await TrustAnchorManager.deployed();
var taed_unique = await TrustAnchorExtraData_Unique.deployed();
result = await tam.setUniqueTrustAnchorExtraDataAddress.call(taed_unique.address, {from: account})
console.log('setUniqueTrustAnchorExtraDataAddress result');
console.log(result);
hash = await tam.setUniqueTrustAnchorExtraDataAddress(taed_unique.address, {from: account})
console.log('setUniqueTrustAnchorExtraDataAddress hash');
console.log(hash);
})();
}
// node -e 'require("./vs-sdk").trustAnchorSetKeyValuePair(account, key_name, key_value)'
module.exports.trustAnchorSetKeyValuePair = function (account, key_name, key_value) {
trustAnchorSetKeyValuePair(account, key_name, key_value);
};
function trustAnchorSetKeyValuePair(account, key_name, key_value) {
(async () => {
taed_unique = await TrustAnchorExtraData_Unique.deployed();
result = await taed_unique.setTrustAnchorKeyValuePair.call(key_name, key_value, {from: account})
console.log('setTrustAnchorKeyValuePair result');
console.log(result);
hash = await taed_unique.setTrustAnchorKeyValuePair(key_name, key_value, {from: account})
console.log('setTrustAnchorKeyValuePair hash');
console.log(hash);
})();
}
// node -e 'require("./vs-sdk").trustAnchorSetEndpoint(account, endpoint_name, ip_address)'
module.exports.trustAnchorSetEndpoint = function (account, endpoint_name, ip_address) {
trustAnchorSetEndpoint(account, endpoint_name, ip_address);
};
function trustAnchorSetEndpoint(account, endpoint_name, ip_address) {
(async () => {
ip_address = ip_address.split('.');
taed_generic = await TrustAnchorExtraData_Generic.deployed();
result = await taed_generic.setTrustAnchorDataRetrievalParameters.call(endpoint_name, ip_address, {from: account})
console.log('setTrustAnchorDataRetrievalParameters result');
console.log(result);
hash = await taed_generic.setTrustAnchorDataRetrievalParameters(endpoint_name, ip_address, {from: account})
console.log('setTrustAnchorDataRetrievalParameters hash');
console.log(hash);
})();
}
// node -e 'require("./vs-sdk").trustAnchorCreateUserAccount()'
module.exports.trustAnchorCreateUserAccount = function () {
trustAnchorCreateUserAccount();
};
function trustAnchorCreateUserAccount() {
var result = web3.eth.accounts.create();
console.log(result);
}
// node -e 'require("./vs-sdk").trustAnchorSetAttestation(user_account, jurisdiction, effective_time, expiry_time, public_data, documents_matrix_encrypted, availability_address_encrypted, is_managed, ta_account)'
// node -e 'require("./vs-sdk").trustAnchorSetAttestation(user_account, 1, "", "", "WALLET", "16Qygw3QgX4TgZxneVWQeVVxp2XNYf9vP9", "BTC", is_managed, ta_account)'
module.exports.trustAnchorSetAttestation = function (user_account, jurisdiction, effective_time, expiry_time, public_data, documents_matrix_encrypted, availability_address_encrypted, is_managed, ta_account) {
trustAnchorSetAttestation(user_account, jurisdiction, effective_time, expiry_time, public_data, documents_matrix_encrypted, availability_address_encrypted, is_managed, ta_account);
};
function trustAnchorSetAttestation(user_account, jurisdiction, effective_time, expiry_time, public_data, documents_matrix_encrypted, availability_address_encrypted, is_managed, ta_account) {
public_data = convertToByte32(public_data);
console.log('public_data');
console.log(public_data);
documents_matrix_encrypted = packDocumentsMatrixEncrypted(documents_matrix_encrypted);
documents_matrix_encrypted = convertToByte32(documents_matrix_encrypted);
console.log('documents_matrix_encrypted');
console.log(documents_matrix_encrypted);
availability_address_encrypted = convertToByte32(availability_address_encrypted);
console.log('availability_address_encrypted');
console.log(availability_address_encrypted);
effective_time = Math.floor(Date.now() / 1000) - (60 * 60 * 24 * (365 + 1));// (in the past a year and a day)
expiry_time = Math.floor(Date.now() / 1000) + (60 * 60 * 24 * (365 + 1));// (in the future a year and a day)
(async () => {
tas = await TrustAnchorStorage.deployed();
result = await tas.setAttestation.call(user_account, jurisdiction, effective_time, expiry_time, public_data, documents_matrix_encrypted, availability_address_encrypted, is_managed, {from: ta_account});
console.log('setAttestation result');
console.log(result);
hash = await tas.setAttestation(user_account, jurisdiction, effective_time, expiry_time, public_data, documents_matrix_encrypted, availability_address_encrypted, is_managed, {from: ta_account});
console.log('setAttestation hash');
console.log(hash);
})();
}
// node -e 'require("./vs-sdk").trustAnchorGetAttestationArrayForTrustAnchorAccount(account)'
module.exports.trustAnchorGetAttestationArrayForTrustAnchorAccount = function (account) {
trustAnchorGetAttestationArrayForTrustAnchorAccount(account);
};
function trustAnchorGetAttestationArrayForTrustAnchorAccount(account) {
(async () => {
tas = await TrustAnchorStorage.deployed();
result = await tas.getAttestationKeccakArrayForTrustAnchor.call(account)
console.log('getAttestationKeccakArrayForTrustAnchor result');
console.log(result);
})();
}
// node -e 'require("./vs-sdk").trustAnchorGetAttestationArrayForUserAccount(account)'
module.exports.trustAnchorGetAttestationArrayForUserAccount = function (account) {
trustAnchorGetAttestationArrayForUserAccount(account);
};
function trustAnchorGetAttestationArrayForUserAccount(account) {
(async () => {
tas = await TrustAnchorStorage.deployed();
result = await tas.getAttestationKeccakArrayForIdentifiedAddress.call(account)
console.log('getAttestationKeccakArrayForIdentifiedAddress result');
console.log(result);
})();
}
// node -e 'require("./vs-sdk").trustAnchorGetAttestationComponents(attestation_hash)'
module.exports.trustAnchorGetAttestationComponents = function (attestation_hash) {
trustAnchorGetAttestationComponents(attestation_hash);
};
function trustAnchorGetAttestationComponents(attestation_hash) {
(async () => {
tas = await TrustAnchorStorage.deployed();
result = await tas.getAttestationComponents.call(attestation_hash)
console.log('getAttestationComponents result');
console.log(result);
var documentsMatrixEncrypted = convertComponentsFromHex(result['documentsMatrixEncrypted']);
documentsMatrixEncrypted = unpackDocumentsMatrixEncrypted(documentsMatrixEncrypted);
result['documentsMatrixEncrypted'] = documentsMatrixEncrypted;
result['publicData'] = convertComponentsFromHex(result['publicData']);
result['availabilityAddressEncrypted'] = convertComponentsFromHex(result['availabilityAddressEncrypted']).replace(/\0/g, '');
console.log('decoded components');
console.log(result);
})();
}